<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" Plugin script for `ff` (https://github.com/mysz/ff).
"""

from __future__ import print_function, unicode_literals

import os.path

def _test_greater(arg1, arg2):
    """ Test for being greater then.
    """
    return arg1 > arg2

def _test_less(arg1, arg2):
    """ Test for being less then.
    """
    return arg1 < arg2

def _test_equal(arg1, arg2):
    """ Test for being equal.
    """
    return arg1 == arg2

_TESTS = {
    '>': _test_greater,
    '<': _test_less,
    '=': _test_equal
}

_MULTI = {
    'b': 1,
    'k': 1024,
    'm': 1024**2,
    'g': 1024**3,
}

_cache = {} # pylint: disable-msg=invalid-name

def _action(__, argument, path):
    """ Test given path for being it's size match specified criteria.

        `__` - not used
        `argument` - data passed by user. It's syntax is: [<>=]?[0-9]+[bkgm]?
        `path` - path to tested file
    """

    if not argument:
        raise PluginError('missing size') # pylint: disable=undefined-variable

    if argument[0] in ('<', '>', '='):
        test = _TESTS[argument[0]]
        size = argument[1:]
    else:
        test = _TESTS['=']
        size = argument

    if size[-1] in 'bkmgBKMG':
        size = int(size[:-1]) * _MULTI[size[-1].lower()]
    else:
        size = int(size)

    path = os.path.realpath(path)
    if path not in _cache:
        _cache[path] = os.path.getsize(path)
    return test(_cache[path], size)

def plugin_action(name, argument, path):
    """ Action used by `ff`. Wrapper for `_action`.
    """
    try:
        return _action(name, argument, path)
    except PluginError: # pylint: disable=undefined-variable
        raise
    except:
        import sys
        ex = sys.exc_info()[1]
        raise PluginError(ex.message) # pylint: disable=undefined-variable

PLUGIN_DESCR = 'Filter files by their size.'
PLUGIN_HELP = '''Size must be given as argument, and must follow pattern (without spaces):<commit_msg>fixed exception name (PluginError -> FFPluginError)<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" Plugin script for `ff` (https://github.com/mysz/ff).
"""

from __future__ import print_function, unicode_literals

import os.path

def _test_greater(arg1, arg2):
    """ Test for being greater then.
    """
    return arg1 > arg2

def _test_less(arg1, arg2):
    """ Test for being less then.
    """
    return arg1 < arg2

def _test_equal(arg1, arg2):
    """ Test for being equal.
    """
    return arg1 == arg2

_TESTS = {
    '>': _test_greater,
    '<': _test_less,
    '=': _test_equal
}

_MULTI = {
    'b': 1,
    'k': 1024,
    'm': 1024**2,
    'g': 1024**3,
}

_cache = {} # pylint: disable-msg=invalid-name

def _action(__, argument, path):
    """ Test given path for being it's size match specified criteria.

        `__` - not used
        `argument` - data passed by user. It's syntax is: [<>=]?[0-9]+[bkgm]?
        `path` - path to tested file
    """

    if not argument:
        raise FFPluginError('missing size') # pylint: disable=undefined-variable

    if argument[0] in ('<', '>', '='):
        test = _TESTS[argument[0]]
        size = argument[1:]
    else:
        test = _TESTS['=']
        size = argument

    if size[-1] in 'bkmgBKMG':
        size = int(size[:-1]) * _MULTI[size[-1].lower()]
    else:
        size = int(size)

    path = os.path.realpath(path)
    if path not in _cache:
        _cache[path] = os.path.getsize(path)
    return test(_cache[path], size)

def plugin_action(name, argument, path):
    """ Action used by `ff`. Wrapper for `_action`.
    """
    try:
        return _action(name, argument, path)
    except FFPluginError: # pylint: disable=undefined-variable
        raise
    except:
        import sys
        ex = sys.exc_info()[1]
        raise FFPluginError(ex.message) # pylint: disable=undefined-variable

PLUGIN_DESCR = 'Filter files by their size.'
PLUGIN_HELP = '''Size must be given as argument, and must follow pattern (without spaces):
{'fraction_non_alphanumeric': 0.1493280238924838, 'fraction_numerical': 0.01841712294673967, 'mean_word_length': 2.6503178928247046, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 2, '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': '7226695', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1342, 'n_words': 373}