# 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_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 pyperclip
import os
from ..utils.logging import logger
from ..utils.exceptions import handle_exceptions
from ..utils.load import get_default_code_name


@click.command(short_help='Copies code from file to clipboard.')
@click.argument('code_file',
                type=click.Path(exists=True, dir_okay=False),
                required=False)
@handle_exceptions(BaseException)
def main(code_file):
    '''
    Copies code from CODE_FILE to the clipboard.

    If CODE_FILE is not passed, a default file is suggested based
    on current directory.

    The suggested file is the most recently edited code file
    recognized by termicoder.
    '''
    if(code_file is None):
        default_file = get_default_code_name()
        if (not os.path.exists(default_file)):
            default_file = None
        code_file = click.prompt(
            "Please enter the file to copy",
            default=default_file,
            type=click.Path(readable=True, exists=True)
        )
    pyperclip.copy(open(code_file, 'r').read())

# 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_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/clip.py
import click
import pyperclip
import os
from ..utils.logging import logger
from ..utils.exceptions import handle_exceptions
from ..utils.load import get_default_code_name



@click.command(short_help='Copies code from file to clipboard.')
@click.argument('code_file',
                type=click.Path(exists=True, dir_okay=False),
                required=False)
@handle_exceptions(BaseException)
def main(code_file):
    '''
    Copies code from CODE_FILE to the clipboard.

    If CODE_FILE is not passed, a default file is suggested based
    on current directory.

    The suggested file is the most recently edited code file
    recognized by termicoder.
    '''
    if(code_file is None):
        default_file = get_default_code_name()
        if (not os.path.exists(default_file)):
            default_file = None
        code_file = click.prompt(
            "Please enter the file to copy",
            default=default_file,
            type=click.Path(readable=True, exists=True)
        )
    pyperclip.copy(open(code_file, 'r').read())

    logger.info("copied %s to clipboard" % code_file)