Abstrakte Klasse zu allen Configs hinzugefügt. Tests zum Auslesen für Werkstoffe und Materialien (Übergang alt -> neu) erstellt
This commit is contained in:
+77
-20
@@ -1,17 +1,37 @@
|
||||
import os
|
||||
import json
|
||||
from configparser import ConfigParser
|
||||
|
||||
"""
|
||||
Diese Lib verwaltet alle Config dateien (als .cfg oder als .json) von CreoMigrate
|
||||
"""
|
||||
class GeneralConfig:
|
||||
"""Klasse mit generellen Methoden für alle Config Objekte"""
|
||||
def __init__(self, config_dir, config_file):
|
||||
"""merke dir den Pfad zu den Configdateien"""
|
||||
self.config_path = os.path.join(config_dir, config_file)
|
||||
|
||||
class TOSGroupsConfig:
|
||||
def load_json_from_file(self):
|
||||
"""lade das json file vom gegeben Pfad in ein json Objekt"""
|
||||
with open(self.config_path, 'r', encoding='utf-8') as fh:
|
||||
return json.loads(fh.read())
|
||||
|
||||
def from_json(self, json_str):
|
||||
"""nimm den Json String zur Initialisierung des Objektes"""
|
||||
return json.loads(json_str)
|
||||
|
||||
def from_cfg_str(self, cfg_str, config):
|
||||
"""nimm den Json String zur Initialisierung des Objektes"""
|
||||
config.read_string(cfg_str)
|
||||
|
||||
|
||||
class TOSGroupsConfig(GeneralConfig):
|
||||
"""Auslesen der Config Angaben zu den TOS Gruppen"""
|
||||
|
||||
def __init__(self, config_dir='.', config_file='TOS-Gruppen.json'):
|
||||
|
||||
self.tos_hierarchy = None
|
||||
self.config_path = os.path.join(config_dir, config_file)
|
||||
super().__init__(config_dir, config_file)
|
||||
|
||||
def get_children_keys(self, tos_hierarchy, currentId, result):
|
||||
# wenn eine Referenz nicht weiter in der TOS Hierarchy vorkommt, dann nicht mit einsammeln
|
||||
@@ -32,7 +52,7 @@ class TOSGroupsConfig:
|
||||
|
||||
# lazy. nur einmal laden, wenn noch nicht geschehen
|
||||
if not self.tos_hierarchy:
|
||||
self.load_tos_hierarchy()
|
||||
self.tos_hierarchy = super().load_json_from_file()
|
||||
|
||||
# beginne mit root als ersten, vordefiniertem Keyword
|
||||
rootid = self.tos_hierarchy["root"]
|
||||
@@ -51,40 +71,77 @@ class TOSGroupsConfig:
|
||||
ret.append(kw)
|
||||
return ret
|
||||
|
||||
def load_tos_hierarchy(self):
|
||||
"""lade die gegebenen Hierarchy aus dem Config File in ein json Objekt"""
|
||||
with open(self.config_path, 'r', encoding='utf-8') as fh:
|
||||
self.tos_hierarchy = json.loads(fh.read())
|
||||
|
||||
def from_json(self, json_str):
|
||||
"""lade die gegebenen Hierarchy aus einem json String in ein json Objekt"""
|
||||
self.tos_hierarchy = json.loads(json_str)
|
||||
|
||||
"""lade den Json String zur Initialisierung des Objektes"""
|
||||
self.tos_hierarchy = super().from_json(json_str)
|
||||
|
||||
class MaterialConfig:
|
||||
"""Auslesen der Config Angaben zu allen Materialangaben"""
|
||||
|
||||
class MaterialTransConfig(GeneralConfig):
|
||||
"""Auslesen der Config zu allen Materialangaben"""
|
||||
|
||||
def __init__(self, config_dir='.', config_file='Materialübersetzung.json'):
|
||||
self.materials = None
|
||||
self.config_path = os.path.join(config_dir, config_file)
|
||||
super().__init__(config_dir, config_file)
|
||||
|
||||
def load_materials(self):
|
||||
"""lade die gegebenen Hierarchy aus dem Config File in eine json Objekt"""
|
||||
with open(self.config_path, 'r', encoding='utf-8') as fh:
|
||||
self.materials = json.loads(fh.read())
|
||||
def from_json(self, json_str):
|
||||
"""lade den Json String zur Initialisierung des Objektes"""
|
||||
self.materials = super().from_json(json_str)
|
||||
|
||||
def get_all_materials(self):
|
||||
"""hole alle Materialien, welche in der config Datei definiert sind"""
|
||||
|
||||
if not self.materials:
|
||||
self.load_materials()
|
||||
self.materials = super().load_json_from_file()
|
||||
ret = list()
|
||||
for k in self.materials.keys():
|
||||
ret.append(k)
|
||||
return ret
|
||||
|
||||
def get_material_of_type(self, mat):
|
||||
"""hole alle Materialien, welche in der config Datei definiert sind"""
|
||||
if not self.materials:
|
||||
self.load_materials()
|
||||
return self.materials[mat]
|
||||
|
||||
|
||||
class WerkstoffeConfig(GeneralConfig):
|
||||
"""Auslesen der Config zu allen Werkstoffen"""
|
||||
def __init__(self, config_dir='.', config_file='Werkstoff.cfg'):
|
||||
self.wkstoff_config_parser = None
|
||||
super().__init__(config_dir, config_file)
|
||||
|
||||
def get_werkstoffe_of_type(self, wtype):
|
||||
"""hole alle Werkstoffe unter der gegebenen Sektion
|
||||
Werkstofftyp wtype
|
||||
Stahl --> [ 1.0035 (S185), 1.0038 (S235JR), 1.0122 (S235JRC), 1.0122+C (S235JRC+C), 1.0330 (DC01), ... ]
|
||||
Aluminium
|
||||
Kunst-/ Verbundstoff
|
||||
Schwermetall
|
||||
Festigkeitsklasse
|
||||
siehe Hersteller
|
||||
|
||||
"""
|
||||
if not self.wkstoff_config_parser:
|
||||
self.load_cfg_from_file()
|
||||
return self.wkstoff_config_parser.options(wtype)
|
||||
|
||||
def from_json(self, json_str):
|
||||
pass
|
||||
|
||||
def from_cfg_str(self, cfg_str):
|
||||
"""if you have to deal with a .cfg file """
|
||||
config = ConfigParser(allow_no_value=True)
|
||||
config.optionxform = lambda option: option # preserve case for letters
|
||||
self.wkstoff_config_parser = config
|
||||
super().from_cfg_str(cfg_str, self.wkstoff_config_parser)
|
||||
|
||||
def load_cfg_from_file(self):
|
||||
"""lade das config file vom gegeben Pfad"""
|
||||
config = ConfigParser(allow_no_value=True)
|
||||
config.optionxform = lambda option: option # preserve case for letters
|
||||
config.read(self.config_path)
|
||||
self.wkstoff_config_parser = config
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user