I also had the same problem, so I decided to crate a python program that creates those within automatically. Here is the program:
import os
import sys
def process_directory(root_path, parent_within=None):
package_name = os.path.basename(root_path)
# Construir el nombre completo del paquete
if parent_within:
full_within = f"{parent_within}.{package_name}"
else:
full_within = package_name
# Crear/reescribir package.mo
package_mo_path = os.path.join(root_path, "package.mo")
with open(package_mo_path, "w", encoding="utf-8") as f:
if parent_within:
f.write(f"within {parent_within};\n")
else:
f.write("within;\n")
f.write(f"package {package_name}\n")
f.write(f"end {package_name};\n")
# Procesar todos los archivos .mo en esta carpeta (excepto package.mo)
for filename in os.listdir(root_path):
file_path = os.path.join(root_path, filename)
if os.path.isfile(file_path) and filename.endswith(".mo") and filename != "package.mo":
with open(file_path, "r", encoding="utf-8") as f:
lines = f.readlines()
# Quitar "within ..." si está en la primera línea
if lines and lines[0].strip().startswith("within "):
lines = lines[1:]
# Añadir la línea correcta
new_lines = [f"within {full_within};\n"] + lines
with open(file_path, "w", encoding="utf-8") as f:
f.writelines(new_lines)
# Recorrer subcarpetas
for subdir in os.listdir(root_path):
subdir_path = os.path.join(root_path, subdir)
if os.path.isdir(subdir_path):
process_directory(subdir_path, full_within)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("❌ Uso: PackageCreater.py <ruta_carpeta>")
sys.exit(1)
root_folder = sys.argv[1]
if not os.path.isdir(root_folder):
print(f"❌ La ruta no existe o no es una carpeta: {root_folder}")
else:
process_directory(root_folder)
print(f"✔ Estructura Modelica actualizada en: {root_folder}")