<commit_before>"""
Command line interface
"""

from abc import ABCMeta, abstractmethod
import argparse
import json
import logging
import httplib as http_client
import sys

import repositorytools

logger = logging.getLogger(sys.argv[0])


def configure_logging(debug):
    if debug:
        logging.basicConfig(level=logging.DEBUG)
        http_client.HTTPConnection.debuglevel = 1
        requests_log = logging.getLogger("requests.packages.urllib3")
        requests_log.setLevel(logging.INFO)
        requests_log.propagate = True
    else:
        logging.basicConfig(level=logging.INFO)


class CLI(object):
    __metaclass__ = ABCMeta

    """
    Base class for cli
    """
    @abstractmethod
    def _get_parser(self):
        """
        :return: argparse.ArgumentParser
        """
        pass

    def __init__(self):
        self.parser = self._get_parser()
        self.parser.add_argument("-D", "--debug", action="store_true", dest="debug", default=False,
                                 help="Print lots of debugging information")
        self.repository = repositorytools.repository_client_factory()

    def run(self, args=None):
        args_namespace = self.parser.parse_args(args)
        configure_logging(args_namespace.debug)
        logger.info('Started %s', str(sys.argv))

        """
        This runs the function that is assigned to the sub-command by calling of set_defaults
        """
        return args_namespace.func(args_namespace)


class ArtifactCLI(CLI):
    def _get_parser(self):
        parser = argparse.ArgumentParser(description='A command line tool for working with artifacts')
        subparsers = parser.add_subparsers()

        # upload
        parser_upload = subparsers.add_parser('upload', help='Uploads an artifact to repository, can detect name and'
                                                             ' version from filename')
        parser_upload.add_argument("-s", "--staging", action="store_true", dest="staging", default=False,
                            help="Uploads to a newly created staging repository which targets given repository")
        parser_upload.add_argument("--upload-filelist", action="store_true", default=False, help="uploads list of uploaded "
                                                                                                 "files")
        parser_upload.add_argument("--artifact", help="name of artifact, if omitted, will be detected from filename")
        parser_upload.add_argument("--version", help="version of artifact, if omitted, will be detected from filename")
        parser_upload.add_argument("-d", "--description", dest="description", default='No description',
                                   help="Description of a staging repository")

        parser_upload.add_argument("local_file", help="path to an artifact on your machine")
        parser_upload.add_argument("repo_id", help="id of target repository")
        parser_upload.add_argument("group", help="artifact group")
        parser_upload.set_defaults(func=self.upload)

        # delete<commit_msg>cli: better logging during tests<commit_after>"""
Command line interface
"""

from abc import ABCMeta, abstractmethod
import argparse
import json
import logging
import httplib as http_client
import sys

import repositorytools

logger = logging.getLogger(sys.argv[0])


def configure_logging(debug):
    if debug:
        logging.basicConfig(level=logging.DEBUG)
        http_client.HTTPConnection.debuglevel = 1
        requests_log = logging.getLogger("requests.packages.urllib3")
        requests_log.setLevel(logging.INFO)
        requests_log.propagate = True
    else:
        logging.basicConfig(level=logging.INFO)


class CLI(object):
    __metaclass__ = ABCMeta

    """
    Base class for cli
    """
    @abstractmethod
    def _get_parser(self):
        """
        :return: argparse.ArgumentParser
        """
        pass

    def __init__(self):
        self.parser = self._get_parser()
        self.parser.add_argument("-D", "--debug", action="store_true", dest="debug", default=False,
                                 help="Print lots of debugging information")
        self.repository = repositorytools.repository_client_factory()

    def run(self, args=None):
        args_namespace = self.parser.parse_args(args)
        configure_logging(args_namespace.debug)
        # we don't use 'str(sys.argv), so we can log actual arguments during tests'
        logger.info('Started %s, with arguments %s', sys.argv[0], str(args))

        """
        This runs the function that is assigned to the sub-command by calling of set_defaults
        """
        return args_namespace.func(args_namespace)


class ArtifactCLI(CLI):
    def _get_parser(self):
        parser = argparse.ArgumentParser(description='A command line tool for working with artifacts')
        subparsers = parser.add_subparsers()

        # upload
        parser_upload = subparsers.add_parser('upload', help='Uploads an artifact to repository, can detect name and'
                                                             ' version from filename')
        parser_upload.add_argument("-s", "--staging", action="store_true", dest="staging", default=False,
                            help="Uploads to a newly created staging repository which targets given repository")
        parser_upload.add_argument("--upload-filelist", action="store_true", default=False, help="uploads list of uploaded "
                                                                                                 "files")
        parser_upload.add_argument("--artifact", help="name of artifact, if omitted, will be detected from filename")
        parser_upload.add_argument("--version", help="version of artifact, if omitted, will be detected from filename")
        parser_upload.add_argument("-d", "--description", dest="description", default='No description',
                                   help="Description of a staging repository")

        parser_upload.add_argument("local_file", help="path to an artifact on your machine")
        parser_upload.add_argument("repo_id", help="id of target repository")
        parser_upload.add_argument("group", help="artifact group")
        parser_upload.set_defaults(func=self.upload)

        # delete
{'fraction_non_alphanumeric': 0.09804854831032842, 'fraction_numerical': 0.0011105822624147231, 'mean_word_length': 2.592022792022792, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2061664', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1557, 'n_words': 415}