]> git.phdru.name Git - xsetbg.git/blob - print-filename.py
Do not include .git* files on export
[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 __author__ = "Oleg Broytman <phd@phdru.name>"
11 __copyright__ = "Copyright (C) 2004-2012 PhiloSoft Design"
12 __license__ = "GNU GPL"
13
14
15 import sys, os, shelve
16
17
18 def usage(code=0):
19    sys.stderr.write("Usage: %s [-o|--old]\n" % sys.argv[0])
20    sys.exit(code)
21
22
23 def get_args():
24    from getopt import getopt, GetoptError
25
26    try:
27       options, arguments = getopt(sys.argv[1:], "he:o",
28                                   ["help", "output-encoding=", "old"])
29    except GetoptError:
30       usage(1)
31
32    old = False
33    output_encoding = None
34
35    for option, value in options:
36       if option in ("-h", "--help"):
37          usage()
38       elif option in ("-e", "--output-encoding"):
39          output_encoding = value
40       elif option in ("-o", "--old"):
41          old = True
42       else:
43          usage(2)
44
45    if arguments:
46       usage(3)
47
48    if output_encoding is None:
49       from m_lib.defenc import default_encoding
50       output_encoding = default_encoding
51    return old, output_encoding
52
53 old, output_encoding = get_args()
54
55
56 xsetbg_dir = os.path.join(os.environ["HOME"], "lib", "xsetbg")
57 os.chdir(xsetbg_dir)
58
59 global_db_name = "xsetbg.db"
60 filename_key = "filename"
61 old_filename_key = "old_filename"
62
63
64 global_db = shelve.open(global_db_name, flag='r')
65 if old:
66    key = old_filename_key
67 else:
68    key = filename_key
69 filename = global_db[key]
70 global_db.close()
71
72 from ConfigParser import SafeConfigParser
73 config = SafeConfigParser()
74 config.read("xsetbg.conf")
75
76 fs_encoding = config.get("images", "fs_encoding")
77 filename = unicode(filename, fs_encoding)
78
79 print filename.encode(output_encoding)