# Path: lib/Errors/SubcommandNotFoundError.py
# class SubcommandNotFoundError(Exception):
#     pass
# 
# 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/UsageHelp.py
# class UsageHelp:
#     def __init__(self):
#         pass
# 
#     def show(self):
#         subcommands = self._get_subcommands_list()
# 
#         a = textwrap.dedent('''
#         Usage: sudo ./open365 SUBCOMMAND [args...]
# 
#         SUBCOMMAND is one of:
#         ''')
#         a += "\t" + "\n\t".join(subcommands)
#         a += textwrap.dedent('''
# 
#         You can get help for a subcommand with
# 
#         \t sudo ./open365 SUBCOMMAND --help
#         ''')
#         print(a)
# 
#     def _get_subcommands_list(self):
#         files = os.listdir(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'Subcommands'))
#         return sorted(Casify.to_dash_case(file[:-3]) for file in files if file.endswith('.py'))
# 
#     def _get_profiles_list(self):
#         profiles_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
#                                      '..',
#                                      'environments',
#                                      'compose_files',
#                                      'latest')
#         all_files = (os.path.join(profiles_path, file) for file in os.listdir(profiles_path) if file.endswith('.yml'))
#         regex = re.compile('^include:$')
#         files = []
#         with fileinput.input(all_files) as line_iter:
#             for line in line_iter:
#                 if regex.search(line):
#                     files.append(os.path.basename(line_iter.filename())[:-4])
#                     line_iter.nextfile()
#         return sorted(files)
# 
# Path: lib/Wrappers/Logger.py
# class Logger:
#     def __init__(self, name):
#         if 'UNITTESTING' in os.environ:
#             self.logging = NullLogger()
#         else:
#             settings = Settings().getSettings()
#             logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
#                                 level=settings["logs"]["level"])
#             self.logging = logging.getLogger(name)
# 
#     def debug(self, *args, **kwargs):
#         self.logging.debug(*args, **kwargs)
# 
#     def info(self, *args, **kwargs):
#         self.logging.info(*args, **kwargs)
# 
#     def warning(self, *args, **kwargs):
#         self.logging.warning(*args, **kwargs)
# 
#     def error(self, *args, **kwargs):
#         self.logging.error(*args, **kwargs)
# 
#     def critical(self, *args, **kwargs):
#         self.logging.critical(*args, **kwargs)
# 
#     def log(self, *args, **kwargs):
#         self.logging.log(*args, **kwargs)


from lib.Errors.SubcommandNotFoundError import SubcommandNotFoundError
from lib.Tools.InstanceResolver import InstanceResolver
from lib.UsageHelp import UsageHelp
from lib.Wrappers.Logger import Logger


class SubcommandRouter:
    def __init__(self, devenvExecutor=None, instanceResolver=None):
        self.instanceResolver = instanceResolver or InstanceResolver()
        self.logger = Logger(__name__)

    def route(self, *args):
        if args[0] in ('help', '-h', '--help'):
            if len(args) == 1:
                usage = UsageHelp()
                usage.show()
                return
            else:
                args = list(args)
                args[0] = args[1]
                args[1] = '--help'

        try:
            instance = self.instanceResolver.resolve(args[0])
            instance.execute(*args)
        except SubcommandNotFoundError:

# Path: lib/Errors/SubcommandNotFoundError.py
# class SubcommandNotFoundError(Exception):
#     pass
# 
# 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/UsageHelp.py
# class UsageHelp:
#     def __init__(self):
#         pass
# 
#     def show(self):
#         subcommands = self._get_subcommands_list()
# 
#         a = textwrap.dedent('''
#         Usage: sudo ./open365 SUBCOMMAND [args...]
# 
#         SUBCOMMAND is one of:
#         ''')
#         a += "\t" + "\n\t".join(subcommands)
#         a += textwrap.dedent('''
# 
#         You can get help for a subcommand with
# 
#         \t sudo ./open365 SUBCOMMAND --help
#         ''')
#         print(a)
# 
#     def _get_subcommands_list(self):
#         files = os.listdir(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'Subcommands'))
#         return sorted(Casify.to_dash_case(file[:-3]) for file in files if file.endswith('.py'))
# 
#     def _get_profiles_list(self):
#         profiles_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
#                                      '..',
#                                      'environments',
#                                      'compose_files',
#                                      'latest')
#         all_files = (os.path.join(profiles_path, file) for file in os.listdir(profiles_path) if file.endswith('.yml'))
#         regex = re.compile('^include:$')
#         files = []
#         with fileinput.input(all_files) as line_iter:
#             for line in line_iter:
#                 if regex.search(line):
#                     files.append(os.path.basename(line_iter.filename())[:-4])
#                     line_iter.nextfile()
#         return sorted(files)
# 
# Path: lib/Wrappers/Logger.py
# class Logger:
#     def __init__(self, name):
#         if 'UNITTESTING' in os.environ:
#             self.logging = NullLogger()
#         else:
#             settings = Settings().getSettings()
#             logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
#                                 level=settings["logs"]["level"])
#             self.logging = logging.getLogger(name)
# 
#     def debug(self, *args, **kwargs):
#         self.logging.debug(*args, **kwargs)
# 
#     def info(self, *args, **kwargs):
#         self.logging.info(*args, **kwargs)
# 
#     def warning(self, *args, **kwargs):
#         self.logging.warning(*args, **kwargs)
# 
#     def error(self, *args, **kwargs):
#         self.logging.error(*args, **kwargs)
# 
#     def critical(self, *args, **kwargs):
#         self.logging.critical(*args, **kwargs)
# 
#     def log(self, *args, **kwargs):
#         self.logging.log(*args, **kwargs)


# Path: lib/SubcommandRouter.py
from lib.Errors.SubcommandNotFoundError import SubcommandNotFoundError
from lib.Tools.InstanceResolver import InstanceResolver
from lib.UsageHelp import UsageHelp
from lib.Wrappers.Logger import Logger



class SubcommandRouter:
    def __init__(self, devenvExecutor=None, instanceResolver=None):
        self.instanceResolver = instanceResolver or InstanceResolver()
        self.logger = Logger(__name__)

    def route(self, *args):
        if args[0] in ('help', '-h', '--help'):
            if len(args) == 1:
                usage = UsageHelp()
                usage.show()
                return
            else:
                args = list(args)
                args[0] = args[1]
                args[1] = '--help'

        try:
            instance = self.instanceResolver.resolve(args[0])
            instance.execute(*args)
        except SubcommandNotFoundError:

            usage = UsageHelp()