def __init__(self, instance, handler=None): self.instance = instance class OptionsForm(WidgetOptionsForm): pass class SensorsForm(WidgetSensorsForm): pass class CommandsForm(WidgetCommandsForm): pass class DevicesForm(WidgetDevicesForm): pass widgetoptions = WidgetOption.getWidget(instance.widget_id) if widgetoptions: self.has_options = True widgetsensors = WidgetSensor.getWidget(instance.widget_id) if widgetsensors: self.has_sensors = True widgetcommands = WidgetCommand.getWidget(instance.widget_id) if widgetcommands: self.has_commands = True widgetdevices = WidgetDevice.getWidget(instance.widget_id) if widgetdevices: self.has_devices = True if not handler: options = WidgetInstanceOption.getInstance(instance.id) dataOptions = dict([(r.key, r.value) for r in options]) sensors = WidgetInstanceSensor.getInstance(instance.id) dataSensors = dict([(r.key, r.sensor_id) for r in sensors]) commands = WidgetInstanceCommand.getInstance(instance.id) dataCommands = dict([(r.key, r.command_id) for r in commands]) devices = WidgetInstanceDevice.getInstance(instance.id) dataDevices = dict([(r.key, r.device_id) for r in devices]) else: dataOptions = None dataSensors = None dataCommands = None dataDevices = None for option in widgetoptions: OptionsForm.addField(option=option) for option in widgetsensors: SensorsForm.addField(option=option) for option in widgetcommands: CommandsForm.addField(option=option) for option in widgetdevices: DevicesForm.addField(option=option) self.optionsform = OptionsForm(instance=instance, handler=handler, data=dataOptions, prefix='optionparam_') self.sensorsform = SensorsForm(instance=instance, handler=handler, data=dataSensors, prefix='sensorparam_') self.commandsform = CommandsForm(instance=instance, handler=handler, data=dataCommands, prefix='commandparam_') self.devicesform = DevicesForm(instance=instance, handler=handler, data=dataDevices, prefix='deviceparam_') self.generalform = WidgetGeneralForm(handler=handler, data=dataOptions, prefix='generalparam_') self.styleform = WidgetStyleForm(handler=handler, data=dataOptions, prefix='styleparam_')
def __init__(self, instance, handler=None): self.instance = instance class OptionsForm(WidgetOptionsForm): pass class SensorsForm(WidgetSensorsForm): pass class CommandsForm(WidgetCommandsForm): pass class DevicesForm(WidgetDevicesForm): pass widgetoptions = WidgetOption.getWidget(instance.widget_id) if widgetoptions: self.has_options = True widgetsensors = WidgetSensor.getWidget(instance.widget_id) if widgetsensors: self.has_sensors = True widgetcommands = WidgetCommand.getWidget(instance.widget_id) if widgetcommands: self.has_commands = True widgetdevices = WidgetDevice.getWidget(instance.widget_id) if widgetdevices: self.has_devices = True if not handler: options = WidgetInstanceOption.getInstance(instance.id) dataOptions = dict([(r.key, r.value) for r in options]) sensors = WidgetInstanceSensor.getInstance(instance.id) dataSensors = dict([(r.key, r.sensor_id) for r in sensors]) # Rebuild array values reg = re.compile(r"(.*)-(\d+)") tmpdict = {} for k, v in dataSensors.iteritems(): res = reg.match(k) if res: if (res.group(1) not in tmpdict): tmpdict[res.group(1)] = [] tmpdict[res.group(1)].append(v) dataSensors = dict(dataSensors.items() + tmpdict.items()) commands = WidgetInstanceCommand.getInstance(instance.id) dataCommands = dict([(r.key, r.command_id) for r in commands]) devices = WidgetInstanceDevice.getInstance(instance.id) dataDevices = dict([(r.key, r.device_id) for r in devices]) else: dataOptions = None dataSensors = None dataCommands = None dataDevices = None for option in widgetoptions: OptionsForm.addField(option=option) for option in widgetsensors: SensorsForm.addField(option=option) for option in widgetcommands: CommandsForm.addField(option=option) for option in widgetdevices: DevicesForm.addField(option=option) self.optionsform = OptionsForm(instance=instance, handler=handler, data=dataOptions, prefix='optionparam_') self.sensorsform = SensorsForm(instance=instance, handler=handler, data=dataSensors, prefix='sensorparam_') self.commandsform = CommandsForm(instance=instance, handler=handler, data=dataCommands, prefix='commandparam_') self.devicesform = DevicesForm(instance=instance, handler=handler, data=dataDevices, prefix='deviceparam_') self.generalform = WidgetGeneralForm(handler=handler, data=dataOptions, prefix='generalparam_') self.styleform = WidgetStyleForm(handler=handler, data=dataOptions, prefix='styleparam_')
def loadWidgets(cls, pack_path, develop): session = Session() # Load all Widgets logger.info(u"PACKS: Loading widgets") widgets_path = os.path.join(pack_path, 'widgets') session.query(Widget).delete() session.query(WidgetOption).delete() session.query(WidgetCommand).delete() session.query(WidgetSensor).delete() session.query(WidgetDevice).delete() if os.path.isdir(widgets_path): for file in os.listdir(widgets_path): if not file.startswith('.'): # not hidden file info = os.path.join(widgets_path, file, "info.json") if os.path.isfile(info): widgetset_file = open(info, "r") try: widgetset_json = json.load( widgetset_file, object_pairs_hook=OrderedDict) except Exception, e: logger.error(u"Parsing error : %s: %s" % (info, str(e))) # raise e else: widgetset_id = widgetset_json["identity"]["id"] widgetset_name = widgetset_json["identity"]["name"] widgetset_version = widgetset_json["identity"][ "version"] widgetset_widgets = widgetset_json["widgets"] for wid, widget in widgetset_widgets.items(): if develop or (not develop and not 'dev' in widget): widget_id = "%s-%s" % (widgetset_id, wid) w = Widget( id=widget_id, set_id=widgetset_id, set_name=unicode(widgetset_name), set_ref=wid, version=widgetset_version, name=unicode(widget['name']), height=widget['height'], width=widget['width']) session.add(w) if 'default_style' in widget: w.default_style = ( widget['default_style'] == True) else: w.default_style = True # Specific Style if 'style' in widget: w.style = unicode( json.dumps(widget['style'])) # Options if 'options' in widget: for pid, param in widget[ 'options'].items(): id = "%s-%s" % (widget_id, pid) try: description = unicode( param['description']) except KeyError: description = None p = WidgetOption( id=id, widget_id=widget_id, key=pid, name=unicode(param['name']), type=param['type']) if 'description' in param: p.description = unicode( param['description']) if 'default' in param: p.default = param['default'] if 'required' in param: p.required = param['required'] else: p.required = True parameters = {} if 'min_length' in param: parameters[ 'min_length'] = param[ 'min_length'] if 'max_length' in param: parameters[ 'max_length'] = param[ 'max_length'] if 'min_value' in param: parameters[ 'min_value'] = param[ 'min_value'] if 'multilignes' in param: parameters[ 'multilignes'] = param[ 'multilignes'] if 'max_value' in param: parameters[ 'max_value'] = param[ 'max_value'] if 'mask' in param: parameters['mask'] = param[ 'mask'] if 'choices' in param: parameters['choices'] = param[ 'choices'] p.parameters = unicode( json.dumps(parameters)) session.add(p) if 'sensors' in widget: # Sensors parameters for pid, param in widget[ 'sensors'].items(): id = "%s-%s" % (widget_id, pid) p = WidgetSensor( id=id, widget_id=widget_id, key=pid, name=unicode(param['name']), types=json.dumps( list(param['types']))) if 'description' in param: p.description = unicode( param['description']) if 'filters' in param: p.filters = ', '.join( param['filters']) if 'required' in param: p.required = param['required'] else: p.required = True if 'group' in param: p.group = param['group'] if 'groupmin' in param: p.groupmin = param['groupmin'] if 'groupmax' in param: p.groupmax = param['groupmax'] session.add(p) if 'commands' in widget: # Commands parameters for pid, param in widget[ 'commands'].items(): id = "%s-%s" % (widget_id, pid) p = WidgetCommand( id=id, widget_id=widget_id, key=pid, name=unicode(param['name']), types=json.dumps( param['types'])) if 'description' in param: p.description = unicode( param['description']) if 'filters' in param: p.filters = ', '.join( param['filters']) if 'required' in param: p.required = param['required'] else: p.required = True session.add(p) if 'devices' in widget: # Devices parameters for pid, param in widget[ 'devices'].items(): id = "%s-%s" % (widget_id, pid) p = WidgetDevice( id=id, widget_id=widget_id, key=pid, name=unicode(param['name']), description=unicode( param['description']), types=json.dumps( param['types'])) if 'required' in param: p.required = param['required'] else: p.required = True session.add(p)