Import der json Datei dazu. Einige Schalter umgebaut und --outname dazu, damit das Muster aller .xlsx Dateien einfach festgelegt werden kann
This commit is contained in:
+55
-36
@@ -3,6 +3,7 @@ import sys
|
||||
import argparse
|
||||
from ioconverter import ExcelConverter
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
def prepare_data(rawdata:dict):
|
||||
sensors = rawdata["sensors"]
|
||||
@@ -39,35 +40,37 @@ def prepare_data(rawdata:dict):
|
||||
return (dsensors, lsensors)
|
||||
|
||||
|
||||
# Funktionen
|
||||
# einfache Funktionen
|
||||
def load_json(jsonfilename):
|
||||
with open(jsonfilename, encoding='utf-8') as fh:
|
||||
return json.load(fh)
|
||||
|
||||
def check_file_in_work(work_dir, filename):
|
||||
def check_file_in_work(work_dir:Path, filename:Path):
|
||||
fexists = True
|
||||
if not os.path.exists(filename):
|
||||
mypath = os.path.join(work_dir, filename)
|
||||
if not os.path.exists(mypath):
|
||||
if not filename.exists(): # dann schau im Work Ordner nach
|
||||
mypath = work_dir.joinpath(filename)
|
||||
ex = mypath.exists()
|
||||
if not mypath.exists():
|
||||
fexists = False
|
||||
else:
|
||||
mypath = filename
|
||||
return (mypath, fexists)
|
||||
|
||||
# Helper für Export
|
||||
def do_export(conv_obj:ExcelConverter, mode_str, output_dir, base_input, lines):
|
||||
def do_export(conv_obj:ExcelConverter, mode_str, base_input, output_dir, lines):
|
||||
# Für WSCAD Dateinamen anpassen (Export-Typ bleibt "WSCAD" oder "WSCAD mit Bezug")
|
||||
if mode_str == "WSCAD":
|
||||
export_type = "WSCAD" if args.no_bezug else "WSCAD mit Bezug"
|
||||
filename = f"{base_input}_{'WSCAD_no_Bezug' if args.no_bezug else 'WSCAD'}.xlsx"
|
||||
if mode_str == "WSCAD":
|
||||
if args.no_bezug:
|
||||
export_type = "WSCAD"
|
||||
filename = f"{base_input}_WSCAD_no_Bezug.xlsx"
|
||||
else:
|
||||
export_type = "WSCAD mit Bezug"
|
||||
filename = f"{base_input}_WSCAD.xlsx"
|
||||
else:
|
||||
export_type = mode_str
|
||||
filename = f"{base_input}_{mode_str}.xlsx"
|
||||
|
||||
if args.output and args.mode == mode_str:
|
||||
export_path = args.output
|
||||
else:
|
||||
export_path = os.path.join(output_dir, filename)
|
||||
export_path = os.path.join(output_dir, filename)
|
||||
|
||||
try:
|
||||
conv_obj.export_excel(lines, export_type, export_path)
|
||||
@@ -75,35 +78,38 @@ def do_export(conv_obj:ExcelConverter, mode_str, output_dir, base_input, lines):
|
||||
except Exception as e:
|
||||
print(f"Fehler bei Export '{export_type}': {e}")
|
||||
|
||||
|
||||
def do_exports(export_mode, converter, output_dir, input_basename, lines):
|
||||
def do_exports(converter, export_mode, output_dir, base_input, lines):
|
||||
if export_mode:
|
||||
do_export(converter, export_mode, output_dir, input_basename, lines)
|
||||
do_export(converter, export_mode, base_input, output_dir, lines)
|
||||
else:
|
||||
# Kein Modus → alle drei exportieren
|
||||
do_export(converter, "EA", output_dir, input_basename, lines)
|
||||
do_export(converter, "TIA", output_dir, input_basename, lines)
|
||||
do_export(converter, "WSCAD", output_dir, input_basename, lines)
|
||||
do_export(converter, "EA", base_input, output_dir, lines)
|
||||
do_export(converter, "TIA", base_input, output_dir, lines)
|
||||
do_export(converter, "WSCAD", base_input, output_dir, lines)
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Excel-Konverter für BMK-Daten")
|
||||
parser.add_argument("--mode", type=str, choices=["EA", "TIA", "WSCAD"],
|
||||
help="Art des Exports (EA, TIA, WSCAD). Wenn nicht gesetzt, werden alle drei erzeugt.")
|
||||
parser.add_argument("--input", type=str, help="Pfad zur Eingabedatei (.txt)")
|
||||
parser.add_argument("--positions", type=str, help="Json Datei von getpositions.py (.json)")
|
||||
parser.add_argument("--output", type=str, help="Pfad zur Zieldatei (.xlsx)")
|
||||
parser.add_argument("--no-bezug", action="store_true", help="(Nur bei WSCAD) Export ohne Bezugsinformationen")
|
||||
parser.add_argument("--mode", type=str, choices=["EA", "TIA", "WSCAD", "WSCAD_OBZG"],
|
||||
help="Art des Exports (EA, TIA, WSCAD, WSCAD_OBZG). Wenn nicht gesetzt, werden die ersten drei erzeugt.")
|
||||
parser.add_argument("--no_bezug", action="store_true", help="(Nur bei WSCAD) Export ohne Bezugsinformationen")
|
||||
|
||||
parser.add_argument("--input", type=str, help="Pfad zur Eingabedatei in Form einer Kommaseparierten .csv Datei (.txt)")
|
||||
parser.add_argument("--filename", type=str, help="Json Datei von getpositions.py als Eingabe (.json)")
|
||||
parser.add_argument("--export_dir", type=str, help="Pfad zum Ordner für die Ausgabe. Default ist PROJECT_WORK")
|
||||
parser.add_argument("--outname", type=str, help="Basisname der Zieldateien wie <name>_TIA.xlsx)")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# Umgebungsvariablen
|
||||
work_dir = os.environ.get("PROJECT_WORK")
|
||||
config_dir = os.environ.get("PROJECT_CFG")
|
||||
work_dir = Path(os.environ.get("PROJECT_WORK"))
|
||||
config_dir = Path(os.environ.get("PROJECT_CFG"))
|
||||
|
||||
|
||||
# Converter initialisieren
|
||||
converter = ExcelConverter()
|
||||
output_dir = os.environ.get("PROJECT_WORK", os.getcwd())
|
||||
output_dir = work_dir
|
||||
if args.export_dir:
|
||||
output_dir = args.export_dir
|
||||
|
||||
# Konfigurationsdatei
|
||||
# config = configparser.ConfigParser(allow_no_value=True, delimiters=("="))
|
||||
@@ -114,6 +120,15 @@ if __name__ == "__main__":
|
||||
export_mode = args.mode
|
||||
|
||||
|
||||
if args.outname:
|
||||
basename_out = args.outname
|
||||
elif args.filename:
|
||||
basename_out = args.filename.split('_')[0]
|
||||
elif args.input:
|
||||
basename_out = os.path.splitext(os.path.basename(args.input))[0]
|
||||
else:
|
||||
raise Exception()
|
||||
|
||||
# Datei einlesen
|
||||
if args.input:
|
||||
# Wenn Input Pfad kein absoluter Pfad, dann in work nach Dateinamen suchen
|
||||
@@ -121,31 +136,35 @@ if __name__ == "__main__":
|
||||
if work_dir:
|
||||
args.input = os.path.join(work_dir, args.input)
|
||||
|
||||
input_basename = os.path.splitext(os.path.basename(args.input))[0]
|
||||
try:
|
||||
with open(args.input, "r", encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Lesen der Datei: {e}")
|
||||
sys.exit(1)
|
||||
do_exports(export_mode, converter, output_dir, input_basename, lines)
|
||||
do_exports(converter, export_mode, output_dir, basename_out, lines)
|
||||
|
||||
elif args.positions:
|
||||
input_basename = os.path.splitext(os.path.basename(args.positions))[0]
|
||||
json_file = args.positions
|
||||
elif args.filename:
|
||||
filename = Path(args.filename)
|
||||
if not filename.suffix == ".json":
|
||||
print("only available for .json files")
|
||||
exit()
|
||||
|
||||
json_file = Path(args.filename)
|
||||
(json_path, dexists) = check_file_in_work(work_dir, json_file)
|
||||
if not dexists:
|
||||
print(f"file {json_file} does not exist")
|
||||
parser.print_help()
|
||||
exit()
|
||||
|
||||
rawdata = load_json(json_path)
|
||||
(dsensors, lsensors) = prepare_data(rawdata)
|
||||
if len(dsensors) == 0:
|
||||
do_exports(export_mode, converter, output_dir, input_basename, lsensors)
|
||||
do_exports(converter, export_mode, output_dir, basename_out, lsensors)
|
||||
else:
|
||||
for sps_nr, lines in dsensors.items():
|
||||
name = f"{sps_nr}{input_basename}"
|
||||
do_exports(export_mode, converter, output_dir, name, lines)
|
||||
name = f"{basename_out}-{sps_nr}"
|
||||
do_exports(converter, export_mode, output_dir, name, lines)
|
||||
|
||||
else:
|
||||
parser.print_help()
|
||||
|
||||
Reference in New Issue
Block a user