<commit_before>"""Utilities to operate Nuxeo Drive from the command line"""
import os
import sys
import argparse
from getpass import getpass
import traceback
try:
    import ipdb
    debugger = ipdb
except ImportError:
    import pdb
    debugger = pdb

from nxdrive.controller import Controller
from nxdrive.controller import default_nuxeo_drive_folder
from nxdrive.logging_config import configure
from nxdrive.logging_config import get_logger

DEFAULT_NX_DRIVE_FOLDER = default_nuxeo_drive_folder()
DEFAULT_DELAY = 5.0
USAGE = """ndrive [command]

Possible commands:
- console
- start
- stop
- bind-server
- unbind-server
- bind-root
- unbind-root

To get options for a specific command:

  ndrive command --help

"""

PROTOCOL_COMMANDS = {
    'nxdriveedit': 'edit',
    'nxdrivebind': 'bind_server',
}


NXDRIVE_EDIT_URL_FORM = ('nxdrive://edit/scheme/server[:port]'
                         '/webappname/nxdoc/reponame/docref')

def parse_protocol_url(url_string):
    """Parse URL for which nxdrive is registered as a protocol handler

    Return None if url_string is not a supported URL pattern or raise a
    ValueError is the URL structure is invalid.

    """
    if "://" not in url_string:
        return None

    protocol_name, data_string = url_string.split('://', 1)
    if protocol_name != 'nxdrive':
        return None

    if '/' not in data_string:
        raise ValueError("Invalid nxdrive URL: " + url_string)

    command, data_string = data_string.split('/', 1)
    if command == 'edit':
        return parse_edit_protocol(data_string)
    else:
        raise ValueError("Unsupported command '%s' in " + url_string)


def parse_edit_protocol(data_string):
    """Parse a nxdriveedit:// URL for quick editing of nuxeo documents"""
    invalid_msg = ('Invalid URL: got nxdrive://edit/%s while expecting %s'
                   % (data_string, NXDRIVE_EDIT_URL_FORM))

    if '/' not in data_string:
        raise ValueError(invalid_msg)

    scheme, data_string = data_string.split('/', 1)
    if scheme not in ('http', 'https'):
        raise ValueError(
            invalid_msg + ' : scheme should be http or https')

    if '/nxdoc/' not in data_string:
        raise ValueError(invalid_msg)

    server_part, doc_part = data_string.split('/nxdoc/', 1)
    server_url = "%s://%s" % (scheme, server_part)

    components = doc_part.split('/')
    if len(components) != 2:
        raise ValueError(invalid_msg)

    repository, docref = components
    return dict(command='edit', server_url=server_url,
                repository=repository, docref=docref)


def make_cli_parser(add_subparsers=True):
    """Parse commandline arguments using a git-like subcommands scheme"""

    common_parser = argparse.ArgumentParser(
        add_help=False,
    )
    common_parser.add_argument(
        "--nxdrive-home",
        default="~/.nuxeo-drive",
        help="Folder to store the Nuxeo Drive configuration."
    )
    common_parser.add_argument(
        "--log-level-file",
        default="DEBUG",
        help="Minimum log level for the file log (under NXDRIVE_HOME/logs)."
    )
    common_parser.add_argument(
        "--log-level-console",
        default="DEBUG",
        help="Minimum log level for the console log."
    )
    common_parser.add_argument(
        "--log-filename",
        help=("File used to store the logs, default "
              "NXDRIVE_HOME/logs/nxaudit.logs")
    )
    common_parser.add_argument(
        "--debug", default=False, action="store_true",
        help="Fire a debugger (ipdb or pdb) one uncaught error."
    )

    parser = argparse.ArgumentParser(
        parents=[common_parser],
        description="Command line interface for Nuxeo Drive operations.",
        usage=USAGE,
    )

    if not add_subparsers:
        return parser

    subparsers = parser.add_subparsers(
        title='Commands',
    )

    # Link to a remote Nuxeo server
    bind_server_parser = subparsers.add_parser(
        'bind-server', help='Attach a local folder to a Nuxeo server.',
        parents=[common_parser],<commit_msg>NXP-10354: console should stay at info level by default<commit_after>"""Utilities to operate Nuxeo Drive from the command line"""
import os
import sys
import argparse
from getpass import getpass
import traceback
try:
    import ipdb
    debugger = ipdb
except ImportError:
    import pdb
    debugger = pdb

from nxdrive.controller import Controller
from nxdrive.controller import default_nuxeo_drive_folder
from nxdrive.logging_config import configure
from nxdrive.logging_config import get_logger

DEFAULT_NX_DRIVE_FOLDER = default_nuxeo_drive_folder()
DEFAULT_DELAY = 5.0
USAGE = """ndrive [command]

Possible commands:
- console
- start
- stop
- bind-server
- unbind-server
- bind-root
- unbind-root

To get options for a specific command:

  ndrive command --help

"""

PROTOCOL_COMMANDS = {
    'nxdriveedit': 'edit',
    'nxdrivebind': 'bind_server',
}


NXDRIVE_EDIT_URL_FORM = ('nxdrive://edit/scheme/server[:port]'
                         '/webappname/nxdoc/reponame/docref')

def parse_protocol_url(url_string):
    """Parse URL for which nxdrive is registered as a protocol handler

    Return None if url_string is not a supported URL pattern or raise a
    ValueError is the URL structure is invalid.

    """
    if "://" not in url_string:
        return None

    protocol_name, data_string = url_string.split('://', 1)
    if protocol_name != 'nxdrive':
        return None

    if '/' not in data_string:
        raise ValueError("Invalid nxdrive URL: " + url_string)

    command, data_string = data_string.split('/', 1)
    if command == 'edit':
        return parse_edit_protocol(data_string)
    else:
        raise ValueError("Unsupported command '%s' in " + url_string)


def parse_edit_protocol(data_string):
    """Parse a nxdriveedit:// URL for quick editing of nuxeo documents"""
    invalid_msg = ('Invalid URL: got nxdrive://edit/%s while expecting %s'
                   % (data_string, NXDRIVE_EDIT_URL_FORM))

    if '/' not in data_string:
        raise ValueError(invalid_msg)

    scheme, data_string = data_string.split('/', 1)
    if scheme not in ('http', 'https'):
        raise ValueError(
            invalid_msg + ' : scheme should be http or https')

    if '/nxdoc/' not in data_string:
        raise ValueError(invalid_msg)

    server_part, doc_part = data_string.split('/nxdoc/', 1)
    server_url = "%s://%s" % (scheme, server_part)

    components = doc_part.split('/')
    if len(components) != 2:
        raise ValueError(invalid_msg)

    repository, docref = components
    return dict(command='edit', server_url=server_url,
                repository=repository, docref=docref)


def make_cli_parser(add_subparsers=True):
    """Parse commandline arguments using a git-like subcommands scheme"""

    common_parser = argparse.ArgumentParser(
        add_help=False,
    )
    common_parser.add_argument(
        "--nxdrive-home",
        default="~/.nuxeo-drive",
        help="Folder to store the Nuxeo Drive configuration."
    )
    common_parser.add_argument(
        "--log-level-file",
        default="DEBUG",
        help="Minimum log level for the file log (under NXDRIVE_HOME/logs)."
    )
    common_parser.add_argument(
        "--log-level-console",
        default="INFO",
        help="Minimum log level for the console log."
    )
    common_parser.add_argument(
        "--log-filename",
        help=("File used to store the logs, default "
              "NXDRIVE_HOME/logs/nxaudit.logs")
    )
    common_parser.add_argument(
        "--debug", default=False, action="store_true",
        help="Fire a debugger (ipdb or pdb) one uncaught error."
    )

    parser = argparse.ArgumentParser(
        parents=[common_parser],
        description="Command line interface for Nuxeo Drive operations.",
        usage=USAGE,
    )

    if not add_subparsers:
        return parser

    subparsers = parser.add_subparsers(
        title='Commands',
    )

    # Link to a remote Nuxeo server
    bind_server_parser = subparsers.add_parser(
        'bind-server', help='Attach a local folder to a Nuxeo server.',
        parents=[common_parser],
{'fraction_non_alphanumeric': 0.10238240684178375, 'fraction_numerical': 0.002321319486866219, 'mean_word_length': 2.9280230326295587, 'pattern_counts': {'":': 2, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4140550', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2449, 'n_words': 681}