lib/utils.py
author terom
Wed, 16 Jan 2008 16:28:00 +0000
changeset 20 6c774496bb00
parent 19 8d3ffd87cb0b
child 23 10841abbc01f
permissions -rw-r--r--
a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
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
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
    21
import os.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
    22
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
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
    24
    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
    25
    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
    26
    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
    27
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
    28
    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
    29
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
    30
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
    31
    """
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
    32
        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
    33
    """
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
    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
    36
        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
    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 '---' 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
    39
            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
    40
        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
    41
            title, descr = 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
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
        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
    44
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
    45
    return "", ""
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
    46
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
    47
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
    48
    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
    49
    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
    50
    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
    51
    
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
    52
    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
    53
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
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
    55
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
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
    57
    return os.path.join(*[part for part in parts if part is not None])