3 from __future__ import print_function
6 from sqlobject import sqlhub, SQLObjectNotFound
9 __all__ = ['import_glst']
12 def parse_glst_file(glst_filename):
13 glst_file = codecs.open(glst_filename, 'r', 'utf-8')
15 for line in glst_file:
21 parts = line.split(None, 1)
23 name, title = parts[1].split(';', 1)
24 except (IndexError, ValueError):
31 def import_glst_file(glst_filename):
33 for name, title in parse_glst_file(glst_filename):
36 except SQLObjectNotFound:
37 Genre(name=name, title=title, count=0)
45 ml_dir = os.path.dirname(__file__)
46 old_fb2, new_fb2 = import_glst_file(
47 os.path.join(ml_dir, 'glst', 'genres_fb2.glst'))
48 old_nonfb2, new_nonfb2 = import_glst_file(
49 os.path.join(ml_dir, 'glst', 'genres_nonfb2.glst'))
50 connection = sqlhub.processConnection
51 if connection.dbName == 'postgres':
52 connection.query("VACUUM %s" % Genre.sqlmeta.table)
53 return old_fb2 + old_nonfb2, new_fb2 + new_nonfb2
57 return sqlhub.doInTransaction(_import_glst)
58 connection = sqlhub.processConnection
59 if connection.dbName == 'sqlite':
60 connection.query("VACUUM")
64 ml_dir = os.path.dirname(__file__)
65 print(parse_glst_file(os.path.join(ml_dir, 'glst', 'genres_fb2.glst')))
68 if __name__ == '__main__':