# Path: toolbox/utils/config.py
# def get_crp_type(config: dict) -> str:
#     crp_type = config.get('crp_type') or 'static'
# 
#     if crp_type not in CRP_TYPES:
#         fatal_error("Unknown crp_type: '%s' / %s", crp_type, CRP_TYPES)
# 
#     return crp_type
# 
# def read_config(path: str, *, pre_validate: bool = True) -> dict:
#     """
#     Read the config.yml file
# 
#     :param path: path to the file or the base directory
#     :param pre_validate: check version and crp_type fields
#     :return: dict
#     """
#     if os.path.isdir(path):
#         path = os.path.join(path, 'config.yml')
# 
#     try:
#         with open(path, 'r') as f:
#             config = yaml.safe_load(f)
# 
#         if pre_validate:
#             validate_version(config)
#             get_crp_type(config)
# 
#         return config
# 
#     except Exception as e:
#         fatal_error('%s(%s)', type(e).__name__, e)
# 
# Path: toolbox/utils/utils.py
# def counted_error_at_exit() -> None:
#     """
#     Call at the end of execution to handle errors
#     """
#     logging.info('Finished with %d error(s).', _error_counter)
#     sys.exit(1 if _error_counter > 0 else 0)
# 
# def get_error_counter() -> int:
#     """
#     Return the current number of non-fatal errors
#     """
#     return _error_counter
# 
# def get_sys_args() -> Tuple[str, str, str]:
#     """
#     Get parsed command line arguments
# 
#     Absolute repository path, repository name (optional)
#     :return tuple: repo_path, repo_name, repo_branch
#     """
#     if not 2 <= len(sys.argv) <= 4:
#         logging.info('Usage: %s <repo_path> [repo_name] [repo_branch]', sys.argv[0])
#         sys.exit(1)
# 
#     repo_path = find_repo_path(os.path.realpath(sys.argv[1]))
#     repo_name = sys.argv[2] if len(sys.argv) >= 3 else os.path.basename(repo_path)
#     repo_branch = sys.argv[3] if len(sys.argv) >= 4 else get_repo_branch(repo_path)
# 
#     return repo_path, repo_name, repo_branch
# 
# def git_submodule_init(repo_path: str = None):
#     if repo_path is None:
#         repo_path = os.getcwd()
# 
#     run_cmd(
#         [
#             'if git rev-parse --git-dir &>/dev/null; then '
#             'git submodule update --init --checkout --recursive; fi'
#         ],
#         cwd=repo_path,
#         shell=True,
#     )
# 
# def init_logger() -> None:
#     """
#     Configure the default python logger
#     """
#     logger = logging.getLogger()
#     logger.setLevel(logging.DEBUG)
#     handler = logging.StreamHandler(sys.stdout)
#     handler.setLevel(logging.DEBUG)
#     formatter = logging.Formatter('[%(levelname)s] %(message)s')
#     handler.setFormatter(formatter)
#     logger.addHandler(handler)


from importlib import import_module
from .config import get_crp_type, read_config
from .utils import counted_error_at_exit, get_error_counter, get_sys_args, git_submodule_init, init_logger



def _run_command(command: str, repo_path: str, repo_name: str, repo_branch: str, config: dict):
    module_path = '.'.join(('toolbox', get_crp_type(config), command))
    module = import_module(module_path)
    module.run(repo_path, repo_name, repo_branch, config)

    if get_error_counter() > 0:
        counted_error_at_exit()


def run(*commands):
    init_logger()
    repo_path, repo_name, repo_branch = get_sys_args()
    config = read_config(repo_path)

# Path: toolbox/utils/config.py
# def get_crp_type(config: dict) -> str:
#     crp_type = config.get('crp_type') or 'static'
# 
#     if crp_type not in CRP_TYPES:
#         fatal_error("Unknown crp_type: '%s' / %s", crp_type, CRP_TYPES)
# 
#     return crp_type
# 
# def read_config(path: str, *, pre_validate: bool = True) -> dict:
#     """
#     Read the config.yml file
# 
#     :param path: path to the file or the base directory
#     :param pre_validate: check version and crp_type fields
#     :return: dict
#     """
#     if os.path.isdir(path):
#         path = os.path.join(path, 'config.yml')
# 
#     try:
#         with open(path, 'r') as f:
#             config = yaml.safe_load(f)
# 
#         if pre_validate:
#             validate_version(config)
#             get_crp_type(config)
# 
#         return config
# 
#     except Exception as e:
#         fatal_error('%s(%s)', type(e).__name__, e)
# 
# Path: toolbox/utils/utils.py
# def counted_error_at_exit() -> None:
#     """
#     Call at the end of execution to handle errors
#     """
#     logging.info('Finished with %d error(s).', _error_counter)
#     sys.exit(1 if _error_counter > 0 else 0)
# 
# def get_error_counter() -> int:
#     """
#     Return the current number of non-fatal errors
#     """
#     return _error_counter
# 
# def get_sys_args() -> Tuple[str, str, str]:
#     """
#     Get parsed command line arguments
# 
#     Absolute repository path, repository name (optional)
#     :return tuple: repo_path, repo_name, repo_branch
#     """
#     if not 2 <= len(sys.argv) <= 4:
#         logging.info('Usage: %s <repo_path> [repo_name] [repo_branch]', sys.argv[0])
#         sys.exit(1)
# 
#     repo_path = find_repo_path(os.path.realpath(sys.argv[1]))
#     repo_name = sys.argv[2] if len(sys.argv) >= 3 else os.path.basename(repo_path)
#     repo_branch = sys.argv[3] if len(sys.argv) >= 4 else get_repo_branch(repo_path)
# 
#     return repo_path, repo_name, repo_branch
# 
# def git_submodule_init(repo_path: str = None):
#     if repo_path is None:
#         repo_path = os.getcwd()
# 
#     run_cmd(
#         [
#             'if git rev-parse --git-dir &>/dev/null; then '
#             'git submodule update --init --checkout --recursive; fi'
#         ],
#         cwd=repo_path,
#         shell=True,
#     )
# 
# def init_logger() -> None:
#     """
#     Configure the default python logger
#     """
#     logger = logging.getLogger()
#     logger.setLevel(logging.DEBUG)
#     handler = logging.StreamHandler(sys.stdout)
#     handler.setLevel(logging.DEBUG)
#     formatter = logging.Formatter('[%(levelname)s] %(message)s')
#     handler.setFormatter(formatter)
#     logger.addHandler(handler)


# Path: toolbox/utils/command.py
from importlib import import_module
from .config import get_crp_type, read_config
from .utils import counted_error_at_exit, get_error_counter, get_sys_args, git_submodule_init, init_logger




def _run_command(command: str, repo_path: str, repo_name: str, repo_branch: str, config: dict):
    module_path = '.'.join(('toolbox', get_crp_type(config), command))
    module = import_module(module_path)
    module.run(repo_path, repo_name, repo_branch, config)

    if get_error_counter() > 0:
        counted_error_at_exit()


def run(*commands):
    init_logger()
    repo_path, repo_name, repo_branch = get_sys_args()
    config = read_config(repo_path)

    git_submodule_init(repo_path)