import os, os.path
import settings
"""
Miscellaneous utilities
"""
def isImage (fname) :
"""
Is the given filename likely to be an image file?
"""
fname = fname.lower()
base, ext = os.path.splitext(fname)
ext = ext.lstrip('.')
return ext in settings.IMAGE_EXTS
def readFile (path) :
fo = open(path, 'r')
data = fo.read()
fo.close()
return data
def fuzzyDecode (bytes) :
try :
return bytes.decode('utf8')
except UnicodeDecodeError :
return bytes.decode('latin1', 'replace')
def readTitleDescr (path) :
"""
Read a title.txt or <imgname>.txt file
"""
if os.path.exists(path) :
content = readFile(path)
if '---' in content :
title, descr = content.split('---', 1)
else :
title, descr = content, ''
title, descr = fuzzyDecode(title), fuzzyDecode(descr)
return title.strip(), descr.strip()
return u"", u""
def url (*parts, **kwargs) :
abs = kwargs.pop('abs', False)
up = kwargs.pop('up', 0)
trailing = kwargs.pop('trailing', False)
return '/'.join(([""]*int(abs)) + ([".."]*up) + list(parts) + ([""]*int(trailing)))
url_join = url
def path_join (*parts) :
return os.path.join(*[part for part in parts if part is not None])
def strip_path (path) :
return path.lstrip('.').lstrip('/')
def mtime (path) :
try :
return os.stat(path).st_mtime
except OSError :
# no such file or directory
return None