<commit_before># Copyright (c) 2013-2014, <NAME> (<EMAIL>)
# Copyright (c) 2014, <NAME> (<EMAIL>)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.  THIS SOFTWARE IS
# PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
# NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from enum import Enum
from enum import unique
from utils import create_instance

PYECLIB_MAX_DATA = 32
PYECLIB_MAX_PARITY = 32


@unique
class PyECLibEnum(Enum):

    def describe(self):
        # returns supported types
        return list(self)

    def __str__(self):
        return "%s: %d" % (self.name, self.value)


# Note: the Enum start value defaults to 1 as the starting value and not 0
# 0 is False in the boolean sense but enum members evaluate to True
class PyECLib_EC_Types(PyECLibEnum):
    rs_vand = 1
    rs_cauchy_orig = 2
    flat_xor_3 = 3
    flat_xor_4 = 4


# Note: the Enum start value defaults to 1 as the starting value and not 0
# 0 is False in the boolean sense but enum members evaluate to True
class PyECLib_HDRCHKSUM_Types(PyECLibEnum):
    none = 1
    inline = 2
    algsig = 3


#
# Generic ECDriverException
#


class ECDriverError(Exception):

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

    def __str__(self):
        return self.error_str


class ECDriver(object):

    def __init__(self, library_import_str, *args, **kwargs):
        self.k = -1
        self.m = -1
        self.library_import_str = None
        for (key, value) in kwargs.items():
            if key == "k":
                self.k = int(value)
            elif key == "m":
                self.m = int(value)
            elif key == "ec_type":
                self.ec_type = value
            elif key == "chksum_type":
                self.chksum_type = value

        if library_import_str is not None:
            self.library_import_str = library_import_str
        else:
            raise ECDriverError(
                "Library import string (library_import_str) was not specified "
                "and is a required argument!")

        if self.k < 0:
            raise ECDriverError(
                "Number of data fragments (k) was not specified "
                "and is a required argument!")
        if self.m < 0:
            raise ECDriverError(
                "Number of parity fragments (m) was not specified "
                "and is a required argument!")

        #
        # We require keyword arguments to prevent ambiguity between EC libs
        #
        self.ec_lib_reference = create_instance(
            library_import_str,
            *args,
            **kwargs)

        #
        # Verify that the imported library implements the required functions
        #
        required_methods = {
            'decode': 0,
            'encode': 0,
            'reconstruct': 0,
            'fragments_needed': 0,
            'get_metadata': 0,
            'verify_stripe_metadata': 0,
            'get_segment_info': 0
        }

        for attr in dir(self.ec_lib_reference):
            if hasattr(getattr(self.ec_lib_reference, attr), "__call__"):
                required_methods[attr] = 1

        not_implemented_str = ""
        for (method, is_implemented) in required_methods.items():<commit_msg>ec_iface: Use positive_int_value for k/m validation<commit_after># Copyright (c) 2013-2014, <NAME> (<EMAIL>)
# Copyright (c) 2014, <NAME> (<EMAIL>)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.  THIS SOFTWARE IS
# PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
# NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from enum import Enum
from enum import unique
from utils import create_instance
from utils import positive_int_value

PYECLIB_MAX_DATA = 32
PYECLIB_MAX_PARITY = 32


@unique
class PyECLibEnum(Enum):

    def describe(self):
        # returns supported types
        return list(self)

    def __str__(self):
        return "%s: %d" % (self.name, self.value)


# Note: the Enum start value defaults to 1 as the starting value and not 0
# 0 is False in the boolean sense but enum members evaluate to True
class PyECLib_EC_Types(PyECLibEnum):
    rs_vand = 1
    rs_cauchy_orig = 2
    flat_xor_3 = 3
    flat_xor_4 = 4


# Note: the Enum start value defaults to 1 as the starting value and not 0
# 0 is False in the boolean sense but enum members evaluate to True
class PyECLib_HDRCHKSUM_Types(PyECLibEnum):
    none = 1
    inline = 2
    algsig = 3


#
# Generic ECDriverException
#


class ECDriverError(Exception):

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

    def __str__(self):
        return self.error_str


class ECDriver(object):

    def __init__(self, library_import_str, *args, **kwargs):
        self.k = -1
        self.m = -1
        self.library_import_str = None
        for (key, value) in kwargs.items():
            if key == "k":
                try:
                    self.k = positive_int_value(value)
                except ValueError as e:
                    raise ECDriverError(
                        "Invalid number of data fragments (k)")
            elif key == "m":
                try:
                    self.m = positive_int_value(value)
                except ValueError as e:
                    raise ECDriverError(
                        "Invalid number of data fragments (m)")
            elif key == "ec_type":
                self.ec_type = value
            elif key == "chksum_type":
                self.chksum_type = value

        if library_import_str is not None:
            self.library_import_str = library_import_str
        else:
            raise ECDriverError(
                "Library import string (library_import_str) was not specified "
                "and is a required argument!")
        #
        # We require keyword arguments to prevent ambiguity between EC libs
        #
        self.ec_lib_reference = create_instance(
            library_import_str,
            *args,
            **kwargs)

        #
        # Verify that the imported library implements the required functions
        #
        required_methods = {
            'decode': 0,
            'encode': 0,
            'reconstruct': 0,
            'fragments_needed': 0,
            'get_metadata': 0,
            'verify_stripe_metadata': 0,
            'get_segment_info': 0
        }

        for attr in dir(self.ec_lib_reference):
            if hasattr(getattr(self.ec_lib_reference, attr), "__call__"):
                required_methods[attr] = 1

        not_implemented_str = ""
        for (method, is_implemented) in required_methods.items():
{'fraction_non_alphanumeric': 0.06942711287577992, 'fraction_numerical': 0.00952921157118548, 'mean_word_length': 2.4749704375246355, 'pattern_counts': {'":': 8, '<': 13, '<?xml version=': 0, '>': 11, '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': '3603904', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2339, 'n_words': 934}