remove the assumption of same name

This commit is contained in:
wfjsw 2023-11-11 10:46:47 -06:00
parent bc1a450124
commit 7af576e745

View File

@ -335,15 +335,9 @@ def list_scripts(scriptdirname, extension, *, include_extensions=True):
for ext in extensions.active(): for ext in extensions.active():
extension_scripts_list = ext.list_files(scriptdirname, extension) extension_scripts_list = ext.list_files(scriptdirname, extension)
for extension_script in extension_scripts_list: for extension_script in extension_scripts_list:
# this is built on the assumption that script name is unique. script_canonical_name = ext.canonical_name + "/" + extension_script.filename
# I think bad thing is gonna happen if name collide in the current implementation anyway, but we if ext.is_builtin:
# will need to refactor here if this assumption is broken later on. script_canonical_name = "builtin/" + script_canonical_name
if extension_script.filename in script_dependency_map:
errors.report(f"Duplicate script name \"{extension_script.filename}\" found in extensions "
f"\"{ext.name}\" and \"{script_dependency_map[extension_script.filename]['extension_dirname'] or 'builtin'}\". "
f"The current loading file will be discarded.", exc_info=False)
continue
relative_path = scriptdirname + "/" + extension_script.filename relative_path = scriptdirname + "/" + extension_script.filename
requires = None requires = None
@ -359,7 +353,7 @@ def list_scripts(scriptdirname, extension, *, include_extensions=True):
load_after = [x.strip() for x in load_after.split(',')] if load_after else [] load_after = [x.strip() for x in load_after.split(',')] if load_after else []
load_before = [x.strip() for x in load_before.split(',')] if load_before else [] load_before = [x.strip() for x in load_before.split(',')] if load_before else []
script_dependency_map[extension_script.filename] = { script_dependency_map[script_canonical_name] = {
"extension": ext.canonical_name, "extension": ext.canonical_name,
"extension_dirname": ext.name, "extension_dirname": ext.name,
"script_file": extension_script, "script_file": extension_script,
@ -374,60 +368,45 @@ def list_scripts(scriptdirname, extension, *, include_extensions=True):
for ext in extensions.active(): for ext in extensions.active():
loaded_extensions.add(ext.canonical_name) loaded_extensions.add(ext.canonical_name)
for script_filename, script_data in script_dependency_map.items(): for script_canonical_name, script_data in script_dependency_map.items():
# load before requires inverse dependency # load before requires inverse dependency
# in this case, append the script name into the load_after list of the specified script # in this case, append the script name into the load_after list of the specified script
for load_before_script in script_data['load_before']: for load_before_script in script_data['load_before']:
if load_before_script.startswith('ext:'): # if this requires an individual script to be loaded before
# if this requires an extension to be loaded before if load_before_script in script_dependency_map:
required_extension = load_before_script[4:] script_dependency_map[load_before_script]['load_after'].append(script_canonical_name)
elif load_before_script in loaded_extensions:
for _, script_data2 in script_dependency_map.items(): for _, script_data2 in script_dependency_map.items():
if script_data2['extension'] == required_extension: if script_data2['extension'] == load_before_script:
script_data2['load_after'].append(script_filename) script_data2['load_after'].append(script_canonical_name)
break break
else:
# if this requires an individual script to be loaded before
if load_before_script in script_dependency_map:
script_dependency_map[load_before_script]['load_after'].append(script_filename)
# resolve extension name in load_after lists # resolve extension name in load_after lists
for load_after_script in script_data['load_after']: for load_after_script in list(script_data['load_after']):
if load_after_script.startswith('ext:'): if load_after_script not in script_dependency_map and load_after_script in loaded_extensions:
# if this requires an extension to be loaded after script_data['load_after'].remove(load_after_script)
required_extension = load_after_script[4:] for script_canonical_name2, script_data2 in script_dependency_map.items():
for script_file_name2, script_data2 in script_dependency_map.items(): if script_data2['extension'] == load_after_script:
if script_data2['extension'] == required_extension: script_data['load_after'].remove(script_canonical_name2)
script_data['load_after'].append(script_file_name2) break
# remove all extension names in load_after lists
script_data['load_after'] = [x for x in script_data['load_after'] if not x.startswith('ext:')]
# build the DAG # build the DAG
sorter = TopologicalSorter() sorter = TopologicalSorter()
for script_filename, script_data in script_dependency_map.items(): for script_canonical_name, script_data in script_dependency_map.items():
requirement_met = True requirement_met = True
for required_script in script_data['requires']: for required_script in script_data['requires']:
if required_script.startswith('ext:'): # if this requires an individual script to be loaded
# if this requires an extension to be installed if required_script not in script_dependency_map and required_script not in loaded_extensions:
required_extension = required_script[4:] errors.report(f"Script \"{script_canonical_name}\" "
if required_extension not in loaded_extensions: f"requires \"{required_script}\" to "
errors.report(f"Script \"{script_filename}\" requires extension \"{required_extension}\" to " f"be loaded, but it is not. Skipping.",
f"be loaded, but it is not. Skipping.", exc_info=False)
exc_info=False) requirement_met = False
requirement_met = False break
break
else:
# if this requires an individual script to be loaded
if required_script not in script_dependency_map:
errors.report(f"Script \"{script_filename}\" requires script \"{required_script}\" to "
f"be loaded, but it is not. Skipping.",
exc_info=False)
requirement_met = False
break
if not requirement_met: if not requirement_met:
continue continue
sorter.add(script_filename, *script_data['load_after']) sorter.add(script_canonical_name, *script_data['load_after'])
# sort the scripts # sort the scripts
try: try:
@ -436,8 +415,8 @@ def list_scripts(scriptdirname, extension, *, include_extensions=True):
errors.report("Cycle detected in script dependencies. Scripts will load in ascending order.", exc_info=True) errors.report("Cycle detected in script dependencies. Scripts will load in ascending order.", exc_info=True)
ordered_script = script_dependency_map.keys() ordered_script = script_dependency_map.keys()
for script_filename in ordered_script: for script_canonical_name in ordered_script:
script_data = script_dependency_map[script_filename] script_data = script_dependency_map[script_canonical_name]
scripts_list.append(script_data['script_file']) scripts_list.append(script_data['script_file'])
scripts_list = [x for x in scripts_list if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)] scripts_list = [x for x in scripts_list if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]