terom@14: # DeGAL - A pretty simple web image gallery terom@14: # Copyright (C) 2007 Tero Marttila terom@14: # http://marttila.de/~terom/degal/ terom@14: # terom@14: # This program is free software; you can redistribute it and/or modify terom@14: # it under the terms of the GNU General Public License as published by terom@14: # the Free Software Foundation; either version 2 of the License, or terom@14: # (at your option) any later version. terom@14: # terom@14: # This program is distributed in the hope that it will be useful, terom@14: # but WITHOUT ANY WARRANTY; without even the implied warranty of terom@14: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terom@14: # GNU General Public License for more details. terom@14: # terom@14: # You should have received a copy of the GNU General Public License terom@14: # along with this program; if not, write to the terom@14: # Free Software Foundation, Inc., terom@14: # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. terom@14: # terom@14: terom@28: import os, os.path terom@12: terom@23: import settings terom@23: terom@23: def isImage (fname) : terom@23: """ terom@23: Is the given filename likely to be an image file? terom@23: """ terom@23: terom@23: fname = fname.lower() terom@23: base, ext = os.path.splitext(fname) terom@23: ext = ext.lstrip('.') terom@23: terom@23: return ext in settings.IMAGE_EXTS terom@23: terom@12: def readFile (path) : terom@12: fo = open(path, 'r') terom@12: data = fo.read() terom@12: fo.close() terom@12: terom@12: return data terom@12: terom@27: def fuzzyDecode (bytes) : terom@27: try : terom@27: return bytes.decode('utf8') terom@27: except UnicodeDecodeError : terom@27: return bytes.decode('latin1', 'replace') terom@27: terom@12: def readTitleDescr (path) : terom@12: """ terom@12: Read a title.txt or .txt file terom@12: """ terom@12: terom@12: if os.path.exists(path) : terom@12: content = readFile(path) terom@12: terom@12: if '---' in content : terom@12: title, descr = content.split('---', 1) terom@12: else : terom@12: title, descr = content, '' terom@27: terom@27: title, descr = fuzzyDecode(title), fuzzyDecode(descr) terom@12: terom@12: return title.strip(), descr.strip() terom@12: terom@27: return u"", u"" terom@12: terom@19: def url (*parts, **kwargs) : terom@19: abs = kwargs.pop('abs', False) terom@19: up = kwargs.pop('up', 0) terom@20: trailing = kwargs.pop('trailing', False) terom@15: terom@20: return '/'.join(([""]*int(abs)) + ([".."]*up) + list(parts) + ([""]*int(trailing))) terom@19: terom@19: url_join = url terom@19: terom@15: def path_join (*parts) : terom@15: return os.path.join(*[part for part in parts if part is not None]) terom@26: terom@26: def strip_path (path) : terom@26: return path.lstrip('.').lstrip('/') terom@26: terom@28: def mtime (path) : terom@28: try : terom@28: return os.stat(path).st_mtime terom@28: except OSError : terom@28: # no such file or directory terom@28: return None terom@28: