]> git.phdru.name Git - xsetbg.git/blobdiff - print-filename.py
Fix(DB): Fix column encoding
[xsetbg.git] / print-filename.py
index 583f54a3b669aeeabe9795bd820e597528a8e16f..e9f217b75b46920f4561b72ce29c0ac6dfcd0cf8 100755 (executable)
@@ -1,72 +1,61 @@
-#! /usr/bin/env python
-"""Print background filename
+#! /usr/bin/env python3
+"""print(background filename)
 
-Print the filename of the current or previous background image.
+print(the filename of the current or previous background image.)
 
 This file is a part of XSetBg.
 
 """
 
-__version__ = "$Revision$"[11:-2]
-__revision__ = "$Id$"[5:-2]
-__date__ = "$Date$"[7:-2]
+import sys
+from xsetbg_db import xsetbg_db
 
-__author__ = "Oleg Broytman <phd@phdru.name>"
-__copyright__ = "Copyright (C) 2004-2010 PhiloSoft Design"
-__license__ = "GNU GPL"
-
-
-import sys, os, shelve
+if not xsetbg_db:
+    sys.exit("Error: no database found")
 
 
 def usage(code=0):
-   sys.stderr.write("Usage: %s [-o|--old]\n" % sys.argv[0])
-   sys.exit(code)
+    sys.stderr.write("Usage: %s [index]\n" % sys.argv[0])
+    sys.exit(code)
 
 
 def get_args():
-   from getopt import getopt, GetoptError
-
-   try:
-      options, arguments = getopt(sys.argv[1:], "ho", ["help", "old"])
-   except GetoptError:
-      usage(1)
-
-   old = False
-
-   for option, value in options:
-      if option in ("-h", "--help"):
-         usage()
-      elif option in ("-o", "--old"):
-         old = True
-      else:
-         usage(2)
-
-   if arguments:
-      usage(3)
-
-   return old
-
-old = get_args()
-
-
-xsetbg_dir = os.path.join(os.environ["HOME"], "lib", "xsetbg")
-os.chdir(xsetbg_dir)
-
-global_db_name = "xsetbg.db"
-filename_key = "filename"
-old_filename_key = "old_filename"
-
-
-global_db = shelve.open(global_db_name, flag='r')
-if old:
-   key = old_filename_key
-else:
-   key = filename_key
-filename = global_db[key]
-global_db.close()
-
-from m_lib.defenc import default_encoding
-filename = unicode(filename, default_encoding)
-
-print filename.encode(default_encoding)
+    from getopt import getopt, GetoptError
+
+    try:
+        options, arguments = getopt(sys.argv[1:], "he:",
+                                    ["help", "output-encoding="])
+    except GetoptError:
+        usage(1)
+
+    index = 0
+    output_encoding = None
+
+    for option, value in options:
+        if option in ("-h", "--help"):
+            usage()
+        elif option in ("-e", "--output-encoding"):
+            output_encoding = value
+        else:
+            usage(2)
+
+    if arguments:
+        if len(arguments) == 1:
+            try:
+                index = int(arguments[0])
+            except ValueError:
+                usage(3)
+        else:
+            usage(4)
+
+    if output_encoding is None:
+        from m_lib.defenc import default_encoding
+        output_encoding = default_encoding
+    return index, output_encoding
+
+
+index, output_encoding = get_args()
+filename = xsetbg_db.select('last_shown IS NOT NULL',
+                            orderBy='-last_shown')[index].full_name
+
+sys.stdout.buffer.write(filename.encode(output_encoding) + b'\n')