]> git.phdru.name Git - xsetbg.git/blob - print-filename.py
Expand tilde in the database path
[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-2014 PhiloSoft Design"
12 __license__ = "GNU GPL"
13
14
15 import sys, os
16 from xsetbg_conf import xsetbg_conf
17 from xsetbg_db import xsetbg_db
18
19
20 def usage(code=0):
21    sys.stderr.write("Usage: %s [-o|--old]\n" % sys.argv[0])
22    sys.exit(code)
23
24
25 def get_args():
26    from getopt import getopt, GetoptError
27
28    try:
29       options, arguments = getopt(sys.argv[1:], "he:o",
30                                   ["help", "output-encoding=", "old"])
31    except GetoptError:
32       usage(1)
33
34    old = False
35    output_encoding = None
36
37    for option, value in options:
38       if option in ("-h", "--help"):
39          usage()
40       elif option in ("-e", "--output-encoding"):
41          output_encoding = value
42       elif option in ("-o", "--old"):
43          old = True
44       else:
45          usage(2)
46
47    if arguments:
48       usage(3)
49
50    if output_encoding is None:
51       from m_lib.defenc import default_encoding
52       output_encoding = default_encoding
53    return old, output_encoding
54
55 old, output_encoding = get_args()
56
57
58 filename_key = "filename"
59 old_filename_key = "old_filename"
60
61 if old:
62    key = old_filename_key
63 else:
64    key = filename_key
65
66 filename = xsetbg_db[key]
67 xsetbg_db.close()
68
69 fs_encoding = xsetbg_conf.get("images", "fs_encoding")
70 filename = unicode(filename, fs_encoding)
71
72 print filename.encode(output_encoding)