]> git.phdru.name Git - xsetbg.git/blob - print-filename.py
Add some empty lines for readability,
[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] [-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:], "0ow:",
27          ["null", "old", "width="])
28    except GetoptError:
29       usage(1)
30
31    print0 = False
32    old = False
33    width = None
34
35    for option, value in options:
36       if option in ("-h", "--help"):
37          usage()
38       elif option in ("-0", "--null"):
39          print0 = True
40       elif option in ("-o", "--old"):
41          old = True
42       elif option in ("-w", "--width"):
43          width = int(value)
44       else:
45          usage(2)
46
47    if arguments:
48       if width is not None:
49          usage(3)
50       elif len(arguments) > 1:
51          usage(4)
52       else:
53          width = int(arguments[0])
54
55    return print0, old, width
56
57 print0, old, width = get_args()
58
59
60 xsetbg_dir = os.path.join(os.environ["HOME"], "lib", "xsetbg")
61 os.chdir(xsetbg_dir)
62
63 global_db_name = "xsetbg.db"
64 filename_key = "filename"
65 old_filename_key = "old_filename"
66
67 global_db = shelve.open(global_db_name, flag='r')
68 if old:
69    key = old_filename_key
70 else:
71    key = filename_key
72 filename = global_db[key]
73 global_db.close()
74
75 if width is not None:
76    lines = []
77    while filename:
78       lines.append(filename[:width])
79       filename = filename[width:]
80    filename = "\n".join(lines)
81
82 sys.stdout.write(filename)
83
84 if print0:
85    sys.stdout.write('\0')
86 else:
87    print