Skip to content

Commit

Permalink
Move subclass finding to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
cphyc committed Apr 22, 2022
1 parent f0a68f8 commit 54f4150
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
12 changes: 4 additions & 8 deletions tangos/input_handlers/finding.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .. import config
from ..log import logger
from ..util.subclasses import find_subclasses


def find(extension=None, mtd=None, ignore=None, basename="", patterns=[]):
Expand Down Expand Up @@ -46,16 +47,11 @@ def best_matching_handler(cls, basename):
handler_names = []
handler_timestep_lengths = []
base = os.path.join(config.base, basename)
if len(cls.__subclasses__()) == 0:
return cls

# Add all subclasses, sub-subclasses, sub-subclasses, ...
all_possible_handlers = []
subclasses = cls.__subclasses__()
while subclasses:
subclass = subclasses.pop()
subclasses.extend(subclass.__subclasses__())
all_possible_handlers.append(subclass)
all_possible_handlers = find_subclasses(cls)
if not all_possible_handlers:
return cls

for possible_handler in all_possible_handlers:
timesteps_detected = find(basename=base + "/", patterns=possible_handler.patterns)
Expand Down
11 changes: 11 additions & 0 deletions tangos/util/subclasses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import List

def find_subclasses(cls: type) -> List[type]:
subclasses = cls.__subclasses__()
all_subclasses = []
while subclasses:
subclass = subclasses.pop()
subclasses.extend(subclass.__subclasses__())
all_subclasses.append(subclass)

return all_subclasses

0 comments on commit 54f4150

Please sign in to comment.