]> git.phdru.name Git - xsetbg.git/blobdiff - reload_db.py
Convert print_all.py
[xsetbg.git] / reload_db.py
index 69bd77c7b577e20e863b705522e37db49f6f1097..187bfb353f04ac8b374042d4724ce3fc5fe2e494 100755 (executable)
@@ -5,25 +5,59 @@ This file is a part of XSetBg.
 
 """
 
-__version__ = "$Revision: 17 $"[11:-2]
-__revision__ = "$Id: print_all.py 17 2007-06-14 10:37:08Z phd $"[5:-2]
-__date__ = "$Date: 2007-06-14 14:37:08 +0400 (Thu, 14 Jun 2007) $"[7:-2]
-
-__author__ = "Oleg Broytman <phd@phd.pp.ru>"
-__copyright__ = "Copyright (C) 2007 PhiloSoft Design"
+__author__ = "Oleg Broytman <phd@phdru.name>"
+__copyright__ = "Copyright (C) 2007-2015 PhiloSoft Design"
 __license__ = "GNU GPL"
 
-import sys, os, shelve
-
-xsetbg_dir = os.path.join(os.environ["HOME"], "lib", "xsetbg")
-os.chdir(xsetbg_dir)
+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
 
-global_db_name = "xsetbg.db"
 dump_file = open(sys.argv[1], 'rU')
 
-global_db = shelve.open(global_db_name, 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)
-   global_db[filename] = float(timestamp)
-global_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()