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