From 595ef1785c205b3c5c0e2361a73f83920684142f Mon Sep 17 00:00:00 2001 From: Ali Muhammad Date: Sat, 26 Jun 2021 03:10:20 +0500 Subject: [PATCH 1/4] for better readability added f-string --- Autocoders/Python/bin/codegen.py | 87 ++++++++++++++------------------ 1 file changed, 39 insertions(+), 48 deletions(-) diff --git a/Autocoders/Python/bin/codegen.py b/Autocoders/Python/bin/codegen.py index 8497c6821e..5dcd2c4437 100755 --- a/Autocoders/Python/bin/codegen.py +++ b/Autocoders/Python/bin/codegen.py @@ -115,7 +115,7 @@ def pinit(): current_dir = os.getcwd() usage = "usage: %prog [options] [xml_filename]" - vers = "%prog " + VERSION.id + " " + VERSION.comment + vers = f"%prog {VERSION.id} {VERSION.comment}" parser = OptionParser(usage, version=vers) @@ -133,7 +133,7 @@ def pinit(): "--path", dest="work_path", type="string", - help="Switch to new working directory (def: %s)." % current_dir, + help=f"Switch to new working directory (def: {current_dir}).", action="store", default=current_dir, ) @@ -299,7 +299,7 @@ def pinit(): def generate_topology(the_parsed_topology_xml, xml_filename, opt): - DEBUG.debug("Topology xml type description file: %s" % xml_filename) + DEBUG.debug(f"Topology xml type description file: {xml_filename}") generator = TopoFactory.TopoFactory.getInstance() if not (opt.default_topology_dict or opt.xml_topology_dict): generator.set_generate_ID(False) @@ -363,9 +363,7 @@ def generate_topology(the_parsed_topology_xml, xml_filename, opt): # comp.set_component_object(comp.) else: PRINT.info( - "Components with type {} aren't in the topology model.".format( - comp.get_type() - ) + f"Components with type {comp.get_type()} aren't in the topology model." ) # Hack to set up deployment path for instanced dictionaries (if one exists remove old one) @@ -378,26 +376,23 @@ def generate_topology(the_parsed_topology_xml, xml_filename, opt): break else: raise FileNotFoundError( - "{} not found in any of: {}".format(DEPLOYMENT, get_build_roots()) + f"{DEPLOYMENT} not found in any of: {get_build_roots()}" ) dict_dir = os.environ["DICT_DIR"] - PRINT.info("Removing old instanced topology dictionaries in: %s", dict_dir) + PRINT.info(f"Removing old instanced topology dictionaries in: {dict_dir}") import shutil if os.path.exists(dict_dir): shutil.rmtree(dict_dir) PRINT.info( - "Overriding for instanced topology dictionaries the --dict_dir option with xml derived path: %s", - dict_dir, + f"Overriding for instanced topology dictionaries the --dict_dir option with xml derived path: {dict_dir}" ) # xml_list = [] for parsed_xml_type in parsed_xml_dict: if parsed_xml_dict[parsed_xml_type] is None: PRINT.info( - "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 - ) + f"XML of type {parsed_xml_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." ) raise Exception() xml_list.append(parsed_xml_dict[parsed_xml_type]) @@ -420,7 +415,7 @@ def generate_topology(the_parsed_topology_xml, xml_filename, opt): comp_name = comp.get_name() comp_id = int(comp.get_base_id()) PRINT.debug( - "Processing {} [{}] ({})".format(comp_name, comp_type, hex(comp_id)) + f"Processing {comp_name} [{comp_type}] ({hex(comp_id)})" ) top_dict_gen.set_current_comp(comp) @@ -446,7 +441,7 @@ def generate_topology(the_parsed_topology_xml, xml_filename, opt): fileName = the_parsed_topology_xml.get_xml_filename().replace( "Ai.xml", "Dictionary.xml" ) - PRINT.info("Generating XML dictionary %s" % fileName) + PRINT.info(f"Generating XML dictionary {fileName}") fd = open( fileName, "wb" ) # Note: binary forces the same encoding of the source files @@ -514,8 +509,7 @@ def generate_component_instance_dictionary( # can't have external non-xml members if len(xml_parser_obj.get_include_header_files()): PRINT.info( - "ERROR: Component include serializables cannot use user-defined types. file: %s" - % serializable_file + f"ERROR: Component include serializables cannot use user-defined types. file: {serializable_file}" ) sys.exit(-1) @@ -535,7 +529,7 @@ def generate_component_instance_dictionary( "Commands", "InstanceCommandVisitor", True, True ) for command_model in component_model.get_commands(): - DEBUG.info("Processing command %s" % command_model.get_mnemonic()) + DEBUG.info(f"Processing command {command_model.get_mnemonic()}") defaultStartCmd = default_dict_generator.create("InstanceDictStart") defaultCmdHeader = default_dict_generator.create("InstanceDictHeader") defaultCmdBody = default_dict_generator.create("InstanceDictBody") @@ -545,7 +539,7 @@ def generate_component_instance_dictionary( defaultCmdBody(command_model, topology_model) for parameter_model in component_model.get_parameters(): - DEBUG.info("Processing parameter %s" % parameter_model.get_name()) + DEBUG.info(f"Processing parameter {parameter_model.get_name()}") defaultStartCmd = default_dict_generator.create("InstanceDictStart") defaultCmdHeader = default_dict_generator.create("InstanceDictHeader") defaultCmdBody = default_dict_generator.create("InstanceDictBody") @@ -560,7 +554,7 @@ def generate_component_instance_dictionary( "Events", "InstanceEventVisitor", True, True ) for event_model in component_model.get_events(): - DEBUG.info("Processing event %s" % event_model.get_name()) + DEBUG.info(f"Processing event {event_model.get_name()}") defaultStartEvent = default_dict_generator.create("InstanceDictStart") defaultEventHeader = default_dict_generator.create("InstanceDictHeader") defaultEventBody = default_dict_generator.create("InstanceDictBody") @@ -575,7 +569,7 @@ def generate_component_instance_dictionary( "Channels", "InstanceChannelVisitor", True, True ) for channel_model in component_model.get_channels(): - DEBUG.info("Processing channel %s" % channel_model.get_name()) + DEBUG.info(f"Processing channel {channel_model.get_name()}") defaultStartChannel = default_dict_generator.create("InstanceDictStart") defaultChannelHeader = default_dict_generator.create("InstanceDictHeader") defaultChannelBody = default_dict_generator.create("InstanceDictBody") @@ -594,7 +588,7 @@ def generate_component( """ parsed_port_xml_list = [] if opt.gen_report: - report_file = open("%sReport.txt" % xml_filename.replace("Ai.xml", ""), "w") + report_file = open(f"{xml_filename.replace('Ai.xml', '')}Report.txt", "w") num_input_ports = 0 num_output_ports = 0 @@ -607,9 +601,9 @@ def generate_component( num_output_ports = num_output_ports + int(port.get_max_number()) if len(the_parsed_component_xml.get_ports()): if num_input_ports: - report_file.write("Input Ports: %d\n" % num_input_ports) + report_file.write(f"Input Ports: {num_input_ports}\n") if num_output_ports: - report_file.write("Output Ports: %d\n" % num_output_ports) + report_file.write(f"Output Ports: {num_output_ports}\n") # Count regular commands commands = 0 @@ -631,7 +625,7 @@ def generate_component( idList += opcode + "," if commands > 0: - report_file.write("Commands: %d\n OpCodes: %s\n" % (commands, idList[:-1])) + report_file.write(f"Commands: {commands}\n OpCodes: {idList[:-1]}\n") if len(the_parsed_component_xml.get_channels()): idList = "" @@ -640,7 +634,7 @@ def generate_component( channels += len(channel.get_ids()) for id in channel.get_ids(): idList += id + "," - report_file.write("Channels: %d\n ChanIds: %s\n" % (channels, idList[:-1])) + report_file.write(f"Channels: {channels}\n ChanIds: {idList[:-1]}\n") if len(the_parsed_component_xml.get_events()): idList = "" @@ -649,7 +643,7 @@ def generate_component( events += len(event.get_ids()) for id in event.get_ids(): idList += id + "," - report_file.write("Events: %d\n EventIds: %s\n" % (events, idList[:-1])) + report_file.write(f"Events: {events}\n EventIds: {idList[:-1]}\n") if len(the_parsed_component_xml.get_parameters()): idList = "" @@ -658,9 +652,7 @@ def generate_component( parameters += len(parameter.get_ids()) for id in parameter.get_ids(): idList += id + "," - report_file.write( - "Parameters: %d\n ParamIds: %s\n" % (parameters, idList[:-1]) - ) + report_file.write(f"Parameters: {parameters}\n ParamIds: {idList[:-1]}\n") # # Configure the meta-model for the component # @@ -689,8 +681,7 @@ def generate_component( # can't have external non-xml members if len(xml_parser_obj.get_include_header_files()): PRINT.info( - "ERROR: Component include serializables cannot use user-defined types. file: %s" - % serializable_file + f"ERROR: Component include serializables cannot use user-defined types. file: {serializable_file}" ) sys.exit(-1) @@ -835,7 +826,7 @@ def generate_component( "Commands", "CommandVisitor", True, True ) for command_model in component_model.get_commands(): - DEBUG.info("Processing command %s" % command_model.get_mnemonic()) + DEBUG.info(f"Processing command {command_model.get_mnemonic()}") defaultStartCmd = default_dict_generator.create("DictStart") defaultCmdHeader = default_dict_generator.create("DictHeader") defaultCmdBody = default_dict_generator.create("DictBody") @@ -845,7 +836,7 @@ def generate_component( defaultCmdBody(command_model) for parameter_model in component_model.get_parameters(): - DEBUG.info("Processing parameter %s" % parameter_model.get_name()) + DEBUG.info(f"Processing parameter {parameter_model.get_name()}") defaultStartCmd = default_dict_generator.create("DictStart") defaultCmdHeader = default_dict_generator.create("DictHeader") defaultCmdBody = default_dict_generator.create("DictBody") @@ -858,7 +849,7 @@ def generate_component( # iterate through command instances default_dict_generator.configureVisitor("Events", "EventVisitor", True, True) for event_model in component_model.get_events(): - DEBUG.info("Processing event %s" % event_model.get_name()) + DEBUG.info(f"Processing event {event_model.get_name()}") defaultStartEvent = default_dict_generator.create("DictStart") defaultEventHeader = default_dict_generator.create("DictHeader") defaultEventBody = default_dict_generator.create("DictBody") @@ -873,7 +864,7 @@ def generate_component( "Channels", "ChannelVisitor", True, True ) for channel_model in component_model.get_channels(): - DEBUG.info("Processing channel %s" % channel_model.get_name()) + DEBUG.info(f"Processing channel {channel_model.get_name()}") defaultStartChannel = default_dict_generator.create("DictStart") defaultChannelHeader = default_dict_generator.create("DictHeader") defaultChannelBody = default_dict_generator.create("DictBody") @@ -923,7 +914,7 @@ def generate_port(the_parsed_port_xml, port_file): # # Configure the meta-model for the component # - DEBUG.debug("Port xml type description file: %s" % port_file) + DEBUG.debug(f"Port xml type description file: {port_file}") generator = PortFactory.PortFactory.getInstance() port_model = generator.create(the_parsed_port_xml) # @@ -1000,7 +991,7 @@ def generate_serializable(the_serial_xml, opt): # Configure the meta-model for the serializable here # f = the_serial_xml.get_xml_filename() - DEBUG.debug("Serializable xml type description file: %s" % f) + DEBUG.debug(f"Serializable xml type description file: {f}") n = the_serial_xml.get_name() ns = the_serial_xml.get_namespace() c = the_serial_xml.get_comment() @@ -1031,7 +1022,7 @@ def generate_serializable(the_serial_xml, opt): # only generate if serializable is usable for dictionary. Can't have includes of other types if opt.default_dict: if len(i) != 0 or len(i2) != 0: - PRINT.info("Dictionary: Skipping %s because of external includes" % (f)) + PRINT.info(f"Dictionary: Skipping {f} because of external includes") else: # borrow source visitor pattern for serializable dictionary if opt.dict_dir is None: @@ -1042,7 +1033,7 @@ def generate_serializable(the_serial_xml, opt): if opt.default_topology_dict: if len(i) != 0 or len(i2) != 0: - PRINT.info("Dictionary: Skipping %s because of external includes" % (f)) + PRINT.info(f"Dictionary: Skipping {f} because of external includes") else: # borrow source visitor pattern for serializable dictionary if opt.dict_dir is None: @@ -1104,7 +1095,7 @@ def generate_dependency_file(filename, target_file, subst_path, parser, type): # verify directory exists for dependency file and is directory if not os.path.isdir(os.path.dirname(filename)): PRINT.info( - "ERROR: Dependency file path %s does not exist!", os.path.dirname(filename) + f"ERROR: Dependency file path {os.path.dirname(filename)} does not exist!" ) sys.exit(-1) @@ -1126,7 +1117,7 @@ def generate_dependency_file(filename, target_file, subst_path, parser, type): # print("sub: %s\ndep_file: %s\ntdir: %s\ntfile: %s\nfp: %s"%(subst_path_local,filename,target_directory,target_file_local,full_path)) # write target to file - dep_file.write("%s:" % full_path) + dep_file.write(f"{full_path}:") # assemble list of files @@ -1161,7 +1152,7 @@ def generate_dependency_file(filename, target_file, subst_path, parser, type): # for f in file_list_tmp: # file_list.append(f.replace("Ai.xml","Ac.hpp")) else: - PRINT.info("ERROR: Unrecognized dependency type %s!", type) + PRINT.info(f"ERROR: Unrecognized dependency type {type}!") sys.exit(-1) # write dependencies @@ -1175,7 +1166,7 @@ def generate_dependency_file(filename, target_file, subst_path, parser, type): ) sys.exit(-1) - dep_file.write("\\\n %s " % full_path) + dep_file.write(f"\\\n {full_path} ") # carriage return dep_file.write("\n\n") @@ -1205,7 +1196,7 @@ def main(): # specifies an alternate working directory. if os.path.exists(opt.work_path) == False: - Parser.error("Specified path does not exist (%s)!" % opt.work_path) + Parser.error(f"Specified path does not exist ({opt.work_path})!") working_dir = opt.work_path @@ -1243,7 +1234,7 @@ def main(): # Parse the input Component XML file and create internal meta-model # if len(args) == 0: - PRINT.info("Usage: %s [options] xml_filename" % sys.argv[0]) + PRINT.info(f"Usage: {sys.argv[0]} [options] xml_filename") return else: xml_filenames = args[0:] @@ -1300,7 +1291,7 @@ def main(): if EnumGenerator.generate_enum(xml_filename): ERROR = False PRINT.info( - "Completed generating files for %s Enum XML...." % xml_filename + f"Completed generating files for {xml_filename} Enum XML...." ) else: ERROR = True @@ -1311,7 +1302,7 @@ def main(): if ArrayGenerator.generate_array(xml_filename): ERROR = False PRINT.info( - "Completed generating files for %s Array XML..." % xml_filename + f"Completed generating files for {xml_filename} Array XML..." ) else: ERROR = True From 2dfff4282af107d15f300daa096e1855d6e48d73 Mon Sep 17 00:00:00 2001 From: Ali Muhammad Date: Sat, 26 Jun 2021 03:23:53 +0500 Subject: [PATCH 2/4] using builtin function type as variable --- Autocoders/Python/bin/codegen.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Autocoders/Python/bin/codegen.py b/Autocoders/Python/bin/codegen.py index 5dcd2c4437..8d7c364b94 100755 --- a/Autocoders/Python/bin/codegen.py +++ b/Autocoders/Python/bin/codegen.py @@ -924,12 +924,12 @@ def generate_port(the_parsed_port_xml, port_file): # # Configure file names and each visitor here. # - type = the_parsed_port_xml.get_interface().get_name() + the_type = the_parsed_port_xml.get_interface().get_name() # # Configure each visitor here. # if "Ai" in port_file: - base = type + base = the_type h_instance_name = base + "_H" cpp_instance_name = base + "_Cpp" else: @@ -1090,7 +1090,7 @@ def generate_serializable(the_serial_xml, opt): finishSource(model) -def generate_dependency_file(filename, target_file, subst_path, parser, type): +def generate_dependency_file(filename, target_file, subst_path, parser, the_type): # verify directory exists for dependency file and is directory if not os.path.isdir(os.path.dirname(filename)): @@ -1121,14 +1121,14 @@ def generate_dependency_file(filename, target_file, subst_path, parser, type): # assemble list of files - if type == "interface": + if the_type == "interface": file_list = ( parser.get_include_header_files() + parser.get_includes_serial_files() + parser.get_include_enum_files() + parser.get_include_array_files() ) - elif type == "component": + elif the_type == "component": file_list = ( parser.get_port_type_files() + parser.get_header_files() @@ -1137,14 +1137,14 @@ def generate_dependency_file(filename, target_file, subst_path, parser, type): + parser.get_enum_type_files() + parser.get_array_type_files() ) - elif type == "serializable": + elif the_type == "serializable": file_list = ( parser.get_include_header_files() + parser.get_includes() + parser.get_include_enums() + parser.get_include_arrays() ) - elif type == "assembly" or type == "deployment": + elif the_type == "assembly" or the_type == "deployment": # get list of dependency files from XML/header file list file_list_tmp = list(parser.get_comp_type_file_header_dict().keys()) file_list = file_list_tmp @@ -1152,7 +1152,7 @@ def generate_dependency_file(filename, target_file, subst_path, parser, type): # for f in file_list_tmp: # file_list.append(f.replace("Ai.xml","Ac.hpp")) else: - PRINT.info(f"ERROR: Unrecognized dependency type {type}!") + PRINT.info(f"ERROR: Unrecognized dependency type {the_type}!") sys.exit(-1) # write dependencies From 9ba83eb367a8df157ed90ebe647cd0f680f872ee Mon Sep 17 00:00:00 2001 From: Ali Muhammad Date: Tue, 29 Jun 2021 00:14:36 +0500 Subject: [PATCH 3/4] removed extra space --- Autocoders/Python/bin/codegen.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Autocoders/Python/bin/codegen.py b/Autocoders/Python/bin/codegen.py index 8d7c364b94..7c19a0750c 100755 --- a/Autocoders/Python/bin/codegen.py +++ b/Autocoders/Python/bin/codegen.py @@ -419,7 +419,6 @@ def generate_topology(the_parsed_topology_xml, xml_filename, opt): ) top_dict_gen.set_current_comp(comp) - top_dict_gen.check_for_enum_xml() top_dict_gen.check_for_serial_xml() top_dict_gen.check_for_commands() From c87dbb8924a8fd3452b864d59855e71c7502dbfc Mon Sep 17 00:00:00 2001 From: M Starch Date: Wed, 30 Jun 2021 08:57:01 -0700 Subject: [PATCH 4/4] lestarch: correcting formatting failure --- Autocoders/Python/bin/codegen.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Autocoders/Python/bin/codegen.py b/Autocoders/Python/bin/codegen.py index 7c19a0750c..6279107ba2 100755 --- a/Autocoders/Python/bin/codegen.py +++ b/Autocoders/Python/bin/codegen.py @@ -414,9 +414,7 @@ def generate_topology(the_parsed_topology_xml, xml_filename, opt): comp_type = comp.get_type() comp_name = comp.get_name() comp_id = int(comp.get_base_id()) - PRINT.debug( - f"Processing {comp_name} [{comp_type}] ({hex(comp_id)})" - ) + PRINT.debug(f"Processing {comp_name} [{comp_type}] ({hex(comp_id)})") top_dict_gen.set_current_comp(comp) top_dict_gen.check_for_enum_xml()