degal/utils.py
branchuse-distutils
changeset 41 3b1579a7bffb
parent 28 70b6c13d084f
child 44 533b7e8b5d3b
equal deleted inserted replaced
40:373392025533 41:3b1579a7bffb
       
     1 # DeGAL - A pretty simple web image gallery
       
     2 # Copyright (C) 2007 Tero Marttila
       
     3 # http://marttila.de/~terom/degal/
       
     4 #
       
     5 # This program is free software; you can redistribute it and/or modify
       
     6 # it under the terms of the GNU General Public License as published by
       
     7 # the Free Software Foundation; either version 2 of the License, or
       
     8 # (at your option) any later version.
       
     9 #
       
    10 # This program is distributed in the hope that it will be useful,
       
    11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 # GNU General Public License for more details.
       
    14 #
       
    15 # You should have received a copy of the GNU General Public License
       
    16 # along with this program; if not, write to the
       
    17 # Free Software Foundation, Inc.,
       
    18 # 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
       
    19 #
       
    20 
       
    21 import os, os.path
       
    22 
       
    23 import settings
       
    24 
       
    25 def isImage (fname) :
       
    26     """
       
    27         Is the given filename likely to be an image file?
       
    28     """
       
    29 
       
    30     fname = fname.lower()
       
    31     base, ext = os.path.splitext(fname)
       
    32     ext = ext.lstrip('.')
       
    33 
       
    34     return ext in settings.IMAGE_EXTS
       
    35 
       
    36 def readFile (path) :
       
    37     fo = open(path, 'r')
       
    38     data = fo.read()
       
    39     fo.close()
       
    40 
       
    41     return data
       
    42 
       
    43 def fuzzyDecode (bytes) :
       
    44     try :
       
    45         return bytes.decode('utf8')
       
    46     except UnicodeDecodeError :
       
    47         return bytes.decode('latin1', 'replace')
       
    48 
       
    49 def readTitleDescr (path) :
       
    50     """
       
    51         Read a title.txt or <imgname>.txt file
       
    52     """
       
    53 
       
    54     if os.path.exists(path) :
       
    55         content = readFile(path)
       
    56 
       
    57         if '---' in content :
       
    58             title, descr = content.split('---', 1)
       
    59         else :
       
    60             title, descr = content, ''
       
    61         
       
    62         title, descr = fuzzyDecode(title), fuzzyDecode(descr)
       
    63 
       
    64         return title.strip(), descr.strip()
       
    65 
       
    66     return u"", u""
       
    67 
       
    68 def url (*parts, **kwargs) :
       
    69     abs = kwargs.pop('abs', False)
       
    70     up = kwargs.pop('up', 0)
       
    71     trailing = kwargs.pop('trailing', False)
       
    72     
       
    73     return '/'.join(([""]*int(abs)) + ([".."]*up) + list(parts) + ([""]*int(trailing)))
       
    74 
       
    75 url_join = url
       
    76 
       
    77 def path_join (*parts) :
       
    78     return os.path.join(*[part for part in parts if part is not None])
       
    79 
       
    80 def strip_path (path) :
       
    81     return path.lstrip('.').lstrip('/')
       
    82 
       
    83 def mtime (path) :
       
    84     try :
       
    85         return os.stat(path).st_mtime
       
    86     except OSError :
       
    87         # no such file or directory
       
    88         return None
       
    89