]> git.phdru.name Git - xsetbg.git/blob - print-filename.py
Added '--no-newline' options.
[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-2007 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] [-n|--no-newline] [-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:], "0nosw:",
32          ["null", "no-newline", "old", "spaces", "width="])
33    except GetoptError:
34       usage(1)
35
36    print0 = False
37    newline = True
38    old = False
39    spaces = ''
40    width = None
41
42    for option, value in options:
43       if option in ("-h", "--help"):
44          usage()
45       elif option in ("-0", "--null"):
46          print0 = True
47       elif option in ("-n", "--no-newline"):
48          newline = False
49       elif option in ("-o", "--old"):
50          old = True
51       elif option in ("-s", "--spaces"):
52          spaces = ' '
53       elif option in ("-w", "--width"):
54          width = int(value)
55       else:
56          usage(2)
57
58    if arguments:
59       if width is not None:
60          usage(3)
61       elif len(arguments) > 1:
62          usage(4)
63       else:
64          width = int(arguments[0])
65
66    return print0, newline, old, spaces, width
67
68 print0, newline, old, spaces, width = get_args()
69
70
71 xsetbg_dir = os.path.join(os.environ["HOME"], "lib", "xsetbg")
72 os.chdir(xsetbg_dir)
73
74 global_db_name = "xsetbg.db"
75 filename_key = "filename"
76 old_filename_key = "old_filename"
77
78
79 global_db = shelve.open(global_db_name, flag='r')
80 if old:
81    key = old_filename_key
82 else:
83    key = filename_key
84 filename = global_db[key]
85 global_db.close()
86
87 from m_lib.defenc import default_encoding
88 filename = unicode(filename, default_encoding)
89
90 if width:
91    lines = []
92    while filename:
93       lines.append("%s%s%s" % (spaces, filename[:width], spaces))
94       filename = filename[width:]
95    filename = u'\n'.join(lines)
96 else:
97    filename = u"%s%s%s" % (spaces, filename, spaces)
98
99 sys.stdout.write(filename.encode(default_encoding))
100
101 if print0:
102    sys.stdout.write('\0')
103 elif newline:
104    print