# Path: lib/Tools/InstanceResolver.py
# class InstanceResolver:
#     def __init__(self, instantiate=True):
#         self.instantiate = instantiate
# 
#     def resolve(self, name, path='lib.Subcommands'):
#         class_name = Casify.to_pascal_case(name)
#         module_name = path + "." + class_name
#         try:
#             mod = importlib.import_module(module_name)
#             cls = getattr(mod, class_name)
#         except ImportError as e:
#             if e.name != module_name:
#                 raise
#             else:
#                 raise SubcommandNotFoundError(name) from e
#         if self.instantiate:
#             return cls()
#         else:
#             return cls


import glob as global_glob
import os
from lib.Tools.InstanceResolver import InstanceResolver


class InstanceCollector:
    def __init__(self, instantiate=True, *, instance_resolver=None, glob=None):
        self.instance_resolver = instance_resolver or InstanceResolver(instantiate)
        self.glob = glob or global_glob

    # this returns an iterator!
    def collect(self, path):
        for filename in self.glob.iglob(os.path.join(path, '*.py')):
            modparts = filename.replace('/', '.').split('.')[:-1]
            clsname = modparts[-1]
            folder = ".".join(modparts[:-1])

# Path: lib/Tools/InstanceResolver.py
# class InstanceResolver:
#     def __init__(self, instantiate=True):
#         self.instantiate = instantiate
# 
#     def resolve(self, name, path='lib.Subcommands'):
#         class_name = Casify.to_pascal_case(name)
#         module_name = path + "." + class_name
#         try:
#             mod = importlib.import_module(module_name)
#             cls = getattr(mod, class_name)
#         except ImportError as e:
#             if e.name != module_name:
#                 raise
#             else:
#                 raise SubcommandNotFoundError(name) from e
#         if self.instantiate:
#             return cls()
#         else:
#             return cls


# Path: lib/Tools/InstanceCollector.py
import glob as global_glob
import os
from lib.Tools.InstanceResolver import InstanceResolver



class InstanceCollector:
    def __init__(self, instantiate=True, *, instance_resolver=None, glob=None):
        self.instance_resolver = instance_resolver or InstanceResolver(instantiate)
        self.glob = glob or global_glob

    # this returns an iterator!
    def collect(self, path):
        for filename in self.glob.iglob(os.path.join(path, '*.py')):
            modparts = filename.replace('/', '.').split('.')[:-1]
            clsname = modparts[-1]
            folder = ".".join(modparts[:-1])

            yield self.instance_resolver.resolve(clsname, folder)