3 from __future__ import print_function
8 from sqlobject import sqlhub, SQLObjectNotFound
11 __all__ = ['import_glst']
14 def parse_glst_file(glst_filename):
15 glst_file = codecs.open(glst_filename, 'r', 'utf-8')
17 for line in glst_file:
23 parts = line.split(None, 1)
25 name, title = parts[1].split(';', 1)
26 except (IndexError, ValueError):
33 def import_glst_file(glst_filename):
35 for name, title in parse_glst_file(glst_filename):
38 except SQLObjectNotFound:
39 Genre(name=name, title=title, count=0)
47 ml_dir = os.path.dirname(__file__)
48 count_old = count_new = 0
49 for glst_file in glob(os.path.join(ml_dir, 'glst', '*.glst')):
50 _count_old, _count_new = import_glst_file(glst_file)
51 count_old += _count_old
52 count_new += _count_new
53 connection = sqlhub.processConnection
54 if connection.dbName == 'postgres':
55 connection.query("VACUUM %s" % Genre.sqlmeta.table)
56 return count_old, count_new
60 count_old, count_new = sqlhub.doInTransaction(_import_glst)
61 connection = sqlhub.processConnection
62 if connection.dbName == 'sqlite':
63 connection.query("VACUUM")
64 return count_old, count_new
68 ml_dir = os.path.dirname(__file__)
69 for i, (name, title) in enumerate(parse_glst_file(
70 os.path.join(ml_dir, 'glst', 'genres_fb2_flibusta.glst')
78 if __name__ == '__main__':