3 __all__ = ['import_data']
7 from sqlobject import sqlhub, SQLObjectNotFound
11 def parse_glst_file(glst_filename):
12 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):
26 genre_list.append((name, title))
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 old_fb2, new_fb2 = import_glst_file(
48 os.path.join(ml_dir, 'data', 'genres_fb2.glst'))
49 old_nonfb2, new_nonfb2 = import_glst_file(
50 os.path.join(ml_dir, 'data', 'genres_nonfb2.glst'))
51 sqlhub.processConnection.query("VACUUM %s" % Genre.sqlmeta.table)
52 return old_fb2 + old_nonfb2, new_fb2 + new_nonfb2
56 return sqlhub.doInTransaction(_import_data)
60 ml_dir = os.path.dirname(__file__)
61 print parse_glst_file(os.path.join(ml_dir, 'data', 'genres_fb2.glst'))
63 if __name__ == '__main__':