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