def generate_enum(xml_file): """ Produce a *Ac.hpp, *Ac.cpp, and *.py files for serializable enum's. Return True if xml file was an enum, othersise return False and generate nothing. """ xml = XmlParser.XmlParser(xml_file) if xml() == "enum": # # Parse enum xml here # enum_xml = XmlEnumParser.XmlEnumParser(xml_file) name = enum_xml.get_name() namespace = enum_xml.get_namespace() items = enum_xml.get_items() max_value = enum_xml.get_max_value() comment = enum_xml.get_comment() # # Generate the hpp file # fp = open_file(name, "hpp") c = enum_hpp.enum_hpp() write_template(fp, c, name, namespace, items, max_value, comment) fp.close() # # Generate the cpp file # fp = open_file(name, "cpp") c = enum_cpp.enum_cpp() write_template(fp, c, name, namespace, items, max_value, comment) fp.close() return True else: return False
def main(): """ Main program. """ global VERBOSE # prevent local creation of variable global DEPLOYMENT # deployment set in topology xml only and used to install new instance dicts ConfigManager.ConfigManager.getInstance() Parser = pinit() (opt, args) = Parser.parse_args() VERBOSE = opt.verbose_flag # # Parse the input Topology XML filename # if len(args) == 0: print("Usage: %s [options] xml_filename" % sys.argv[0]) return elif len(args) == 1: xml_filename = args[0] else: print("ERROR: Too many filenames, should only have one") return # # Check for BUILD_ROOT variable for XML port searches # if not opt.build_root_overwrite is None: set_build_roots(opt.build_root_overwrite) if VERBOSE: print("BUILD_ROOT set to %s" % ",".join(get_build_roots())) else: if ("BUILD_ROOT" in os.environ.keys()) == False: print("ERROR: Build root not set to root build path...") sys.exit(-1) set_build_roots(os.environ["BUILD_ROOT"]) if VERBOSE: print("BUILD_ROOT set to %s" % ",".join(get_build_roots())) if not "Ai" in xml_filename: print("ERROR: Missing Ai at end of file name...") raise OSError xml_type = XmlParser.XmlParser(xml_filename)() if xml_type == "assembly" or xml_type == "deployment": if VERBOSE: print("Detected Topology XML so Generating Topology C++ Files...") the_parsed_topology_xml = XmlTopologyParser.XmlTopologyParser( xml_filename) DEPLOYMENT = the_parsed_topology_xml.get_deployment() print("Found assembly or deployment named: %s\n" % DEPLOYMENT) generate_xml_dict(the_parsed_topology_xml, xml_filename, opt) else: PRINT.info("Invalid XML found...this format not supported") sys.exit(-1) sys.exit(0)
def main(): """ Main program. """ global VERBOSE global DEPLOYMENT Parser = pinit() (opt, args) = Parser.parse_args() VERBOSE = opt.verbose_flag CONFIG = ConfigManager.ConfigManager.getInstance() # Check for BUILD_ROOT env. variable if ('BUILD_ROOT' in os.environ.keys()) == False: print("ERROR: Build root not set to root build path...") sys.exit(-1) else: # Handle BUILD_ROOT BUILD_ROOT = os.environ['BUILD_ROOT'] ModelParser.BUILD_ROOT = BUILD_ROOT if VERBOSE: print("BUILD_ROOT set to %s in environment" % BUILD_ROOT) # # Parse the input Topology XML filename # if len(args) == 0: print("ERROR: Usage: %s [options] xml_filename" % sys.argv[0]) return elif len(args) == 1: xml_filename = args[0] else: print("ERROR: Too many filenames, should only have one") return # # Check for BUILD_ROOT variable for XML port searches # if not opt.build_root_overwrite == None: BUILD_ROOT = opt.build_root_overwrite ModelParser.BUILD_ROOT = BUILD_ROOT if VERBOSE: print("BUILD_ROOT set to %s" % BUILD_ROOT) else: if ('BUILD_ROOT' in os.environ.keys()) == False: print("ERROR: Build root not set to root build path...") sys.exit(-1) BUILD_ROOT = os.environ['BUILD_ROOT'] ModelParser.BUILD_ROOT = BUILD_ROOT if VERBOSE: print("BUILD_ROOT set to %s in environment" % BUILD_ROOT) if not "Ai" in xml_filename: print("ERROR: Missing Ai at end of file name...") raise IOError # # Create python dictionaries # xml_type = XmlParser.XmlParser(xml_filename)() # Only Topologies can be inputted if xml_type == "assembly" or xml_type == "deployment": if VERBOSE: print("Detected Topology XML so Generating Topology C++ Files...") the_parsed_topology_xml = XmlTopologyParser.XmlTopologyParser( xml_filename) DEPLOYMENT = the_parsed_topology_xml.get_deployment() if VERBOSE: print("Found assembly or deployment named: %s\n" % DEPLOYMENT) generate_pymods(the_parsed_topology_xml, xml_filename, opt) else: print("ERROR: Invalid XML found...this format not supported") sys.exit(-1) sys.exit(0)
def main(): """ Main program. """ global VERBOSE global BUILD_ROOT global DEPLOYMENT # Enable printing model error output to STDOUT Logger.connectOutputLogger(None) Parser = pinit() (opt, args) = Parser.parse_args() VERBOSE = opt.verbose_flag ConfigManager.ConfigManager.getInstance() # # Handle command line arguments # # # Parse the input Topology XML filename # if len(args) == 0: print("ERROR: Usage: %s [options] xml_filename" % sys.argv[0]) return elif len(args) == 1: xml_filename = args[0] else: print("ERROR: Too many filenames, should only have one") return # # Check for BUILD_ROOT variable for XML port searches # if not opt.build_root_overwrite is None: set_build_roots(opt.build_root_overwrite) if VERBOSE: print("BUILD_ROOT set to %s" % ",".join(get_build_roots())) else: if ("BUILD_ROOT" in os.environ.keys()) == False: print("ERROR: Build root not set to root build path...") sys.exit(-1) set_build_roots(os.environ["BUILD_ROOT"]) if VERBOSE: print("BUILD_ROOT set to %s" % ",".join(get_build_roots())) # # Write test component # if not "Ai" in xml_filename: print("ERROR: Missing Ai at end of file name...") raise OSError # # Create python dictionaries # print(xml_filename) xml_type = XmlParser.XmlParser(xml_filename)() # Only Components can be inputted if xml_type == "component": if VERBOSE: print("Detected Component XML so GeneratingComponent C++ Files...") the_parsed_component_xml = XmlComponentParser.XmlComponentParser( xml_filename) component_model = parse_component(the_parsed_component_xml, xml_filename, opt) if VERBOSE: print("\nGenerating tests...") generate_impl_files(opt, component_model) else: print("ERROR: {} is used for component XML files, not {} XML files". format(sys.argv[0], xml_type)) sys.exit(-1) sys.exit(0)
def main(): """ Main program. """ global ERROR # prevent local creation of variable global VERBOSE # prevent local creation of variable global GEN_TEST_CODE # indicate if test code should be generated global DEPLOYMENT # deployment set in topology xml only and used to install new instance dicts ERROR = False # Sets up the initial (singleton) instance ConfigManager.ConfigManager.getInstance() Parser = pinit() (opt, args) = Parser.parse_args() VERBOSE = opt.verbose_flag # Check that the specified working directory exists. Remember, the # default working directory is the current working directory which # always exists. We are basically only checking for when the user # specifies an alternate working directory. if os.path.exists(opt.work_path) == False: Parser.error(f"Specified path does not exist ({opt.work_path})!") working_dir = opt.work_path # Get the current working directory so that we can return to it when # the program completes. We always want to return to the place where # we started. starting_directory = os.getcwd() os.chdir(working_dir) # print working_dir # print os.getcwd() # Configure the logging. log_level = opt.logger.upper() log_level_dict = dict() log_level_dict["QUIET"] = None log_level_dict["DEBUG"] = logging.DEBUG log_level_dict["INFO"] = logging.INFO log_level_dict["WARNING"] = logging.WARN log_level_dict["ERROR"] = logging.ERROR log_level_dict["CRITICAL"] = logging.CRITICAL if log_level_dict[log_level] is None: stdout_enable = False else: stdout_enable = True log_fd = opt.logger_output # For now no log file Logger.connectDebugLogger(log_level_dict[log_level], log_fd, stdout_enable) Logger.connectOutputLogger(log_fd) # # Parse the input Component XML file and create internal meta-model # if len(args) == 0: PRINT.info(f"Usage: {sys.argv[0]} [options] xml_filename") return else: xml_filenames = args[0:] # # Check for BUILD_ROOT variable for XML port searches # if opt.build_root_flag == True: # Check for BUILD_ROOT env. variable if ("BUILD_ROOT" in list(os.environ.keys())) == False: PRINT.info( "ERROR: The -b command option requires that BUILD_ROOT environmental variable be set to root build path..." ) sys.exit(-1) else: set_build_roots(os.environ.get("BUILD_ROOT")) for xml_filename in xml_filenames: xml_type = XmlParser.XmlParser(xml_filename)() if xml_type == "component": DEBUG.info( "Detected Component XML so Generating Component C++ Files...") the_parsed_component_xml = XmlComponentParser.XmlComponentParser( xml_filename) generate_component(the_parsed_component_xml, os.path.basename(xml_filename), opt) dependency_parser = the_parsed_component_xml elif xml_type == "interface": DEBUG.info( "Detected Port type XML so Generating Port type C++ Files...") the_parsed_port_xml = XmlPortsParser.XmlPortsParser(xml_filename) generate_port(the_parsed_port_xml, os.path.basename(xml_filename)) dependency_parser = the_parsed_port_xml elif xml_type == "serializable": DEBUG.info( "Detected Serializable XML so Generating Serializable C++ Files..." ) the_serial_xml = XmlSerializeParser.XmlSerializeParser( xml_filename) generate_serializable(the_serial_xml, opt) dependency_parser = the_serial_xml elif xml_type == "assembly" or xml_type == "deployment": DEBUG.info( "Detected Topology XML so Generating Topology C++ Files...") the_parsed_topology_xml = XmlTopologyParser.XmlTopologyParser( xml_filename) DEPLOYMENT = the_parsed_topology_xml.get_deployment() print("Found assembly or deployment named: %s\n" % DEPLOYMENT) generate_topology(the_parsed_topology_xml, os.path.basename(xml_filename), opt) dependency_parser = the_parsed_topology_xml elif xml_type == "enum": DEBUG.info( "Detected Enum XML so Generating hpp, cpp, and py files...") curdir = os.getcwd() if EnumGenerator.generate_enum(xml_filename): ERROR = False PRINT.info( f"Completed generating files for {xml_filename} Enum XML...." ) else: ERROR = True os.chdir(curdir) elif xml_type == "array": DEBUG.info( "Detected Array XML so Generating hpp, cpp, and py files...") curdir = os.getcwd() if ArrayGenerator.generate_array(xml_filename): ERROR = False PRINT.info( f"Completed generating files for {xml_filename} Array XML..." ) else: ERROR = True os.chdir(curdir) else: PRINT.info("Invalid XML found...this format not supported") ERROR = True if opt.dependency_file is not None: if opt.build_root_flag: generate_dependency_file( opt.dependency_file, os.path.basename(xml_filename), list(get_build_roots())[0], dependency_parser, xml_type, ) # Always return to directory where we started. os.chdir(starting_directory) if ERROR == True: sys.exit(-1) else: sys.exit(0)
def generate_array(xml_file): """ Produce a *Ac.hpp, *Ac.cpp, and *.py files for serializable arrays. Return True if xml file was an array, otherwise return False and generate nothing. """ xml = XmlParser.XmlParser(xml_file) if xml() == "array": # # Parse array xml here # array_xml = XmlArrayParser.XmlArrayParser(xml_file) name = array_xml.get_name() namespace = array_xml.get_namespace() arr_type = array_xml.get_type() arr_typeinfo = array_xml.get_typeinfo() arr_size = int(array_xml.get_size()) format_string = array_xml.get_format() default_values = array_xml.get_default() type_id = array_xml.get_type_id() string_size = array_xml.get_string_size() if string_size: string_size = int(string_size) comment = array_xml.get_comment() include_headers = array_xml.get_include_header_files() import_serializables = array_xml.get_includes() import_enums = array_xml.get_include_enum_files() import_arrays = array_xml.get_include_array_files() include_path = array_xml.get_include_path() # Set up imports headers = [] for h in include_headers: headers.append(h.replace("Ai.xml", "Ac.hpp")) serials = [] for s in import_serializables: serials.append(s.replace("Ai.xml", "Ac.hpp")) enums = [] for e in import_enums: enums.append(e.replace("Ai.xml", "Ac.hpp")) arrays = [] for a in import_arrays: arrays.append(a.replace("Ai.xml", "Ac.hpp")) # # Generate the hpp file # fp = open_file(name, "hpp") c = array_hpp.array_hpp() write_template( fp, c, name, namespace, arr_type, arr_typeinfo, arr_size, format_string, default_values, type_id, string_size, include_path, comment, headers, serials, enums, arrays, ) fp.close() # # Generate the cpp file # fp = open_file(name, "cpp") c = array_cpp.array_cpp() write_template( fp, c, name, namespace, arr_type, arr_typeinfo, arr_size, format_string, default_values, type_id, string_size, include_path, comment, headers, serials, enums, arrays, ) fp.close() return True else: return False
for item in self.__items: max_value = max(max_value, item[1]) else: max_value = str(len(self.__items) - 1) return max_value def get_name(self): return self.__name def get_namespace(self): return self.__namespace def get_items(self): return self.__items if __name__ == '__main__': xmlfile = sys.argv[1] xml = XmlParser.XmlParser(xmlfile) print("Type of XML is: %s" % xml()) print("Enum XML parse test (%s)" % xmlfile) xml_parser = XmlEnumParser(xmlfile) print("Enum name: %s, namespace: %s" % (xml_parser.get_name(), xml_parser.get_namespace())) print("Items") for item in xml_parser.get_items(): print("%s=%s // %s" % item)
def main(): """ Main program. """ global VERBOSE global BUILD_ROOT global DEPLOYMENT Parser = pinit() (opt, args) = Parser.parse_args() VERBOSE = opt.verbose_flag CONFIG = ConfigManager.ConfigManager.getInstance() # # Handle command line arguments # # # Parse the input Topology XML filename # if len(args) == 0: print("ERROR: Usage: %s [options] xml_filename" % sys.argv[0]) return elif len(args) == 1: xml_filename = args[0] else: print("ERROR: Too many filenames, should only have one") return # # Check for BUILD_ROOT variable for XML port searches # if not opt.build_root_overwrite == None: BUILD_ROOT = opt.build_root_overwrite ModelParser.BUILD_ROOT = BUILD_ROOT if VERBOSE: print("BUILD_ROOT set to %s" % BUILD_ROOT) else: if ('BUILD_ROOT' in os.environ.keys()) == False: print("ERROR: Build root not set to root build path...") sys.exit(-1) BUILD_ROOT = os.environ['BUILD_ROOT'] ModelParser.BUILD_ROOT = BUILD_ROOT if VERBOSE: print("BUILD_ROOT set to %s in environment" % BUILD_ROOT) # # Write test component # if not "Ai" in xml_filename: print("ERROR: Missing Ai at end of file name...") raise IOError # # Create python dictionaries # print(xml_filename) xml_type = XmlParser.XmlParser(xml_filename)() # Only Components can be inputted if xml_type == "component": if VERBOSE: print("Detected Component XML so GeneratingComponent C++ Files...") the_parsed_component_xml = XmlComponentParser.XmlComponentParser( xml_filename) component_model = parse_component(the_parsed_component_xml, xml_filename, opt) if VERBOSE: print("\nGenerating tests...") generate_tests(opt, component_model) else: print("ERROR: Invalid XML found...this format not supported") sys.exit(-1) sys.exit(0)
def parse_topology(self, xml_filename, overwrite = True): """ Takes an XML File and puts all channel, event, and command data into CosmosChannel, CosmosEvent, and CosmosCommand model class instances to be passed to the Generator that creates the config files @param xml_filename: XML File Name, should be Topology if not quits @param overwrite: Flag whether to overwrite channels, events, and commands lists """ bot_dir = os.getcwd() os.chdir(CosmosUtil.STARTING_DIRECTORY) # Parser needs to be in Autocoders/bin directory to be able to find Topology XML print("\nUsing XmlParser and XmlTopologyParser instances") xml_type = XmlParser.XmlParser(xml_filename)() if xml_type == "assembly" or xml_type == "deployment": if CosmosUtil.VERBOSE: print("Detected ISF Topology XML Files...") topology = XmlTopologyParser.XmlTopologyParser(xml_filename) # Name of COSMOS target to be created self.deployment = topology.get_deployment() if CosmosUtil.VERBOSE: print("\nFound assembly or deployment named: " + self.deployment + "\n") else: print("ERROR: XML File Not a Topology File") sys.exit(-1) # Change back os.chdir(bot_dir) print("Finished Reusing XmlParser and XmlTopologyParser instances\n") if overwrite: self.channels = [] self.events = [] self.commands = [] print("Parsing Topology") print("Found %s components.\n" % len(topology.get_instances())) for inst in topology.get_instances(): comp_name = inst.get_name() comp_type = inst.get_type() base_id = inst.get_base_id() #print comp_name, comp_type, base_id # # If base_id is not set for a component assume it has nothing # and skip it. if base_id is None: print("Skipping %s:%s component - has not commands or telemetry" % (comp_name, comp_type)) continue if '0x' in base_id: base_id = int(base_id, 16) else: base_id = int(base_id) comp_parser = inst.get_comp_xml() # # Parse command data here... # if 'get_commands' in dir(comp_parser): if CosmosUtil.VERBOSE: print ("Parsing Commands for instance: " + comp_name) cmds = comp_parser.get_commands() for cmd in cmds: opcode = cmd.get_opcodes()[0] if '0x' in opcode: opcode = int(opcode, 16) else: opcode = int(opcode) opcode += base_id n = cmd.get_mnemonic() c = cmd.get_comment() p = cmd.get_priority() s = cmd.get_sync() f = cmd.get_full() source = comp_parser.get_xml_filename() cosmos_cmd = CosmosCommand.CosmosCommand(n, opcode, c) cosmos_cmd.set_component_attributes(comp_name, comp_type, source) cosmos_cmd.set_xml_attributes(p, s, f) # Count strings to see if 2 (if so needs block argument) string_count = 0 args = cmd.get_args() for arg in args: t = arg.get_type() if t == 'string': string_count += 1 is_multi_string_command = False if string_count >= 2: is_multi_string_command = True # # Parse command arg data here... # num = 0 if CosmosUtil.VERBOSE: print("Command " + n + " Found") for arg in args: n = arg.get_name() t = arg.get_type() c = arg.get_comment() # # Parse command enum here # if type(t) is type(tuple()): enum = t t = t[0][0] num += 1 if not is_multi_string_command: cosmos_cmd.add_item(n, t, c, enum) if is_multi_string_command: if CosmosUtil.VERBOSE: print("Multi-string commands not supported in COSMOS at: " + cmd.get_mnemonic() + " from " + source) else: print("Multi-string command " + cmd.get_mnemonic() + " not supported") else: self.commands.append(cosmos_cmd) if CosmosUtil.VERBOSE: print("Finished Parsing Commands for " + comp_name) # # Parse parameter data here... # if 'get_parameters' in dir(comp_parser): for prm in comp_parser.get_parameters(): enum = None n = prm.get_name() s = prm.get_size() t = prm.get_type() c = prm.get_comment() d = prm.get_default() source = comp_parser.get_xml_filename() # Parse param enum if type(t) is type(tuple()): enum = t t = t[0][0] else: if not t in CosmosUtil.TYPE_DICT.keys(): # Skip channel if has a serializable type / incorrect type print("Unsupported type " + t + ", skipping Parameter " + n) continue num += 1 # Calculate opcodes set_opcode = prm.get_set_opcodes()[0] if '0x' in set_opcode: set_opcode = int(set_opcode, 16) else: set_opcode = int(set_opcode) set_opcode += base_id save_opcode = prm.get_save_opcodes()[0] if '0x' in save_opcode: save_opcode = int(save_opcode, 16) else: save_opcode = int(save_opcode) save_opcode += base_id # Create models cosmos_prm_set = CosmosCommand.CosmosCommand((n + "_prm_set"), set_opcode, c) cosmos_prm_save = CosmosCommand.CosmosCommand((n + "_prm_save"), save_opcode, c) cosmos_prm_set.set_component_attributes(comp_name, comp_type, source) cosmos_prm_save.set_component_attributes(comp_name, comp_type, source) # Add single arguments cosmos_prm_set.add_item(n, t, c, enum, d) self.commands.append(cosmos_prm_set) self.commands.append(cosmos_prm_save) # # Parse event data here... # if "get_events" in dir(comp_parser): if CosmosUtil.VERBOSE: print("Parsing Events for " + comp_name) evrs = comp_parser.get_events() for evr in evrs: contains_unsupp_type = False evr_id =evr.get_ids()[0] if '0x' in evr_id: evr_id = int(evr_id, 16) else: evr_id = int(evr_id) evr_id += base_id n = evr.get_name() comment = evr.get_comment() s = evr.get_severity() f = evr.get_format_string() source = comp_parser.get_xml_filename() cosmos_evr = CosmosEvent.CosmosEvent(n, evr_id, comment) cosmos_evr.set_component_attributes(comp_name, comp_type, source) cosmos_evr.set_xml_attributes(s, f) if CosmosUtil.VERBOSE: print("Event " + n + " Found") # # Parse event enums here... # bit_count = 0 args = evr.get_args() for arg in args: n = arg.get_name() t = arg.get_type() s = arg.get_size() c = arg.get_comment() enum = None if type(t) is type(tuple()): enum = t t = t[0][0] cosmos_evr.add_item(n, t, c, enum) if not contains_unsupp_type: self.events.append(cosmos_evr) else: print("Skipping evr " + evr.get_name() + ", contains unsupported type") if CosmosUtil.VERBOSE: print("Finished Parsing Events for " + comp_name) # # Parse channel data here... # if "get_channels" in dir(comp_parser): if CosmosUtil.VERBOSE: print("Parsing Channels for " + comp_name) channels = comp_parser.get_channels() for ch in channels: ch_id = ch.get_ids()[0] if '0x' in ch_id: ch_id = int(ch_id, 16) else: ch_id = int(ch_id) ch_id += base_id n = ch.get_name() t = ch.get_type() enum = None if type(t) is type(tuple()): enum = t t = t[0][0] else: if not t in CosmosUtil.TYPE_DICT.keys(): # Skip channel if has a serializable type / incorrect type print("Unsupported type " + t + ", skipping Channel " + n) continue c = ch.get_comment() limits = ch.get_limits() source = comp_parser.get_xml_filename() cosmos_ch = CosmosChannel.CosmosChannel(n, ch_id, c) cosmos_ch.set_component_attributes(comp_name, comp_type, source) cosmos_ch.set_item(t, enum, ch.get_format_string()) cosmos_ch.set_limits(limits) if CosmosUtil.VERBOSE: print("Found channel " + n + " with argument type: " + t) self.channels.append(cosmos_ch) if CosmosUtil.VERBOSE: print("Finished Parsing Channels for " + comp_name) # Check command and EVR packets to see if they should apply a negative offset # NO CMD OR TLM PACKETS SHOULD BE ADDED ONCE THIS CHECK IS DONE for evr in self.events: CheetahUtil.evr_update_variable_lengths(evr) for cmd in self.commands: CheetahUtil.cmd_update_variable_lengths(cmd) print("Parsed Topology\n")