]> git.phdru.name Git - xsetbg.git/blob - print-filename.py
Fix(DB): Fix column encoding
[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 import sys
11 from xsetbg_db import xsetbg_db
12
13 if not xsetbg_db:
14     sys.exit("Error: no database found")
15
16
17 def usage(code=0):
18     sys.stderr.write("Usage: %s [index]\n" % sys.argv[0])
19     sys.exit(code)
20
21
22 def get_args():
23     from getopt import getopt, GetoptError
24
25     try:
26         options, arguments = getopt(sys.argv[1:], "he:",
27                                     ["help", "output-encoding="])
28     except GetoptError:
29         usage(1)
30
31     index = 0
32     output_encoding = None
33
34     for option, value in options:
35         if option in ("-h", "--help"):
36             usage()
37         elif option in ("-e", "--output-encoding"):
38             output_encoding = value
39         else:
40             usage(2)
41
42     if arguments:
43         if len(arguments) == 1:
44             try:
45                 index = int(arguments[0])
46             except ValueError:
47                 usage(3)
48         else:
49             usage(4)
50
51     if output_encoding is None:
52         from m_lib.defenc import default_encoding
53         output_encoding = default_encoding
54     return index, output_encoding
55
56 index, output_encoding = get_args()
57
58 filename = xsetbg_db.select('last_shown IS NOT NULL',
59                             orderBy='-last_shown')[index].full_name
60
61 print filename.encode(output_encoding)