degal/utils.py
author Tero Marttila <terom@fixme.fi>
Wed, 03 Jun 2009 19:23:10 +0300
branchuse-distutils
changeset 44 533b7e8b5d3b
parent 41 3b1579a7bffb
child 59 fbbe956229cc
permissions -rw-r--r--
strip copyright/license boilerplate from modules, except dexif and formatbytes
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
     1
import os, os.path
12
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
     2
23
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
     3
import settings
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
     4
44
533b7e8b5d3b strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
     5
"""
533b7e8b5d3b strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
     6
    Miscellaneous utilities
533b7e8b5d3b strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
     7
"""
533b7e8b5d3b strip copyright/license boilerplate from modules, except dexif and formatbytes
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
     8
23
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
     9
def isImage (fname) :
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    10
    """
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    11
        Is the given filename likely to be an image file?
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    12
    """
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    13
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    14
    fname = fname.lower()
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    15
    base, ext = os.path.splitext(fname)
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    16
    ext = ext.lstrip('.')
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    17
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    18
    return ext in settings.IMAGE_EXTS
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    19
12
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    20
def readFile (path) :
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    21
    fo = open(path, 'r')
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    22
    data = fo.read()
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    23
    fo.close()
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    24
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    25
    return data
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    26
27
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    27
def fuzzyDecode (bytes) :
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    28
    try :
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    29
        return bytes.decode('utf8')
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    30
    except UnicodeDecodeError :
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    31
        return bytes.decode('latin1', 'replace')
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    32
12
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    33
def readTitleDescr (path) :
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    34
    """
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    35
        Read a title.txt or <imgname>.txt file
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    36
    """
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    37
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    38
    if os.path.exists(path) :
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    39
        content = readFile(path)
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    40
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    41
        if '---' in content :
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    42
            title, descr = content.split('---', 1)
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    43
        else :
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    44
            title, descr = content, ''
27
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    45
        
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    46
        title, descr = fuzzyDecode(title), fuzzyDecode(descr)
12
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    47
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    48
        return title.strip(), descr.strip()
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    49
27
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    50
    return u"", u""
12
c2d8e9a754a1 Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
diff changeset
    51
19
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 18
diff changeset
    52
def url (*parts, **kwargs) :
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 18
diff changeset
    53
    abs = kwargs.pop('abs', False)
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 18
diff changeset
    54
    up = kwargs.pop('up', 0)
20
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    55
    trailing = kwargs.pop('trailing', False)
15
0fe851a29bc6 detool.py, meant to be a collection of utility operations, currently lets you move images and their html/thumb/preview files at the same time
terom
parents: 14
diff changeset
    56
    
20
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents: 19
diff changeset
    57
    return '/'.join(([""]*int(abs)) + ([".."]*up) + list(parts) + ([""]*int(trailing)))
19
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 18
diff changeset
    58
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 18
diff changeset
    59
url_join = url
8d3ffd87cb0b * move cgi-bin to de-cgi-bin so it doesn't conflict with my default alias... need to come up with a real solution to this
terom
parents: 18
diff changeset
    60
15
0fe851a29bc6 detool.py, meant to be a collection of utility operations, currently lets you move images and their html/thumb/preview files at the same time
terom
parents: 14
diff changeset
    61
def path_join (*parts) :
0fe851a29bc6 detool.py, meant to be a collection of utility operations, currently lets you move images and their html/thumb/preview files at the same time
terom
parents: 14
diff changeset
    62
    return os.path.join(*[part for part in parts if part is not None])
26
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 23
diff changeset
    63
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 23
diff changeset
    64
def strip_path (path) :
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 23
diff changeset
    65
    return path.lstrip('.').lstrip('/')
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 23
diff changeset
    66
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    67
def mtime (path) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    68
    try :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    69
        return os.stat(path).st_mtime
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    70
    except OSError :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    71
        # no such file or directory
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    72
        return None
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    73