3 from __future__ import print_function
7 from sqlobject import sqlhub, SQLObjectNotFound
10 __all__ = ['import_glst']
13 def parse_glst_file(glst_filename):
14 glst_file = codecs.open(glst_filename, 'r', 'utf-8')
16 for line in glst_file:
22 parts = line.split(None, 1)
24 name, title = parts[1].split(';', 1)
25 except (IndexError, ValueError):
32 def import_glst_file(glst_filename):
34 for name, title in parse_glst_file(glst_filename):
37 except SQLObjectNotFound:
38 Genre(name=name, title=title, count=0)
46 ml_dir = os.path.dirname(__file__)
47 count_old = count_new = 0
48 for glst_file in glob(os.path.join(ml_dir, 'glst', '*.glst')):
49 _count_old, _count_new = import_glst_file(glst_file)
50 count_old += _count_old
51 count_new += _count_new
52 connection = sqlhub.processConnection
53 if connection.dbName == 'postgres':
54 connection.query("VACUUM %s" % Genre.sqlmeta.table)
55 return count_old, count_new
59 count_old, count_new = sqlhub.doInTransaction(_import_glst)
60 connection = sqlhub.processConnection
61 if connection.dbName == 'sqlite':
62 connection.query("VACUUM")
63 return count_old, count_new
67 ml_dir = os.path.dirname(__file__)
68 for i, (name, title) in enumerate(parse_glst_file(
69 os.path.join(ml_dir, 'glst', 'genres_fb2_flibusta.glst')
77 if __name__ == '__main__':