From bc04f70d3ae1ef5fc15bdf95db1e61fe54db2b95 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Tue, 21 Jul 2015 23:19:52 +0300 Subject: [PATCH] Convert dump_db.py and reload_db.py to use SQLObject --- dump_db.py | 14 +++++++------ reload_db.py | 55 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/dump_db.py b/dump_db.py index 8c80932..2bdffed 100755 --- a/dump_db.py +++ b/dump_db.py @@ -6,13 +6,15 @@ This file is a part of XSetBg. """ __author__ = "Oleg Broytman " -__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) diff --git a/reload_db.py b/reload_db.py index 1bee46e..187bfb3 100755 --- a/reload_db.py +++ b/reload_db.py @@ -6,17 +6,58 @@ This file is a part of XSetBg. """ __author__ = "Oleg Broytman " -__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() -- 2.39.2