]> git.phdru.name Git - xsetbg.git/blob - reload_db.py
Fix flake8 warnings
[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, sqlhub
14 from sqlobject.sqlbuilder import Insert
15 from xsetbg_conf import xsetbg_conf
16 from xsetbg_db import SqliteSequence, XSetBg, xsetbg_db
17
18 fs_encoding = xsetbg_conf.get("images", "fs_encoding")
19 dump_file = open(sys.argv[1], 'rU')
20
21 if xsetbg_db:
22     try:
23         seq = SqliteSequence.byName(XSetBg.sqlmeta.table)
24     except SQLObjectNotFound:
25         SqliteSequence(name=XSetBg.sqlmeta.table, seq=0)
26     else:
27         seq.seq = 0  # Reset autoincrement counter
28     xsetbg_db.clearTable()
29 else:
30     xsetbg_db = XSetBg
31     xsetbg_db.createTable()
32
33 def convert_str(s):
34     if s == "None":
35         return None
36     else:
37         return int(float(s))
38
39 connection = xsetbg_db._connection
40
41 connection.query("PRAGMA synchronous=OFF")
42 connection.query("PRAGMA count_changes=OFF")
43 connection.query("PRAGMA journal_mode=MEMORY")
44 connection.query("PRAGMA temp_store=MEMORY")
45
46 txn = connection.transaction()
47 sqlhub.processConnection = txn
48
49 for line in dump_file:
50     id, timestamp, filename = line.strip().split(None, 2)
51     id = convert_str(id)
52     timestamp = convert_str(timestamp)
53     if fs_encoding != 'utf-8':
54         filename = filename.decode(fs_encoding).encode('utf-8')
55     values = {'last_shown': timestamp, 'full_name': filename}
56     if id:
57         values['id'] = id
58     query = txn.sqlrepr(Insert(XSetBg.sqlmeta.table, values=values))
59     txn.query(query)
60
61 txn.commit()
62 sqlhub.processConnection = connection
63
64 dump_file.close()