]> git.phdru.name Git - xsetbg.git/blob - reload_db.py
Fix a bug: id can be None
[xsetbg.git] / reload_db.py
1 #! /usr/bin/env python
2 """Reload a dump into DB
3
4 This file is a part of XSetBg.
5
6 """
7
8 __author__ = "Oleg Broytman <phd@phdru.name>"
9 __copyright__ = "Copyright (C) 2007-2015 PhiloSoft Design"
10 __license__ = "GNU GPL"
11
12 import sys
13 from sqlobject import sqlhub, SQLObjectNotFound
14 from sqlobject.sqlbuilder import Insert
15 from xsetbg_conf import xsetbg_conf
16 from xsetbg_db import recreate_db
17
18 fs_encoding = xsetbg_conf.get("images", "fs_encoding")
19 dump_file = open(sys.argv[1], 'rU')
20 xsetbg_db = recreate_db()
21
22
23 def convert_str(s):
24     if s == "None":
25         return None
26     else:
27         return int(float(s))
28
29 connection = xsetbg_db._connection
30
31 connection.query("PRAGMA synchronous=OFF")
32 connection.query("PRAGMA count_changes=OFF")
33 connection.query("PRAGMA journal_mode=MEMORY")
34 connection.query("PRAGMA temp_store=MEMORY")
35
36 txn = connection.transaction()
37 sqlhub.processConnection = txn
38
39 for line in dump_file:
40     id, timestamp, filename = line.strip().split(None, 2)
41     id = convert_str(id)
42     timestamp = convert_str(timestamp)
43     if fs_encoding != 'utf-8':
44         filename = filename.decode(fs_encoding).encode('utf-8')
45     try:
46         row = xsetbg_db.byFull_name(filename)
47     except SQLObjectNotFound:
48         values = {'last_shown': timestamp, 'full_name': filename}
49         if id:
50             values['id'] = id
51         query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table, values=values))
52         txn.query(query)
53     else:
54         assert id is None or row.id == id
55         assert row.last_shown == timestamp
56
57 txn.commit()
58 sqlhub.processConnection = connection
59
60 dump_file.close()