<commit_before>from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from __future__ import print_function

import os
import sys

from ..pkg.workspace import Workspace, CurrentDirWorkspace
from ..pkg import spec as pkg_spec
from ..pkg.archive import Archive
from .. import repos
from . import arg_help
from . import arg_metavar

ERROR_EXIT = 1


def die(msg):
    sys.stderr.write('ERROR: ')
    sys.stderr.write(msg)
    sys.stderr.write('\n')
    sys.exit(ERROR_EXIT)


def warning(msg):
    sys.stderr.write('WARNING: ')
    sys.stderr.write(msg)
    sys.stderr.write('\n')


def OPTIONAL_WORKSPACE(parser):
    '''
    Define `workspace` as option, defaulting to current directory
    '''
    parser.arg(
        '--workspace', '-w', metavar=arg_metavar.WORKSPACE,
        type=Workspace, default=CurrentDirWorkspace(),
        help=arg_help.WORKSPACE)


class DefaultArgSentinel(object):
    '''
    I am a sentinel for default values.

    I.e. If you see me, it means that you got the default value.

    I also provide human sensible description for the default value.
    '''

    def __init__(self, description):
        self.description = description

    def __repr__(self):
        return self.description


def tag(tag):
    '''
    Make a function that tags its input
    '''
    return lambda value: (tag, value)


def package_spec_kwargs(parser):
    arg = parser.arg
    # TODO: implement more options
    # -r, --repo, --repository

    # package_filters
    arg('-o', '--older', '--older-than', dest='query',
        metavar='TIMEDELTA', type=tag('older_than'))
    arg('-n', '--newer', '--newer-than', dest='query',
        metavar='TIMEDELTA', type=tag('newer_than'))
    # arg('-d', '--date', dest='date'),

    # match reducers
    # -N, --next
    # -P, --prev, --previous
    # --newest, --latest (default)
    # --oldest


def parse_package_spec_kwargs(kwargs):
    # assert False, kwargs
    query = pkg_spec.PackageQuery()
    query_modifier = {
        'older_than': query.is_older_than,
        'newer_than': query.is_newer_than,
    }
    for attr in kwargs:
        if kwargs[attr] is not None:
            query = query_modifier[attr](kwargs[attr])<commit_msg>package_spec_kwargs is added as an argument group<commit_after>from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from __future__ import print_function

import os
import sys

from ..pkg.workspace import Workspace, CurrentDirWorkspace
from ..pkg import spec as pkg_spec
from ..pkg.archive import Archive
from .. import repos
from . import arg_help
from . import arg_metavar

ERROR_EXIT = 1


def die(msg):
    sys.stderr.write('ERROR: ')
    sys.stderr.write(msg)
    sys.stderr.write('\n')
    sys.exit(ERROR_EXIT)


def warning(msg):
    sys.stderr.write('WARNING: ')
    sys.stderr.write(msg)
    sys.stderr.write('\n')


def OPTIONAL_WORKSPACE(parser):
    '''
    Define `workspace` as option, defaulting to current directory
    '''
    parser.arg(
        '--workspace', '-w', metavar=arg_metavar.WORKSPACE,
        type=Workspace, default=CurrentDirWorkspace(),
        help=arg_help.WORKSPACE)


class DefaultArgSentinel(object):
    '''
    I am a sentinel for default values.

    I.e. If you see me, it means that you got the default value.

    I also provide human sensible description for the default value.
    '''

    def __init__(self, description):
        self.description = description

    def __repr__(self):
        return self.description


def tag(tag):
    '''
    Make a function that tags its input
    '''
    return lambda value: (tag, value)


def package_spec_kwargs(parser):
    group = parser.argparser.add_argument_group(
        'package query', 'Restrict the package version with these options')
    arg = group.add_argument
    # TODO: implement more options
    # -r, --repo, --repository

    # package_filters
    arg('-o', '--older', '--older-than', dest='query',
        metavar='TIMEDELTA', type=tag('older_than'))
    arg('-n', '--newer', '--newer-than', dest='query',
        metavar='TIMEDELTA', type=tag('newer_than'))
    # arg('-d', '--date', dest='date'),

    # match reducers
    # -N, --next
    # -P, --prev, --previous
    # --newest, --latest (default)
    # --oldest


def parse_package_spec_kwargs(kwargs):
    # assert False, kwargs
    query = pkg_spec.PackageQuery()
    query_modifier = {
        'older_than': query.is_older_than,
        'newer_than': query.is_newer_than,
    }
    for attr in kwargs:
        if kwargs[attr] is not None:
            query = query_modifier[attr](kwargs[attr])
{'fraction_non_alphanumeric': 0.12417041318775422, 'fraction_numerical': 0.00042817383857846286, 'mean_word_length': 3.0034275921165383, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6400440', 'n_tokens_mistral': 1482, 'n_tokens_neox': 1465, 'n_words': 396}