def DictBodyWrite(self, obj, topology_model): """ Defined to generate the body of the Python command class @param obj: the instance of the command model to operation on. """ try: instance_obj_list = topology_model.get_base_id_dict()[ obj.get_component_base_name()] except Exception: PRINT.info( "ERROR: Could not find instance object for component " + obj.get_component_base_name() + ". Check topology model to see if the component was instanced." ) raise if type(obj) is Command.Command: for instance_obj in instance_obj_list: c = CommandBody.CommandBody() # only add the suffix if there is more than one opcode per command if instance_obj[3].get_dict_short_name() is not None: fname = "{}_{}".format( instance_obj[3].get_dict_short_name(), obj.get_mnemonic()) elif (not topology_model.get_prepend_instance_name() and len(instance_obj_list) == 1): fname = obj.get_mnemonic() else: fname = "{}_{}".format(instance_obj[0], obj.get_mnemonic()) c.mnemonic = fname try: c.opcode = hex( int(float(obj.get_base_opcode())) + instance_obj[1]) except (ValueError, TypeError): c.opcode = hex( int(obj.get_base_opcode(), 16) + instance_obj[1]) c.description = obj.get_comment() c.component = obj.get_component_name() c.arglist = list() c.ser_import_list = list() for arg_obj in obj.get_args(): # convert XML types to Python classes ( type_string, ser_import, dontcare, dontcare2, ) = DictTypeConverter.DictTypeConverter().convert( arg_obj.get_type(), arg_obj.get_size()) if ser_import is not None: c.ser_import_list.append(ser_import) c.arglist.append((arg_obj.get_name(), arg_obj.get_comment(), type_string)) self._writeTmpl(c, self.__fp1[fname], "commandBodyWrite") self.__fp1[fname].close() if type(obj) is Parameter.Parameter: for instance_obj in instance_obj_list: # Set Command c = CommandBody.CommandBody() if instance_obj[3].get_dict_short_name() is not None: fname = "{}_{}".format( instance_obj[3].get_dict_short_name(), self.__stem) elif (not topology_model.get_prepend_instance_name() and len(instance_obj_list) == 1): fname = self.__stem else: fname = "{}_{}".format(instance_obj[0], self.__stem) c.mnemonic = fname + "_PRM_SET" try: c.opcode = hex( int(float(obj.get_base_setop())) + instance_obj[1]) except (ValueError, TypeError): c.opcode = hex( int(obj.get_base_setop(), 16) + instance_obj[1]) c.description = obj.get_comment() c.component = obj.get_component_name() c.arglist = list() c.ser_import_list = list() # convert XML types to Python classes ( type_string, ser_import, dontcare, dontcare2, ) = DictTypeConverter.DictTypeConverter().convert( obj.get_type(), obj.get_size()) if ser_import is not None: c.ser_import_list.append(ser_import) c.arglist.append( (obj.get_name(), obj.get_comment(), type_string)) self._writeTmpl(c, self.__fp1[fname], "commandBodyWrite") self.__fp1[fname].close() for instance_obj in instance_obj_list: # Set Command c = CommandBody.CommandBody() if instance_obj[3].get_dict_short_name() is not None: fname = "{}_{}".format( instance_obj[3].get_dict_short_name(), self.__stem) elif (not topology_model.get_prepend_instance_name() and len(instance_obj_list) == 1): fname = self.__stem else: fname = "{}_{}".format(instance_obj[0], self.__stem) c.mnemonic = fname + "_PRM_SAVE" try: c.opcode = hex( int(float(obj.get_base_saveop())) + instance_obj[1]) except (ValueError, TypeError): c.opcode = hex( int(obj.get_base_saveop(), 16) + instance_obj[1]) c.description = obj.get_comment() c.component = obj.get_component_name() c.arglist = list() c.ser_import_list = list() self._writeTmpl(c, self.__fp2[fname], "commandBodyWrite") self.__fp2[fname].close()
def DictBodyVisit(self, obj, topology_model): """ Defined to generate the body of the Python event class @parms obj: the instance of the event model to operation on. """ try: instance_obj_list = topology_model.get_base_id_dict()[ obj.get_component_base_name()] except Exception: PRINT.info( "ERROR: Could not find instance object for component " + obj.get_component_base_name() + ". Check topology model to see if the component was instanced." ) raise for instance_obj in instance_obj_list: c = EventBody.EventBody() if instance_obj[3].get_dict_short_name() != None: fname = "{}_{}".format(instance_obj[3].get_dict_short_name(), obj.get_name()) elif not topology_model.get_prepend_instance_name() and len( instance_obj_list) == 1: fname = obj.get_name() else: fname = "{}_{}".format(instance_obj[0], obj.get_name()) c.name = fname if len(obj.get_ids()) > 1: raise Exception( "There is more than one event id when creating dictionaries. Check xml of {} or see if multiple explicit IDs exist in the AcConstants.ini file" .format(fname)) try: c.id = hex(instance_obj[1] + int(float(obj.get_ids()[0]))) except: c.id = hex(instance_obj[1] + int(obj.get_ids()[0], 16)) c.severity = obj.get_severity() c.format_string = obj.get_format_string() c.description = obj.get_comment() c.component = obj.get_component_name() c.arglist = list() c.ser_import_list = list() arg_num = 0 for arg_obj in obj.get_args(): n = arg_obj.get_name() t = arg_obj.get_type() s = arg_obj.get_size() d = arg_obj.get_comment() # convert XML types to Python classes (type_string, ser_import, type_name, dontcare) = DictTypeConverter.DictTypeConverter().convert( t, s) if ser_import != None: c.ser_import_list.append(ser_import) # convert format specifier if necessary if type_name == "enum": format_string = DictTypeConverter.DictTypeConverter( ).format_replace(c.format_string, arg_num, 'd', 's') # check for an error if format_string == None: PRINT.info( "Event %s in component %s had error processing format specifier" % (c.name, c.component)) sys.exit(-1) else: c.format_string = format_string c.arglist.append((n, d, type_string)) arg_num += 1 self._writeTmpl(c, self.__fp[fname], "eventBodyVisit") self.__fp[fname].close()
def DictBodyVisit(self, obj): """ Defined to generate the body of the Python command class @parms obj: the instance of the command model to operation on. """ if type(obj) is Command.Command: inst = 0 for opcode in obj.get_opcodes(): c = CommandBody.CommandBody() # only add the suffix if there is more than one opcode per command if (len(obj.get_opcodes()) > 1): c.mnemonic = obj.get_mnemonic() + "_%d" % inst else: c.mnemonic = obj.get_mnemonic() c.opcode = opcode c.description = obj.get_comment() c.component = obj.get_component_name() c.arglist = list() c.ser_import_list = list() for arg_obj in obj.get_args(): # convert XML types to Python classes (type_string, ser_import, dontcare, dontcare2 ) = DictTypeConverter.DictTypeConverter().convert( arg_obj.get_type(), arg_obj.get_size()) if ser_import != None: c.ser_import_list.append(ser_import) c.arglist.append((arg_obj.get_name(), arg_obj.get_comment(), type_string)) self._writeTmpl(c, self.__fp1[inst], "commandBodyVisit") self.__fp1[inst].close() inst += 1 if type(obj) is Parameter.Parameter: inst = 0 for opcode in obj.get_set_opcodes(): # Set Command c = CommandBody.CommandBody() if len(obj.get_set_opcodes()) > 1: c.mnemonic = "%s_%d_PRM_SET" % (self.__stem, inst) else: c.mnemonic = "%s_PRM_SET" % (self.__stem) c.opcode = opcode c.description = obj.get_comment() c.component = obj.get_component_name() c.arglist = list() c.ser_import_list = list() # convert XML types to Python classes (type_string, ser_import, dontcare, dontcare2) = DictTypeConverter.DictTypeConverter().convert( obj.get_type(), obj.get_size()) if ser_import != None: c.ser_import_list.append(ser_import) c.arglist.append( (obj.get_name(), obj.get_comment(), type_string)) self._writeTmpl(c, self.__fp1[inst], "commandBodyVisit") self.__fp1[inst].close() inst += 1 inst = 0 for opcode in obj.get_save_opcodes(): # Save Command c = CommandBody.CommandBody() if (len(obj.get_save_opcodes()) > 1): c.mnemonic = "%s_%d_PRM_SAVE" % (self.__stem, inst) else: c.mnemonic = "%s_PRM_SAVE" % (self.__stem) c.opcode = opcode c.description = obj.get_comment() c.component = obj.get_component_name() c.arglist = list() c.ser_import_list = list() self._writeTmpl(c, self.__fp2[inst], "commandBodyVisit") self.__fp2[inst].close() inst += 1
def DictBodyVisit(self, obj, topology_model): """ Defined to generate the body of the Python channel class @param obj: the instance of the channel model to operation on. """ try: instance_obj_list = topology_model.get_base_id_dict()[ obj.get_component_base_name()] except Exception: PRINT.info( "ERROR: Could not find instance object for component " + obj.get_component_base_name() + ". Check topology model to see if the component was instanced." ) raise for instance_obj in instance_obj_list: c = ChannelBody.ChannelBody() if instance_obj[3].get_dict_short_name() is not None: fname = "{}_{}".format(instance_obj[3].get_dict_short_name(), obj.get_name()) elif (not topology_model.get_prepend_instance_name() and len(instance_obj_list) == 1): fname = obj.get_name() else: fname = "{}_{}".format(instance_obj[0], obj.get_name()) c.name = fname if len(obj.get_ids()) > 1: raise Exception( "There is more than one event id when creating dictionaries. Check xml of {} or see if multiple explicit IDs exist in the AcConstants.ini file" .format(fname)) try: c.id = hex(instance_obj[1] + int(float(obj.get_ids()[0]))) except (ValueError, TypeError): c.id = hex(instance_obj[1] + int(obj.get_ids()[0], 16)) c.description = obj.get_comment() c.format_string = obj.get_format_string() c.component = obj.get_component_name() ( c.low_red, c.low_orange, c.low_yellow, c.high_yellow, c.high_orange, c.high_red, ) = obj.get_limits() c.ser_import = None ( c.type, c.ser_import, type_name, dontcare, ) = DictTypeConverter.DictTypeConverter().convert( obj.get_type(), obj.get_size()) # special case for enums and Gse GUI. Needs to convert %d to %s if type_name == "enum": c.format_string = "%s" self._writeTmpl(c, self.__fp[fname], "channelBodyVisit") self.__fp[fname].close()
def generate_xml_dict(the_parsed_topology_xml, xml_filename, opt): """ Generates GDS XML dictionary from parsed topology XML """ if VERBOSE: print("Topology xml type description file: %s" % xml_filename) model = TopoFactory.TopoFactory.getInstance() topology_model = model.create(the_parsed_topology_xml) generator = GenFactory.GenFactory.getInstance() #uses the topology model to process the items #create list of used parsed component xmls parsed_xml_dict = {} for comp in the_parsed_topology_xml.get_instances(): if comp.get_type() in topology_model.get_base_id_dict(): parsed_xml_dict[comp.get_type()] = comp.get_comp_xml() else: PRINT.info( "Components with type {} aren't in the topology model.".format( comp.get_type())) # xml_list = [] for parsed_xml_type in parsed_xml_dict: if parsed_xml_dict[parsed_xml_type] == None: print( "ERROR: XML of type {} is being used, but has not been parsed correctly. Check if file exists or add xml file with the 'import_component_type' tag to the Topology file." .format(parsed_xml_type)) raise Exception() xml_list.append(parsed_xml_dict[parsed_xml_type]) topology_model.set_instance_xml_list(xml_list) topology_dict = etree.Element("dictionary") topology_dict.attrib["topology"] = the_parsed_topology_xml.get_name() # create a new XML tree for dictionary enum_list = etree.Element("enums") serializable_list = etree.Element("serializables") command_list = etree.Element("commands") event_list = etree.Element("events") telemetry_list = etree.Element("channels") parameter_list = etree.Element("parameters") for comp in the_parsed_topology_xml.get_instances(): comp_type = comp.get_type() comp_name = comp.get_name() comp_id = int(comp.get_base_id()) PRINT.debug("Processing %s [%s] (%s)" % (comp_name, comp_type, hex(comp_id))) # check for included serializable XML if (parsed_xml_dict[comp_type].get_serializable_type_files() != None): serializable_file_list = parsed_xml_dict[ comp_type].get_serializable_type_files() for serializable_file in serializable_file_list: serializable_file = search_for_file("Serializable", serializable_file) serializable_model = XmlSerializeParser.XmlSerializeParser( serializable_file) if (len(serializable_model.get_includes()) != 0): raise Exception( "%s: Can only include one level of serializable for dictionaries" % serializable_file) serializable_elem = etree.Element("serializable") serializable_type = serializable_model.get_namespace( ) + "::" + serializable_model.get_name() serializable_elem.attrib["type"] = serializable_type members_elem = etree.Element("members") for (member_name, member_type, member_size, member_format_specifier, member_comment) in serializable_model.get_members(): member_elem = etree.Element("member") member_elem.attrib["name"] = member_name member_elem.attrib[ "format_specifier"] = member_format_specifier if member_comment != None: member_elem.attrib["description"] = member_comment if type(member_type) == type(tuple()): enum_value = 0 type_name = "%s::%s::%s" % ( serializable_type, member_name, member_type[0][1]) # Add enum entry enum_elem = etree.Element("enum") enum_elem.attrib["type"] = type_name # Add enum members for (membername, value, comment) in member_type[1]: enum_mem = etree.Element("item") enum_mem.attrib["name"] = membername # keep track of incrementing enum value if value != None: enum_value = int(value) enum_mem.attrib["value"] = "%d" % enum_value enum_value = enum_value + 1 if comment != None: enum_mem.attrib["description"] = comment enum_elem.append(enum_mem) enum_list.append(enum_elem) else: type_name = member_type if member_type == "string": member_elem.attrib["len"] = member.get_size() member_elem.attrib["type"] = type_name members_elem.append(member_elem) serializable_elem.append(members_elem) dup = False for ser in serializable_list: if ser.attrib["type"] == serializable_elem.attrib["type"]: dup = True if not dup: serializable_list.append(serializable_elem) # check for commands if (parsed_xml_dict[comp_type].get_commands() != None): for command in parsed_xml_dict[comp_type].get_commands(): if VERBOSE: print("Processing Command %s" % command.get_mnemonic()) command_elem = etree.Element("command") command_elem.attrib["component"] = comp_name command_elem.attrib["mnemonic"] = command.get_mnemonic() command_elem.attrib["opcode"] = "%s" % ( hex(int(command.get_opcodes()[0], base=0) + comp_id)) if ("comment" in list(command_elem.attrib.keys())): command_elem.attrib["description"] = command_elem.attrib[ "comment"] args_elem = etree.Element("args") for arg in command.get_args(): arg_elem = etree.Element("arg") arg_elem.attrib["name"] = arg.get_name() arg_type = arg.get_type() if type(arg_type) == type(tuple()): enum_value = 0 type_name = "%s::%s::%s" % (comp_type, arg.get_name(), arg_type[0][1]) # Add enum entry enum_elem = etree.Element("enum") enum_elem.attrib["type"] = type_name # Add enum members for (membername, value, comment) in arg_type[1]: enum_mem = etree.Element("item") enum_mem.attrib["name"] = membername # keep track of incrementing enum value if value != None: enum_value = int(value) enum_mem.attrib["value"] = "%d" % enum_value enum_value = enum_value + 1 if comment != None: enum_mem.attrib["description"] = comment enum_elem.append(enum_mem) enum_list.append(enum_elem) else: type_name = arg_type if arg_type == "string": arg_elem.attrib["len"] = arg.get_size() arg_elem.attrib["type"] = type_name args_elem.append(arg_elem) command_elem.append(args_elem) command_list.append(command_elem) # check for channels if (parsed_xml_dict[comp_type].get_channels() != None): for chan in parsed_xml_dict[comp_type].get_channels(): if VERBOSE: print("Processing Channel %s" % chan.get_name()) channel_elem = etree.Element("channel") channel_elem.attrib["component"] = comp_name channel_elem.attrib["name"] = chan.get_name() channel_elem.attrib["id"] = "%s" % ( hex(int(chan.get_ids()[0], base=0) + comp_id)) if chan.get_format_string() != None: channel_elem.attrib[ "format_string"] = chan.get_format_string() if chan.get_comment() != None: channel_elem.attrib["description"] = chan.get_comment() channel_elem.attrib["id"] = "%s" % ( hex(int(chan.get_ids()[0], base=0) + comp_id)) if ("comment" in list(channel_elem.attrib.keys())): channel_elem.attrib["description"] = channel_elem.attrib[ "comment"] channel_type = chan.get_type() if type(channel_type) == type(tuple()): enum_value = 0 type_name = "%s::%s::%s" % (comp_type, chan.get_name(), channel_type[0][1]) # Add enum entry enum_elem = etree.Element("enum") enum_elem.attrib["type"] = type_name # Add enum members for (membername, value, comment) in channel_type[1]: enum_mem = etree.Element("item") enum_mem.attrib["name"] = membername # keep track of incrementing enum value if value != None: enum_value = int(value) enum_mem.attrib["value"] = "%d" % enum_value enum_value = enum_value + 1 if comment != None: enum_mem.attrib["description"] = comment enum_elem.append(enum_mem) enum_list.append(enum_elem) else: type_name = channel_type if channel_type == "string": channel_elem.attrib["len"] = chan.get_size() (lr, lo, ly, hy, ho, hr) = chan.get_limits() if (lr != None): channel_elem.attrib["low_red"] = lr if (lo != None): channel_elem.attrib["low_orange"] = lo if (ly != None): channel_elem.attrib["low_yellow"] = ly if (hy != None): channel_elem.attrib["high_yellow"] = hy if (ho != None): channel_elem.attrib["high_orange"] = ho if (hr != None): channel_elem.attrib["hight_red"] = hr channel_elem.attrib["type"] = type_name telemetry_list.append(channel_elem) # check for events if (parsed_xml_dict[comp_type].get_events() != None): for event in parsed_xml_dict[comp_type].get_events(): if VERBOSE: print("Processing Event %s" % event.get_name()) event_elem = etree.Element("event") event_elem.attrib["component"] = comp_name event_elem.attrib["name"] = event.get_name() event_elem.attrib["id"] = "%s" % ( hex(int(event.get_ids()[0], base=0) + comp_id)) event_elem.attrib["severity"] = event.get_severity() format_string = event.get_format_string() if ("comment" in list(event_elem.attrib.keys())): event_elem.attrib["description"] = event_elem.attrib[ "comment"] args_elem = etree.Element("args") arg_num = 0 for arg in event.get_args(): arg_elem = etree.Element("arg") arg_elem.attrib["name"] = arg.get_name() arg_type = arg.get_type() if type(arg_type) == type(tuple()): enum_value = 0 type_name = "%s::%s::%s" % (comp_type, arg.get_name(), arg_type[0][1]) # Add enum entry enum_elem = etree.Element("enum") enum_elem.attrib["type"] = type_name # Add enum members for (membername, value, comment) in arg_type[1]: enum_mem = etree.Element("item") enum_mem.attrib["name"] = membername # keep track of incrementing enum value if value != None: enum_value = int(value) enum_mem.attrib["value"] = "%d" % enum_value enum_value = enum_value + 1 if comment != None: enum_mem.attrib["description"] = comment enum_elem.append(enum_mem) enum_list.append(enum_elem) # replace enum format string %d with %s for ground system format_string = DictTypeConverter.DictTypeConverter( ).format_replace(format_string, arg_num, 'd', 's') else: type_name = arg_type if arg_type == "string": arg_elem.attrib["len"] = arg.get_size() arg_elem.attrib["type"] = type_name args_elem.append(arg_elem) arg_num += 1 event_elem.attrib["format_string"] = format_string event_elem.append(args_elem) event_list.append(event_elem) # check for parameters if (parsed_xml_dict[comp_type].get_parameters() != None): for parameter in parsed_xml_dict[comp_type].get_parameters(): if VERBOSE: print("Processing Parameter %s" % chan.get_name()) param_default = None command_elem_set = etree.Element("command") command_elem_set.attrib["component"] = comp_name command_elem_set.attrib["mnemonic"] = parameter.get_name( ) + "_PRM_SET" command_elem_set.attrib["opcode"] = "%s" % ( hex(int(parameter.get_set_opcodes()[0], base=0) + comp_id)) if ("comment" in list(command_elem.attrib.keys())): command_elem_set.attrib[ "description"] = command_elem_set.attrib[ "comment"] + " parameter set" else: command_elem_set.attrib[ "description"] = parameter.get_name( ) + " parameter set" args_elem = etree.Element("args") arg_elem = etree.Element("arg") arg_elem.attrib["name"] = "val" arg_type = parameter.get_type() if type(arg_type) == type(tuple()): enum_value = 0 type_name = "%s::%s::%s" % (comp_type, arg.get_name(), arg_type[0][1]) # Add enum entry enum_elem = etree.Element("enum") enum_elem.attrib["type"] = type_name # Add enum members for (membername, value, comment) in arg_type[1]: enum_mem = etree.Element("item") enum_mem.attrib["name"] = membername # keep track of incrementing enum value if value != None: enum_value = int(value) enum_mem.attrib["value"] = "%d" % enum_value enum_value = enum_value + 1 if comment != None: enum_mem.attrib["description"] = comment enum_elem.append(enum_mem) # assign default to be first enum member if param_default == None: param_default = membername enum_list.append(enum_elem) else: type_name = arg_type if arg_type == "string": arg_elem.attrib["len"] = arg.get_size() else: param_default = "0" arg_elem.attrib["type"] = type_name args_elem.append(arg_elem) command_elem_set.append(args_elem) command_list.append(command_elem_set) command_elem_save = etree.Element("command") command_elem_save.attrib["component"] = comp_name command_elem_save.attrib["mnemonic"] = parameter.get_name( ) + "_PRM_SAVE" command_elem_save.attrib["opcode"] = "%s" % (hex( int(parameter.get_save_opcodes()[0], base=0) + comp_id)) if ("comment" in list(command_elem.attrib.keys())): command_elem_save.attrib[ "description"] = command_elem_set.attrib[ "comment"] + " parameter set" else: command_elem_save.attrib[ "description"] = parameter.get_name( ) + " parameter save" command_list.append(command_elem_save) param_elem = etree.Element("parameter") param_elem.attrib["component"] = comp_name param_elem.attrib["name"] = parameter.get_name() param_elem.attrib["id"] = "%s" % ( hex(int(parameter.get_ids()[0], base=0) + comp_id)) if parameter.get_default() != None: param_default = parameter.get_default() param_elem.attrib["default"] = param_default parameter_list.append(param_elem) # Remove duplicates from enum list temp_enum_list = [] for enum_elem in enum_list: temp_enum_list.append(enum_elem) for enum_elem in temp_enum_list: should_remove = False for temp_enum in enum_list: # Skip over comparisons between same exact element if (id(enum_elem) == id(temp_enum)): continue # Check all attributes if temp_enum.attrib["type"] == enum_elem.attrib["type"]: should_remove = True if not len(temp_enum.getchildren()) == len( enum_elem.getchildren()) and should_remove: should_remove = False children1 = temp_enum.getchildren() children2 = enum_elem.getchildren() if children1 and children2: i = 0 while i < len(children1) and i < len(children2): if not children1[i].attrib["name"] == children2[i].attrib[ "name"] and should_remove: should_remove = False i += 1 if should_remove: break if should_remove: enum_list.remove(enum_elem) topology_dict.append(enum_list) topology_dict.append(serializable_list) topology_dict.append(command_list) topology_dict.append(event_list) topology_dict.append(telemetry_list) topology_dict.append(parameter_list) fileName = the_parsed_topology_xml.get_xml_filename().replace( "Ai.xml", "Dictionary.xml") if VERBOSE: print("Generating XML dictionary %s" % fileName) fd = open(fileName, "wb") #Note: binary forces the same encoding of the source files fd.write(etree.tostring(topology_dict, pretty_print=True)) if VERBOSE: print("Generated XML dictionary %s" % fileName) return (topology_model)