]> git.phdru.name Git - m_librarian.git/blob - m_librarian/glst.py
867d4136590e2a0f4ccee0c429e0e7e270f065bf
[m_librarian.git] / m_librarian / glst.py
1 #! /usr/bin/env python
2
3 __all__ = ['import_data']
4
5 import codecs
6 import os
7 from sqlobject import sqlhub, SQLObjectNotFound
8 from .db import Genre
9
10
11 def parse_glst_file(glst_filename):
12     glst_file = codecs.open(glst_filename, 'r', 'utf-8')
13     genre_list = []
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             genre_list.append((name, title))
27     finally:
28         glst_file.close()
29     return genre_list
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_data():
46     ml_dir = os.path.dirname(__file__)
47     old_fb2, new_fb2 = import_glst_file(
48         os.path.join(ml_dir, 'data', 'genres_fb2.glst'))
49     old_nonfb2, new_nonfb2 = import_glst_file(
50         os.path.join(ml_dir, 'data', 'genres_nonfb2.glst'))
51     sqlhub.processConnection.query("VACUUM %s" % Genre.sqlmeta.table)
52     return old_fb2 + old_nonfb2, new_fb2 + new_nonfb2
53
54
55 def import_data():
56     return sqlhub.doInTransaction(_import_data)
57
58
59 def test():
60     ml_dir = os.path.dirname(__file__)
61     print parse_glst_file(os.path.join(ml_dir, 'data', 'genres_fb2.glst'))
62
63 if __name__ == '__main__':
64     test()