# 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/config.py
# def get_config_path(ensure_exists=False):
# def check_config_path():
# def read(rel_path, key=None, safe=False):
# def write(rel_path, key, value):
# 
# Path: termicoder/utils/logging.py

# 
# Path: termicoder/utils/launch.py
# def launch(app_args, url):
#     logger.debug(app_args)
#     if(app_args is None):
#         logger.warn('Preferred application is not set,'
#                     'launching %s in default application' % url)
#         click.launch(url)
#     else:
#         args = app_args[:]
#         if(not isinstance(app_args, list)):
#             args = [app_args]
#         if(isinstance(args[0], list) and args[0] != []):
#             launch(args[0], '')
#             if(len(args) > 1):
#                 logger.debug("len>1")
#                 launch(args[1:], url)
#             return
#         logger.info('launching command with following params')
#         logger.info(args)
#         if url not in [None, '']:
#             args.append(url)
#         logger.debug(args)
#         subprocess.run(args, check=True)
# 
# def substitute(args, keymap):
#     logger.debug('in substitute args')
#     logger.debug(args)
#     logger.debug(keymap)
#     if args not in [None, '', []]:
#         if(not (isinstance(args, list))):
#             logger.debug('executed base')
#             final_args = args[:]
#             for key in keymap:
#                 value = keymap[key]
#                 final_args = final_args.replace("{{%s}}" % key, str(value))
#                 logger.debug('final_args')
#                 logger.debug(final_args)
#             status = final_args != args
#         else:
#             status1, temp_arg = substitute(args[0], keymap)
#             status2, temp_arg1 = substitute(args[1:], keymap)
#             if(temp_arg1 not in [None, '', []]):
#                 final_args = [temp_arg]
#                 final_args.extend(temp_arg1)
#             else:
#                 final_args = [temp_arg]
#             status = status1 or status2
#             logger.debug('ran for')
#             logger.debug(args[0])
#             logger.debug(args[1:])
#             logger.debug("got back")
#             logger.debug(temp_arg)
#             logger.debug(temp_arg1)
# 
#         logger.debug(keymap)
#         logger.debug("args")
#         logger.debug(final_args)
#         return (status, final_args)
#     else:
#         return (False, None)
# 
# Path: termicoder/utils/load.py
# def get_default_code_name():
#     default_name = config.read('settings.yml', 'default_code_file')
#     if ".problem.yml" in os.listdir():
#         problem = yaml.read('.problem.yml')
#         default_name = problem.code + "." + config.read(
#             'settings.yml', 'default_extension')
#     return default_name


import click
import os
from ..utils.exceptions import handle_exceptions
from ..utils import config
from ..utils.logging import logger
from ..utils.launch import launch, substitute
from ..utils.load import get_default_code_name
    If other code file(s) exist, it should suggests to open the most
    recently edited one.

    Template for the code is loaded based upon extension.

    See 'termicoder config' for editing default templates,
    editor, and language preferences.
    '''
    if code_file is None:
        default_name = get_default_code_name()
        code_file = click.prompt(
            "Please enter a file name", default=default_name,
            type=click.Path(writable=True, dir_okay=False))

    extension = code_file.split('.')[-1]
    template = config.read('lang/%s/template.yml' % extension)
    if(template is not None):
        try:
            code_to_write = template['code']
            # allow jinja style template substitution in command
            # example {{row_no}} and {{col_no}}
            # see settings.yml for info on usage
            status, editor = substitute(editor, template)
            # useful for sublime's go to line functionality
            # see settings.yml for info on usage
            status, editor = substitute(editor, {
                r"CODE_FILE": code_file
            })
            logger.debug(code_to_write)
        except (AttributeError, KeyError):

# 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/config.py
# def get_config_path(ensure_exists=False):
# def check_config_path():
# def read(rel_path, key=None, safe=False):
# def write(rel_path, key, value):
# 
# Path: termicoder/utils/logging.py

# 
# Path: termicoder/utils/launch.py
# def launch(app_args, url):
#     logger.debug(app_args)
#     if(app_args is None):
#         logger.warn('Preferred application is not set,'
#                     'launching %s in default application' % url)
#         click.launch(url)
#     else:
#         args = app_args[:]
#         if(not isinstance(app_args, list)):
#             args = [app_args]
#         if(isinstance(args[0], list) and args[0] != []):
#             launch(args[0], '')
#             if(len(args) > 1):
#                 logger.debug("len>1")
#                 launch(args[1:], url)
#             return
#         logger.info('launching command with following params')
#         logger.info(args)
#         if url not in [None, '']:
#             args.append(url)
#         logger.debug(args)
#         subprocess.run(args, check=True)
# 
# def substitute(args, keymap):
#     logger.debug('in substitute args')
#     logger.debug(args)
#     logger.debug(keymap)
#     if args not in [None, '', []]:
#         if(not (isinstance(args, list))):
#             logger.debug('executed base')
#             final_args = args[:]
#             for key in keymap:
#                 value = keymap[key]
#                 final_args = final_args.replace("{{%s}}" % key, str(value))
#                 logger.debug('final_args')
#                 logger.debug(final_args)
#             status = final_args != args
#         else:
#             status1, temp_arg = substitute(args[0], keymap)
#             status2, temp_arg1 = substitute(args[1:], keymap)
#             if(temp_arg1 not in [None, '', []]):
#                 final_args = [temp_arg]
#                 final_args.extend(temp_arg1)
#             else:
#                 final_args = [temp_arg]
#             status = status1 or status2
#             logger.debug('ran for')
#             logger.debug(args[0])
#             logger.debug(args[1:])
#             logger.debug("got back")
#             logger.debug(temp_arg)
#             logger.debug(temp_arg1)
# 
#         logger.debug(keymap)
#         logger.debug("args")
#         logger.debug(final_args)
#         return (status, final_args)
#     else:
#         return (False, None)
# 
# Path: termicoder/utils/load.py
# def get_default_code_name():
#     default_name = config.read('settings.yml', 'default_code_file')
#     if ".problem.yml" in os.listdir():
#         problem = yaml.read('.problem.yml')
#         default_name = problem.code + "." + config.read(
#             'settings.yml', 'default_extension')
#     return default_name


# Path: termicoder/cli/code.py
import click
import os
from ..utils.exceptions import handle_exceptions
from ..utils import config
from ..utils.logging import logger
from ..utils.launch import launch, substitute
from ..utils.load import get_default_code_name

    If other code file(s) exist, it should suggests to open the most
    recently edited one.

    Template for the code is loaded based upon extension.

    See 'termicoder config' for editing default templates,
    editor, and language preferences.
    '''
    if code_file is None:
        default_name = get_default_code_name()
        code_file = click.prompt(
            "Please enter a file name", default=default_name,
            type=click.Path(writable=True, dir_okay=False))

    extension = code_file.split('.')[-1]
    template = config.read('lang/%s/template.yml' % extension)
    if(template is not None):
        try:
            code_to_write = template['code']
            # allow jinja style template substitution in command
            # example {{row_no}} and {{col_no}}
            # see settings.yml for info on usage
            status, editor = substitute(editor, template)
            # useful for sublime's go to line functionality
            # see settings.yml for info on usage
            status, editor = substitute(editor, {
                r"CODE_FILE": code_file
            })
            logger.debug(code_to_write)
        except (AttributeError, KeyError):

            logger.error("Probelm with template file")