Skip to content

Commit 49005f6

Browse files
authored
Merge pull request #370 from capocchi/version-5.1
Version 5.1
2 parents 14d1bab + f7efef9 commit 49005f6

File tree

17 files changed

+1494
-635
lines changed

17 files changed

+1494
-635
lines changed

devsimpy/Components.py

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import importlib
3333
import subprocess
3434
from shutil import which
35+
from pathlib import Path
3536

3637
import inspect
3738
if not hasattr(inspect, 'getargspec'):
@@ -98,25 +99,49 @@ def GetClass(elem):
9899
### return only the class that inherite of DomainBehavoir or DomainStructure which are present in the clsmembers dict
99100
# return next(filter(lambda c: c != DomainClass and issubclass(c, DomainClass), clsmembers.values()), None)
100101

101-
DomainClass = (DomainBehavior,DomainStructure,)
102+
# Build base domain class tuple
103+
DomainClass = (DomainBehavior, DomainStructure)
102104
if 'Port' in clsmembers:
103105
DomainClass += (clsmembers['Port'],)
104-
105-
cls = next(filter(lambda c: c not in DomainClass and issubclass(c, DomainClass) and c.__name__ in elem, clsmembers.values()), None)
106-
107-
### try to catch and resolve instanciation of unknown class
106+
107+
# First attempt: find class matching element name
108+
cls = next(
109+
(c for c in clsmembers.values()
110+
if c not in DomainClass
111+
and issubclass(c, DomainClass)
112+
and c.__name__ in elem),
113+
None
114+
)
115+
116+
# Second attempt: find any valid subclass if exact match fails
108117
if not cls:
109-
sys.stderr.write(_(f"\nClass name and model name are different!\nWe try to instance the class {elem} that inherite of DomainClass!"))
110-
cls = next(filter(lambda c: c not in DomainClass and issubclass(c, DomainClass), clsmembers.values()), None)
118+
sys.stderr.write(_(
119+
f"\nWarning: Class name and model name mismatch!\n"
120+
f"Attempting to instantiate class '{elem}' that inherits from DomainClass...\n"
121+
))
122+
123+
cls = next(
124+
(c for c in clsmembers.values()
125+
if c not in DomainClass
126+
and issubclass(c, DomainClass)),
127+
None
128+
)
129+
111130
if cls:
112-
sys.stderr.write(_("\nClass instanciated but you need to verify if its a good one!\n"))
131+
sys.stderr.write(_(
132+
f"Class '{cls.__name__}' instantiated, but please verify it's the correct one!\n"
133+
))
134+
135+
# Report failure if no valid class found
113136
if not cls:
114-
115-
sys.stderr.write(_("\nClass unknown..."))
116-
# sys.stderr.write(f"path: {elem} \n clsmembers:{clsmembers}\n")
137+
sys.stderr.write(_(f"\nError: Unknown class '{elem}'\n"))
138+
# Uncomment for debugging:
139+
# sys.stderr.write(f"Path: {elem}\nClass members: {clsmembers}\n")
117140
# for c in clsmembers.values():
118-
# print(c.__name__, c not in DomainClass, issubclass(c, DomainClass), c.__name__ in elem)
119-
141+
# print(f"{c.__name__}: not in DomainClass={c not in DomainClass}, "
142+
# f"is subclass={issubclass(c, DomainClass)}, "
143+
# f"name matches={c.__name__ in elem}")
144+
120145
return cls
121146

122147
#for cls in [c for c in clsmembers.values() if c != DomainClass]:
@@ -533,16 +558,8 @@ def BlockModelAdapter(cls, label="", specific_behavior=""):
533558
""" Return block model considering its class hierarchy
534559
The implementation depends only of the argument of the class. There is no dependance with the collector module (in comment bellow)
535560
"""
536-
from Container import DiskGUI, ScopeGUI, CodeBlock
537-
#from Domain.Collector import *
538-
#if issubclass(cls, QuickScope.QuickScope):
539-
#m = ScopeGUI(label)
540-
#elif issubclass(cls, (To_Disk.To_Disk, Messagecollector.Messagecollector)):
541-
#m = DiskGUI(label)
542-
#else:
543-
## mew CodeBlock instance
544-
#m = CodeBlock()
545-
561+
from Container import DiskGUI, ScopeGUI, CodeBlock, CollectorGUI, GeneratorGUI
562+
546563
# associated python class membre
547564

548565
clsmbr = getClassMember(inspect.getfile(cls))
@@ -564,11 +581,18 @@ def BlockModelAdapter(cls, label="", specific_behavior=""):
564581
match = [re.match('[-_a-zA-z]*collector[-_a-zA-z]*',s, re.IGNORECASE) for s in list(clsmbr.keys())+[specific_behavior]]
565582
messagecollector_model = [a.group(0) for a in [s for s in match if s is not None]] != []
566583

584+
cls_path = Path(inspect.getfile(cls)).resolve()
585+
567586
# new codeBlcok instance
568-
if disk_model or messagecollector_model:
569-
m = DiskGUI(label)
570-
elif scope_model:
571-
m = ScopeGUI(label)
587+
if "Collector" in cls_path.parts:
588+
if disk_model or messagecollector_model:
589+
m = DiskGUI(label)
590+
elif scope_model:
591+
m = ScopeGUI(label)
592+
else:
593+
m = CollectorGUI(label)
594+
elif "Generator" in cls_path.parts:
595+
m = GeneratorGUI(label)
572596
else:
573597
m = CodeBlock(label)
574598

0 commit comments

Comments
 (0)