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