def inject(obj, session): if DEBUG: print "%s: input information: %s " % (TAG, obj); errors = {}; preset = Session.__defaults__; preset.update(obj); # Perform errors check if (preset["time_begin"] == None): errors["time_begin"] = "time_begin couldn't have zero value, please check it"; if (preset["time_end"] == None): errors["time_end"] = "time_end couldn't have zero value, please check it"; if (len(errors)): return session, obj, errors; # Create session element toBePushed = Session(obj["time_begin"], obj["time_end"]); # Connect session with channels, there rules we are working with # 1. Scope object controls correctness of all information it contains # 2. Scope translates all information into one usual format scope = Scope.state(); if DEBUG: print "%s: current scope %s" % (TAG, str(scope)); if scope.has_key("channels") and len(scope["channels"]): for channel_title in scope["channels"]: if DEBUG: print "Connection %s with <Channel %s>" % (str(session), channel_title); channel = session.query(Channel).filter_by(title=channel_title).first(); if channel: toBePushed.channels.append(channel); session.add(toBePushed); session.flush(); if DEBUG: print "%s: %s" % (TAG, toBePushed); Scope.inject({"sessions_id": toBePushed.id}, None, True); for i in Session.__defaults__.keys(): del obj[i]; return session, obj, {};
def inject(obj, session): if DEBUG: print "%s: input information: %s " % (TAG, obj); errors = {}; preset = MeasurementPoint.__defaults__; preset.update(obj); # Perform errors check if (preset['time'] == None): errors['time'] = "Time of Measurement point couldn't have Null value, please check it"; # Create measurement point element # DESIGNING_QUATION: Can be set latitude and longitude without altitude in Measurement points? toBePushed = MeasurementPoint(preset['time'], preset['latitude'], preset["longitude"], preset["altitude"]); # Perform connection session option with session into 'session_id' field scope = Scope.state(); if DEBUG: print "%s: current scope %s" % (TAG, str(scope)); if scope.has_key("sessions_id"): toBePushed.sessions_id = scope["session_id"] else: pass #WARNING: "Session id for Measurement points have zero value, is this not mistake?"; if (len(errors)): return session, obj, errors; session.add(toBePushed); session.flush(); Scope.inject({"measurement_point_id": toBePushed.id}, None, True); if DEBUG: print "%s: %s" % (TAG, toBePushed); for i in MeasurementPoint.__defaults__.keys(): if i in obj.keys(): del obj[i]; return session, obj, {};
def inject(obj, session): if DEBUG: print "%s: input information: %s " % (TAG, obj); errors = {}; preset = SessionOption.__defaults__; preset.update(obj); # Perform errors check if (preset['title'] == None): errors['title'] = "Session option title couldn't have zero value, please check it"; # Create session option element toBePushed = SessionOption(preset['title'], preset['sessions_options_value']); # Perform connection session option with session into 'session_id' field scope = Scope.state(); if DEBUG: print "%s: current scope %s" % (TAG, str(scope)); if scope.has_key("sessions_id"): toBePushed.sessions_id = scope["session_id"] else: errors['session_id'] = "Session id for session options couldn't have zero value, we should have information about session in json-file"; if (len(errors)): return session, obj, errors; session.add(toBePushed); session.flush(); if DEBUG: print "%s: %s" % (TAG, toBePushed); for i in SessionOption.__defaults__.keys(): if i in obj.keys(): del obj[i]; return session, obj, {};
def inject(obj, session): if DEBUG: print "%s: input information: %s " % (TAG, obj); errors = {}; preset = Measurement.__defaults__; preset.update(obj); # Perform errors check if (preset['measurement'] == None): errors['measurement'] = "Measurement couldn't have Null value, please check it"; toBePushed = Measurement(preset['measurement'], preset['level_marker'], preset["relative_error"]); # Perform connection measurement with measurement_point, parameter and channel scope = Scope.state(); if DEBUG: print "%s: current scope %s" % (TAG, str(scope)); if scope.has_key("measurement_point_id"): toBePushed.measurement_points_id = scope["measurement_point_id"] else: errors["measurement_point_id"] = "Measurement point for Measurement couldn't have Null value, please check it"; if (len(errors)): return session, obj, errors; session.add(toBePushed); session.flush(); if DEBUG: print "%s: %s" % (TAG, toBePushed); for i in Measurement.__defaults__.keys(): if i in obj.keys(): del obj[i]; return session, obj, {};
def process(key, obj, session=None): ''' Process JSON-key element and recursively goes deep inside @param key: element-name of some list or JSON-dictionary which should be processed @param obj: element-value which should be processed, it's an JSON-node @param session: data base session data to be pushed to ''' # Guaranteed container of data base session __session = None; # Flag which indicates that session created at this iteration and should be # deleted here. Also if __emitter is True, it means that it's base level # of process inheritance. __emitter = False; # We should verify data base session status and assign right value for it if (session == None): # Creating new session, cause it wasn't created before __session = __connection__.session(); __emitter = True; else: # Inherit already created session __session = session; # Stop doing nothing, cause empty object couldn't be processed if (obj == None) : return; # Process list of objects with same key if (type(obj) == type([])): if DEBUG: print "%s: [%s] list of %i elements" % (TAG, str(key), len(obj)); for i in obj: process(key, i); # Process dictionary of multiple objects elif (type(obj) == type({})): # # New scope creation flag # __new_scope = False; Scope.pushLevel(); if DEBUG: print "%s: [%s] dictionary -> exact object" % (TAG, str(key)); # Select type of injector to be used to process object for injector in injectors: # Comparing injector possible keys with current if injector.check(key): if DEBUG: print "%s: [%s] injector found `%s`!" % (TAG, str(key), injector.tableName()); # # @attention: specific behavior for Scope to check __new_scope flag # # @todo: remove it with global `push` & `pop` functions for scope # if injector.check("scope"): # if DEBUG: # print "%s: new scope registered" % (TAG); # # __new_scope = True; __session, _children, _errors = injector.inject(obj, __session); # If we have any errors we should stop and give them back to user if len(_errors): __session.rollback(); return _errors; if DEBUG: print "%s: [%s] children [%i]" % (TAG, str(key), len(_children)); for child in _children.keys(): if DEBUG: print "%s: [%s] child %s" % (TAG, str(key), str(child)); process(child, _children[child]); # if __new_scope: # if DEBUG: # print "%s: removing current level scope" % (TAG); # # Scope.pop(); Scope.popLevel(); if __emitter: if DEBUG: print "%s: commit and close session" % (TAG); __session.commit(); __session.close(); return {};
injectors = [Session, SessionOption, MeasurementPoint, Measurement, Scope]; TAG = "process" # List of classes which could be used as # __db_conf__ = dbConf.select('contributor'); __db_conf__ = db.select('root'); __connection__ = DBConnection( __db_conf__['user'], __db_conf__['password'], __db_conf__['db_name'], __db_conf__['host'], __db_conf__['driver'] ); Scope.put_injectors(injectors); def process(key, obj, session=None): ''' Process JSON-key element and recursively goes deep inside @param key: element-name of some list or JSON-dictionary which should be processed @param obj: element-value which should be processed, it's an JSON-node @param session: data base session data to be pushed to ''' # Guaranteed container of data base session __session = None; # Flag which indicates that session created at this iteration and should be # deleted here. Also if __emitter is True, it means that it's base level