]> git.phdru.name Git - xsetbg.git/blob - print-filename.py
Added spaces.
[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
14 import sys, os, shelve
15
16
17 def usage(code=0):
18    sys.stderr.write("Usage: %s [-0|--null] [-o|--old] [-s|--spaces] [-w|--width] [width]\n" % sys.argv[0])
19    sys.exit(code)
20
21
22 def get_args():
23    from getopt import getopt, GetoptError
24
25    try:
26       options, arguments = getopt(sys.argv[1:], "0osw:",
27          ["null", "old", "spaces", "width="])
28    except GetoptError:
29       usage(1)
30
31    print0 = False
32    old = False
33    spaces = ''
34    width = None
35
36    for option, value in options:
37       if option in ("-h", "--help"):
38          usage()
39       elif option in ("-0", "--null"):
40          print0 = True
41       elif option in ("-o", "--old"):
42          old = True
43       elif option in ("-s", "--spaces"):
44          spaces = ' '
45       elif option in ("-w", "--width"):
46          width = int(value)
47       else:
48          usage(2)
49
50    if arguments:
51       if width is not None:
52          usage(3)
53       elif len(arguments) > 1:
54          usage(4)
55       else:
56          width = int(arguments[0])
57
58    return print0, old, spaces, width
59
60 print0, old, spaces, width = get_args()
61
62
63 xsetbg_dir = os.path.join(os.environ["HOME"], "lib", "xsetbg")
64 os.chdir(xsetbg_dir)
65
66 global_db_name = "xsetbg.db"
67 filename_key = "filename"
68 old_filename_key = "old_filename"
69
70
71 global_db = shelve.open(global_db_name, flag='r')
72 if old:
73    key = old_filename_key
74 else:
75    key = filename_key
76 filename = global_db[key]
77 global_db.close()
78
79
80 if width:
81    lines = []
82    while filename:
83       lines.append("%s%s%s" % (spaces, filename[:width], spaces))
84       filename = filename[width:]
85    filename = "\n".join(lines)
86 else:
87    filename = "%s%s%s" % (spaces, filename, spaces)
88
89
90 sys.stdout.write(filename)
91
92 if print0:
93    sys.stdout.write('\0')
94 else:
95    print