Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for better readability added f-string for formatting #779

Merged
merged 5 commits into from
Jul 2, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 46 additions & 55 deletions Autocoders/Python/bin/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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])
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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 = ""
Expand All @@ -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 = ""
Expand All @@ -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 = ""
Expand All @@ -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
#
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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)
#
Expand All @@ -933,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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -1099,12 +1090,12 @@ 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)):
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)

Expand All @@ -1126,18 +1117,18 @@ 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

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()
Expand All @@ -1146,22 +1137,22 @@ 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
# file_list = list()
# 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 {the_type}!")
sys.exit(-1)

# write dependencies
Expand All @@ -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")
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:]
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down