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