]> git.phdru.name Git - xsetbg.git/blob - reload_db.py
Decode filename to unicode, encode to utf-8 for Insert
[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 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
30 with SQLiteMassInsert() as txn:
31     for line in dump_file:
32         id, timestamp, filename = line.strip().split(None, 2)
33         id = convert_str(id)
34         timestamp = convert_str(timestamp)
35         filename = filename.decode(fs_encoding)
36         try:
37             row = xsetbg_db.byFull_name(filename)
38         except SQLObjectNotFound:
39             values = {'last_shown': timestamp,
40                       'full_name': filename.encode('utf-8')}
41             if id:
42                 values['id'] = id
43             query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table, values=values))
44             txn.query(query)
45         else:
46             assert id is None or row.id == id
47             assert row.last_shown == timestamp
48
49 dump_file.close()