@@ -290,6 +290,11 @@ def __str__(self) -> str:
290
290
def view (self ):
291
291
print (self )
292
292
293
+ @staticmethod
294
+ @abstractmethod
295
+ def is_instance (path : Path ) -> bool :
296
+ raise Exception ('Not all AbstractEntry subclasses have specialized is_instance()' )
297
+
293
298
class step (AbstractEntry ):
294
299
"""
295
300
Interface to a step folder
@@ -300,7 +305,7 @@ class step(AbstractEntry):
300
305
# Optionnal files
301
306
# - thumbnail.png
302
307
303
- def __init__ (self ,path : Path , step_file : Path ):
308
+ def __init__ (self ,path : Path , step_file : Path = None ):
304
309
path = Path (path )
305
310
if (step_file != None ):
306
311
if path .exists ():
@@ -329,6 +334,10 @@ def view(self):
329
334
False ,
330
335
step = self .step_file ()
331
336
)
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
332
341
333
342
def Gmsh (self ,mesh_size ) -> Path :
334
343
return GenerativeAlgorithm (
@@ -351,6 +360,10 @@ class tetra_mesh(AbstractEntry):
351
360
def __init__ (self ,path : Path ):
352
361
path = Path (path )
353
362
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
354
367
355
368
def tetra_mesh_file (self ):
356
369
return self .path / 'tetra.mesh'
@@ -407,14 +420,25 @@ def naive_labeling(self):
407
420
labeling = 'surface_labeling.txt'
408
421
)
409
422
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
+
410
435
def instantiate (path : Path ):
411
436
"""
412
437
Instanciate an AbstractEntry subclass by infering the type of the given data folder
413
438
"""
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