136 lines
4.7 KiB
Python
136 lines
4.7 KiB
Python
import argparse
|
|
import configparser
|
|
import os
|
|
import subprocess
|
|
import csv
|
|
|
|
|
|
def get_sivas_teilestamm(sivas_ids, out_dir=None, overwrite=True):
|
|
"""
|
|
Generiert für jede angegebene SIVAS-Teilenummer eine CSV-Datei, in der die Teilestamm-Informationen gespeichert sind
|
|
"""
|
|
sivas_teilestamm_exe = os.environ.get('SIVAS_TEILESTAMM')
|
|
if not os.path.isfile(sivas_teilestamm_exe):
|
|
raise FileNotFoundError(
|
|
f"Programmpfad '{sivas_teilestamm_exe}' nicht erreichbar. "
|
|
f"Bitte Zugriff und Variable SIVAS_TEILESTAMM in setenv.bat prüfen."
|
|
)
|
|
|
|
if out_dir is None:
|
|
out_dir = os.environ.get('PROJECT_DATA')
|
|
|
|
if not os.path.isdir(out_dir):
|
|
raise NotADirectoryError(
|
|
f"Ausgabeverzeichnis '{out_dir}' existiert nicht. "
|
|
f"Bitte VPN oder setenv.bat prüfen."
|
|
)
|
|
|
|
if isinstance(sivas_ids, str):
|
|
sivas_ids = list(set(sivas_ids.strip().split(",")))
|
|
|
|
ids_to_generate = []
|
|
for id in sivas_ids:
|
|
if not overwrite and os.path.isfile(os.path.join(out_dir, id + ".csv")):
|
|
continue
|
|
ids_to_generate.append(id)
|
|
|
|
if len(ids_to_generate) == 0:
|
|
print("Alle CSVs vorhanden oder keine neuen Nummern")
|
|
return
|
|
|
|
ids_to_generate_str = ",".join([f'"{id}"' for id in ids_to_generate])
|
|
|
|
os.environ["SIVAS_EXCEL_EXPORT_DIR"] = out_dir
|
|
print(f"Erstelle CSV(s) mit Teilestamminformationen...")
|
|
subprocess.run(f'{sivas_teilestamm_exe} -teilenr={ids_to_generate_str} -open_target_dir=n')
|
|
|
|
for id in ids_to_generate:
|
|
csv_path = os.path.join(out_dir, id + ".csv")
|
|
with open(csv_path, encoding="utf-8", newline="") as csv_file:
|
|
csv_data = csv.reader(csv_file, delimiter=";")
|
|
rows = list(csv_data)
|
|
if len(rows) != 2:
|
|
raise ValueError(f"SIVAS-Teilenummer '{id}' existiert nicht oder liefert keine gültige CSV.")
|
|
# Rückgabe des Bezeichners (zweite Zeile, zweite Spalte)
|
|
yield id, rows[1][1].strip()
|
|
|
|
|
|
|
|
def read_bezeichner_from_csv(teilenr):
|
|
"""
|
|
Liest den Bezeichner aus einer SIVAS-CSV für eine gegebene Teilenummer.
|
|
|
|
:param teilenr: SIVAS Teilenummer als String
|
|
:return: Bezeichner-String, falls erfolgreich. Wirft Exception bei Fehler.
|
|
"""
|
|
csv_dir = os.environ.get("PROJECT_DATA", ".")
|
|
|
|
csv_path = os.path.join(csv_dir, f"{teilenr}.csv")
|
|
|
|
if not os.path.isfile(csv_path):
|
|
raise FileNotFoundError(f"CSV für Teilenummer '{teilenr}' nicht gefunden unter {csv_path}")
|
|
|
|
with open(csv_path, encoding="utf-8", newline="") as f:
|
|
reader = csv.reader(f, delimiter=";")
|
|
rows = list(reader)
|
|
if len(rows) != 2:
|
|
raise ValueError(f"Ungültiges Format oder keine Daten in CSV für '{teilenr}'")
|
|
|
|
bezeichner = rows[1][1].strip()
|
|
return bezeichner
|
|
|
|
def update_bezeichner(names_config):
|
|
|
|
if "Missing" not in names_config or not names_config["Missing"]:
|
|
print("Keine fehlenden Einträge in [Missing]. Keine Abfrage angestoßen.")
|
|
return
|
|
|
|
missing_ids = list(names_config["Missing"].keys())
|
|
|
|
print(f"Starte SIVAS-Abfrage für {len(missing_ids)} fehlende Teilenummer(n)...")
|
|
|
|
try:
|
|
get_sivas_teilestamm(missing_ids)
|
|
except Exception as e:
|
|
print(f"Fehler bei der SIVAS-Abfrage: {e}")
|
|
return
|
|
|
|
updated = 0
|
|
for id in missing_ids:
|
|
try:
|
|
bezeichner = read_bezeichner_from_csv(id)
|
|
if "Sivasnummern" not in names_config:
|
|
names_config["Sivasnummern"] = {}
|
|
names_config["Sivasnummern"][id] = bezeichner
|
|
updated += 1
|
|
except Exception as e:
|
|
print(f"WARNUNG: Bezeichner für {id} konnte nicht gelesen werden: {e}")
|
|
|
|
for id in missing_ids:
|
|
names_config["Missing"].pop(id, None)
|
|
|
|
if "Missing" in names_config and not names_config["Missing"]:
|
|
names_config.remove_section("Missing")
|
|
|
|
with open(config_path, "w", encoding="utf-8") as f:
|
|
names_config.write(f)
|
|
|
|
print(f"{updated} Bezeichner erfolgreich ergänzt und Konfiguration aktualisiert.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(description="Aktualisiert fehlende Bezeichner in bezeichner.cfg anhand von SIVAS-Teilestamm.")
|
|
parser.add_argument("-c", "--config", type=str, default=os.path.join(os.environ.get("PROJECT_CFG", "."), "bezeichner.cfg"), help="Pfad zur bezeichner.cfg")
|
|
|
|
args = parser.parse_args()
|
|
config_path = args.config
|
|
|
|
if not os.path.isfile(config_path):
|
|
raise FileNotFoundError(f"Config-Datei nicht gefunden: {config_path}")
|
|
|
|
config = configparser.ConfigParser()
|
|
config.optionxform = str # Groß-/Kleinschreibung erhalten
|
|
config.read(config_path, encoding="utf-8")
|
|
|
|
update_bezeichner(config) |