commit f037ae51b4b6a0091ad95cf20dab17298e55ed2a Author: MuedeHydra Date: Tue Jul 1 23:35:10 2025 +0200 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..bdca94f --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# auer_translator +Ein Python-Programm welches die LATEX Auer files in lesbaren code umwandelt. + +--- +## Bedienung + +```sh +# im Terminal ausgeben: +python auer_translator.py aufgabe_3a.py -c + +# Datei überschreiben: +python auer_translator.py aufgabe_3a.py -O + +# in eine neue Datei schreiben: +python auer_translator.py aufgabe_3a.py -o aufgabe_3a_formatiert.py +``` + +--- + +![example](example.png) + diff --git a/auer_translator.py b/auer_translator.py new file mode 100644 index 0000000..b29cf82 --- /dev/null +++ b/auer_translator.py @@ -0,0 +1,159 @@ +#!/usr/bin/env python3 + +import argparse + + +# Gloab vars +normal = [] +lines = [] +in_def = False + + +def pars(): + parser = argparse.ArgumentParser( + prog="Auer Translator", + description="Ein Python-Programm welches die LATEX Auer files in lesbaren code umwandelt.") + + parser.add_argument("input_file", metavar="DATEIPFAD", help="Pfad des python Files.") + parser.add_argument("-o", "--output", required=False, metavar="DATEIPFAD", + help="Pfad des output Files.") + parser.add_argument("-O", "--overrite", required=False, default=False, + action="store_true", help="Überschreibt das input_file.") + parser.add_argument('-c', '--cli', required=False, default=False, + action="store_true", help="Printet die Datei in das Terminal.") + + return parser.parse_args() + + +def one_line_def_fix(line: str): + if line.find("def") != -1: + if line.strip().endswith(":"): + lines.append(line) + else: + line_li = line.split(":") + for n, j in enumerate(line_li): + j = j.strip().rstrip() + if n == 0: + lines.append(f"{j}:") + else: + lines.append(j) + else: + lines.append(line) + + +def read(filname): + with open(filname, "r", encoding="utf-8") as auer: + for i in auer: + line = i.removesuffix("\n") + if line.find("; ") != -1: + line = line.removesuffix(";").rstrip() + line = line.split(";") + for j in line: + j = j.strip().rstrip() + one_line_def_fix(j) + # lines.append(j) + else: + one_line_def_fix(line) + # lines.append(line) + + +def format_line(): + global in_def + for line in lines: + if line == "": + continue + line = line.replace(" . ", ".") + line = line.replace(" ( ", "(") + line = line.replace(" (", "(") + line = line.replace("( ", "(") + line = line.replace(" ) ", ")") + line = line.replace(" );", ")") + line = line.replace(" )", ")") + + line = line.replace("[ ", "[") + line = line.replace(" ]", "]") + + line = line.replace("{ ", "{") + line = line.replace(" }", "}") + line = line.replace(" g}", "g}") + + line = line.replace(" ’ ", '"') + line = line.replace("’", '"') + line = line.replace(' "', '"') + line = line.replace(' , ', ', ') + line = line.replace(' ,', ', ') + line = line.replace(' :#', ':#') + + line = line.replace(' **', '**') + line = line.replace('* ', ' * ') + + line = line.replace('\\ n', '\\n') + + line = line.replace(' pl ;', ' plt') + line = line.replace('pl.', 'plt.') + + if line[-1] == ";": + line = line.removesuffix(" ;") + line = line.removesuffix(";") + + if line.startswith("#"): + if normal != []: + normal.append("\n") + + if "=" in line: + li = line.split("=", 1) + if len(li[0]) < 25: + line = f"{li[0].rstrip()} = {str(li[1]).strip()}" + else: + line = f"{li[0].rstrip()}={str(li[1]).strip()}" + + if in_def: + line = " " + line + + if line.startswith("def"): + in_def = True + + if line.startswith(" return"): + in_def = False + + normal.append(line) + + +def is_plt(): + for i in lines: + if "matplotlib" in i: + normal.append("plt.show()") + return + + +def write(filname): + with open(filname, "w", encoding="utf-8") as auer: + for i in normal: + # print(i) + auer.write(i + "\n") + + +if __name__ == "__main__": + args = pars() + + if args.output is None and not args.overrite and not args.cli: + print("Bitte mindestens eine Aktion auswählen. (--help für hilfe)") + exit(1) + + read(args.input_file) + + # Formating + format_line() + is_plt() + + if args.output is not None: + write(args.output) + + if args.overrite: + write(args.input_file) + + if args.cli: + for i in normal: + print(i) + else: + print("Erfolgreich übersetzt") diff --git a/example.png b/example.png new file mode 100644 index 0000000..db32dea Binary files /dev/null and b/example.png differ