terom@28: import os, os.path terom@12: terom@23: import settings terom@23: terom@44: """ terom@44: Miscellaneous utilities terom@44: """ terom@44: 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: