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