From a2aad2f3b4eb3b18eaeb8522dbb6211c18f717c2 Mon Sep 17 00:00:00 2001 From: Sohrab Behdani Date: Tue, 20 Aug 2024 19:28:08 +0330 Subject: [PATCH] fixed a problem with generate text function --- profiler.py | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/profiler.py b/profiler.py index ac2230d..7a86864 100644 --- a/profiler.py +++ b/profiler.py @@ -87,26 +87,39 @@ def generate_file_toml(input_file, output_file): config = { "packages": {"packages": []}, "aur": {"aur_packages": []}, - "flatpak": {"flatpak_packages": []}, + "flatpak": {"flatpak_packages": []} } - with open(input_file, "r") as f: - for line in f: - key, value = line.split(":") - key = key.strip() - packages = [pkg.strip() for pkg in value.split(",")] + with open(input_file, 'r') as file: + for line in file: + line = line.strip() + if not line or ":" not in line: + # Ignore empty lines or lines without a colon + continue - if key == "packages": - config["packages"]["packages"].extend(packages) - elif key == "aur": - config["aur"]["aur_packages"].extend(packages) - elif key == "flatpak": - config["flatpak"]["flatpak_packages"].extend(packages) + try: + key, value = line.split(":", 1) + key = key.strip() + values = [v.strip() for v in value.split(",")] - with open(output_file, "w") as f: - toml.dump(config, f) + if key == "packages": + config["packages"]["packages"].extend(values) + elif key == "aur": + config["aur"]["aur_packages"].extend(values) + elif key == "flatpak": + config["flatpak"]["flatpak_packages"].extend(values) + else: + print(f"Warning: Unrecognized key '{key}' in input file.") - print(f"Generated TOML file from {input_file} and saved as {output_file}") + except ValueError as e: + print(f"Error processing line: '{line}'. Error: {e}") + continue + + # Write the config to the output TOML file + with open(output_file, 'w') as toml_file: + toml.dump(config, toml_file) + + print(f"TOML file generated at: {output_file}") # Ask user to review and edit the file review = input("Do you want to review and edit the file? (y/n): ").strip().lower()