]> git.phdru.name Git - xsetbg.git/blob - print-filename.py
Version 5.0.2
[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 [index]\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:",
32                                     ["help", "output-encoding="])
33     except GetoptError:
34         usage(1)
35
36     index = 0
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         else:
45             usage(2)
46
47     if arguments:
48         if len(arguments) == 1:
49             try:
50                 index = int(arguments[0])
51             except ValueError:
52                 usage(3)
53         else:
54             usage(4)
55
56     if output_encoding is None:
57         from m_lib.defenc import default_encoding
58         output_encoding = default_encoding
59     return index, output_encoding
60
61 index, output_encoding = get_args()
62
63 filename = xsetbg_db.select('last_shown IS NOT NULL',
64                             orderBy='-last_shown')[index].full_name
65
66 print filename.encode(output_encoding)