#!/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")