5 from sqlobject import sqlhub, SQLObjectNotFound
8 __all__ = ['import_glst']
11 def parse_glst_file(glst_filename):
12 glst_file = codecs.open(glst_filename, 'r', 'utf-8')
14 for line in glst_file:
20 parts = line.split(None, 1)
22 name, title = parts[1].split(';', 1)
23 except (IndexError, ValueError):
30 def import_glst_file(glst_filename):
32 for name, title in parse_glst_file(glst_filename):
35 except SQLObjectNotFound:
36 Genre(name=name, title=title, count=0)
44 ml_dir = os.path.dirname(__file__)
45 old_fb2, new_fb2 = import_glst_file(
46 os.path.join(ml_dir, 'glst', 'genres_fb2.glst'))
47 old_nonfb2, new_nonfb2 = import_glst_file(
48 os.path.join(ml_dir, 'glst', 'genres_nonfb2.glst'))
49 connection = sqlhub.processConnection
50 if connection.dbName in ('postgres', 'sqlite'):
51 connection.query("VACUUM %s" % Genre.sqlmeta.table)
52 return old_fb2 + old_nonfb2, new_fb2 + new_nonfb2
56 return sqlhub.doInTransaction(_import_glst)
60 ml_dir = os.path.dirname(__file__)
61 print parse_glst_file(os.path.join(ml_dir, 'glst', 'genres_fb2.glst'))
63 if __name__ == '__main__':