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