]> git.phdru.name Git - xsetbg.git/commitdiff
Convert dump_db.py and reload_db.py to use SQLObject
authorOleg Broytman <phd@phdru.name>
Tue, 21 Jul 2015 20:19:52 +0000 (23:19 +0300)
committerOleg Broytman <phd@phdru.name>
Tue, 21 Jul 2015 20:19:52 +0000 (23:19 +0300)
dump_db.py
reload_db.py

index 8c80932bb70172da338ae15a8f256c09e9cc3c15..2bdffedf9972887873326bbf132c573347a40770 100755 (executable)
@@ -6,13 +6,15 @@ This file is a part of XSetBg.
 """
 
 __author__ = "Oleg Broytman <phd@phdru.name>"
-__copyright__ = "Copyright (C) 2006-2014 PhiloSoft Design"
+__copyright__ = "Copyright (C) 2006-2015 PhiloSoft Design"
 __license__ = "GNU GPL"
 
-from operator import itemgetter
+import sys
+from m_lib.defenc import default_encoding
 from xsetbg_db import xsetbg_db
 
-for key, value in sorted(xsetbg_db.items(), key=itemgetter(1), reverse=1):
-   if key.startswith('/'):
-      print value, key
-xsetbg_db.close()
+if not xsetbg_db:
+    sys.exit("Error: no database found")
+
+for row in xsetbg_db.select(orderBy='-last_shown'):
+    print row.id, row.last_shown, row.full_name.encode(default_encoding)
index 1bee46e92dde9517f826152043dced4f5ff87469..187bfb353f04ac8b374042d4724ce3fc5fe2e494 100755 (executable)
@@ -6,17 +6,58 @@ This file is a part of XSetBg.
 """
 
 __author__ = "Oleg Broytman <phd@phdru.name>"
-__copyright__ = "Copyright (C) 2007-2014 PhiloSoft Design"
+__copyright__ = "Copyright (C) 2007-2015 PhiloSoft Design"
 __license__ = "GNU GPL"
 
-import sys, shelve
-from xsetbg_db import xsetbg_db_path
+import sys
+from sqlobject import SQLObjectNotFound, sqlhub
+from sqlobject.sqlbuilder import Insert
+from m_lib.defenc import default_encoding
+from xsetbg_db import SqliteSequence, XSetBg, xsetbg_db
 
 dump_file = open(sys.argv[1], 'rU')
 
-xsetbg_db = shelve.open(xsetbg_db_path, flag='n')
+if xsetbg_db:
+    try:
+        seq = SqliteSequence.byName(XSetBg.sqlmeta.table)
+    except SQLObjectNotFound:
+        SqliteSequence(name=XSetBg.sqlmeta.table, seq=0)
+    else:
+        seq.seq = 0  # Reset autoincrement counter
+    xsetbg_db.clearTable()
+else:
+    xsetbg_db = XSetBg
+    xsetbg_db.createTable()
+
+def convert_str(s):
+    if s == "None":
+        return None
+    else:
+        return int(float(s))
+
+connection = xsetbg_db._connection
+
+connection.query("PRAGMA synchronous=OFF")
+connection.query("PRAGMA count_changes=OFF")
+connection.query("PRAGMA journal_mode=MEMORY")
+connection.query("PRAGMA temp_store=MEMORY")
+
+txn = connection.transaction()
+sqlhub.processConnection = txn
+
 for line in dump_file:
-   timestamp, filename = line.strip().split(None, 1)
-   xsetbg_db[filename] = float(timestamp)
-xsetbg_db.close()
+    id, timestamp, filename = line.strip().split(None, 2)
+    id = convert_str(id)
+    timestamp = convert_str(timestamp)
+    if default_encoding != 'utf-8':
+        filename = filename.decode(default_encoding).encode('utf-8')
+    values = {'last_shown': timestamp, 'full_name': filename}
+    if id:
+        values['id'] = id
+    query = txn.sqlrepr(Insert(XSetBg.sqlmeta.table, values=values))
+    txn.query(query)
+
+txn.commit()
+sqlhub.processConnection = connection
+
 dump_file.close()