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