5 from sqlobject import sqlhub, SQLObjectNotFound
9 def parse_glst_file(glst_filename):
10 glst_file = codecs.open(glst_filename, 'r', 'utf-8')
13 for line in glst_file:
19 parts = line.split(None, 1)
21 name, title = parts[1].split(';', 1)
22 except (IndexError, ValueError):
24 genre_list.append((name, title))
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, 'data', 'genres_fb2.glst'))
47 old_nonfb2, new_nonfb2 = import_glst_file(
48 os.path.join(ml_dir, 'data', 'genres_nonfb2.glst'))
49 sqlhub.processConnection.query("VACUUM %s" % Genre.sqlmeta.table)
50 return old_fb2 + old_nonfb2, new_fb2 + new_nonfb2
54 return sqlhub.doInTransaction(_import_data)
57 if __name__ == '__main__':
58 ml_dir = os.path.dirname(__file__)
59 print parse_glst_file(os.path.join(ml_dir, 'data', 'genres_fb2.glst'))