44
55import os , sys
66import json
7+ import logging
78
89from sqlalchemy import create_engine
910from sqlalchemy .orm import sessionmaker , scoped_session
1314import tilde .core .model as model
1415
1516
16- DB_SCHEMA_VERSION = '5.11 '
17+ DB_SCHEMA_VERSION = '5.12 '
1718SETTINGS_FILE = 'settings.json'
1819DEFAULT_SQLITE_DB = 'default.db'
1920BASE_DIR = os .path .dirname (os .path .realpath (os .path .abspath (__file__ )))
@@ -96,12 +97,19 @@ def connect_database(settings, named=None, no_pooling=False, default_actions=Tru
9697 # 3.
9798 chk = session .query (model .Hierarchy_value ).first ()
9899 if not chk :
99- if not os .path .exists (INIT_DATA ): sys .exit (INIT_DATA + ' not found!' )
100- with open (INIT_DATA ) as f :
101- while True :
102- sql = f .readline ().strip ()
103- if not sql : break
104- engine .execute (sql )
100+ if not os .path .exists (INIT_DATA ):
101+ sys .exit (INIT_DATA + ' not found!' )
102+
103+ nlines = 0
104+ f = open (INIT_DATA )
105+ statements = filter (None , f .read ().splitlines ())
106+ f .close ()
107+ for stmt in statements :
108+ engine .execute (stmt )
109+ nlines += 1
110+
111+ logging .warning ("Applied DB model from file %s" % INIT_DATA )
112+ logging .warning ("SQL statements executed: %s" % nlines )
105113
106114 session .commit ()
107115 session .close ()
@@ -120,7 +128,7 @@ def write_settings(settings):
120128 if not os .access (DATA_DIR , os .W_OK ): return False
121129 try :
122130 f = open (SETTINGS_PATH , 'w' )
123- f .writelines (json .dumps (settings , indent = 0 ))
131+ f .writelines (json .dumps (settings , indent = 4 ))
124132 f .close ()
125133 os .chmod (os .path .abspath (SETTINGS_PATH ), 0o777 ) # to avoid (or create?) IO problems with multiple users
126134 except IOError :
@@ -136,7 +144,9 @@ def get_hierarchy(settings):
136144 '''
137145 hierarchy , hierarchy_groups , hierarchy_values = [], [], {}
138146 hgroup_ids , enumerated_vals = {}, set ()
147+
139148 session = connect_database (settings )
149+
140150 for item in session .query (model .Hierarchy_value ).all ():
141151 try :
142152 hierarchy_values [item .cid ].update ({item .num : item .name })
@@ -145,27 +155,32 @@ def get_hierarchy(settings):
145155 enumerated_vals .add (item .cid )
146156 try :
147157 for item in session .query (model .Hierarchy ).all ():
148- if item .has_facet and not item .has_topic : raise RuntimeError ('Fatal error: "has_facet" implies "has_topic"' )
149- if item .slider and not '.' in item .slider : raise RuntimeError ('Fatal error: "has_slider" must have a reference to some table field' )
158+ if item .has_facet and not item .has_topic :
159+ raise RuntimeError ('Fatal error: "has_facet" implies "has_topic"' )
160+ if item .slider and not '.' in item .slider :
161+ raise RuntimeError ('Fatal error: "has_slider" must have a reference to some table field' )
162+
150163 hierarchy .append ({
151- 'cid' :item .cid ,
152- 'category' :item .name ,
153- 'source' :item .source ,
154- 'html' :item .html ,
155- 'has_slider' :item .slider ,
156- 'sort' :item .sort ,
157- 'multiple' :item .multiple ,
158- 'optional' :item .optional ,
159- 'has_summary_contrb' :item .has_summary_contrb ,
160- 'has_column' :item .has_column ,
161- 'has_facet' :item .has_facet ,
162- 'creates_topic' :item .has_topic ,
163- 'is_chem_formula' :item .chem_formula ,
164- 'plottable' :item .plottable ,
165- 'enumerated' :True if item .cid in enumerated_vals else False
164+ 'cid' : item .cid ,
165+ 'category' : item .name ,
166+ 'source' : item .source ,
167+ 'html' : item .html ,
168+ 'has_slider' : item .slider ,
169+ 'sort' : item .sort ,
170+ 'multiple' : item .multiple ,
171+ 'optional' : item .optional ,
172+ 'has_summary_contrb' : item .has_summary_contrb ,
173+ 'has_column' : item .has_column ,
174+ 'has_facet' : item .has_facet ,
175+ 'creates_topic' : item .has_topic ,
176+ 'is_chem_formula' : item .chem_formula ,
177+ 'plottable' : item .plottable ,
178+ 'enumerated' : True if item .cid in enumerated_vals else False
166179 })
167- try : hgroup_ids [item .hgroup_id ].append (item .cid )
168- except KeyError : hgroup_ids [item .hgroup_id ] = [item .cid ]
180+ try :
181+ hgroup_ids [item .hgroup_id ].append (item .cid )
182+ except KeyError :
183+ hgroup_ids [item .hgroup_id ] = [item .cid ]
169184 except RuntimeError as e :
170185 session .close ()
171186 sys .exit (e )
0 commit comments