]> git.phdru.name Git - xsetbg.git/blob - print-filename.py
Convert print-filename.py
[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-2015 PhiloSoft Design"
12 __license__ = "GNU GPL"
13
14
15 import sys
16 from xsetbg_db import xsetbg_db
17
18 if not xsetbg_db:
19     sys.exit("Error: no database found")
20
21
22 def usage(code=0):
23    sys.stderr.write("Usage: %s [-o|--old]\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:], "he:o",
32                                   ["help", "output-encoding=", "old"])
33    except GetoptError:
34       usage(1)
35
36    old = False
37    output_encoding = None
38
39    for option, value in options:
40       if option in ("-h", "--help"):
41          usage()
42       elif option in ("-e", "--output-encoding"):
43          output_encoding = value
44       elif option in ("-o", "--old"):
45          old = True
46       else:
47          usage(2)
48
49    if arguments:
50       usage(3)
51
52    if output_encoding is None:
53       from m_lib.defenc import default_encoding
54       output_encoding = default_encoding
55    return old, output_encoding
56
57 old, output_encoding = get_args()
58
59
60 if old:
61    index = 1
62 else:
63    index = 0
64
65 filename = xsetbg_db.select('last_shown IS NOT NULL',
66                             orderBy='-last_shown')[index].full_name
67
68 print filename.encode(output_encoding)