# Path: termicoder/utils/logging.py

# 
# Path: termicoder/utils/exceptions.py
# def handle_exceptions(*exceptions):
#     """
#     Used as a decorator.
#     Takes exceptions and a function,
#     returns function warpped with try except and debug functionality
#     """
#     # TODO correct the line number
#     def wrapper(function):
#         def customized_function(*args, **kwargs):
#             logger.debug(
#                 "calling function %s" % sys.modules[function.__module__])
#             try:
#                 function(*args, **kwargs)
#             except (exceptions) as e:
#                 if(logger.level == 10 or isinstance(e, click.Abort)):
#                     raise
#                 else:
#                     logger.error("in module %s:function %s:line %s" % (
#                         sys.modules[function.__module__].__file__,
#                         function.__name__, e.__traceback__.tb_lineno))
#                     logger.error("%s %s" % (e.__class__.__name__, e))
#                     raise click.Abort
#         customized_function.__wrapped__ = True
#         customized_function.__doc__ = function.__doc__
#         return customized_function
#     return wrapper
# 
# Path: termicoder/utils/load.py
# def get_problem_or_contest(folder):
#     if ".contest.yml" in os.listdir(folder):
#         return yaml.read(os.path.join(folder, '.contest.yml'))
#     elif ".problem.yml" in os.listdir(folder):
#         return yaml.read(os.path.join(folder, '.problem.yml'))
# 
# Path: termicoder/models/Problem.py
# class Problem(ABC):
#     @abstractmethod
#     def __init__(self, data):
#         self.name = None
#         self.status = None
#         self.submissions_count = 0
#         self.data = data
#         self.code = None
#         self.html = None
#         self.testcases = None
#         self.contest_code = None
#         self.judge_name = None
#         self.timelimit = 3.0  # in seconds
# 
#     # used by list
#     @abstractmethod
#     def __str__(self):
#         pass
# 
# Path: termicoder/models/Contest.py
# class Contest(ABC):
#     @abstractmethod
#     def __init__(self, data=None):
#         # If contest_data, problems is passed, it should take priority
#         self.code = None
#         self.problems = None
#         self.judge_name = None
#         self.data = data
# 
#     @abstractmethod
#     def __str__(self):
#         pass


import click
import subprocess
from ...utils.logging import logger
from ...utils.exceptions import handle_exceptions
from ...utils.load import get_problem_or_contest
from ...models import Problem, Contest


@click.command(short_help="View contents of folder.")
@click.argument("FOLDER", type=click.Path(
                    exists=True, file_okay=False, dir_okay=True), default='.')
@handle_exceptions(BaseException)
def main(folder):
    '''
    Display the termicoder contents in current/passed folder.
    Current folder is the default.

    \b
    If it is a contest folder it displays the list of problems in the browser.
    If its a problem folder, displays the problem in a browser.
    '''
    logger.warn('list is not colored in this version')
    p_or_c = get_problem_or_contest(folder)
    assert(isinstance(p_or_c, Problem) or isinstance(p_or_c, Contest))
    if(p_or_c is None):

# Path: termicoder/utils/logging.py

# 
# Path: termicoder/utils/exceptions.py
# def handle_exceptions(*exceptions):
#     """
#     Used as a decorator.
#     Takes exceptions and a function,
#     returns function warpped with try except and debug functionality
#     """
#     # TODO correct the line number
#     def wrapper(function):
#         def customized_function(*args, **kwargs):
#             logger.debug(
#                 "calling function %s" % sys.modules[function.__module__])
#             try:
#                 function(*args, **kwargs)
#             except (exceptions) as e:
#                 if(logger.level == 10 or isinstance(e, click.Abort)):
#                     raise
#                 else:
#                     logger.error("in module %s:function %s:line %s" % (
#                         sys.modules[function.__module__].__file__,
#                         function.__name__, e.__traceback__.tb_lineno))
#                     logger.error("%s %s" % (e.__class__.__name__, e))
#                     raise click.Abort
#         customized_function.__wrapped__ = True
#         customized_function.__doc__ = function.__doc__
#         return customized_function
#     return wrapper
# 
# Path: termicoder/utils/load.py
# def get_problem_or_contest(folder):
#     if ".contest.yml" in os.listdir(folder):
#         return yaml.read(os.path.join(folder, '.contest.yml'))
#     elif ".problem.yml" in os.listdir(folder):
#         return yaml.read(os.path.join(folder, '.problem.yml'))
# 
# Path: termicoder/models/Problem.py
# class Problem(ABC):
#     @abstractmethod
#     def __init__(self, data):
#         self.name = None
#         self.status = None
#         self.submissions_count = 0
#         self.data = data
#         self.code = None
#         self.html = None
#         self.testcases = None
#         self.contest_code = None
#         self.judge_name = None
#         self.timelimit = 3.0  # in seconds
# 
#     # used by list
#     @abstractmethod
#     def __str__(self):
#         pass
# 
# Path: termicoder/models/Contest.py
# class Contest(ABC):
#     @abstractmethod
#     def __init__(self, data=None):
#         # If contest_data, problems is passed, it should take priority
#         self.code = None
#         self.problems = None
#         self.judge_name = None
#         self.data = data
# 
#     @abstractmethod
#     def __str__(self):
#         pass


# Path: termicoder/cli/ls/folder.py
import click
import subprocess
from ...utils.logging import logger
from ...utils.exceptions import handle_exceptions
from ...utils.load import get_problem_or_contest
from ...models import Problem, Contest



@click.command(short_help="View contents of folder.")
@click.argument("FOLDER", type=click.Path(
                    exists=True, file_okay=False, dir_okay=True), default='.')
@handle_exceptions(BaseException)
def main(folder):
    '''
    Display the termicoder contents in current/passed folder.
    Current folder is the default.

    \b
    If it is a contest folder it displays the list of problems in the browser.
    If its a problem folder, displays the problem in a browser.
    '''
    logger.warn('list is not colored in this version')
    p_or_c = get_problem_or_contest(folder)
    assert(isinstance(p_or_c, Problem) or isinstance(p_or_c, Contest))
    if(p_or_c is None):

        logger.warn('no .problem or .contest in current folder, running ls -l')