]> git.phdru.name Git - m_librarian.git/blob - m_librarian/glst.py
Import genres from LRE files
[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
9 def parse_glst_file(glst_filename):
10     glst_file = codecs.open(glst_filename, 'r', 'utf-8')
11     genre_list = []
12     try:
13         for line in glst_file:
14             line = line.strip()
15             if not line:
16                 continue
17             if line[0] == '#':
18                 continue
19             parts = line.split(None, 1)
20             try:
21                 name, title = parts[1].split(';', 1)
22             except (IndexError, ValueError):
23                 continue
24             genre_list.append((name, title))
25     finally:
26         glst_file.close()
27     return genre_list
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_data():
44     ml_dir = os.path.dirname(__file__)
45     old_fb2, new_fb2 = import_glst_file(
46         os.path.join(ml_dir, 'data', 'genres_fb2.glst'))
47     old_nonfb2, new_nonfb2 = import_glst_file(
48         os.path.join(ml_dir, 'data', 'genres_nonfb2.glst'))
49     return old_fb2 + old_nonfb2, new_fb2 + new_nonfb2
50
51
52 def import_data():
53     return sqlhub.doInTransaction(_import_data)
54
55
56 if __name__ == '__main__':
57     ml_dir = os.path.dirname(__file__)
58     print parse_glst_file(os.path.join(ml_dir, 'data', 'genres_fb2.glst'))