degal/log.py
author Tero Marttila <terom@fixme.fi>
Wed, 03 Jun 2009 19:03:28 +0300
branchuse-distutils
changeset 41 3b1579a7bffb
parent 30 lib/log.py@b1d5c32ab771
child 44 533b7e8b5d3b
permissions -rw-r--r--
reorganize files to move lib, templates, www into 'degal' package, keep separate 'cgi-bin' for now
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 logging, sys
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
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    23
log_level = logging.INFO
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    24
stack = []
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
    25
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    26
class g :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    27
    out_depth = 0
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    28
    node = None
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
    29
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    30
def title (title, *args) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    31
    stack.append(title)
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
    32
29
990300aa8010 some small tweaks to log output
terom
parents: 28
diff changeset
    33
    print "%s - %s" % (" "*g.out_depth, title % args)
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    34
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    35
    g.out_depth += 1
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    36
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    37
def down (dir_name, *args) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    38
    stack.append(dir_name % args)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    39
    g.node = None
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    40
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    41
def next (fname, *args) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    42
    g.node = fname % args
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    43
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    44
def up () :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    45
    stack.pop(-1)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    46
    g.node = None
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    47
    g.out_depth = min(g.out_depth, len(stack))
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    48
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    49
def done () :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    50
    print "done"
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    51
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    52
def log (level, message, *args, **kwargs) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    53
    wait = kwargs.get("wait", False)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    54
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    55
    if level >= log_level :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    56
        if g.out_depth != len(stack) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    57
            for segment in stack[g.out_depth:] :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    58
                print "%sd %s" % (" "*g.out_depth, segment)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    59
                g.out_depth += 1
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    60
30
b1d5c32ab771 moar tweaks/small bugfixes
terom
parents: 29
diff changeset
    61
        if g.node :
b1d5c32ab771 moar tweaks/small bugfixes
terom
parents: 29
diff changeset
    62
            print "%sf %s" % (" "*g.out_depth, g.node)
b1d5c32ab771 moar tweaks/small bugfixes
terom
parents: 29
diff changeset
    63
            g.node = None
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    64
        
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    65
        if wait :
29
990300aa8010 some small tweaks to log output
terom
parents: 28
diff changeset
    66
            print "%s - %s..." % (" "*g.out_depth, message % args),
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    67
            sys.stdout.flush()
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    68
        else :
29
990300aa8010 some small tweaks to log output
terom
parents: 28
diff changeset
    69
            print "%s - %s" % (" "*g.out_depth, message % args)
28
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    70
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    71
def _level (level) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    72
    def _log_func (message, *args, **kwargs) :
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    73
        log(level, message, *args, **kwargs)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    74
    
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    75
    return _log_func
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    76
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    77
debug       = _level(logging.DEBUG)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    78
info        = _level(logging.INFO)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    79
warning     = _level(logging.WARNING)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    80
error       = _level(logging.ERROR)
70b6c13d084f fancy new log format
terom
parents: 27
diff changeset
    81