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
|
||||
|
||||
+157
@@ -209,6 +209,163 @@ class TestTOSGroupsGeneral(unittest.TestCase):
|
||||
'TOS_Betriebsmittel': 2}
|
||||
self.assertCountEqual(allKeywords, result)
|
||||
|
||||
class TestMaterialien(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
||||
self.materials_str = """
|
||||
{
|
||||
"1.4301": {
|
||||
"Werkstoff": "1.4301 (X5CrNi18-10)",
|
||||
"Werkstofftyp": "Stahl"
|
||||
},
|
||||
"1.4305": {
|
||||
"Werkstoff": "1.4305 (X8CrNiS18-9)",
|
||||
"Werkstofftyp": "Stahl"
|
||||
},
|
||||
"1.4305 / 671": {
|
||||
"Werkstoff": "1.4305 (X8CrNiS18-9)",
|
||||
"Werkstofftyp": "Stahl"
|
||||
},
|
||||
"PA6 GF30": {
|
||||
"Werkstoff": "PA 6 GF 30",
|
||||
"Werkstofftyp": "Kunst-/ Verbundstoff",
|
||||
"Oberfläche": "Masterbatch (Grundfarbe)"
|
||||
},
|
||||
"S235JR galv. verzinkt Fe/Zn 8-15 B": {
|
||||
"Werkstoff": "1.0038 (S235JR) ",
|
||||
"Werkstofftyp": "Stahl",
|
||||
"Oberfläche": "verzinken"
|
||||
},
|
||||
"VA": {
|
||||
"Werkstoff": "false",
|
||||
"Message": "individuell nach Verwendungszweck anhand von Stahlschlüssel anpassen"
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
def test_get_all_materials_from_string(self):
|
||||
"""
|
||||
Test whether all materials are found
|
||||
"""
|
||||
|
||||
TOS_CFG = manage_configs.MaterialTransConfig()
|
||||
TOS_CFG.from_json(self.materials_str)
|
||||
|
||||
allMaterials = TOS_CFG.get_all_materials()
|
||||
|
||||
result = ["1.4301", "1.4305", "1.4305 / 671", "PA6 GF30", "S235JR galv. verzinkt Fe/Zn 8-15 B", "VA"]
|
||||
self.assertCountEqual(allMaterials, result)
|
||||
|
||||
def test_get_material_of_type(self):
|
||||
"""
|
||||
Test whether material of steel, etc. is found
|
||||
"""
|
||||
|
||||
TOS_CFG = manage_configs.MaterialTransConfig()
|
||||
TOS_CFG.from_json(self.materials_str)
|
||||
|
||||
content = TOS_CFG.get_material_of_type("1.4301")
|
||||
self.assertCountEqual(content, { 'Werkstoff': '1.4301 (X5CrNi18-10)', 'Werkstofftyp': 'Stahl'})
|
||||
|
||||
content = TOS_CFG.get_material_of_type("VA")
|
||||
self.assertCountEqual(content, {"Werkstoff": "false", "Message": "individuell nach Verwendungszweck anhand von Stahlschlüssel anpassen"})
|
||||
|
||||
|
||||
class TestWerkstoffeFromLocalConfigs(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.config_dir = os.environ.get('CREMIG_CFG')
|
||||
|
||||
def test_get_wkstoffe_of_type(self):
|
||||
"""
|
||||
get list of steels, aluminum, etc.
|
||||
"""
|
||||
|
||||
WKST_CFG = manage_configs.WerkstoffeConfig(self.config_dir, 'Werkstoff.cfg')
|
||||
|
||||
steels = WKST_CFG.get_werkstoffe_of_type("Stahl")
|
||||
result = ['1.0035 (S185)', '1.0038 (S235JR)', '1.0122 (S235JRC)',
|
||||
'1.0122+C (S235JRC+C)', '1.0330 (DC01)', '1.0401 (C15)',
|
||||
'1.0501 (C35)', '1.0503 (C45)', '1.0503+N (C45+N)',
|
||||
'1.0553 (S355J0)', '1.0577 (S355J2)', '1.0711 (9S20)',
|
||||
'1.0715 (11SMn30)', '1.0715+C (11SMn30+C)', '1.0917 (DX51D+Z)',
|
||||
'1.1191 (C45E)', '1.2842 (90MnCrV8)', '1.4035 (X45CrS13)',
|
||||
'1.4301 (X5CrNi18-10)', '1.4305 (X8CrNiS18-9)',
|
||||
'1.4310 (X10CrNi18-8)', '1.5023 (38Si7)', '1.7131 (16MnCr5)',
|
||||
'siehe Hersteller' ]
|
||||
self.assertCountEqual(steels, result)
|
||||
|
||||
steels = WKST_CFG.get_werkstoffe_of_type("Aluminium")
|
||||
result = [ "EN AW-1050A (3.0255)", "EN AW-2007 (3.1645)", "EN AW-2014-T3 (3.1255-T3)",
|
||||
"EN AW-2014-T4 (3.1255-T4)", "EN AW-6060 (3.3206)",
|
||||
"EN AW-6060-T66 (3.3206-T66)", "EN AW-7075 (3.4365)",
|
||||
"EN AW-6063-T66", 'siehe Hersteller' ]
|
||||
self.assertCountEqual(steels, result)
|
||||
|
||||
class TestWerkstoffe(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
||||
self.wkstoffe = """
|
||||
[Werkstofftyp]
|
||||
Stahl
|
||||
Aluminium
|
||||
Kunst-/ Verbundstoff
|
||||
Schwermetall
|
||||
Festigkeitsklasse
|
||||
siehe Hersteller
|
||||
|
||||
[Stahl]
|
||||
1.0035 (S185)
|
||||
1.0038 (S235JR)
|
||||
1.0122 (S235JRC)
|
||||
1.0122+C (S235JRC+C)
|
||||
1.0330 (DC01)
|
||||
1.0401 (C15)
|
||||
1.0501 (C35)
|
||||
1.0503 (C45)
|
||||
1.0503+N (C45+N)
|
||||
siehe Hersteller
|
||||
|
||||
[Aluminium]
|
||||
EN AW-1050A (3.0255)
|
||||
EN AW-2007 (3.1645)
|
||||
EN AW-2014-T3 (3.1255-T3)
|
||||
EN AW-7075 (3.4365)
|
||||
EN AW-6063-T66
|
||||
siehe Hersteller
|
||||
|
||||
[Kunst-/ Verbundstoff]
|
||||
ABS
|
||||
ASA
|
||||
CR/SBR-A65
|
||||
Hostaform 13031 MB1.5
|
||||
Irogran A87
|
||||
Murtfeldt "S" 1000
|
||||
PA
|
||||
PA 2200
|
||||
PA 2200 SLS
|
||||
POM
|
||||
POM-C
|
||||
POM-H
|
||||
siehe Hersteller
|
||||
"""
|
||||
|
||||
def test_get_wkstoffe_of_type(self):
|
||||
"""
|
||||
get list of steels
|
||||
"""
|
||||
|
||||
WKST_CFG = manage_configs.WerkstoffeConfig()
|
||||
WKST_CFG.from_cfg_str(self.wkstoffe)
|
||||
|
||||
steels = WKST_CFG.get_werkstoffe_of_type("Stahl")
|
||||
|
||||
result = ["1.0035 (S185)", "1.0038 (S235JR)", "1.0122 (S235JRC)",
|
||||
"1.0122+C (S235JRC+C)", "1.0330 (DC01)", "1.0401 (C15)", "1.0501 (C35)",
|
||||
"1.0503 (C45)", "1.0503+N (C45+N)", "siehe Hersteller"]
|
||||
self.assertCountEqual(steels, result)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user