text:
from typing import Iterable
from collections import OrderedDict
from functools import wraps, partial
from itertools import chain
import re
import importlib
import inspect
import types
import json
import logging

from fontbakery.errors import NamespaceError, SetupError, CircularAliasError
from fontbakery.callable import (
    FontbakeryCallable,
    FontBakeryCheck,
    FontBakeryCondition,
    FontBakeryExpectedValue,
)
from fontbakery.configuration import Configuration
from fontbakery.message import Message
from fontbakery.section import Section
from fontbakery.utils import is_negated
from fontbakery.status import DEBUG

def get_module_profile(module, name=None):
    """
    Get or create a profile from a module and return it.

    If the name `module.profile` is present the value of that is returned.
    Otherwise, if the name `module.profile_factory` is present, a new profile
    is created using `module.profile_factory` and then `profile.auto_register`
    is called with the module namespace.
    If neither name is defined, the module is not considered a profile-module
    and None is returned.

    TODO: describe the `name` argument and better define the signature of `profile_factory`.

    The `module` argument is expected to behave like a python module.
    The optional `name` argument is used when `profile_factory` is called to
    give a name to the default section of the new profile. If name is not
    present `module.__name__` is the fallback.

    `profile_factory` is called like this:
        `profile = module.profile_factory(default_section=default_section)`

    """
    try:
        # if profile is defined we just use it
        return module.profile
    except AttributeError:  # > 'module' object has no attribute 'profile'
        # try to create one on the fly.
        # e.g. module.__name__ == "fontbakery.profiles.cmap"
        if "profile_factory" not in module.__dict__:
            return None
        default_section = Section(name or module.__name__)
        profile = module.profile_factory(
            default_section=default_section, module_spec=module.__spec__
        )
        profile.auto_register(module.__dict__)
        return profile

class Profile:
    """
    Profiles may specify default configuration values (used to parameterize
    checks), which are then overridden by values in the user's configuration
    file.
    """
    configuration_defaults = {}

    def __init__(
        self,
        sections=None,
        iterargs=None,
        derived_iterables=None,
        conditions=None,
        aliases=None,
        expected_values=None,
        default_section=None,
        check_skip_filter=None,
        profile_tag=None,
        module_spec=None,
    ):
        """
          sections: a list of sections, which are ideally ordered sets of
              individual checks.
              It makes no sense to have checks repeatedly, they yield the same
              results anyway, thus we don't allow this.
          iterargs: maping 'singular' variable names to the iterable in values
              e.g.: `{'font': 'fonts'}` in this case fonts must be iterable AND
              'font' may not be a value NOR a condition name.
          derived_iterables: a dictionary {"plural": ("singular", bool simple)}
              where singular points to a condition, that consumes directly or indirectly
              iterargs. plural will be a list of all values the condition produces
              with all combination of it's iterargs.
              If simple is False, the result returns tuples of: (iterars, value)
              where iterargs is a tuple of ('iterargname', number index)
              Especially for cases where only one iterarg is involved, simple
              can be set to True and the result list will just contain the values.
              Example:

              @condition
              def ttFont(font):
                  return TTFont(font)

              values={'fonts': ['font_0', 'font_1']}
              iterargs={'font': 'fonts'}

              derived_iterables={'ttFonts': ('ttFont', True)}
              # Then:
              ttfons = (
                  <TTFont object from font_0>
                , <TTFont object from font_1>
              )

              # However
              derived_iterables={'ttFonts': ('ttFont', False)}
              ttfons = [
                  ((('font', 0), ), <TTFont object from font_0>)
                , ((('font', 1), ), <TTFont object from font_1>)
              ]

        We will:
          a) get all needed values/variable names from here
          b) add some validation, so that we know the values match
             our expectations! These values must be treated as user input!
        """
        self._namespace = {
            "config": "config"  # Filled in by checkrunner
        }

        self.iterargs = {}
        if iterargs:
            self._add_dict_to_namespace("iterargs", iterargs)

        self.derived_iterables = {}
        if derived_iterables:
            self._add_dict_to_namespace("derived_iterables", derived_iterables)

        self.aliases = {}
        if aliases:
            self._add_dict_to_namespace("aliases", aliases)

        self.conditions = {}
        if conditions:
            self._add_dict_to_namespace("conditions", conditions)

        self.expected_values = {}
        if expected_values:
            self._add_dict_to_namespace("expected_values", expected_values)

        self._check_registry = {}
        self._sections = OrderedDict()
        if sections:
            for section in sections:
                self.add_section(section)

        if not default_section:
            default_section = (
                sections[0] if sections and len(sections) else Section("Default")
            )
        self._default_section = default_section
        self.add_section(self._default_section)

        # currently only used for new check ids in self.check_log_override
        # only a-z everything else is deleted
        self.profile_tag = re.sub(
            r"[^a-z]", "", (profile_tag or self._default_section.name).lower()
        )

        self._check_skip_filter = check_skip_filter

        # Used in multiprocessing because pickling the profiles fail on
        # Mac and Windows. See: googlefonts/fontbakery#2982
        # module_locator can actually a module.__spec__ but also just a dict
        # self.module_locator will always be just a dict
        if module_spec is None:
            # This is a bit of a hack, but the idea is to reduce boilerplate
            # when writing modules that directly define a profile.
            try:
                frame = inspect.currentframe().f_back
                while frame:
                    # Note, if __spec__ is a local variable we shpuld be at a
                    # module top level. It should also be the correct ModuleSpec
                    # according to how we do this "usually" (as documented and
                    # practiced as far as I'm aware of), e.g. the profile module
                    # defines a profile object directly by calling a Profile constructor
                    # (e.g. profies.ufo_sources) or indirectly via a profile_factory
                    # (e.g. profiles.google_fonts). Otherwise, if this fails
                    # or finds a wrong ModuleSpec, there's still the option to
                    # pass module_spec as an argument (module.__spec__), which is
                    # actually demonstrated in get_module_profile.
                    if "__spec__" in frame.f_locals:
                        module_spec = frame.f_locals["__spec__"]
                        if module_spec and isinstance(
                            module_spec, importlib.machinery.ModuleSpec
                        ):
                            break
                        module_spec = None  # reset
                    frame = frame.f_back
            finally:
                del frame

        # If not module_spec: this is only a problem in multiprocessing, in
        # that case we'll be failing to access this with an AttributeError.
        if module_spec is not None:
            self.module_locator = dict(name=module_spec.name, origin=module_spec.origin)

    _valid_namespace_types = {
        "iterargs": "iterarg",
        "derived_iterables": "derived_iterable",
        "aliases": "alias",
        "conditions": "condition",
        "expected_values": "expected_value",
    }

    @property
    def sections(self):
        return self._sections.values()

    def _add_dict_to_namespace(self, type, data):
        for key, value in data.items():
            self.add_to_namespace(
                type, key, value, force=getattr(value, "force", False)
            )

    def add_to_namespace(self, type, name, value, force=False):
        if type not in self._valid_namespace_types:
            valid_types = ", ".join(self._valid_namespace_types)
            raise TypeError(f'Unknow type "{type}"' f" Valid types are: {valid_types}")

        if name in self._namespace:
            registered_type = self._namespace[name]
            registered_value = getattr(self, registered_type)[name]
            if type == registered_type and registered_value == value:
                # if the registered equals: skip silently. Registering the same
                # value multiple times is allowed, so we can easily expand profiles
                # that define (partly) the same entries
                return

            if not force:
                msg = (
                    f'Name "{name}" is already registered'
                    f' in "{registered_type}" (value: {registered_value}).'
                    f' Requested registering in "{type}" (value: {value}).'
                )
                raise NamespaceError(msg)
            else:
                # clean the old type up
                del getattr(self, registered_type)[name]

        self._namespace[name] = type
        target = getattr(self, type)
        target[name] = value

    def test_dependencies(self):
        """Raises SetupError if profile uses any names that are not declared
        in the its namespace.
        """
        seen = set()
        failed = []
        # make this simple, collect all used names
        for section_name, section in self._sections.items():
            for check in section.checks:
                dependencies = list(check.args)
                if hasattr(check, "conditions"):
                    dependencies += [
                        name for negated, name in map(is_negated, check.conditions)
                    ]

                while dependencies:
                    name = dependencies.pop()
                    if name in seen:
                        continue
                    seen.add(name)
                    if name not in self._namespace:
                        failed.append(name)
                        continue
                    # if this is a condition, expand its dependencies
                    condition = self.conditions.get(name, None)
                    if condition is not None:
                        dependencies += condition.args
        if len(failed):
            comma_separated = ", ".join(failed)
            raise SetupError(
                f"Profile uses names that are not declared"
                f" in its namespace: {comma_separated}."
            )

    def test_expected_checks(self, expected_check_ids, exclusive=False):
        """Self-test to make a sure profile maintainer is aware of changes in
        the profile.
        Raises SetupError if expected check ids are missing in the profile (removed)
        If `exclusive=True` also raises SetupError if check ids are in the
        profile that are not in expected_check_ids (newly added).

        This is handy if `profile.auto_register` is used and the profile maintainer
        is looking for a high level of control over the profile contents,
        especially for a warning when the profile contents have changed after an
        update.
        """
        s = set()
        duplicates = set(x for x in expected_check_ids if x in s or s.add(x))
        if len(duplicates):
            raise SetupError(
                "Profile has duplicated entries in its list"
                " of expected check IDs:\n" + "\n".join(duplicates)
            )

        expected_check_ids = set(expected_check_ids)
        registered_checks = set(self._check_registry.keys())
        missing_checks = expected_check_ids - registered_checks
        unexpected_checks = None
        if exclusive:
            unexpected_checks = registered_checks - expected_check_ids
        message = []
        if missing_checks:
            message.append("missing checks: {};".format(", ".join(missing_checks)))
        if unexpected_checks:
            message.append(
                "unexpected checks: {};".format(", ".join(unexpected_checks))
            )
        if message:
            raise SetupError(
                "Profile fails expected checks test:\n" + "\n".join(message)
            )

        def is_numerical_id(checkid):
            try:
                int(checkid.split("/")[-1])
                return True
            except:
                return False

        numerical_check_ids = [c for c in registered_checks if is_numerical_id(c)]
        if numerical_check_ids:
            list_of_checks = "\t- " + "\n\t- ".join(numerical_check_ids)
            raise SetupError(
                f"\n"
                f"\n"
                f"Numerical check IDs must be renamed to keyword-based IDs:\n"
                f"{list_of_checks}\n"
                f"\n"
                f"See also: https://github.com/googlefonts/fontbakery/issues/2238\n"
                f"\n"
            )

    def resolve_alias(self, original_name):
        name = original_name
        seen = set()
        path = []
        while name in self.aliases:
            if name in seen:
                names = " -> ".join(path)
                raise CircularAliasError(
                    f'Alias for "{original_name}" has'
                    f" a circular reference in {names}"
                )
            seen.add(name)
            path.append(name)
            name = self.aliases[name]
        return name

    def validate_values(self, values):
        """
        Validate values if they are registered as expected_values and present.

        * If they are not registered they shouldn't be used anywhere at all
          because profile can self check (profile.check_dependencies) for
          missing/undefined dependencies.

        * If they are not present in values but registered as expected_values
          either the expected value has a default value OR a request for that
          name will raise a KeyError on runtime. We don't know if all expected
          values are actually needed/used, thus this fails late.
        """
        messages = []
        for name, value in values.items():
            if name not in self.expected_values:
                continue
            valid, message = self.expected_values[name].validate(value)
            if valid:
                continue
            messages.append(f"{name}: {message} (value: {value})")
        if len(messages):
            return False, "\n".join(messages)
        return True, None

    def get_type(self, name, *args):
        has_fallback = bool(args)
        if has_fallback:
            fallback = args[0]

        if not name in self._namespace:
            if has_fallback:
                return fallback
            raise KeyError(name)

        return self._namespace[name]

    def get(self, name, *args):
        has_fallback = bool(args)
        if has_fallback:
            fallback = args[0]

        try:
            target_type = self.get_type(name)
        except KeyError:
            if not has_fallback:
                raise
            return fallback

        target = getattr(self, target_type)
        if name not in target:
            if has_fallback:
                return fallback
            raise KeyError(name)
        return target[name]

    def has(self, name):
        marker_fallback = object()
        val = self.get(name, marker_fallback)
        return val is not marker_fallback

    def _get_aggregate_args(self, item, key):
        """
        Get all arguments or mandatory arguments of the item.

        Item is a check or a condition, which means it can be dependent on
        more conditions, this climbs down all the way.
        """
        if not key in ("args", "mandatoryArgs"):
            raise TypeError(f'key must be "args" or "mandatoryArgs", got {key}')
        dependencies = list(getattr(item, key))
        if hasattr(item, "conditions"):
            dependencies += [name for negated, name in map(is_negated, item.conditions)]
        args = set()
        while dependencies:
            name = dependencies.pop()
            if name in args:
                continue
            args.add(name)
            # if this is a condition, expand its dependencies
            c = self.conditions.get(name, None)
            if c is None:
                continue
            dependencies += [
                dependency for dependency in getattr(c, key) if dependency not in args
            ]
        return args

    def get_iterargs(self, item):
        """ Returns a tuple of all iterags for item, sorted by name."""
        # iterargs should always be mandatory, unless there's a good reason
        # not to, which I can't think of right now.

        args = self._get_aggregate_args(item, "mandatoryArgs")
        return tuple(sorted([arg for arg in args if arg in self.iterargs]))

    def _analyze_checks(self, all_args, checks):
        args = list(all_args)
        args.reverse()
        # (check, signature, scope)
        scopes = [(check, tuple(), tuple()) for check in checks]
        aggregatedArgs = {
            "args": {
                check.name: self._get_aggregate_args(check, "args") for check in checks
            },
            "mandatoryArgs": {
                check.name: self._get_aggregate_args(check, "mandatoryArgs")
                for check in checks
            },
        }
        saturated = []
        while args:
            new_scopes = []
            # args_set must contain all current args, hence it's before the pop
            args_set = set(args)
            arg = args.pop()
            for check, signature, scope in scopes:
                if not len(aggregatedArgs["args"][check.name] & args_set):
                    # there's no args no more or no arguments of check are
                    # in args
                    target = saturated
                elif (
                    arg == "*check"
                    or arg in aggregatedArgs["mandatoryArgs"][check.name]
                ):
                    signature += (1,)
                    scope += (arg,)
                    target = new_scopes
                else:
                    # there's still a tail of args and check requires one of the
                    # args in tail but not the current arg
                    signature += (0,)
                    target = new_scopes
                target.append((check, signature, scope))
            scopes = new_scopes
        return saturated + scopes

    def _execute_section(self, iterargs, section, items):
        if section is None:
            # base case: terminate recursion
            for check, signature, scope in items:
                yield check, []
        elif not section[0]:
            # no sectioning on this level
            for item in self._execute_scopes(iterargs, items):
                yield item
        elif section[1] == "*check":
            # enforce sectioning by check
            for section_item in items:
                for item in self._execute_scopes(iterargs, [section_item]):
                    yield item
        else:
            # section by gen_arg, i.e. ammend with changing arg.
            _, gen_arg = section
            for index in range(iterargs[gen_arg]):
                for check, args in self._execute_scopes(iterargs, items):
                    yield check, [(gen_arg, index)] + args

    def _execute_scopes(self, iterargs, scopes):
        generators = []
        items = []
        current_section = None
        last_section = None
        seen = set()
        for check, signature, scope in scopes:
            if len(signature):
                # items are left
                if signature[0]:
                    gen_arg = scope[0]
                    scope = scope[1:]
                    current_section = True, gen_arg
                else:
                    current_section = False, None
                signature = signature[1:]
            else:
                current_section = None

            assert (
                current_section not in seen
            ), f"Scopes are badly sorted. {current_section} in {seen}"

            if current_section != last_section:
                if len(items):
                    # flush items
                    generators.append(
                        self._execute_section(iterargs, last_section, items)
                    )
                    items = []
                    seen.add(last_section)
                last_section = current_section
            items.append((check, signature, scope))
        # clean up left overs
        if len(items):
            generators.append(self._execute_section(iterargs, current_section, items))

        for item in chain(*generators):
            yield item

    def _section_execution_order(
        self,
        section,
        iterargs,
        reverse=False,
        custom_order=None,
        explicit_checks: Iterable = None,
        exclude_checks: Iterable = None,
    ):
        """
        order must:
          a) contain all variable args (we're appending missing ones)
          b) not contian duplictates (we're removing repeated items)

        order may contain *iterargs otherwise it is appended
        to the end

        order may contain "*check" otherwise, it is like *check is appended
        to the end (Not done explicitly though).
        """
        stack = list(custom_order) if custom_order is not None else list(section.order)
        if "*iterargs" not in stack:
            stack.append("*iterargs")
        stack.reverse()

        full_order = []
        seen = set()
        while len(stack):
            item = stack.pop()
            if item in seen:
                continue
            seen.add(item)
            if item == "*iterargs":
                all_iterargs = list(iterargs.keys())
                # assuming there is a meaningful order
                all_iterargs.reverse()
                stack += all_iterargs
                continue
            full_order.append(item)

        # Filter down checks. Checks to exclude are filtered for last as the user
        # might e.g. want to include all tests with "kerning" in the ID, except for
        # "kerning_something". explicit_checks could then be ["kerning"] and
        # exclude_checks ["something"].
        checks = section.checks
        if explicit_checks:
            checks = [
                check
                for check in checks
                if any(include_string in check.id for include_string in explicit_checks)
            ]
        if exclude_checks:
            checks = [
                check
                for check in checks
                if not any(
                    exclude_string in check.id for exclude_string in exclude_checks
                )
            ]

        scopes = self._analyze_checks(full_order, checks)
        # check, signature, scope = item
        scopes.sort(key=lambda item: item[1], reverse=reverse)

        for check, args in self._execute_scopes(iterargs, scopes):
            # this is the iterargs tuple that will be used as a key for caching
            # and so on. we could sort it, to ensure it yields in the same
            # cache locations always, but then again, it is already in a well
            # defined order, by clustering.
            yield check, tuple(args)

    def execution_order(
        self, iterargs, custom_order=None, explicit_checks=None, exclude_checks=None
    ):
        # TODO: a custom_order per section may become necessary one day
        explicit_checks = set() if not explicit_checks else set(explicit_checks)
        for _, section in self._sections.items():
            for check, section_iterargs in self._section_execution_order(
                section,
                iterargs,
                custom_order=custom_order,
                explicit_checks=explicit_checks,
                exclude_checks=exclude_checks,
            ):
                yield (section, check, section_iterargs)

    def _register_check(self, section, func):
        other_section = self._check_registry.get(func.id, None)
        if other_section:
            other_check = other_section.get_check(func.id)
            if other_check is func:
                if other_section is not section:
                    logging.debug(
                        "Check {} is already registered in {}, skipping "
                        "register in {}.".format(func, other_section, section)
                    )
                return False  # skipped
            else:
                raise SetupError(
                    f'Check id "{func}" is not unique!'
                    f" It is already registered in {other_section} and"
                    f" registration for that id is now requested in {section}."
                    f" BUT the current check is a different object than"
                    f" the registered check."
                )
        self._check_registry[func.id] = section
        return True

    def _unregister_check(self, section, check_id):
        assert (
            section == self._check_registry[check_id]
        ), "Registered section must match"
        del self._check_registry[check_id]
        return True

    def remove_check(self, check_id):
        section = self._check_registry[check_id]
        section.remove_check(check_id)

    def check_log_override(
        self,
        override_check_id
        # see def check_log_override
        ,
        *args,
        **kwds,
    ):
        new_id = f"{override_check_id}:{self.profile_tag}"
        old_check, section = self.get_check(override_check_id)
        new_check = check_log_override(old_check, new_id, *args, **kwds)
        new_check.conditions = old_check.conditions
        section.replace_check(override_check_id, new_check)
        return new_check

    def get_check(self, check_id):
        section = self._check_registry[check_id]
        return section.get_check(check_id), section

    def add_section(self, section):
        key = str(section)
        if key in self._sections:
            # the string representation of a section must be unique.
            # string representations of section and check will be used as unique keys
            if self._sections[key] is not section:
                raise SetupError(f"A section with key {section} is already registered")
            return
        self._sections[key] = section
        section.on_add_check(self._register_check)
        section.on_remove_check(self._unregister_check)

        for check in section.checks:
            self._register_check(section, check)

    def _get_section(self, key):
        return self._sections[key]

    def _add_check(self, section, func):
        self.add_section(section)
        section.add_check(func)
        return func

    def register_check(self, section=None, *args, **kwds):
        """
        Usage:
        # register in default section
        @profile.register_check
        @check(id='com.example.fontbakery/check/0')
        def my_check():
          yield PASS, 'example'

        # register in `special_section` also register that section in the profile
        @profile.register_check(special_section)
        @check(id='com.example.fontbakery/check/0')
        def my_check():
          yield PASS, 'example'

        """
        if section and len(kwds) == 0 and callable(section):
            func = section
            section = self._default_section
            return self._add_check(section, func)
        else:
            return partial(self._add_check, section)

    def _add_condition(self, condition, name=None):
        self.add_to_namespace(
            "conditions", name or condition.name, condition, force=condition.force
        )
        return condition

    def register_condition(self, *args, **kwds):
        """
        Usage:

        @profile.register_condition
        @condition
        def myCondition():
          return 123

        #or

        @profile.register_condition(name='my_condition')
        @condition
        def myCondition():
          return 123
        """
        if len(args) == 1 and len(kwds) == 0 and callable(args[0]):
            return self._add_condition(args[0])
        else:
            return partial(self._add_condition, *args, **kwds)

    def register_expected_value(self, expected_value, name=None):
        name = name or expected_value.name
        self.add_to_namespace(
            "expected_values", name, expected_value, force=expected_value.force
        )
        return True

    @staticmethod
    def _get_package(symbol_table):
        package = symbol_table.get("__package__", None)
        if package is not None:
            return package
        name = symbol_table.get("__name__", None)
        if name is None or "." not in name:
            return None
        return name.rpartition(".")[0]

    def _load_profile_imports(self, symbol_table):
        """
        profile_imports is a list of module names or tuples
        of (module_name, names to import)
        in the form of ('.', names) it behaces like:
        from . import name1, name2, name3
        or similarly
        import .name1, .name2, .name3

        i.e. "name" in names becomes ".name"
        """
        results = []
        if "profile_imports" not in symbol_table:
            return results

        package = self._get_package(symbol_table)
        profile_imports = symbol_table["profile_imports"]

        for item in profile_imports:
            if isinstance(item, str):
                # import the whole module
                module_name, names = (item, None)
            else:
                # expecting a 2 items tuple or list
                # import only the names from the module
                module_name, names = item

            if "." in module_name and len(set(module_name)) == 1 and names is not None:
                # if you execute `from . import mod` from a module in the pkg package
                # then you will end up importing pkg.mod
                module_names = [f"{module_name}{name}" for name in names]
                names = None
            else:
                module_names = [module_name]

            for module_name in module_names:
                module = importlib.import_module(module_name, package=package)
                if names is None:
                    results.append(module)
                else:
                    #  1. check if the imported module has an attribute by that name
                    #  2. if not, attempt to import a submodule with that name
                    #  3. if the attribute is not found, ImportError is raised.
                    #  …
                    for name in names:
                        try:
                            results.append(getattr(module, name))
                        except AttributeError:
                            # attempt to import a submodule with that name
                            sub_module_name = ".".join([module_name, name])
                            sub_module = importlib.import_module(
                                sub_module_name, package=package
                            )
                            results.append(sub_module)
        return results

    def auto_register(self, symbol_table, filter_func=None, profile_imports=None):
        """Register items from `symbol_table` in the profile.

        Get all items from `symbol_table` dict and from `symbol_table.profile_imports`
        if it is present. If they an item is an instance of FontBakeryCheck,
        FontBakeryCondition or FontBakeryExpectedValue and register it in
        the default section.
        If an item is a python module, try to get a profile using `get_module_profile(item)`
        and then using `merge_profile`;
        If the profile_imports kwarg is given, it is used instead of the one taken from
        the module namespace.

        To register the current module use explicitly:
          `profile.auto_register(globals())`
          OR maybe: `profile.auto_register(sys.modules[__name__].__dict__)`
        To register an imported module explicitly:
          `profile.auto_register(module.__dict__)`

        if filter_func is defined it is called like:
        filter_func(type, name_or_id, item)
        where
        type: one of "check", "module", "condition", "expected_value", "iterarg",
              "derived_iterable", "alias"
        name_or_id: the name at which the item will be registered.
              if type == 'check': the check.id
              if type == 'module': the module name (module.__name__)
        item: the item to be registered
        if filter_func returns a falsy value for an item, the item will
        not be registered.
        """
        if profile_imports:
            symbol_table = symbol_table.copy()  # Avoid messing with original table
            symbol_table["profile_imports"] = profile_imports

        all_items = list(symbol_table.values()) + self._load_profile_imports(
            symbol_table
        )
        namespace_types = (FontBakeryCondition, FontBakeryExpectedValue)
        namespace_items = []

        for item in all_items:
            if isinstance(item, namespace_types):
                # register these after all modules have been registered. That way,
                # "local" items can optionally force override items registered
                # previously by modules.
                namespace_items.append(item)
            elif isinstance(item, FontBakeryCheck):
                if filter_func and not filter_func("check", item.id, item):
                    continue
                self.register_check(item)
            elif isinstance(item, types.ModuleType):
                if filter_func and not filter_func("module", item.__name__, item):
                    continue
                profile = get_module_profile(item)
                if profile:
                    self.merge_profile(profile, filter_func=filter_func)

        for item in namespace_items:
            if isinstance(item, FontBakeryCondition):
                if filter_func and not filter_func("condition", item.name, item):
                    continue
                self.register_condition(item)
            elif isinstance(item, FontBakeryExpectedValue):
                if filter_func and not filter_func("expected_value", item.name, item):
                    continue
                self.register_expected_value(item)

    def merge_profile(self, profile, filter_func=None):
        """Copy all namespace items from profile to self.

        Namespace items are: 'iterargs', 'derived_iterables', 'aliases',
                             'conditions', 'expected_values'

        Don't change any contents of profile ever!
        That means sections are cloned not used directly

        filter_func: see description in auto_register
        """
        # 'iterargs', 'derived_iterables', 'aliases', 'conditions', 'expected_values'
        for ns_type in self._valid_namespace_types:
            # this will raise a NamespaceError if an item of profile.{ns_type}
            # is already registered.
            ns_dict = getattr(profile, ns_type)
            if filter_func:
                ns_type_singular = self._valid_namespace_types[ns_type]
                ns_dict = {
                    name: item
                    for name, item in ns_dict.items()
                    if filter_func(ns_type_singular, name, item)
                }
            self._add_dict_to_namespace(ns_type, ns_dict)

        check_filter_func = (
            None
            if not filter_func
            else lambda check: filter_func("check", check.id, check)
        )
        for section in profile.sections:
            my_section = self._sections.get(str(section), None)
            if not len(section.checks):
                continue
            if my_section is None:
                # create a new section: don't change other module/profile contents
                my_section = section.clone(check_filter_func)
                self.add_section(my_section)
            else:
                # order, description are not updated
                my_section.merge_section(section, check_filter_func)

    @property
    def check_skip_filter(self):
        """ return the current check_skip_filter function or None """
        return self._check_skip_filter

    @check_skip_filter.setter
    def check_skip_filter(self, check_skip_filter):
        """ Set a check_skip_filter function.

        A check_skip_filter has a signature like:

        ```
        def check_skip_filter(check_id: str, **iterargsDict : dict) \
                                    -> Tuple[accepted: bool, message: str]
          …
        ```

        If present, this function is called just before a check
        with `check_id` is executed. `iterargsDict` is a dictionary
        containing key:value pairs of the "iterable arguments" that will be
        applied for that check execution.

        If the returned `accepted` is falsy, the check will be SKIPed using
        `message` for reporting.

        There's no full resolution of all check arguments at this point,
        That can be achieved with the `conditions=[]` argument of the
        check constructor/decorator. This is for more general filtering.
        """
        self._check_skip_filter = check_skip_filter

    @staticmethod
    def serialize_identity(identity):
        """Return a json string that can also  be used as a key.

        The JSON is explicitly unambiguous in the item order
        entries (dictionaries are not ordered usually)
        Otherwise it is valid JSON
        """
        section, check, iterargs = identity
        values = map(
            # separators are without space, which is the default in JavaScript;
            # just in case we need to make these keys in JS.
            partial(json.dumps, separators=(",", ":"))
            # iterargs are sorted, because it doesn't matter for the result
            # but it gives more predictable keys.
            # Though, arguably, the order generated by the profile is also good
            # and conveys insights on how the order came to be (clustering of
            # iterargs). `sorted(iterargs)` however is more robust over time,
            # the keys will be the same, even if the sorting order changes.
            ,
            [str(section), check.id, sorted(iterargs)],
        )
        return '{{"section":{},"check":{},"iterargs":{}}}'.format(*values)

    def deserialize_identity(self, key):
        item = json.loads(key)
        section = self._get_section(item["section"])
        check, _ = self.get_check(item["check"])
        # tuple of tuples instead list of lists
        iterargs = tuple(tuple(item) for item in item["iterargs"])
        return section, check, iterargs

    def serialize_order(self, order):
        return map(self.serialize_identity, order)

    def deserialize_order(self, serialized_order):
        return tuple(self.deserialize_identity(item) for item in serialized_order)

    def setup_argparse(self, argument_parser):
        """
        Set up custom arguments needed for this profile.
        Return a list of keys that will be set to the `values` dictonary
        """
        pass

    def get_deep_check_dependencies(self, check):
        seen = set()
        dependencies = list(check.args)
        if hasattr(check, "conditions"):
            dependencies += [
                name for negated, name in map(is_negated, check.conditions)
            ]
        while dependencies:
            name = dependencies.pop()
            if name in seen:
                continue
            seen.add(name)
            condition = self.conditions.get(name, None)
            if condition is not None:
                dependencies += condition.args
        return seen

    @property
    def checks(self):
        for section in self.sections:
            for check in section.checks:
                yield check

    def get_checks_by_dependencies(self, *dependencies, subset=False):
        deps = set(dependencies)  # faster membership checking
        result = []
        for check in self.checks:
            check_deps = self.get_deep_check_dependencies(check)
            if (subset and deps.issubset(check_deps)) or (
                not subset and len(deps.intersection(check_deps))
            ):
                result.append(check)
        return result

    def merge_default_config(self, user_config):
        """
        Forms a configuration object based on defaults provided by the profile,
        overridden by values in the user's configuration file.
        """
        copy = Configuration(**self.configuration_defaults)
        copy.update(user_config)
        return copy

def _check_log_override(overrides, status, message):
    # These constants are merely meant to be used
    # so that the check_override declarations are more readable:
    from fontbakery.message import (KEEP_ORIGINAL_STATUS,
                                    KEEP_ORIGINAL_MESSAGE)
    result_status = status
    result_message = message
    override = False
    for override_target, new_status, new_message_string in overrides:
        # Override is only possible by matching message.code
        if (
            not hasattr(result_message, "code")
            or result_message.code != override_target
        ):
            continue
        override = True
        if new_status is not KEEP_ORIGINAL_STATUS:
            result_status = new_status
        if new_message_string is not KEEP_ORIGINAL_MESSAGE:
            # If it looks like an instance of Message we reuse the code,
            # as it is the same condition this makes totally sense.
            result_message = Message(result_message.code, new_message_string)
        # Break the for loop, we had a successful override.
        break
    return override, result_status, result_message

def check_log_override(check, new_id, overrides, reason=None):
    """Returns a new FontBakeryCheck that is decorating (wrapping) check,
    but with overrides applied to returned statuses when they match.

    The new FontBakeryCheck is always a generator check, even if the old
    check is just a normal function that returns (instead of yields)
    its result. Also, the new check yields an INFO Status for each
    overridden original status.

    Arguments:

    check: the FontBakeryCheck to be decorated
    new_id: string, must be unique of course and should not(!) be check.id
            as we essentially create a new, different check.
    overrides: a tuple of override triple-tuples
               ((override_target, new_status, new_message_string), ...)
               override_target: string, specific Message.code
               new_status: Status or None, keep old status
               new_message_string: string or None, keep old message
    """

    @wraps(check)  # defines __wrapped__
    def override_wrapper(*args, **kwds):
        # A check can be either a normal function that returns one Status or a
        # generator that yields one or more. The latter will return a generator
        # object that we can detect with types.GeneratorType.
        result = check(*args, **kwds)  # Might raise.
        if not isinstance(result, types.GeneratorType):
            # Now it iterates
            # make these always iterators, it's nicer to handle
            # also we can mix-in new status messages
            result = (result,)
        # Iterate over sub-results one-by-one, list(result) would abort on
        # encountering the first exception.
        for (status, message) in result:  # Might raise.
            overriden, result_status, result_message = _check_log_override(
                overrides, status, message
            )
            if overriden:
                # nothing changed (despite of a match in override rules)
                if result_status == status and result_message == message:
                    yield DEBUG, (
                        "A check status override rule matched but"
                        " did not change the resulting status."
                    )
                # Both changed
                elif result_status != status and result_message != message:
                    yield DEBUG, (
                        f"Overridden check status and message,"
                        f" original: {status} {message}"
                    )
                # Only status changed
                elif result_status != status and result_message == message:
                    yield DEBUG, f"Overridden check status, original: {status}"
                # Only message changed
                elif result_status == status and result_message != message:
                    yield DEBUG, f"Overridden check message, original: {message}"

            yield result_status, result_message

    # Make the callable here and return that.
    new_check = FontBakeryCheck(
        override_wrapper,
        new_id
        # Untouched, the reason for this checks existence stays the same!
        ,
        rationale=check.rationale
        # the "Derived ..." part should be prominent, so we always see it
        ,
        description=f"{check.description} (derived from {check.id})"
        # ONLY if there's a reason for derivation, otherwise will take
        # the documentation from the __doc__ string of check.
        ,
        documentation=(f"{reason}\n" f"\n" f"{check.documentation}")
        if reason and check.documentation
        else (reason or check.documentation or None),
    )

    # reconstruct a proper doc string from the changes we made.
    # This is really backwards! But, it's so fundamental how python doc
    # strings work, that I think it's solid enough.
    new_check.__doc__ = f"{new_check.description}\n" f"\n" f"{new_check.documentation}"
    return new_check


text:
define(["require", "exports", "text!../views/sidebar.html"], function (require, exports) {
    var template = require('text!../views/sidebar.html');
    exports.sidebar = function () {
        return {
            restrict: 'E',
            transclude: true,
            template: template
        };
    };
});
//# sourceMappingURL=sidebar.js.map

redpajama_set_name: RedPajamaGithub


text:
package com.ataraxer.apps.chess.scala

import com.ataraxer.apps.chess.scala.Color._

class Game {
  private var moves: List[Board] = List(Board())
  private var turn: Color = White

  private def lastMove = moves.head
  private def addMove(move: Board) { moves ::= move }
  private def switchTurn() { turn = turn.opposite }

  def move(from: Coord, to: Coord) {
    addMove(lastMove.movePiece(from, to))
    switchTurn()
  }
}


text:
package org.wso2.carbon.identity.oauth.endpoint.exception;

/**
 * Exception class for InvalidRequestParentException.
 */
public class InvalidRequestParentException extends Exception {

    protected String errorCode = null;
    protected String errorMessage = null;
    private String subErrorCode = null;

    public InvalidRequestParentException(String message) {

        super(message);
    }

    public InvalidRequestParentException(Throwable cause) {

        super(cause);
    }

    public InvalidRequestParentException(String message, Throwable cause) {

        super(message, cause);
    }

    public InvalidRequestParentException(String message, String errorCode) {

        super(message);
        this.errorCode = errorCode;
    }

    public InvalidRequestParentException(String message, String errorCode, Throwable cause) {

        super(message, cause);
        this.errorCode = errorCode;
    }

    public InvalidRequestParentException(String message, String errorCode, String subErrorCode) {

        super(message);
        this.errorCode = errorCode;
        this.subErrorCode = subErrorCode;
    }

    public String getErrorCode() {

        return errorCode;
    }

    public String getErrorMessage() {

        return errorMessage;
    }

    public String getSubErrorCode() {

        return subErrorCode;
    }
}


text:
package ignite.scala

import com.twitter.algebird.Semigroup
import com.twitter.logging.Logger
import org.apache.ignite._
import org.apache.ignite.lang._
import scala.collection.JavaConverters._

/**
 * Basic accumulating version of IgniteReducer.
 *
 * Values are summed in a local var as they are received
 * from closure computations on the cluster as defined by
 * the supplied Semigroup.
 */
class AccumulatingReducer[A](sg: Semigroup[A], n: Int)
    extends IgniteReducer[A, A] {

  require(n > 0)

  private[this] val log = Logger.get(getClass)
  private[this] object Lock

  private[this] var collected = Option.empty[A]
  private[this] var count = 0

  override def collect(a: A): Boolean = Lock.synchronized {
    log.debug(s"Collecting value $a")
    if (count == n)
      sys.error(s"Buffer overflow. Already reached size $count")
    else {
      collected match {
        case None => collected = Some(a)
        case Some(b) => collected = Some(sg.plus(b, a))
      }
      count = count + 1
      if (count == n) false
      else true
    }
  }

  override def reduce: A = collected match {
    case None => sys.error("No values collected by reducer")
    case Some(c) => c
  }
}

/**
 * Conversions from Scala function to Ignite's closure classes.
 */
private object IgniteClosureConversions {

  def scala2closure[A, B](f: Function1[A, B]): IgniteClosure[A, B] =
    new IgniteClosure[A, B]() { override def apply(a: A) = f(a) }

  def scala2runnable[A](f: () => Unit): IgniteRunnable =
    new IgniteRunnable() { override def run = f() }

  def scala2callable[A](f: () => A): IgniteCallable[A] =
    new IgniteCallable[A]() { override def call = f() }

  def scala2reducer[A](sg: Semigroup[A], n: Int): IgniteReducer[A, A] =
    new AccumulatingReducer[A](sg, n)
}

final case class CacheAffinity[K, V](cacheName: String, key: K)

/**
 * Provides Scala-friendly api to IgniteCompute class.
 */
final case class ComputeRunner(ic: IgniteCompute) {
  import IgniteClosureConversions._

  def apply[A, B](x: A)(f: A => B): B =
    ic.apply[B, A](scala2closure(f), x)

  def apply[A, B](xs: Iterable[A])(f: A => B): Iterable[B] =
    xs match {
      case Nil => Nil // ignite does not handle empty iterables correctly
      case iter => ic.apply[A, B](scala2closure(f), iter.asJavaCollection).asScala
    }

  def flatMapApply[A, B](xs: Iterable[A])(f: A => TraversableOnce[B]): Iterable[B] =
    apply[A, TraversableOnce[B]](xs)(f).flatten

  def affinityApply[A, K, V](cas: Iterable[CacheAffinity[K, V]])(f: CacheAffinity[K, V] => A): Iterable[A] = cas
    .map { ca =>
      List(ic.affinityCall(ca.cacheName, ca.key,
        scala2callable(() => f(ca))))
    }
    .reduceOption(Semigroup.listSemigroup[A].plus(_, _))
    .toIterable.flatten

  def flatMapAffinityApply[A, K, V](cas: Iterable[CacheAffinity[K, V]])(f: CacheAffinity[K, V] => TraversableOnce[A]): Iterable[A] =
    affinityApply[TraversableOnce[A], K, V](cas)(f).flatten

  def affinityReduceOption[A, K, V](cas: Iterable[CacheAffinity[K, V]])(f: CacheAffinity[K, V] => A)(sg: Semigroup[A]): Option[A] =
    affinityApply[A, K, V](cas)(f).reduceOption(sg.plus(_, _))

  // TODO: size is O(n) for Iterables
  def reduceOption[A, B](xs: Iterable[A])(m: A => B)(sg: Semigroup[B]): Option[B] =
    xs match {
      case Nil => None
      case iter => Some(
        ic.apply[B, B, A](scala2closure(m), iter.toList.asJavaCollection,
          scala2reducer(sg, iter.size)))
    }

  //
  // the following are currently unused
  //

  def broadcastFn[A, B](x: A)(f: A => B): Iterable[B] =
    ic.broadcast[B, A](scala2closure(f), x).asScala

  def broadcastRun(x: () => Unit): Unit =
    ic.broadcast(scala2runnable(x))

  def broadcastCall[A](x: () => A): Iterable[A] =
    ic.broadcast[A](scala2callable(x)).asScala

  def call[A](x: () => A): A =
    ic.call[A](scala2callable(x))

  def call[A](xs: Iterable[() => A]): Iterable[A] =
    xs match {
      case Nil => Nil
      case iter => ic.call[A](iter.map(scala2callable(_)).asJavaCollection).asScala
    }

  def reduceOptionCall[A](xs: Iterable[() => A])(sg: Semigroup[A]): Option[A] =
    xs match {
      case Nil => None
      case iter => Some(
        ic.call[A, A](iter.map(scala2callable(_)).toList.asJavaCollection,
          scala2reducer(sg, iter.size)))
    }
}


text:
import {Inject, Injectable, EventEmitter} from '@angular/core'
import {Http, Response, RequestOptionsArgs, Request, Headers} from '@angular/http'
import * as Rx from 'rxjs/Rx'
import { TeamDTO, BadgeDTO, RegionDTO } from '../../dtos'
import { Badge, Team } from '../../types'
import { urlConfig } from '../../util/urlConfig'
import * as LocalStorage from 'application-settings'

@Injectable()
export class MainMenuService {

    constructor ( private http : Http ) {

    }

}


text:
.. _centos7_install:

CentOS 7 Install
================

The steps to install each configuration is below.  Note, there are one line versions of the install in each section below.  The average install time is between 4-9 minutes depending on the resources on your vm/server.

Note: You can add a "-b <version number>" to the end of the git command to install and specific version of dSIPRouter.

Install (Don't Proxy audio (RTP) traffic)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: bash

  yum install -y git
  cd /opt
  git clone https://github.com/dOpensource/dsiprouter.git
  cd dsiprouter
  ./dsiprouter.sh install -kam -dsip

One Line Version:

.. code-block:: bash

   yum install -y git && cd /opt && git clone https://github.com/dOpensource/dsiprouter.git && cd dsiprouter && ./dsiprouter.sh install -kam -dsip

Once the install is complete, dSIPRouter will automatically start MySQL, Kamailio and the UI.

Install (Proxy audio (RTP) traffic)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you need to proxy RTP traffic then use -all install option. The command to install dSIPRouter and the RTPEngine would be:

.. code-block:: bash

  yum install -y git
  cd /opt
  git clone https://github.com/dOpensource/dsiprouter.git
  cd dsiprouter
  ./dsiprouter.sh install -all

One Line Version:

.. code-block:: bash

  yum install -y git && cd /opt && git clone https://github.com/dOpensource/dsiprouter.git && cd dsiprouter && ./dsiprouter.sh install -all

Install (Proxy audio (RTP) traffic with PBX and dSIPRouter behind NAT)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you have a requirement where the PBX's and dSIPRouter are behind NAT then use the steps below, which are the same as above, but you will add a -servernat parameter.

.. code-block:: bash

  yum install -y git
  cd /opt
  git clone https://github.com/dOpensource/dsiprouter.git
  cd dsiprouter
  ./dsiprouter.sh install -all -servernat

One Line Version:

.. code-block:: bash

  yum install -y git && cd /opt && git clone https://github.com/dOpensource/dsiprouter.git && cd dsiprouter && ./dsiprouter.sh install -all


text:
module Mokio
  module Concerns
    module Models
      module SeoTag

        extend ActiveSupport::Concern

        included do
          belongs_to :seo_tagable, polymorphic: true
          validates_uniqueness_of :tag_key, :scope => [:seo_tagable_type, :seo_tagable_id], case_sensitive: true

          def is_title?
            tag_key == "title"
          end

        end

        module ClassMethods

          def find_key_in_seo_list(default_key)
            selected_tag = Mokio::SeoTag.seo_tags_list.select { |t| t[:key].downcase == default_key.downcase }
            if(selected_tag.any?)
              selected_tag.first[:key]
            else
              nil
            end
          end

          def seo_tag_attributes
            [:tag_key, :tag_value, :id, :_destroy]
          end

          def seo_tags_list_keys
            self.seo_tags_list.pluck(:key)
          end

          def seo_tags_list
            [
              { type: "name", key: "title"},
              { type: "name", key: "keywords"},
              { type: "name", key: "description"},
              { type: "name", key: "subject"},
              { type: "name", key: "copyright"},
              { type: "name", key: "language"},
              { type: "name", key: "robots"},
              { type: "name", key: "revised"},
              { type: "name", key: "abstract"},
              { type: "name", key: "topic"},
              { type: "name", key: "summary"},
              { type: "name", key: "Classification"},
              { type: "name", key: "author"},
              { type: "name", key: "designer"},
              { type: "name", key: "copyright"},
              { type: "name", key: "reply-to"},
              { type: "name", key: "owner"},
              { type: "name", key: "url"},
              { type: "name", key: "identifier-URL"},
              { type: "name", key: "directory"},
              { type: "name", key: "category"},
              { type: "name", key: "coverage"},
              { type: "name", key: "distribution"},
              { type: "name", key: "rating"},
              { type: "name", key: "revisit-after"},
              { type: "name", key: "microid"},
              { type: "name", key: "apple-mobile-web-app-capable"},
              { type: "name", key: "apple-touch-fullscreen"},
              { type: "name", key: "apple-mobile-web-app-status-bar-style"},
              { type: "name", key: "format-detection"},
              { type: "name", key: "mssmarttagspreventparsing"},
              { type: "name", key: "msapplication-starturl"},
              { type: "name", key: "msapplication-window"},
              { type: "name", key: "msapplication-navbutton-color"},
              { type: "name", key: "application-name"},
              { type: "name", key: "msapplication-tooltip"},
              { type: "name", key: "msapplication-task"},
              { type: "name", key: "tweetmeme-title"},
              { type: "name", key: "apple-mobile-web-app-capable"},
              { type: "name", key: "apple-mobile-web-app-status-bar-style"},
              { type: "name", key: "format-detection"},
              { type: "name", key: "viewport"},

              { type: "property", key: "og:title"},
              { type: "property", key: "og:url"},
              { type: "property", key: "og:image"},
              { type: "property", key: "og:site_name"},
              { type: "property", key: "og:description"},
              { type: "property", key: "fb:page_id"},
              { type: "property", key: "og:email"},
              { type: "property", key: "og:phone_number"},
              { type: "property", key: "og:fax_number"},
              { type: "property", key: "og:latitude"},
              { type: "property", key: "og:longitude"},
              { type: "property", key: "og:street-address"},
              { type: "property", key: "og:locality"},
              { type: "property", key: "og:region"},
              { type: "property", key: "og:postal-code"},
              { type: "property", key: "og:country-name"},
              { type: "property", key: "og:type" },
              { type: "property", key: "og:points" },
              { type: "property", key: "og:video" },
              { type: "property", key: "og:video:height" },
              { type: "property", key: "og:video:width" },
              { type: "property", key: "og:video:type" },
              { type: "property", key: "og:video" },
              { type: "property", key: "og:video:type" },
              { type: "property", key: "og:video" },
              { type: "property", key: "og:video:type"},
              { type: "property", key: "og:audio" },
              { type: "property", key: "og:audio:title" },
              { type: "property", key: "og:audio:artist" },
              { type: "property", key: "og:audio:album" },
              { type: "property", key: "og:audio:type" },

              { type: "http-equiv", key: "Page-Enter"},
              { type: "http-equiv", key: "Page-Exit"},
              { type: "http-equiv", key: "X-UA-Compatible"},
              { type: "http-equiv", key: "Expires"},
              { type: "http-equiv", key: "Pragma"},
              { type: "http-equiv", key: "Cache-Control"},
            ]

          end
        end
      end
    end
  end
end


text:
<?php

// autoload_classmap.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'Gregwar\\Captcha\\CaptchaBuilder' => $vendorDir . '/gregwar/captcha/CaptchaBuilder.php',
    'Gregwar\\Captcha\\CaptchaBuilderInterface' => $vendorDir . '/gregwar/captcha/CaptchaBuilderInterface.php',
    'Gregwar\\Captcha\\ImageFileHandler' => $vendorDir . '/gregwar/captcha/ImageFileHandler.php',
    'Gregwar\\Captcha\\PhraseBuilder' => $vendorDir . '/gregwar/captcha/PhraseBuilder.php',
    'Gregwar\\Captcha\\PhraseBuilderInterface' => $vendorDir . '/gregwar/captcha/PhraseBuilderInterface.php',
    'JasonGrimes\\Paginator' => $vendorDir . '/jasongrimes/paginator/src/JasonGrimes/Paginator.php',
);


text:
namespace DecoupageStore.Web.Infrastructure.ValidationAttributes
{
    using System.Web;
    using System.ComponentModel.DataAnnotations;

    public class ElementsFileSizeAttribute : ValidationAttribute
    {
        private readonly int maxSize;
        private string fileNameForError;

        public ElementsFileSizeAttribute(int maxSize)
        {
            this.maxSize = maxSize;
        }

        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return true;
            }

            HttpPostedFileBase[] postedFiles = (HttpPostedFileBase[])value;

            foreach (var file in postedFiles)
            {
                if (file == null)
                {
                    continue;
                }

                if (file.ContentLength > maxSize)
                {
                    this.fileNameForError = file.FileName;
                    return false;
                }
            }

            return true;
        }

        public override string FormatErrorMessage(string name)
        {
            return string.Format("The file with name {0} size should not exceed {1}MB",
                this.fileNameForError, maxSize / (1024 * 1024));
        }
    }
}


text:
package org.sagebionetworks.file.worker;

import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.sagebionetworks.repo.manager.file.FileHandleArchivalManager;
import org.sagebionetworks.repo.model.UserInfo;
import org.sagebionetworks.repo.model.file.FileHandleRestoreRequest;
import org.sagebionetworks.repo.model.file.FileHandleRestoreResponse;
import org.sagebionetworks.repo.model.file.FileHandleRestoreResult;
import org.sagebionetworks.repo.model.file.FileHandleRestoreStatus;
import org.sagebionetworks.util.ValidateArgument;
import org.sagebionetworks.worker.AsyncJobProgressCallback;
import org.sagebionetworks.worker.AsyncJobRunner;
import org.sagebionetworks.workers.util.aws.message.RecoverableMessageException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FileHandleRestoreRequestWorker implements AsyncJobRunner<FileHandleRestoreRequest, FileHandleRestoreResponse> {

	static final int MAX_BATCH_SIZE = 1000;
	private static final Logger LOG = LogManager.getLogger(FileHandleRestoreRequestWorker.class);

	private FileHandleArchivalManager archivalManager;

	@Autowired
	public FileHandleRestoreRequestWorker(FileHandleArchivalManager archivalManager) {
		this.archivalManager = archivalManager;
	}

	@Override
	public Class<FileHandleRestoreRequest> getRequestType() {
		return FileHandleRestoreRequest.class;
	}

	@Override
	public Class<FileHandleRestoreResponse> getResponseType() {
		return FileHandleRestoreResponse.class;
	}

	@Override
	public FileHandleRestoreResponse run(String jobId, UserInfo user, FileHandleRestoreRequest request, AsyncJobProgressCallback jobProgressCallback) throws RecoverableMessageException, Exception {
		ValidateArgument.requiredNotEmpty(request.getFileHandleIds(), "The fileHandleIds list");
		ValidateArgument.requirement(request.getFileHandleIds().size() <= MAX_BATCH_SIZE, "The number of file handles exceed the maximum allowed (Was: " + request.getFileHandleIds().size() + ", Max:" +MAX_BATCH_SIZE + ").");

		List<FileHandleRestoreResult> results = new ArrayList<>(request.getFileHandleIds().size());

		for (String id : request.getFileHandleIds()) {
			FileHandleRestoreResult result;

			try {
				result = archivalManager.restoreFileHandle(user, id);
			} catch (Exception e) {
				LOG.error("Restore request for file handle " + id + " FAILED: " + e.getMessage(), e);
				result = new FileHandleRestoreResult().setFileHandleId(id).setStatus(FileHandleRestoreStatus.FAILED).setStatusMessage(e.getMessage());
			}

			results.add(result);
		}

		FileHandleRestoreResponse response = new FileHandleRestoreResponse()
				.setRestoreResults(results);

		return response;
	}

}


text:
"""
Unit tests for datastore module.
"""

import mock
from oslo_utils import units
from oslo_vmware import exceptions

from jacket.storage import test
from jacket.storage.volume.drivers.vmware import datastore as ds_sel
from jacket.storage.volume.drivers.vmware import exceptions as vmdk_exceptions

class DatastoreTest(test.TestCase):
    """Unit tests for Datastore."""

    def setUp(self):
        super(DatastoreTest, self).setUp()
        self._session = mock.Mock()
        self._vops = mock.Mock()
        self._ds_sel = ds_sel.DatastoreSelector(self._vops, self._session)

    @mock.patch('oslo_vmware.pbm.get_profile_id_by_name')
    def test_get_profile_id(self, get_profile_id_by_name):
        profile_id = mock.sentinel.profile_id
        get_profile_id_by_name.return_value = profile_id
        profile_name = mock.sentinel.profile_name

        self.assertEqual(profile_id, self._ds_sel.get_profile_id(profile_name))
        get_profile_id_by_name.assert_called_once_with(self._session,
                                                       profile_name)

    @mock.patch('oslo_vmware.pbm.get_profile_id_by_name')
    def test_get_profile_id_with_invalid_profile(self, get_profile_id_by_name):
        get_profile_id_by_name.return_value = None
        profile_name = mock.sentinel.profile_name

        self.assertRaises(vmdk_exceptions.ProfileNotFoundException,
                          self._ds_sel.get_profile_id,
                          profile_name)
        get_profile_id_by_name.assert_called_once_with(self._session,
                                                       profile_name)

    def _create_datastore(self, moref):
        return mock.Mock(value=moref)

    def _create_summary(
            self, ds, free_space=units.Mi, _type=ds_sel.DatastoreType.VMFS,
            capacity=2 * units.Mi):
        return mock.Mock(datastore=ds, freeSpace=free_space, type=_type,
                         capacity=capacity)

    def _create_host(self, value):
        host = mock.Mock(spec=['_type', 'value'])
        host._type = 'HostSystem'
        host.value = value
        return host

    def test_filter_datastores_with_unsupported_type(self):
        ds_1 = self._create_datastore('ds-1')
        ds_2 = self._create_datastore('ds-2')
        datastores = [ds_1, ds_2]

        self._vops.get_summary.side_effect = [
            self._create_summary(ds_1),
            self._create_summary(ds_2, _type='foo')]

        res = self._ds_sel._filter_datastores(
            datastores, units.Ki, None, None, None)

        self.assertEqual(1, len(res))
        self.assertEqual(ds_1, res[0].datastore)

    @mock.patch('storage.volume.drivers.vmware.datastore.DatastoreSelector.'
                '_filter_by_profile')
    def test_filter_datastores(self, filter_by_profile):
        # Test with empty datastore list.
        datastores = []
        size_bytes = 2 * units.Mi
        profile_id = mock.sentinel.profile_id
        hard_anti_affinity_datastores = None
        hard_affinity_ds_types = None

        self.assertEqual([], self._ds_sel._filter_datastores(
            datastores, size_bytes, profile_id, hard_anti_affinity_datastores,
            hard_affinity_ds_types))

        # Test with single datastore with hard anti-affinity.
        ds_1 = self._create_datastore('ds-1')
        datastores = [ds_1]
        hard_anti_affinity_datastores = [ds_1.value]

        self.assertEqual([], self._ds_sel._filter_datastores(
            datastores, size_bytes, profile_id, hard_anti_affinity_datastores,
            hard_affinity_ds_types))

        # Extend previous case with a profile non-compliant datastore.
        ds_2 = self._create_datastore('ds-2')
        datastores.append(ds_2)
        filter_by_profile.return_value = []

        self.assertEqual([], self._ds_sel._filter_datastores(
            datastores, size_bytes, profile_id, hard_anti_affinity_datastores,
            hard_affinity_ds_types))
        filter_by_profile.assert_called_once_with([ds_2], profile_id)

        # Extend previous case with a less free space datastore.
        ds_3 = self._create_datastore('ds-3')
        datastores.append(ds_3)
        filter_by_profile.return_value = [ds_3]

        free_space_list = [units.Mi]
        type_list = [ds_sel.DatastoreType.NFS]
        self._vops.get_summary.side_effect = (
            lambda ds: self._create_summary(ds,
                                            free_space_list.pop(0),
                                            type_list.pop(0)))

        self.assertEqual([], self._ds_sel._filter_datastores(
            datastores, size_bytes, profile_id, hard_anti_affinity_datastores,
            hard_affinity_ds_types))

        # Extend previous case with a datastore not satisfying hard affinity
        # datastore type requirement.
        ds_4 = self._create_datastore('ds-4')
        datastores.append(ds_4)
        filter_by_profile.return_value = [ds_3, ds_4]

        free_space_list = [units.Mi, 4 * units.Mi]
        type_list = [ds_sel.DatastoreType.NFS, ds_sel.DatastoreType.VSAN]
        hard_affinity_ds_types = [ds_sel.DatastoreType.NFS]

        self.assertEqual([], self._ds_sel._filter_datastores(
            datastores, size_bytes, profile_id, hard_anti_affinity_datastores,
            hard_affinity_ds_types))

        # Modify the previous case to remove hard affinity datastore type
        # requirement.
        free_space_list = [units.Mi, 4 * units.Mi]
        type_list = [ds_sel.DatastoreType.NFS, ds_sel.DatastoreType.VSAN]
        hard_affinity_ds_types = None

        res = self._ds_sel._filter_datastores(
            datastores, size_bytes, profile_id, hard_anti_affinity_datastores,
            hard_affinity_ds_types)
        self.assertTrue(len(res) == 1)
        self.assertEqual(ds_4, res[0].datastore)

        # Extend the previous case by adding a datastore satisfying
        # hard affinity datastore type requirement.
        ds_5 = self._create_datastore('ds-5')
        datastores.append(ds_5)
        filter_by_profile.return_value = [ds_3, ds_4, ds_5]

        free_space_list = [units.Mi, 4 * units.Mi, 5 * units.Mi]
        type_list = [ds_sel.DatastoreType.NFS, ds_sel.DatastoreType.VSAN,
                     ds_sel.DatastoreType.VMFS]
        hard_affinity_ds_types = [ds_sel.DatastoreType.VMFS]

        res = self._ds_sel._filter_datastores(
            datastores, size_bytes, profile_id, hard_anti_affinity_datastores,
            hard_affinity_ds_types)
        self.assertTrue(len(res) == 1)
        self.assertEqual(ds_5, res[0].datastore)

        # Modify the previous case to have two datastores satisfying
        # hard affinity datastore type requirement.
        free_space_list = [units.Mi, 4 * units.Mi, 5 * units.Mi]
        type_list = [ds_sel.DatastoreType.NFS, ds_sel.DatastoreType.VSAN,
                     ds_sel.DatastoreType.VSAN]
        hard_affinity_ds_types = [ds_sel.DatastoreType.VSAN]

        res = self._ds_sel._filter_datastores(
            datastores, size_bytes, profile_id, hard_anti_affinity_datastores,
            hard_affinity_ds_types)
        self.assertTrue(len(res) == 2)
        self.assertEqual(ds_4, res[0].datastore)
        self.assertEqual(ds_5, res[1].datastore)

        # Clear side effects.
        self._vops.get_summary.side_effect = None

    def test_select_best_summary(self):
        # No tie-- all datastores with different host mount count.
        summary_1 = self._create_summary(mock.sentinel.ds_1,
                                         free_space=units.Mi,
                                         capacity=2 * units.Mi)
        summary_2 = self._create_summary(mock.sentinel.ds_2,
                                         free_space=units.Mi,
                                         capacity=3 * units.Mi)
        summary_3 = self._create_summary(mock.sentinel.ds_3,
                                         free_space=units.Mi,
                                         capacity=4 * units.Mi)

        host_1 = self._create_host('host-1')
        host_2 = self._create_host('host-2')
        host_3 = self._create_host('host-3')

        connected_hosts = {mock.sentinel.ds_1: [host_1.value],
                           mock.sentinel.ds_2: [host_1.value, host_2.value],
                           mock.sentinel.ds_3: [host_1.value, host_2.value,
                                                host_3.value]}
        self._vops.get_connected_hosts.side_effect = (
            lambda summary: connected_hosts[summary])

        summaries = [summary_1, summary_2, summary_3]
        (best_summary, best_utilization) = self._ds_sel._select_best_summary(
            summaries)

        self.assertEqual(summary_3, best_summary)
        self.assertEqual(3 / 4.0, best_utilization)

        # Tie-- two datastores with max host mount count.
        summary_4 = self._create_summary(mock.sentinel.ds_4,
                                         free_space=2 * units.Mi,
                                         capacity=4 * units.Mi)
        connected_hosts[mock.sentinel.ds_4] = (
            connected_hosts[mock.sentinel.ds_3])
        summaries.append(summary_4)
        (best_summary, best_utilization) = self._ds_sel._select_best_summary(
            summaries)

        self.assertEqual(summary_4, best_summary)
        self.assertEqual(1 / 2.0, best_utilization)

        # Clear side effects.
        self._vops.get_connected_hosts.side_effect = None

    @mock.patch('storage.volume.drivers.vmware.datastore.DatastoreSelector.'
                'get_profile_id')
    @mock.patch('storage.volume.drivers.vmware.datastore.DatastoreSelector.'
                '_filter_datastores')
    def test_select_datastore(self, filter_datastores, get_profile_id):
        # Test with no hosts.
        size_bytes = units.Ki
        req = {self._ds_sel.SIZE_BYTES: size_bytes}
        self._vops.get_hosts.return_value = mock.Mock(objects=[])

        self.assertEqual((), self._ds_sel.select_datastore(req))
        self._vops.get_hosts.assert_called_once_with()

        # Test with single host with no valid datastores.
        host_1 = self._create_host('host-1')
        self._vops.get_hosts.return_value = mock.Mock(
            objects=[mock.Mock(obj=host_1)])
        self._vops.continue_retrieval.return_value = None
        self._vops.get_dss_rp.side_effect = exceptions.VimException('error')

        self.assertEqual((), self._ds_sel.select_datastore(req))
        self._vops.get_dss_rp.assert_called_once_with(host_1)

        # Test with three hosts and vCenter connection problem while fetching
        # datastores for the second host.
        self._vops.get_dss_rp.reset_mock()
        host_2 = self._create_host('host-2')
        host_3 = self._create_host('host-3')
        self._vops.get_hosts.return_value = mock.Mock(
            objects=[mock.Mock(obj=host_1),
                     mock.Mock(obj=host_2),
                     mock.Mock(obj=host_3)])
        self._vops.get_dss_rp.side_effect = [
            exceptions.VimException('no valid datastores'),
            exceptions.VimConnectionException('connection error')]

        self.assertRaises(exceptions.VimConnectionException,
                          self._ds_sel.select_datastore,
                          req)
        get_dss_rp_exp_calls = [mock.call(host_1), mock.call(host_2)]
        self.assertEqual(get_dss_rp_exp_calls,
                         self._vops.get_dss_rp.call_args_list)

        # Modify previous case to return datastores for second and third host,
        # where none of them meet the requirements which include a storage
        # profile and affinity requirements.
        aff_ds_types = [ds_sel.DatastoreType.VMFS]
        req[ds_sel.DatastoreSelector.HARD_AFFINITY_DS_TYPE] = aff_ds_types

        ds_1a = mock.sentinel.ds_1a
        anti_affinity_ds = [ds_1a]
        req[ds_sel.DatastoreSelector.HARD_ANTI_AFFINITY_DS] = anti_affinity_ds

        profile_name = mock.sentinel.profile_name
        req[ds_sel.DatastoreSelector.PROFILE_NAME] = profile_name

        profile_id = mock.sentinel.profile_id
        get_profile_id.return_value = profile_id

        ds_2a = mock.sentinel.ds_2a
        ds_2b = mock.sentinel.ds_2b
        ds_3a = mock.sentinel.ds_3a

        self._vops.get_dss_rp.reset_mock()
        rp_2 = mock.sentinel.rp_2
        rp_3 = mock.sentinel.rp_3
        self._vops.get_dss_rp.side_effect = [
            exceptions.VimException('no valid datastores'),
            ([ds_2a, ds_2b], rp_2),
            ([ds_3a], rp_3)]

        filter_datastores.return_value = []

        self.assertEqual((), self._ds_sel.select_datastore(req))
        get_profile_id.assert_called_once_with(profile_name)
        get_dss_rp_exp_calls.append(mock.call(host_3))
        self.assertEqual(get_dss_rp_exp_calls,
                         self._vops.get_dss_rp.call_args_list)
        filter_datastores_exp_calls = [
            mock.call([ds_2a, ds_2b], size_bytes, profile_id, anti_affinity_ds,
                      aff_ds_types),
            mock.call([ds_3a], size_bytes, profile_id, anti_affinity_ds,
                      aff_ds_types)]
        self.assertEqual(filter_datastores_exp_calls,
                         filter_datastores.call_args_list)

        # Modify previous case to have a non-empty summary list after filtering
        # with preferred utilization threshold unset.
        self._vops.get_dss_rp.side_effect = [
            exceptions.VimException('no valid datastores'),
            ([ds_2a, ds_2b], rp_2),
            ([ds_3a], rp_3)]

        summary_2b = self._create_summary(ds_2b, free_space=0.5 * units.Mi,
                                          capacity=units.Mi)
        filter_datastores.side_effect = [[summary_2b]]
        self._vops.get_connected_hosts.return_value = [host_1]

        self.assertEqual((host_2, rp_2, summary_2b),
                         self._ds_sel.select_datastore(req))

        # Modify previous case to have a preferred utilization threshold
        # satsified by one datastore.
        self._vops.get_dss_rp.side_effect = [
            exceptions.VimException('no valid datastores'),
            ([ds_2a, ds_2b], rp_2),
            ([ds_3a], rp_3)]

        req[ds_sel.DatastoreSelector.PREF_UTIL_THRESH] = 0.4
        summary_3a = self._create_summary(ds_3a, free_space=0.7 * units.Mi,
                                          capacity=units.Mi)
        filter_datastores.side_effect = [[summary_2b], [summary_3a]]

        self.assertEqual((host_3, rp_3, summary_3a),
                         self._ds_sel.select_datastore(req))

        # Modify previous case to have a preferred utilization threshold
        # which cannot be satisfied.
        self._vops.get_dss_rp.side_effect = [
            exceptions.VimException('no valid datastores'),
            ([ds_2a, ds_2b], rp_2),
            ([ds_3a], rp_3)]
        filter_datastores.side_effect = [[summary_2b], [summary_3a]]

        req[ds_sel.DatastoreSelector.PREF_UTIL_THRESH] = 0.2
        summary_2b.freeSpace = 0.75 * units.Mi

        self.assertEqual((host_2, rp_2, summary_2b),
                         self._ds_sel.select_datastore(req))

        # Clear side effects.
        self._vops.get_dss_rp.side_effect = None

    @mock.patch('storage.volume.drivers.vmware.datastore.DatastoreSelector.'
                '_filter_datastores')
    def test_select_datastore_with_single_host(self, filter_datastores):
        host = self._create_host('host-1')
        req = {self._ds_sel.SIZE_BYTES: units.Gi}

        ds = mock.sentinel.ds
        rp = mock.sentinel.rp
        self._vops.get_dss_rp.return_value = ([ds], rp)

        summary = self._create_summary(ds, free_space=2 * units.Gi,
                                       capacity=3 * units.Gi)
        filter_datastores.return_value = [summary]
        self._vops.get_connected_hosts.return_value = [host.value]

        self.assertEqual((host, rp, summary),
                         self._ds_sel.select_datastore(req, [host]))

        # reset mocks
        self._vops.get_dss_rp.reset_mock()
        self._vops.get_dss_rp.return_value = None
        self._vops.get_connected_hosts.reset_mock()
        self._vops.get_connected_hosts.return_value = None

    def test_select_datastore_with_empty_host_list(self):
        size_bytes = units.Ki
        req = {self._ds_sel.SIZE_BYTES: size_bytes}
        self._vops.get_hosts.return_value = mock.Mock(objects=[])

        self.assertEqual((), self._ds_sel.select_datastore(req, hosts=[]))
        self._vops.get_hosts.assert_called_once_with()

    @mock.patch('oslo_vmware.pbm.get_profile_id_by_name')
    @mock.patch('storage.volume.drivers.vmware.datastore.DatastoreSelector.'
                '_filter_by_profile')
    def test_is_datastore_compliant(self, filter_by_profile,
                                    get_profile_id_by_name):
        # Test with empty profile.
        profile_name = None
        datastore = mock.sentinel.datastore
        self.assertTrue(self._ds_sel.is_datastore_compliant(datastore,
                                                            profile_name))

        # Test with invalid profile.
        profile_name = mock.sentinel.profile_name
        get_profile_id_by_name.return_value = None
        self.assertRaises(vmdk_exceptions.ProfileNotFoundException,
                          self._ds_sel.is_datastore_compliant,
                          datastore,
                          profile_name)
        get_profile_id_by_name.assert_called_once_with(self._session,
                                                       profile_name)

        # Test with valid profile and non-compliant datastore.
        get_profile_id_by_name.reset_mock()
        profile_id = mock.sentinel.profile_id
        get_profile_id_by_name.return_value = profile_id
        filter_by_profile.return_value = []
        self.assertFalse(self._ds_sel.is_datastore_compliant(datastore,
                                                             profile_name))
        get_profile_id_by_name.assert_called_once_with(self._session,
                                                       profile_name)
        filter_by_profile.assert_called_once_with([datastore], profile_id)

        # Test with valid profile and compliant datastore.
        get_profile_id_by_name.reset_mock()
        filter_by_profile.reset_mock()
        filter_by_profile.return_value = [datastore]
        self.assertTrue(self._ds_sel.is_datastore_compliant(datastore,
                                                            profile_name))
        get_profile_id_by_name.assert_called_once_with(self._session,
                                                       profile_name)
        filter_by_profile.assert_called_once_with([datastore], profile_id)

    def test_get_all_hosts(self):
        host_1 = self._create_host('host-1')
        host_2 = self._create_host('host-2')
        hosts = mock.Mock(objects=[mock.Mock(obj=host_1),
                                   mock.Mock(obj=host_2)])

        self._vops.get_hosts.return_value = hosts
        self._vops.continue_retrieval.return_value = None
        # host_1 is usable and host_2 is not usable
        self._vops.is_host_usable.side_effect = [True, False]

        ret = self._ds_sel._get_all_hosts()

        self.assertEqual([host_1], ret)
        self._vops.get_hosts.assert_called_once_with()
        self._vops.continue_retrieval.assert_called_once_with(hosts)
        exp_calls = [mock.call(host_1), mock.call(host_2)]
        self.assertEqual(exp_calls, self._vops.is_host_usable.call_args_list)


text:
#pragma once
#include <aws/codecommit/CodeCommit_EXPORTS.h>
#include <aws/codecommit/CodeCommitRequest.h>
#include <aws/core/utils/memory/stl/AWSString.h>
#include <utility>

namespace Aws
{
namespace CodeCommit
{
namespace Model
{

  /**
   */
  class AWS_CODECOMMIT_API GetApprovalRuleTemplateRequest : public CodeCommitRequest
  {
  public:
    GetApprovalRuleTemplateRequest();

    // Service request name is the Operation name which will send this request out,
    // each operation should has unique request name, so that we can get operation's name from this request.
    // Note: this is not true for response, multiple operations may have the same response name,
    // so we can not get operation's name from response.
    inline virtual const char* GetServiceRequestName() const override { return "GetApprovalRuleTemplate"; }

    Aws::String SerializePayload() const override;

    Aws::Http::HeaderValueCollection GetRequestSpecificHeaders() const override;

    /**
     *
The name of the approval rule template for which you want to get
     * information.

     */
    inline const Aws::String& GetApprovalRuleTemplateName() const{ return m_approvalRuleTemplateName; }

    /**
     *
The name of the approval rule template for which you want to get
     * information.

     */
    inline bool ApprovalRuleTemplateNameHasBeenSet() const { return m_approvalRuleTemplateNameHasBeenSet; }

    /**
     *
The name of the approval rule template for which you want to get
     * information.

     */
    inline void SetApprovalRuleTemplateName(const Aws::String& value) { m_approvalRuleTemplateNameHasBeenSet = true; m_approvalRuleTemplateName = value; }

    /**
     *
The name of the approval rule template for which you want to get
     * information.

     */
    inline void SetApprovalRuleTemplateName(Aws::String&& value) { m_approvalRuleTemplateNameHasBeenSet = true; m_approvalRuleTemplateName = std::move(value); }

    /**
     *
The name of the approval rule template for which you want to get
     * information.

     */
    inline void SetApprovalRuleTemplateName(const char* value) { m_approvalRuleTemplateNameHasBeenSet = true; m_approvalRuleTemplateName.assign(value); }

    /**
     *
The name of the approval rule template for which you want to get
     * information.

     */
    inline GetApprovalRuleTemplateRequest& WithApprovalRuleTemplateName(const Aws::String& value) { SetApprovalRuleTemplateName(value); return *this;}

    /**
     *
The name of the approval rule template for which you want to get
     * information.

     */
    inline GetApprovalRuleTemplateRequest& WithApprovalRuleTemplateName(Aws::String&& value) { SetApprovalRuleTemplateName(std::move(value)); return *this;}

    /**
     *
The name of the approval rule template for which you want to get
     * information.

     */
    inline GetApprovalRuleTemplateRequest& WithApprovalRuleTemplateName(const char* value) { SetApprovalRuleTemplateName(value); return *this;}

  private:

    Aws::String m_approvalRuleTemplateName;
    bool m_approvalRuleTemplateNameHasBeenSet;
  };

} // namespace Model
} // namespace CodeCommit
} // namespace Aws


text:
#include "McrouterClient.h"

#include "mcrouter/lib/fbi/asox_queue.h"
#include "mcrouter/McrouterInstance.h"
#include "mcrouter/proxy.h"
#include "mcrouter/ProxyRequestContext.h"

namespace facebook { namespace memcache { namespace mcrouter {

namespace {

/**
 * @return true  If precheck finds an interesting request and has the reply
 *   set up otherwise this request needs to go through normal flow.
 */
bool precheckRequest(ProxyRequestContext& preq) {
  switch (preq.origReq()->op) {
    // Return error (pretend to not even understand the protocol)
    case mc_op_shutdown:
      preq.sendReply(McReply(mc_res_bad_command));
      break;

    // Return 'Not supported' message
    case mc_op_append:
    case mc_op_prepend:
    case mc_op_flushre:
      preq.sendReply(McReply(mc_res_local_error, "Command not supported"));
      break;

    case mc_op_flushall:
      if (!preq.proxy().opts.enable_flush_cmd) {
        preq.sendReply(McReply(mc_res_local_error, "Command disabled"));
        break;
      }
      /* fallthrough */

    // Everything else is supported
    default:
      auto err = mc_client_req_check(preq.origReq().get());
      if (err != mc_req_err_valid) {
        preq.sendReply(McReply(mc_res_local_error, mc_req_err_to_string(err)));
        break;
      }
      return false;
  }
  return true;
}

}

size_t McrouterClient::send(const mcrouter_msg_t* requests, size_t nreqs) {
  if (nreqs == 0) {
    return 0;
  }
  assert(!isZombie_);

  asox_queue_entry_t scratch[100];
  asox_queue_entry_t* entries;

  if (nreqs <= sizeof(scratch)/sizeof(scratch[0])) {
    entries = scratch;
  } else {
    entries = (asox_queue_entry_t*)malloc(sizeof(entries[0]) * nreqs);
    if (entries == nullptr) {
      // errno is ENOMEM
      return 0;
    }
  }

  __sync_fetch_and_add(&stats_.nreq, nreqs);
  for (size_t i = 0; i < nreqs; i++) {
    auto preq = ProxyRequestContext::create(
      *proxy_,
      McMsgRef::cloneRef(requests[i].req),
      [] (ProxyRequestContext& prq) {
        prq.requester_->onReply(prq);
      },
      requests[i].context);
    preq->requester_ = incref();
    if (requests[i].saved_request.hasValue()) {
      preq->savedRequest_.emplace(
        std::move(*requests[i].saved_request));
    }

    __sync_fetch_and_add(&stats_.op_count[requests[i].req->op], 1);
    __sync_fetch_and_add(&stats_.op_value_bytes[requests[i].req->op],
                         requests[i].req->value.len);
    __sync_fetch_and_add(&stats_.op_key_bytes[requests[i].req->op],
                         requests[i].req->key.len);

    entries[i].data = preq.release();
    entries[i].nbytes = sizeof(ProxyRequestContext*);
    entries[i].priority = 0;
    entries[i].type = request_type_request;
  }

  if (router_->opts().standalone) {
    /*
     * Skip the extra asox queue hop and directly call the queue callback,
     * since we're standalone and thus staying in the same thread
     */
    if (maxOutstanding_ == 0) {
      for (int i = 0; i < nreqs; i++) {
        requestReady(proxy_->request_queue, &entries[i], proxy_);
      }
    } else {
      size_t i = 0;
      size_t n = 0;

      while (i < nreqs) {
        while (counting_sem_value(&outstandingReqsSem_) == 0) {
          mcrouterLoopOnce(proxy_->eventBase);
        }
        n += counting_sem_lazy_wait(&outstandingReqsSem_, nreqs - n);

        for (int j = i; j < n; j++) {
          requestReady(proxy_->request_queue, &entries[j], proxy_);
        }

        i = n;
      }
    }
  } else if (maxOutstanding_ == 0) {
    asox_queue_multi_enqueue(proxy_->request_queue, entries, nreqs);
  } else {
    size_t i = 0;
    size_t n = 0;

    while (i < nreqs) {
      n += counting_sem_lazy_wait(&outstandingReqsSem_, nreqs - n);
      asox_queue_multi_enqueue(proxy_->request_queue, &entries[i],
                               n - i);
      i = n;
    }
  }

  if (entries != scratch) {
    free(entries);
  }

  return nreqs;
}

folly::EventBase* McrouterClient::getBase() const {
  if (router_->opts().standalone) {
    return proxy_->eventBase;
  } else {
    return nullptr;
  }
}

McrouterClient::McrouterClient(
  McrouterInstance* router,
  mcrouter_client_callbacks_t callbacks,
  void* arg,
  size_t maxOutstanding) :
    router_(router),
    callbacks_(callbacks),
    arg_(arg),
    maxOutstanding_(maxOutstanding) {

  static std::atomic<uint64_t> nextClientId(0ULL);
  clientId_ = nextClientId++;

  if (maxOutstanding_ != 0) {
    counting_sem_init(&outstandingReqsSem_, maxOutstanding_);
  }

  memset(&stats_, 0, sizeof(stats_));
  {
    std::lock_guard<std::mutex> guard(router_->clientListLock_);
    router_->clientList_.push_front(*this);
  }

  {
    std::lock_guard<std::mutex> guard(router_->nextProxyMutex_);
    assert(router_->nextProxy_ < router_->opts().num_proxies);
    proxy_ = router_->getProxy(router_->nextProxy_);
    router_->nextProxy_ =
      (router_->nextProxy_ + 1) % router_->opts().num_proxies;
  }
}

void McrouterClient::onReply(ProxyRequestContext& preq) {
  mcrouter_msg_t router_reply;

  // Don't increment refcounts, because these are transient stack
  // references, and are guaranteed to be shorted lived than router_entry's
  // reference.  This is a premature optimization.
  router_reply.req = const_cast<mc_msg_t*>(preq.origReq().get());
  router_reply.reply = std::move(preq.reply_.value());
  router_reply.context = preq.context_;

  if (router_reply.reply.result() == mc_res_timeout ||
      router_reply.reply.result() == mc_res_connect_timeout) {
    __sync_fetch_and_add(&stats_.ntmo, 1);
  }

  __sync_fetch_and_add(&stats_.op_value_bytes[preq.origReq()->op],
                       router_reply.reply.value().length());

  if (LIKELY(callbacks_.on_reply && !disconnected_)) {
      callbacks_.on_reply(&router_reply, arg_);
  } else if (callbacks_.on_cancel && disconnected_) {
    // This should be called for all canceled requests, when cancellation is
    // implemented properly.
    callbacks_.on_cancel(preq.context_, arg_);
  }

  numPending_--;
  if (numPending_ == 0 && disconnected_) {
    cleanup();
  }
}

void McrouterClient::disconnect() {
  if (isZombie_) {
    return;
  }
  asox_queue_entry_t entry;
  entry.type = request_type_disconnect;
  // the libevent priority for disconnect must be greater than or equal to
  // normal request to avoid race condition. (In libevent,
  // higher priority value means lower priority)
  entry.priority = 0;
  entry.data = this;
  entry.nbytes = sizeof(*this);
  asox_queue_enqueue(proxy_->request_queue, &entry);
}

void McrouterClient::cleanup() {
  {
    std::lock_guard<std::mutex> guard(router_->clientListLock_);
    router_->clientList_.erase(router_->clientList_.iterator_to(*this));
  }
  if (callbacks_.on_disconnect) {
    callbacks_.on_disconnect(arg_);
  }
  decref();
}

McrouterClient* McrouterClient::incref() {
  refcount_++;
  return this;
}

void McrouterClient::decref() {
  assert(refcount_ > 0);
  refcount_--;
  if (refcount_ == 0) {
    router_->onClientDestroyed();
    delete this;
  }
}

std::unordered_map<std::string, int64_t>
McrouterClient::getStatsHelper(bool clear) {

  std::function<uint32_t(uint32_t*)> fetch_func;

  if (clear) {
    fetch_func = [](uint32_t* ptr) {
      return xchg32_barrier(ptr, 0);
    };
  } else {
    fetch_func = [](uint32_t* ptr) {
      return *ptr;
    };
  }

  std::unordered_map<std::string, int64_t> ret;
  ret["nreq"] = fetch_func(&stats_.nreq);
  for (int op = 0; op < mc_nops; op++) {
    std::string op_name = mc_op_to_string((mc_op_t)op);
    ret[op_name + "_count"] = fetch_func(&stats_.op_count[op]);
    ret[op_name + "_key_bytes"] = fetch_func(&stats_.op_key_bytes[op]);
    ret[op_name + "_value_bytes"] = fetch_func(
      &stats_.op_value_bytes[op]);
  }
  ret["ntmo"] = fetch_func(&stats_.ntmo);

  return ret;
}

void McrouterClient::requestReady(asox_queue_t q,
                                  asox_queue_entry_t* entry,
                                  void* arg) {
  if (entry->type == request_type_request) {
    proxy_t* proxy = (proxy_t*)arg;
    auto preq =
      std::unique_ptr<ProxyRequestContext>(
        reinterpret_cast<ProxyRequestContext*>(entry->data));
    auto client = preq->requester_;

    client->numPending_++;

    if (precheckRequest(*preq)) {
      return;
    }

    if (proxy->being_destroyed) {
      /* We can't process this, since 1) we destroyed the config already,
         and 2) the clients are winding down, so we wouldn't get any
         meaningful response back anyway. */
      LOG(ERROR) << "Outstanding request on a proxy that's being destroyed";
      preq->sendReply(McReply(mc_res_unknown));
      return;
    }
    proxy->dispatchRequest(std::move(preq));
  } else if (entry->type == request_type_old_config) {
    auto oldConfig = (old_config_req_t*) entry->data;
    delete oldConfig;
  } else if (entry->type == request_type_disconnect) {
    auto client = (McrouterClient*) entry->data;
    client->disconnected_ = true;
    if (client->numPending_ == 0) client->cleanup();
  } else if (entry->type == request_type_router_shutdown) {
    /*
     * No-op. We just wanted to wake this event base up so that
     * it can exit event loop and check router->shutdown
     */
  } else {
    LOG(ERROR) << "CRITICAL: Unrecognized request type " << entry->type << "!";
    FBI_ASSERT(0);
  }
}

}}}  // facebook::memcache::mcrouter


text:
cd deb-install
./deb-install.py
cd ..

# download and use oh-my-zsh
wget --no-check-certificate http://install.ohmyz.sh -O - | sh
# make zsh default shell
chsh -s $(which zsh)

# use my dotfiles
cd dotfiles
./link.py
cd ..

# Change dconf settings of some programs
./set-dconf.sh


text:
/*! `profile` grammar compiled for Highlight.js 11.6.0 */
(()=>{var e=(()=>{"use strict";return e=>({name:"Python profiler",
contains:[e.C_NUMBER_MODE,{begin:"[a-zA-Z_][\\da-zA-Z_]+\\.[\\da-zA-Z_]{1,3}",
end:":",excludeEnd:!0},{begin:"(ncalls|tottime|cumtime)",end:"$",
keywords:"ncalls tottime|10 cumtime|10 filename",relevance:10},{
begin:"function calls",end:"$",contains:[e.C_NUMBER_MODE],relevance:10
},e.APOS_STRING_MODE,e.QUOTE_STRING_MODE,{className:"string",begin:"\\(",
end:"\\)$",excludeBegin:!0,excludeEnd:!0,relevance:0}]})})()
;hljs.registerLanguage("profile",e)})();