]> git.phdru.name Git - xsetbg.git/blob - reload_db.py
Fix(DB): Fix column encoding
[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 import sys
9 from sqlobject import SQLObjectNotFound
10 from sqlobject.sqlbuilder import Insert
11 from xsetbg_conf import xsetbg_conf
12 from xsetbg_db import recreate_db, SQLiteMassInsert
13
14
15 def convert_str(s):
16     if s == "None":
17         return None
18     else:
19         return int(float(s))
20
21
22 fs_encoding = xsetbg_conf.get("images", "fs_encoding")
23 dump_file = open(sys.argv[1], 'rU')
24 xsetbg_db = recreate_db()
25 count_new = count_old = 0
26
27 with SQLiteMassInsert() as txn:
28     for line in dump_file:
29         id, timestamp, filename = line.strip().split(None, 2)
30         id = convert_str(id)
31         timestamp = convert_str(timestamp)
32         filename = filename.decode(fs_encoding)
33         try:
34             row = xsetbg_db.byFull_name(filename)
35         except SQLObjectNotFound:
36             values = {'last_shown': timestamp,
37                       'full_name': filename.encode('utf-8')}
38             if id:
39                 values['id'] = id
40             query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table, values=values))
41             txn.query(query)
42             count_new += 1
43         else:
44             assert id is None or row.id == id
45             if row.last_shown is not None:
46                 assert row.last_shown == timestamp
47             count_old += 1
48
49 dump_file.close()
50
51 print "New images:", count_new
52 print "Existing images:", count_old