]> git.phdru.name Git - xsetbg.git/blob - print-filename.py
Set __version__ and __revision__.
[xsetbg.git] / print-filename.py
1 #! /usr/local/bin/python -O
2 """This file is a part of XSetBg.
3 Author: Oleg BroytMann <phd@phd.pp.ru>
4 Copyright (C) 2004-2006 PhiloSoft Design
5 """
6
7 __version__ = "$Revision$"[11:-2]
8 __author__ = "Oleg BroytMann <phd@phd.pp.ru>"
9 __copyright__ = "Copyright (C) 2004-2006 PhiloSoft Design"
10 __date__ = "$Date$"[7:-2]
11 __revision__ = "$Id$"[5:-2]
12
13 import sys, os, shelve
14
15 def usage(code=0):
16    sys.stderr.write("Usage: %s [-0|--null] [-o|--old] [-w|--width] [width]\n" % sys.argv[0])
17    sys.exit(code)
18
19
20 def get_args():
21    from getopt import getopt, GetoptError
22
23    try:
24       options, arguments = getopt(sys.argv[1:], "0ow:",
25          ["null", "old", "width="])
26    except GetoptError:
27       usage(1)
28
29    print0 = False
30    old = False
31    width = None
32
33    for option, value in options:
34       if option in ("-h", "--help"):
35          usage()
36       elif option in ("-0", "--null"):
37          print0 = True
38       elif option in ("-o", "--old"):
39          old = True
40       elif option in ("-w", "--width"):
41          width = int(value)
42       else:
43          usage(2)
44
45    if arguments:
46       if width is not None:
47          usage(3)
48       elif len(arguments) > 1:
49          usage(4)
50       else:
51          width = int(arguments[0])
52
53    return print0, old, width
54
55 print0, old, width = get_args()
56
57
58 xsetbg_dir = os.path.join(os.environ["HOME"], "lib", "xsetbg")
59 os.chdir(xsetbg_dir)
60
61 global_db_name = "xsetbg.db"
62 filename_key = "filename"
63 old_filename_key = "old_filename"
64
65 global_db = shelve.open(global_db_name, flag='r')
66 if old:
67    key = old_filename_key
68 else:
69    key = filename_key
70 filename = global_db[key]
71 global_db.close()
72
73 if width is not None:
74    lines = []
75    while filename:
76       lines.append(filename[:width])
77       filename = filename[width:]
78    filename = "\n".join(lines)
79
80 sys.stdout.write(filename)
81
82 if print0:
83    sys.stdout.write('\0')
84 else:
85    print