lib/utils.py
author terom
Thu, 31 Jan 2008 19:13:00 +0000
changeset 28 70b6c13d084f
parent 27 301d738b1181
permissions -rw-r--r--
fancy new log format
14
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     1
# DeGAL - A pretty simple web image gallery
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     2
# Copyright (C) 2007 Tero Marttila
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     3
# http://marttila.de/~terom/degal/
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     4
#
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     5
# This program is free software; you can redistribute it and/or modify
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     6
# it under the terms of the GNU General Public License as published by
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     7
# the Free Software Foundation; either version 2 of the License, or
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     8
# (at your option) any later version.
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
     9
#
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    10
# This program is distributed in the hope that it will be useful,
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    13
# GNU General Public License for more details.
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    14
#
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    15
# You should have received a copy of the GNU General Public License
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    16
# along with this program; if not, write to the
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    17
# Free Software Foundation, Inc.,
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    18
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    19
#
4b5478da5850 remove hanging utils.py in root, and add GPL license header to all .py files
terom
parents: 12
diff changeset
    20
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    21
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
    22
23
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    23
import settings
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    24
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    25
def isImage (fname) :
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    26
    """
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    27
        Is the given filename likely to be an image file?
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    28
    """
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    29
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    30
    fname = fname.lower()
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    31
    base, ext = os.path.splitext(fname)
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    32
    ext = ext.lstrip('.')
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    33
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    34
    return ext in settings.IMAGE_EXTS
10841abbc01f taggr2, which is starting to shape up
terom
parents: 20
diff changeset
    35
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
    36
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
    37
    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
    38
    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
    39
    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
    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
    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
    42
27
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    43
def fuzzyDecode (bytes) :
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    44
    try :
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    45
        return bytes.decode('utf8')
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    46
    except UnicodeDecodeError :
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    47
        return bytes.decode('latin1', 'replace')
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    48
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
    49
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
    50
    """
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
        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
    52
    """
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
    53
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
    54
    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
    55
        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
    56
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
    57
        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
    58
            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
    59
        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
    60
            title, descr = content, ''
27
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    61
        
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    62
        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
    63
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
    64
        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
    65
27
301d738b1181 fix some unicode issues, and not-alive-dirs-in-subdirs-list issues
terom
parents: 26
diff changeset
    66
    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
    67
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
    68
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
    69
    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
    70
    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
    71
    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
    72
    
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
    73
    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
    74
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
    75
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
    76
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
    77
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
    78
    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
    79
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 23
diff changeset
    80
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
    81
    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
    82
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    83
def mtime (path) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    84
    try :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    85
        return os.stat(path).st_mtime
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    86
    except OSError :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    87
        # no such file or directory
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    88
        return None
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    89