# Path: termicoder/models/JudgeFactory.py
# class JudgeFactory:
#     def __init__(self):
#         self.available_judges = []
#         self._judge_classes = {}
#         self._load_judges()
# 
#     def _load_judges(self):
#         judges = iter_entry_points('termicoder.judge_plugins')
#         for judge in judges:
#             try:
#                 judge_class = judge.load()
#                 assert(issubclass(judge_class, Judge))
#                 # Try instantiating judge to check if abstract methods
#                 #  are implemented
#                 # TODO: see for a better alternative instead of instantiating
#                 self.available_judges.append(judge.name)
#                 judge_class()
# 
#                 # decorate login and logout with _write_session_data
#                 judge_class.login = self._write_session_data(
#                     judge.name)(judge_class.login)
#                 judge_class.logout = self._write_session_data(
#                     judge.name)(judge_class.logout)
#                 judge_class.refresh_login = self._write_session_data(
#                     judge.name)(judge_class.refresh_login)
# 
#                 self._judge_classes[judge.name] = judge_class
# 
#             # TODO log about 'why could not load judge'
#             # also pass the exception instead of raising
#             except AssertionError as e:
#                 logger.error("%s is not a subclass of Judge" % judge_class)
#             except TypeError as e:
#                 # Abstract methods are not implemented
#                 logger.error(
#                     "%s \ndoes not implement required "
#                     "abstract methods of class Judge" % judge_class)
#             except BaseException as e:
#                 logger.error(e)
#                 # think about something using click.abort without
#                 # printing traceback
#                 sys.exit(1)
# 
#         # sorting judges for statefulness
#         self.available_judges.sort()
# 
#     def get_judge(self, judge_name):
#         if(judge_name not in self.available_judges):
#             raise JudgeNotFoundError
#         else:
#             # Return an instance of the judge
#             # TODO load session data if available
#             session_data = config.read('judges/sessions.yml', judge_name)
#             return self._judge_classes[judge_name](session_data=session_data)
# 
#     def _write_session_data(self, judge_name):
#         def decorator(function):
#             def decorated_function(self_, *args, **kwargs):
#                 function(self_, *args, **kwargs)
#                 # TODO save to the appropriate location
#                 logger.debug("_write_session_data for %s" % judge_name)
#                 logger.debug(self_.session_data)
#                 config.write(
#                     'judges/sessions.yml', judge_name, self_.session_data)
#             return decorated_function
#         return decorator
# 
# Path: termicoder/utils/constants.py

# 
# 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


import click
from ...models import JudgeFactory
from ...utils.constants import default_judge
from ...utils.logging import logger
from ...utils.exceptions import handle_exceptions

judge_factory = JudgeFactory()
OJs = judge_factory.available_judges
if default_judge is None:
    try:

# Path: termicoder/models/JudgeFactory.py
# class JudgeFactory:
#     def __init__(self):
#         self.available_judges = []
#         self._judge_classes = {}
#         self._load_judges()
# 
#     def _load_judges(self):
#         judges = iter_entry_points('termicoder.judge_plugins')
#         for judge in judges:
#             try:
#                 judge_class = judge.load()
#                 assert(issubclass(judge_class, Judge))
#                 # Try instantiating judge to check if abstract methods
#                 #  are implemented
#                 # TODO: see for a better alternative instead of instantiating
#                 self.available_judges.append(judge.name)
#                 judge_class()
# 
#                 # decorate login and logout with _write_session_data
#                 judge_class.login = self._write_session_data(
#                     judge.name)(judge_class.login)
#                 judge_class.logout = self._write_session_data(
#                     judge.name)(judge_class.logout)
#                 judge_class.refresh_login = self._write_session_data(
#                     judge.name)(judge_class.refresh_login)
# 
#                 self._judge_classes[judge.name] = judge_class
# 
#             # TODO log about 'why could not load judge'
#             # also pass the exception instead of raising
#             except AssertionError as e:
#                 logger.error("%s is not a subclass of Judge" % judge_class)
#             except TypeError as e:
#                 # Abstract methods are not implemented
#                 logger.error(
#                     "%s \ndoes not implement required "
#                     "abstract methods of class Judge" % judge_class)
#             except BaseException as e:
#                 logger.error(e)
#                 # think about something using click.abort without
#                 # printing traceback
#                 sys.exit(1)
# 
#         # sorting judges for statefulness
#         self.available_judges.sort()
# 
#     def get_judge(self, judge_name):
#         if(judge_name not in self.available_judges):
#             raise JudgeNotFoundError
#         else:
#             # Return an instance of the judge
#             # TODO load session data if available
#             session_data = config.read('judges/sessions.yml', judge_name)
#             return self._judge_classes[judge_name](session_data=session_data)
# 
#     def _write_session_data(self, judge_name):
#         def decorator(function):
#             def decorated_function(self_, *args, **kwargs):
#                 function(self_, *args, **kwargs)
#                 # TODO save to the appropriate location
#                 logger.debug("_write_session_data for %s" % judge_name)
#                 logger.debug(self_.session_data)
#                 config.write(
#                     'judges/sessions.yml', judge_name, self_.session_data)
#             return decorated_function
#         return decorator
# 
# Path: termicoder/utils/constants.py

# 
# 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/cli/ls/contest.py
import click
from ...models import JudgeFactory
from ...utils.constants import default_judge
from ...utils.logging import logger
from ...utils.exceptions import handle_exceptions


judge_factory = JudgeFactory()
OJs = judge_factory.available_judges
if default_judge is None:
    try:

        default_judge = OJs[0]