Skip to content

Commit 38d5bbe

Browse files
committed
better type inference
1 parent b13ab57 commit 38d5bbe

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

HexMeshWorkshop.py

+32-8
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ def __str__(self) -> str:
290290
def view(self):
291291
print(self)
292292

293+
@staticmethod
294+
@abstractmethod
295+
def is_instance(path: Path) -> bool:
296+
raise Exception('Not all AbstractEntry subclasses have specialized is_instance()')
297+
293298
class step(AbstractEntry):
294299
"""
295300
Interface to a step folder
@@ -300,7 +305,7 @@ class step(AbstractEntry):
300305
# Optionnal files
301306
# - thumbnail.png
302307

303-
def __init__(self,path: Path, step_file: Path):
308+
def __init__(self,path: Path, step_file: Path = None):
304309
path = Path(path)
305310
if(step_file!=None):
306311
if path.exists():
@@ -329,6 +334,10 @@ def view(self):
329334
False,
330335
step = self.step_file()
331336
)
337+
338+
@staticmethod
339+
def is_instance(path: Path) -> bool:
340+
return (path / 'CAD.step').exists() # path is an instance of step if it has a CAD.step file
332341

333342
def Gmsh(self,mesh_size) -> Path:
334343
return GenerativeAlgorithm(
@@ -351,6 +360,10 @@ class tetra_mesh(AbstractEntry):
351360
def __init__(self,path: Path):
352361
path = Path(path)
353362
AbstractEntry.__init__(self,path)
363+
364+
@staticmethod
365+
def is_instance(path: Path) -> bool:
366+
return (path / 'tetra.mesh').exists() # path is an instance of tetra_mesh if it has a tetra.mesh file
354367

355368
def tetra_mesh_file(self):
356369
return self.path / 'tetra.mesh'
@@ -407,14 +420,25 @@ def naive_labeling(self):
407420
labeling = 'surface_labeling.txt'
408421
)
409422

423+
def type_inference(path: Path):
424+
infered_types = list() # will store all AbstractEntry subclasses recognizing path as an instance
425+
for subclass in AbstractEntry.__subclasses__():
426+
if subclass.is_instance(path):
427+
infered_types.append(subclass) # current subclass recognize path
428+
if len(infered_types) == 0:
429+
raise Exception('No known class recognize the folder ' + str(path.absolute()))
430+
elif len(infered_types) > 1: # if multiple AbstractEntry subclasses recognize path
431+
raise Exception('Multiple classes recognize the folder ' + str(path.absolute()) + ' : ' + str([x.__name__ for x in infered_types]))
432+
else:
433+
return infered_types[0]
434+
410435
def instantiate(path: Path):
411436
"""
412437
Instanciate an AbstractEntry subclass by infering the type of the given data folder
413438
"""
414-
if((path / 'CAD.step').exists()): # TODO the step class should manage the check
415-
assert('step' in globals().keys())
416-
return globals()['step'](path,None)
417-
elif((path / 'surface.obj').exists() | (path / 'tetra.mesh').exists()): # TODO the tetra_mesh class should manage the check &.obj should not be mandatory
418-
assert('tetra_mesh' in globals().keys())
419-
return globals()['tetra_mesh'](path)
420-
raise Exception('No known class recognize the folder ' + str(path.absolute()))
439+
# TODO look inside info.json for a 'type' entry
440+
# else, type inference from files inside the folder
441+
442+
# call type_inference() to have the class, then instantiate it
443+
type = type_inference(path)
444+
return type(path)

0 commit comments

Comments
 (0)