]> git.phdru.name Git - m_librarian.git/blob - m_librarian/glst.py
Fix(glst): Return counter after VACUUM
[m_librarian.git] / m_librarian / glst.py
1 #! /usr/bin/env python
2
3 from __future__ import print_function
4 import codecs
5 import os
6 from sqlobject import sqlhub, SQLObjectNotFound
7 from .db import Genre
8
9 __all__ = ['import_glst']
10
11
12 def parse_glst_file(glst_filename):
13     glst_file = codecs.open(glst_filename, 'r', 'utf-8')
14     try:
15         for line in glst_file:
16             line = line.strip()
17             if not line:
18                 continue
19             if line[0] == '#':
20                 continue
21             parts = line.split(None, 1)
22             try:
23                 name, title = parts[1].split(';', 1)
24             except (IndexError, ValueError):
25                 continue
26             yield name, title
27     finally:
28         glst_file.close()
29
30
31 def import_glst_file(glst_filename):
32     old = new = 0
33     for name, title in parse_glst_file(glst_filename):
34         try:
35             Genre.byName(name)
36         except SQLObjectNotFound:
37             Genre(name=name, title=title, count=0)
38             new += 1
39         else:
40             old += 1
41     return old, new
42
43
44 def _import_glst():
45     ml_dir = os.path.dirname(__file__)
46     old_fb2, new_fb2 = import_glst_file(
47         os.path.join(ml_dir, 'glst', 'genres_fb2.glst'))
48     old_nonfb2, new_nonfb2 = import_glst_file(
49         os.path.join(ml_dir, 'glst', 'genres_nonfb2.glst'))
50     connection = sqlhub.processConnection
51     if connection.dbName == 'postgres':
52         connection.query("VACUUM %s" % Genre.sqlmeta.table)
53     return old_fb2 + old_nonfb2, new_fb2 + new_nonfb2
54
55
56 def import_glst():
57     count_old, count_new = sqlhub.doInTransaction(_import_glst)
58     connection = sqlhub.processConnection
59     if connection.dbName == 'sqlite':
60         connection.query("VACUUM")
61     return count_old, count_new
62
63
64 def test():
65     ml_dir = os.path.dirname(__file__)
66     print(parse_glst_file(os.path.join(ml_dir, 'glst', 'genres_fb2.glst')))
67
68
69 if __name__ == '__main__':
70     test()