text:
import ana
import simuvex
import claripy
import mulpyplexer

import logging
l = logging.getLogger('angr.path_group')

class CallReturn(simuvex.SimProcedure):
    NO_RET = True

    def run(self):
        l.info("A PathGroup.call-created path returned!")
        return

class PathGroup(ana.Storable):
    '''
    Path groups are the future.

    Path groups allow you to wrangle multiple paths in a slick way.  Paths are
    organized into "stashes", which you can step forward, filter, merge, and
    move around as you wish. This allows you to, for example, step two
    different stashes of paths at different rates, then merge them together.

    Note that path groups are immutable by default (all operations will return
    new PathGroup objects). See the immutable argument to __init__.

    Stashes can be accessed as attributes (i.e., pg.active). A mulpyplexed
    stash can be retrieved by prepending the name with "mp_" (i.e.,
    pg.mp_active).
    '''

    ALL = '_ALL'
    DROP = '_DROP'

    def __init__(self, project, active_paths=None, stashes=None, hierarchy=None, veritesting=None,
                 veritesting_options=None, immutable=None, resilience=None, save_unconstrained=None,
                 save_unsat=None, strong_path_mapping=None):
        '''
        Initializes a new PathGroup.

        @param project: an angr.Project instance
        @param active_paths: active paths to seed the "active" stash with.
        @param stashes: a dictionary to use as the stash store
        @param hierarchy: a PathHierarchy object to use to track path reachability
        @param immutable: if True (the default), all operations will return a new
                          PathGroup. Otherwise, all operations will modify the
                          PathGroup (and return it, for consistency and chaining).
        '''
        self._project = project
        self._hierarchy = PathHierarchy(strong_path_mapping=strong_path_mapping) if hierarchy is None else hierarchy
        self._immutable = True if immutable is None else immutable
        self._veritesting = False if veritesting is None else veritesting
        self._resilience = False if resilience is None else resilience
        self._veritesting_options = { } if veritesting_options is None else veritesting_options

        # public options
        self.save_unconstrained = False if save_unconstrained is None else save_unconstrained
        self.save_unsat = False if save_unsat is None else save_unsat

        self.stashes = {
            'active': [ ] if active_paths is None else active_paths,
            'stashed': [ ],
            'pruned': [ ],
            'unsat': [ ],
            'errored': [ ],
            'deadended': [ ],
            'unconstrained': [ ]
        } if stashes is None else stashes

    @classmethod
    def call(cls, project, target, args=(), start=None, prototype=None, **kwargs): #pylint:disable=unused-argument
        """"Calls" a function in the binary, returning a PathGroup with the call set up.

        :param project: :class:`angr.Project` instance
        :param target: address of function to call
        :param args: arguments to call the function with
        :param start: Optional: path (or paths) to start the call with
        :param prototype: Optional: A SimTypeFunction to typecheck arguments against
        :param **kwargs: other kwargs to pass to construct :class:`PathGroup`
        :return: a :class:`PathGroup` calling the function
        """

        fake_return_addr = project._extern_obj.get_pseudo_addr('FAKE_RETURN_ADDR')
        if not project.is_hooked(fake_return_addr):
            project.hook(fake_return_addr, CallReturn)
        cc = simuvex.DefaultCC[project.arch.name](project.arch)

        active_paths = []
        if start is None:
            active_paths.append(project.factory.path(addr=target))
        elif hasattr(start, '__iter__'):
            active_paths.extend(start)
        else:
            active_paths.append(start)

        ret_addr = claripy.BVV(fake_return_addr, project.arch.bits)

        def fix_arg(st, arg):
            if isinstance(arg, str):
                # store the string, nul-terminated, at the current heap location
                # then return a pointer to that location
                ptr = st.libc.heap_location
                st.memory.store(ptr, st.BVV(arg))
                st.memory.store(ptr + len(arg), st.BVV(0, 8))
                st.libc.heap_location += len(arg) + 1
                return ptr
            elif isinstance(arg, (tuple, list)):
                # fix the entries of the list, then store them in a
                # NULL-terminated array at the current heap location and return
                # a pointer to there
                # N.B.: uses host endianness to store entries!!! mostly useful for string arrays
                fixed_entries = [fix_arg(st, entry) for entry in arg]
                cur_ptr = start_ptr = st.libc.heap_location
                for entry in fixed_entries:
                    st.memory.store(cur_ptr, entry, endness=st.arch.memory_endness)
                    cur_ptr += entry.length

                entry_length = fixed_entries[0].length if len(fixed_entries) > 0 else st.arch.bits
                st.memory.store(cur_ptr, st.se.BVV(0, entry_length))

                st.libc.heap_location = cur_ptr + entry_length
                return start_ptr
            elif isinstance(arg, (int, long)):
                return st.se.BVV(arg, st.arch.bits)
            elif isinstance(arg, StringSpec):
                ptr = st.libc.heap_location
                arg.dump(st, ptr)
                st.libc.heap_location += len(arg)
                return ptr
            else:
                return arg

        for p in active_paths:
            p.state.ip = target
            fixed_args = [fix_arg(p.state, arg) for arg in args]
            cc.setup_callsite(p.state, ret_addr, fixed_args)

        return cls(project, active_paths=active_paths, **kwargs)

    #
    # Util functions
    #

    def copy(self, stashes=None):
        stashes = stashes if stashes is not None else self._copy_stashes(immutable=True)
        return PathGroup(self._project, stashes=stashes, hierarchy=self._hierarchy, immutable=self._immutable, veritesting=self._veritesting, veritesting_options=self._veritesting_options, resilience=self._resilience, save_unconstrained=self.save_unconstrained, save_unsat=self.save_unsat)

    def _copy_stashes(self, immutable=None):
        '''
        Returns a copy of the stashes (if immutable) or the stashes themselves
        (if not immutable). Used to abstract away immutability.
        '''
        if self._immutable if immutable is None else immutable:
            return { k:list(v) for k,v in self.stashes.items() }
        else:
            return self.stashes

    def _copy_paths(self, paths):
        '''
        Returns a copy of a list of paths (if immutable) or the paths themselves
        (if not immutable). Used to abstract away immutability.
        '''
        if self._immutable:
            return [ p.copy() for p in paths ]
        else:
            return paths

    def _successor(self, new_stashes):
        '''
        Creates a new PathGroup with the provided stashes (if immutable), or sets
        the stashes (if not immutable). Used to abstract away immutability.

        @returns a PathGroup
        '''
        if '_DROP' in new_stashes:
            del new_stashes['_DROP']

        if not self._immutable:
            self.stashes = new_stashes
            return self
        else:
            return self.copy(stashes=new_stashes)

    @staticmethod
    def _condition_to_lambda(condition, default=False):
        '''
        Translates an integer, set, or list into a lambda that checks a path address
        against the given addresses.

        @param condition: an integer, set, or list to convert to a lambda
        @param default: the default return value of the lambda (in case condition
                        is None). Default: false.
        @returns a lambda that takes a path and returns True or False
        '''
        if condition is None:
            condition = lambda p: default

        if isinstance(condition, (int, long)):
            condition = { condition }

        if isinstance(condition, (tuple, set, list)):
            addrs = set(condition)
            condition = lambda p: p.addr in addrs

        return condition

    @staticmethod
    def _filter_paths(filter_func, paths):
        '''
        Filters a sequence of paths according to a filter_func.

        @param filter_func: the filter function. Should take a path as input and
                            return a boolean
        @param paths: a sequence of paths

        @returns a tuple, with the first element the matching paths and the second
                 element the non-matching paths.
        '''
        l.debug("Filtering %d paths", len(paths))
        match = [ ]
        nomatch = [ ]

        for p in paths:
            if filter_func is None or filter_func(p):
                l.debug("... path %s matched!", p)
                match.append(p)
            else:
                l.debug("... path %s didn't match!", p)
                nomatch.append(p)

        l.debug("... returning %d matches and %d non-matches", len(match), len(nomatch))
        return match, nomatch

    def _one_step(self, stash=None, selector_func=None, successor_func=None, check_func=None, **kwargs):
        '''
        Takes a single step in a given stash.

        @param stash: the name of the stash (default: 'active').
        @param successor_func: if provided, this function is called with the path as
                               its only argument. It should return the path's
                               successors. If this is None, path.successors is used,
                               instead.
        @param selector_func: if provided, should be a lambda that takes a Path and
                              returns a boolean. If True, the path will be stepped.
                              Otherwise, it will be kept as-is.
        @param check_func: if provided, this function will be called to decide whether
                            the current path is errored or not. Path.errored will not be
                            called anymore.

        @param returns the successor PathGroup
        '''
        stash = 'active' if stash is None else stash

        new_stashes = self._copy_stashes()
        new_active = [ ]

        for a in self.stashes[stash]:
            try:
                if selector_func is not None and not selector_func(a):
                    new_active.append(a)
                    continue

                has_stashed = False # Flag that whether we have put a into a stash or not
                successors = [ ]

                veritesting_worked = False
                if self._veritesting:
                    veritesting = self._project.analyses.Veritesting(a, **self._veritesting_options)
                    if veritesting.result['result'] and veritesting.result['final_path_group']:
                        pg = veritesting.result['final_path_group']
                        pg.stash(from_stash='deviated', to_stash='active')
                        pg.stash(from_stash='successful', to_stash='active')
                        successors = pg.active
                        pg.drop(stash='active')
                        for s in pg.stashes:
                            if s not in new_stashes:
                                new_stashes[s] = []
                            new_stashes[s] += pg.stashes[s]
                        veritesting_worked = True

                if not veritesting_worked:
                    # `check_func` will not be called for Veritesting, this is
                    # intended so that we can avoid unnecessarily creating
                    # Path._run

                    if (check_func is not None and check_func(a)) or (check_func is None and a.errored):
                        # This path has error(s)!
                        if hasattr(a, "error") and isinstance(a.error, PathUnreachableError):
                            new_stashes['pruned'].append(a)
                        else:
                            if self._hierarchy:
                                self._hierarchy.unreachable(a)
                            new_stashes['errored'].append(a)
                        has_stashed = True
                    else:
                        if successor_func is not None:
                            successors = successor_func(a)
                        else:
                            successors = a.step(**kwargs)
                            if self.save_unconstrained:
                                if 'unconstrained' not in new_stashes:
                                    new_stashes['unconstrained'] = [ ]
                                new_stashes['unconstrained'] += a.unconstrained_successors
                            if self.save_unsat:
                                if 'unsat' not in new_stashes:
                                    new_stashes['unsat'] = [ ]
                                new_stashes['unsat'] += a.unsat_successors

                if self._hierarchy:
                    self._hierarchy.add_successors(a, successors)
                if not has_stashed:
                    if len(successors) == 0:
                        new_stashes['deadended'].append(a)
                    else:
                        new_active.extend(successors)
            except (AngrError, simuvex.SimError, claripy.ClaripyError):
                if not self._resilience:
                    raise
                else:
                    l.warning("PathGroup resilience squashed an exception", exc_info=True)
                    new_stashes['errored'].append(a)

        new_stashes[stash] = new_active
        return self._successor(new_stashes)

    @staticmethod
    def _move(stashes, filter_func, src, dst):
        '''
        Moves all stashes that match the filter_func from src to dst.

        @returns a new stashes dictionary
        '''
        if dst == PathGroup.ALL:
            raise AngrPathGroupError("Can't handle '_ALL' as a target stash.")
        if src == PathGroup.DROP:
            raise AngrPathGroupError("Can't handle '_DROP' as a source stash.")

        if src == PathGroup.ALL:
            srces = [ a for a in stashes.keys() if a != dst ]
        else:
            srces = [ src ]

        matches = [ ]
        for f in srces:
            to_move, to_keep = PathGroup._filter_paths(filter_func, stashes[f])
            stashes[f] = to_keep
            matches.extend(to_move)

        if dst != PathGroup.DROP:
            if dst not in stashes:
                stashes[dst] = [ ]
            stashes[dst].extend(matches)
        return stashes

    def __repr__(self):
        s = "<PathGroup with "
        s += ', '.join(("%d %s" % (len(v),k)) for k,v in self.stashes.items() if len(v) != 0)
        s += ">"
        return s

    def __getattr__(self, k):
        if k.startswith('mp_'):
            return mulpyplexer.MP(self.stashes[k[3:]])
        else:
            return self.stashes[k]

    def __dir__(self):
        return sorted(set(self.__dict__.keys() + dir(super(PathGroup, self)) + self.stashes.keys() + [ 'mp_'+k for k in self.stashes.keys() ]))

    #
    # Interface
    #

    def apply(self, path_func=None, stash_func=None, stash=None):
        '''
        Applies a given function to a given stash.

        @param path_func: a function to apply to every path. Should take a path and
                          return a path. The returned path will take the place of the
                          old path. If the function *doesn't* return a path, the old
                          path will be used. If the function returns a list of paths,
                          they will replace the original paths.
        @param stash_func: a function to apply to the whole stash. Should take a
                           list of paths and return a list of paths. The resulting
                           list will replace the stash.

        If both path_func and stash_func are provided, path_func is applied first,
        then stash_func is applied on the results.

        @returns the resulting PathGroup
        '''
        stash = 'active' if stash is None else stash

        new_stashes = self._copy_stashes()
        new_paths = new_stashes[stash]
        if path_func is not None:
            new_new_paths = [ ]
            for p in new_paths:
                np = path_func(p)
                if isinstance(np, Path):
                    new_new_paths.append(np)
                elif isinstance(np, (list, tuple, set)):
                    new_new_paths.extend(np)
                else:
                    new_new_paths.append(p)
            new_paths = new_new_paths
        if stash_func is not None:
            new_paths = stash_func(new_paths)

        new_stashes[stash] = new_paths
        return self._successor(new_stashes)

    def split(self, stash_splitter=None, stash_ranker=None, path_ranker=None, limit=None, from_stash=None, to_stash=None):
        '''
        Split a stash of paths. The stash from_stash will be split into two
        stashes depending on the other options passed in. If to_stash is provided,
        the second stash will be written there.

        @param stash_splitter: a function that should take a list of paths and return
                               a tuple of two lists (the two resulting stashes).
        @param stash_ranker: a function that should take a list of paths and return
                             a sorted list of paths. This list will then be split
                             according to "limit".
        @param path_ranker: an alternative to stash_splitter. Paths will be sorted
                            with outputs of this function used as a key. The first
                            "limit" of them will be kept, the rest split off.
        @param limit: for use with path_ranker. The number of paths to keep. Default: 8
        @param from_stash: the stash to split (default: 'active')
        @param to_stash: the stash to write to (default: 'stashed')

        stash_splitter overrides stash_ranker, which in turn overrides path_ranker.
        If no functions are provided, the paths are simply split according to the limit.

        The sort done with path_ranker is ascending.

        @returns the resulting PathGroup
        '''

        limit = 8 if limit is None else limit
        from_stash = 'active' if from_stash is None else from_stash
        to_stash = 'stashed' if to_stash is None else to_stash

        new_stashes = self._copy_stashes()
        old_paths = new_stashes[from_stash]

        if stash_splitter is not None:
            keep, split = stash_splitter(old_paths)
        elif stash_ranker is not None:
            ranked_paths = stash_ranker(old_paths)
            keep, split = ranked_paths[:limit], ranked_paths[limit:]
        elif path_ranker is not None:
            ranked_paths = sorted(old_paths, key=path_ranker)
            keep, split = ranked_paths[:limit], ranked_paths[limit:]
        else:
            keep, split = old_paths[:limit], old_paths[limit:]

        new_stashes[from_stash] = keep
        new_stashes[to_stash] = split if to_stash in new_stashes else new_stashes[to_stash] + split
        return self._successor(new_stashes)

    def step(self, n=None, selector_func=None, step_func=None, stash=None,
             successor_func=None, until=None, check_func=None, **kwargs):
        '''
        Step a stash of paths forward.

        @param n: the number of times to step (default: 1 if "until" is not provided)
        @param selector_func: if provided, should be a lambda that takes a Path and
                              returns a boolean. If True, the path will be stepped.
                              Otherwise, it will be kept as-is.
        @param step_func: if provided, should be a lambda that takes a PathGroup and
                          returns a PathGroup. Will be called with the PathGroup at
                          at every step.
        @param stash: the name of the stash to step (default: 'active')
        @param successor_func: if provided, this function will be called with a path
                               to get its successors. Otherwise, path.successors will
                               be used.
        @param until: if provided, should be a lambda that takes a PathGroup and returns
                      True or False. Stepping will terminate when it is True.
        @param check_func: if provided, this function will be called to decide whether
                            the current path is errored or not. Path.errored will not be
                            called anymore.

        Additionally, you can pass in any of the following keyword args
        for project.factory.sim_run:

        @param jumpkind: the jumpkind of the previous exit
        @param addr an address: to execute at instead of the state's ip
        @param stmt_whitelist: a list of stmt indexes to which to confine execution
        @param last_stmt: a statement index at which to stop execution
        @param thumb: whether the block should be lifted in ARM's THUMB mode
        @param backup_state: a state to read bytes from instead of using project memory
        @param opt_level: the VEX optimization level to use
        @param insn_bytes: a string of bytes to use for the block instead of the project
        @param max_size: the maximum size of the block, in bytes
        @param num_inst: the maximum number of instructions
        @param traceflags: traceflags to be passed to VEX. Default: 0
        @returns the resulting PathGroup
        '''
        stash = 'active' if stash is None else stash
        n = n if n is not None else 1 if until is None else 100000
        pg = self

        for i in range(n):
            l.debug("Round %d: stepping %s", i, pg)

            pg = pg._one_step(stash=stash, selector_func=selector_func, successor_func=successor_func, check_func=check_func, **kwargs)
            if step_func is not None:
                pg = step_func(pg)

            if len(pg.stashes[stash]) == 0:
                l.debug("Out of paths in stash %s", stash)
                break

            if until is not None and until(pg):
                l.debug("Until function returned true")
                break

        return pg

    def prune(self, filter_func=None, from_stash=None, to_stash=None):
        '''
        Prune unsatisfiable paths from a stash.

        @param filter_func: only prune paths that match this filter
        @param from_stash: prune paths from this stash (default: 'active')
        @param to_stash: put pruned paths in this stash (default: 'pruned')

        @returns the resulting PathGroup
        '''
        to_stash = 'pruned' if to_stash is None else to_stash
        from_stash = 'active' if from_stash is None else from_stash

        to_prune, new_active = self._filter_paths(filter_func, self.stashes[from_stash])
        new_stashes = self._copy_stashes()

        for p in to_prune:
            if p.errored or not p.state.satisfiable():
                if to_stash not in new_stashes:
                    new_stashes[to_stash] = [ ]
                new_stashes[to_stash].append(p)
                if self._hierarchy:
                    self._hierarchy.unreachable(p)
            else:
                new_active.append(p)

        new_stashes[from_stash] = new_active
        return self._successor(new_stashes)

    def move(self, from_stash, to_stash, filter_func=None):
        '''
        Move paths from one stash to another.

        @param from_stash: take matching paths from this stash.
        @param to_stash: put matching paths into this stash.
        @param filter_func: stash paths that match this filter. Should be a function
                            that takes a path and returns True or False. Default: stash
                            all paths

        @returns the resulting PathGroup
        '''
        new_stashes = self._copy_stashes()
        self._move(new_stashes, filter_func, from_stash, to_stash)
        return self._successor(new_stashes)

    def stash(self, filter_func=None, from_stash=None, to_stash=None):
        '''
        Stash some paths. This is an alias for move(), with defaults for the stashes.

        @param filter_func: stash paths that match this filter. Should be a function
                            that takes a path and returns True or False. Default: stash
                            all paths
        @param from_stash: take matching paths from this stash (default: 'active')
        @param to_stash: put matching paths into this stash: (default: 'stashed')

        @returns the resulting PathGroup
        '''
        to_stash = 'stashed' if to_stash is None else to_stash
        from_stash = 'active' if from_stash is None else from_stash
        return self.move(from_stash, to_stash, filter_func=filter_func)

    def drop(self, filter_func=None, stash=None):
        '''
        Drops paths from a stash. This is an alias for move(), with defaults for the
        stashes.

        @param filter_func: drop paths that match this filter. Should be a function that
                            takes a path and returns True or False. Default:
                            drop all paths
        @param stash: drop matching paths from this stash (default: 'active')

        @returns the resulting PathGroup
        '''
        stash = 'active' if stash is None else stash
        return self.move(stash, self.DROP, filter_func=filter_func)

    def unstash(self, filter_func=None, to_stash=None, from_stash=None):
        '''
        Unstash some paths. This is an alias for move(), with defaults for the stashes.

        @param filter_func: unstash paths that match this filter. Should be a function
                            that takes a path and returns True or False. Default:
                            unstash all paths
        @param from_stash: take matching paths from this stash (default: 'stashed')
        @param to_stash: put matching paths into this stash: (default: 'active')

        @returns the resulting PathGroup
        '''
        to_stash = 'active' if to_stash is None else to_stash
        from_stash = 'stashed' if from_stash is None else from_stash
        return self.move(from_stash, to_stash, filter_func=filter_func)

    def merge(self, merge_func=None, stash=None):
        '''
        Merge the states in a given stash.

        @param stash: the stash (default: 'active')
        @param merge_func: if provided, instead of using path.merge, call this
                           function with the paths as the argument. Should return
                           the merged path.

        @returns the result PathGroup
        '''
        stash = 'active' if stash is None else stash
        to_merge = self.stashes[stash]
        not_to_merge = [ ]

        merge_groups = [ ]
        while len(to_merge) > 0:
            g, to_merge = self._filter_paths(lambda p: p.addr == to_merge[0].addr, to_merge)
            if len(g) <= 1:
                not_to_merge.extend(g)
            else:
                merge_groups.append(g)

        for g in merge_groups:
            try:
                m = g[0].merge(*g[1:]) if merge_func is None else merge_func(*g)
                not_to_merge.append(m)
            except simuvex.SimMergeError:
                l.warning("SimMergeError while merging %d paths", len(g), exc_info=True)
                not_to_merge.extend(g)

        new_stashes = self._copy_stashes()
        new_stashes[stash] = not_to_merge
        return self._successor(new_stashes)

    #
    # Various canned functionality
    #

    def stash_not_addr(self, addr, from_stash=None, to_stash=None):
        '''
        Stash all paths not at address addr from stash from_stash to stash to_stash.
        '''
        return self.stash(lambda p: p.addr != addr, from_stash=from_stash, to_stash=to_stash)

    def stash_addr(self, addr, from_stash=None, to_stash=None):
        '''
        Stash all paths at address addr from stash from_stash to stash to_stash.
        '''
        return self.stash(lambda p: p.addr == addr, from_stash=from_stash, to_stash=to_stash)

    def stash_addr_past(self, addr, from_stash=None, to_stash=None):
        '''
        Stash all paths containg address addr in their backtrace from stash
        from_stash to stash to_stash.
        '''
        return self.stash(lambda p: addr in p.addr_backtrace, from_stash=from_stash, to_stash=to_stash)

    def stash_not_addr_past(self, addr, from_stash=None, to_stash=None):
        '''
        Stash all paths not containg address addr in their backtrace from stash
        from_stash to stash to_stash.
        '''
        return self.stash(lambda p: addr not in p.addr_backtrace, from_stash=from_stash, to_stash=to_stash)

    def stash_all(self, from_stash=None, to_stash=None):
        '''
        Stash all paths from stash from_stash to stash to_stash.
        '''
        return self.stash(lambda p: True, from_stash=from_stash, to_stash=to_stash)

    def unstash_addr(self, addr, from_stash=None, to_stash=None):
        '''
        Untash all paths at address addr.
        '''
        return self.unstash(lambda p: p.addr == addr, from_stash=from_stash, to_stash=to_stash)

    def unstash_addr_past(self, addr, from_stash=None, to_stash=None):
        '''
        Untash all paths containing address addr in their backtrace.
        '''
        return self.unstash(lambda p: addr in p.addr_backtrace, from_stash=from_stash, to_stash=to_stash)

    def unstash_not_addr(self, addr, from_stash=None, to_stash=None):
        '''
        Untash all paths not at address addr.
        '''
        return self.unstash(lambda p: p.addr != addr, from_stash=from_stash, to_stash=to_stash)

    def unstash_not_addr_past(self, addr, from_stash=None, to_stash=None):
        '''
        Untash all paths not containing address addr in their backtrace.
        '''
        return self.unstash(lambda p: addr not in p.addr_backtrace, from_stash=from_stash, to_stash=to_stash)

    def unstash_all(self, from_stash=None, to_stash=None):
        '''
        Untash all paths.
        '''
        return self.unstash(lambda p: True, from_stash=from_stash, to_stash=to_stash)

    #
    # High-level functionality
    #

    def explore(self, stash=None, n=None, find=None, avoid=None, num_find=None, found_stash=None, avoid_stash=None):
        '''
        A replacement for the Explorer surveyor. Tick stash "stash" forward (up to n
        times or until num_find paths are found), looking for condition "find",
        avoiding condition "avoid". Stashes found paths into "found_stash' and
        avoided paths into "avoid_stash".
        '''
        find = self._condition_to_lambda(find)
        avoid = self._condition_to_lambda(avoid)
        found_stash = 'found' if found_stash is None else found_stash
        avoid_stash = 'avoid' if avoid_stash is None else avoid_stash
        num_find = 1 if num_find is None else num_find
        cur_found = len(self.stashes[found_stash]) if found_stash in self.stashes else 0

        explore_step_func = lambda pg: pg.stash(find, from_stash=stash, to_stash=found_stash) \
                                         .stash(avoid, from_stash=stash, to_stash=avoid_stash) \
                                         .prune(from_stash=found_stash)
        until_func = lambda pg: len(pg.stashes[found_stash]) >= cur_found + num_find
        return self.step(n=n, step_func=explore_step_func, until=until_func, stash=stash)

from .path_hierarchy import PathHierarchy
from .errors import PathUnreachableError, AngrError, AngrPathGroupError
from .path import Path
from .tablespecs import StringSpec


text:
/**@author hoxnox <hoxnox@gmail.com>
 * @date 20150316 16:36:18 */

#include "PCAProxy.hpp"
#include "HttpReqInfo.hpp"

#include <Logger.hpp>
#include <Config.hpp>
#include <utils/Utils.hpp>

#include <gettext.h>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>

namespace pcaproxy {

inline int init_sock(const struct sockaddr_storage& addr)
{
	SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
	if(!IS_VALID_SOCK(sock))
	{
		ELOG << _("Error socket initializing.")
		     << _(" Message: ") << strerror(GET_LAST_SOCK_ERROR());
		return INVALID_SOCKET;
	}
	if (SetNonBlock(sock) < 0)
	{
		ELOG << _("Error switching socket to non-blocking mode.")
		     <<  _(" Message: ") << strerror(GET_LAST_SOCK_ERROR());
		return INVALID_SOCKET;
	}
	if (SetReusable(sock) < 0)
	{
		ELOG << _("Error making socket reusable.")
		     << _(" Message: ") << strerror(GET_LAST_SOCK_ERROR());
		return INVALID_SOCKET;
	}
	if (bind(sock, (sockaddr *)&addr, sizeof(addr)) != 0)
	{
		ELOG << _("Error socket binding.")
		     << _("Message: ") << strerror(GET_LAST_SOCK_ERROR());
		return INVALID_SOCKET;
	}
	return sock;
}

void PCAProxy::heartbeat(int, short int, void *self)
{
	//VLOG << "Heartbeat";
	if (!self)
		return;
	PCAProxy* this_ = reinterpret_cast<PCAProxy*>(self);
	if (this_->stop_)
	{
		event_base_loopbreak(this_->evbase_.get());
		return;
	}
	Config::Ptr cfg = Config::GetInstance();
	struct timeval tv = cfg->Tick();
	event_add(this_->evtimeout_.get(), &tv);
	return;
}

PCAProxy::PCAProxy()
	: stop_(true)
	, evbase_(event_base_new(), event_base_free)
	, evtimeout_(event_new(evbase_.get(), 0, EV_TIMEOUT, heartbeat, this), event_free)
	, evhttp_(evhttp_new(evbase_.get()), evhttp_free)
	, evbuf_(evbuffer_new(), evbuffer_free)
{
}

void
PCAProxy::onRequest(struct evhttp_request * evreq, void * arg)
{
	if(!arg)
	{
		ELOG << _("onRequest bad arg value");
		return;
	}
	if(!evreq)
		return;
	PCAProxy *this_ = reinterpret_cast<PCAProxy*>(arg);
	if (evreq->type != EVHTTP_REQ_GET)
	{
		evhttp_send_error(evreq, HTTP_BADREQUEST, "Can't handle the query.");
		return;
	}
	HttpReqInfo req(std::string(evreq->uri));
	VLOG << _("PCAProxy: received HTTP request.")
	     << _(" Method: ") << req.Method()
	     << _(" UrlHash: ") << req.UrlHash()
	     << _(" URL: ") << req.Url();
	std::ifstream ifile(req.FName(), std::ios::in | std::ios::binary);
	if (!ifile.good())
	{
		VLOG << _("PCAProxy: no file corresponds.")
		     << _(" FileName: ") << req.FName()
		     << _(" URL: ") << req.Url();
		evbuffer_add(this_->evbuf_.get(), &this_->index_page_[0],
		                                  this_->index_page_.size());
		evhttp_send_reply(evreq, HTTP_OK, "", this_->evbuf_.get());
		return;
	}
	std::vector<char> header_raw;
	std::istreambuf_iterator<char> reader(ifile.rdbuf());
	for (;reader != std::istreambuf_iterator<char>(); ++reader)
	{
		header_raw.push_back(*reader);
		if (std::string(header_raw.rbegin(), header_raw.rbegin() + 4) == "\n\r\n\r")
			break;
	}
	++reader;
	std::streampos curpos = ifile.tellg();
	ifile.seekg(0, std::ios_base::end);
	std::streampos endpos = ifile.tellg();
	ifile.seekg(curpos);
	ssize_t body_size_n = endpos - curpos;
	std::stringstream ss;
	ss << body_size_n;
	std::string body_size = ss.str();

	std::vector<std::string> hlines;
	split(std::string(header_raw.begin(), header_raw.end()),
	      std::back_inserter(hlines), "\r\n", true);

	for (auto i = hlines.begin() + 1; i != hlines.end(); ++i)
	{
		ssize_t colon_pos = i->find_first_of(':');
		if (colon_pos == std::string::npos)
		{
			ELOG << _("PCAProxy: wrong file structure."
			          " Can't find colon in the header line.")
			     << _(" Filename: ") << req.FName()
			     << _(" Line: ") << *i;
			evhttp_send_error(evreq, 500, "Datafile has wrong structure.");
			return;
		}
		std::string key = i->substr(0, colon_pos);
		std::string val = i->substr(colon_pos + 1, i->length() - colon_pos -1);
		if (tolower(key) != "connection")
			evhttp_add_header(evreq->output_headers, key.c_str(), val.c_str());
		//VLOG << _("PCAProxy: header line: ") << key << ": " << val;
	}

	std::vector<char> fbuf;
	std::copy(reader, std::istreambuf_iterator<char>(), std::back_inserter(fbuf));
	evbuffer_add(this_->evbuf_.get(), &fbuf[0], fbuf.size());
	evhttp_send_reply(evreq, HTTP_OK, "", this_->evbuf_.get());
	return;
}

void
PCAProxy::Loop(PCAProxy* this_)
{
	if (this_->stop_ != true)
	{
		ELOG << _("Attempt to Loop() already running PCAProxy instance.");
		return;
	}
	this_->stop_ = false;
	Config::Ptr cfg = Config::GetInstance();
	struct timeval tv = cfg->Tick();
	SOCKET sock = init_sock(Config::Str2Inaddr(cfg->BindAddr()));
	if (!IS_VALID_SOCK(sock))
		return;
	if (listen(sock, cfg->Backlog()) == -1)
	{
		ELOG << _("Error listening.")
		     << _(" Message: ") << GET_LAST_SOCK_ERROR();
		return;
	}
	this_->evhttp_.reset(evhttp_new(this_->evbase_.get()));
	if (evhttp_accept_socket(this_->evhttp_.get(), sock) == -1) {
		ILOG << "Error evhttp_accept_socket(): "
		     << strerror(errno) << std::endl;
		return;
	}
	evhttp_set_gencb(this_->evhttp_.get(), PCAProxy::onRequest, this_);
	event_add(this_->evtimeout_.get(), &tv);
	event_base_dispatch(this_->evbase_.get());
	return;
}

} // namespace

redpajama_set_name: RedPajamaGithub


text:
import zlib
from .stypes import *
from . import tags
pbinary.setbyteorder(pbinary.bigendian)

class Header(pstruct.type):
    _fields_ = [
        (dyn.clone(pstr.string,length=3), 'Signature'),
        (UI8, 'Version'),
        (UI32, 'FileLength'),
    ]
    def summary(self):
        return "Signature={:s} Version={:d} FileLength={!s}".format(self['Signature'].str(), self['Version'].int(), self['FileLength'].summary())

class FrameInfo(pstruct.type):
    _fields_ = [
        (RECT, 'FrameSize'),
        (pint.bigendian(UI16), 'FrameRate'),
        (UI16, 'FrameCount')
    ]

class Data(pstruct.type):
    _fields_ = [
        (FrameInfo, 'frameinfo'),
        (tags.TagList, 'tags')
    ]

## Encoded data types
class EncodedDataType(ptype.definition):
    cache = {}

@EncodedDataType.define
class UncompressedData(ptype.encoded_t):
    type = 'FWS'
    _value_ = ptype.block
    _object_ = Data

EncodedDataType.default = UncompressedData

class CompressedData(ptype.encoded_t):
    _value_ = ptype.block
    _object_ = Data

    _compress = lambda s: s
    _decompress = lambda s: s
    def encode(self, object, **attrs):
        block = object.serialize()
        compressed_block = self._compress(block)
        print('%s: compressed %x to %x bytes'%(self.__class__.__name__,len(block),len(compressed_block)))
        return super(CompressedData,self).encode(ptype.block(length=len(compressed_block)).set(compressed_block))
    @ptypes.utils.memoize_method(self=lambda self: self.value, attrs=lambda self: tuple(sorted(self.items())))
    def decode(self, object, **attrs):
        block = object.serialize()
        decompressed_block = self._decompress(block)
        print('%s: decompressed %x to %x bytes'%(self.__class__.__name__,len(block),len(decompressed_block)))
        return super(CompressedData,self).decode(ptype.block(length=len(decompressed_block)).set(decompressed_block))

@EncodedDataType.define
class ZlibData(CompressedData):
    type = 'CWS'
    _compress = zlib.compress
    _decompress = zlib.decompress

try:
    import pylzma
    @EncodedDataType.define
    class LzmaData(CompressedData):
        type = 'ZWS'
        _compress = pylzma.compress
        _decompress = pylzma.decompress

except ImportError:
    # logging.warning("swf.%s : Unable to import pylzma. lzma support not available."% __name__)
    pass

class File(pstruct.type, ptype.boundary):
    def __data(self):
        header = self['Header'].li
        sig = header['Signature'].str()

        # if it's compressed then use the 'zlib' structure
        t = EncodedDataType.withdefault(sig, type=sig)
        length = min(header['FileLength'].int(), self.source.size()) - header.size()
        return dyn.clone(t, _value_=dyn.clone(t._value_, length=length))

    _fields_ = [
        (Header, 'header'),
        (__data, 'data')
    ]

if __name__ == '__main__':
    import sys
    import ptypes,vector.swf as swf
    ptypes.setsource(ptypes.prov.file(sys.argv[1] if len(sys.argv) > 1 else '../../../samples/dog.swf', mode='rb'))

    z = swf.File()
    z = z.l
    print(z['header'])

    data = z['data'].d
    data = data.l

    tags = data['tags']
    for item in tags:
        print('-'*32)
        print(item)

    correct=b'\x44\x11\x08\x00\x00\x00'
    print(ptypes.utils.hexdump(correct))
    print(a.serialize() == correct)


text:
"""
Test cases for L{upnp.services.clients.switch_power_client}
"""

import os

from twisted.trial import unittest
from twisted.internet import reactor
from twisted.internet.defer import Deferred

from coherence import __version__
from coherence.base import Coherence
from coherence.upnp.core import uuid
from coherence.upnp.devices.control_point import DeviceQuery
import coherence.extern.louie as louie

from coherence.test import wrapped

class TestSwitchPowerClient(unittest.TestCase):

    def setUp(self):
        louie.reset()
        self.coherence = Coherence(
            {'unittest': 'yes',
             'logmode': 'error',
             'subsystem_log': {'controlpoint': 'error'},
             'controlpoint': 'yes'})
        self.uuid = str(uuid.UUID())
        p = self.coherence.add_plugin('SimpleLight',
                                      name='test-light-%d' % os.getpid(),
                                      uuid=self.uuid)

    def tearDown(self):

        def cleaner(r):
            self.coherence.clear()
            return r

        dl = self.coherence.shutdown()
        dl.addBoth(cleaner)
        return dl

    def test_get_state(self):
        """ tries to find the activated SimpleLight backend
            and queries its state.
            The state is expected to be "off"
        """
        d = Deferred()

        @wrapped(d)
        def the_result(r):
            self.assertEqual(self.uuid, r.udn)
            call = r.client.switch_power.get_status()
            call.addCallback(got_answer)

        @wrapped(d)
        def got_answer(r):
            self.assertEqual(int(r['ResultStatus']), 0)
            d.callback(None)

        self.coherence.ctrl.add_query(
            DeviceQuery('uuid', self.uuid, the_result,
                        timeout=10, oneshot=True))
        return d


text:
"""
Mock unit tests for the NetApp block storage C-mode library
"""

import ddt
import mock
from oslo_service import loopingcall

from cinder import exception
from cinder import test
from cinder.tests.unit.volume.drivers.netapp.dataontap.client import (
    fake_api as netapp_api)
import cinder.tests.unit.volume.drivers.netapp.dataontap.fakes as fake
import cinder.tests.unit.volume.drivers.netapp.fakes as na_fakes
from cinder.volume.drivers.netapp.dataontap import block_base
from cinder.volume.drivers.netapp.dataontap import block_cmode
from cinder.volume.drivers.netapp.dataontap.client import client_base
from cinder.volume.drivers.netapp.dataontap import ssc_cmode
from cinder.volume.drivers.netapp import utils as na_utils

@ddt.ddt
class NetAppBlockStorageCmodeLibraryTestCase(test.TestCase):
    """Test case for NetApp's C-Mode iSCSI library."""

    def setUp(self):
        super(NetAppBlockStorageCmodeLibraryTestCase, self).setUp()

        # Inject fake netapp_lib module classes.
        netapp_api.mock_netapp_lib([block_cmode])

        kwargs = {'configuration': self.get_config_cmode()}
        self.library = block_cmode.NetAppBlockStorageCmodeLibrary(
            'driver', 'protocol', **kwargs)

        self.library.zapi_client = mock.Mock()
        self.zapi_client = self.library.zapi_client
        self.library.vserver = mock.Mock()
        self.library.ssc_vols = None
        self.fake_lun = block_base.NetAppLun(fake.LUN_HANDLE, fake.LUN_NAME,
                                             fake.SIZE, None)
        self.mock_object(self.library, 'lun_table')
        self.library.lun_table = {fake.LUN_NAME: self.fake_lun}
        self.mock_object(block_base.NetAppBlockStorageLibrary, 'delete_volume')

    def tearDown(self):
        super(NetAppBlockStorageCmodeLibraryTestCase, self).tearDown()

    def get_config_cmode(self):
        config = na_fakes.create_configuration_cmode()
        config.netapp_storage_protocol = 'iscsi'
        config.netapp_login = 'admin'
        config.netapp_password = 'pass'
        config.netapp_server_hostname = '127.0.0.1'
        config.netapp_transport_type = 'https'
        config.netapp_server_port = '443'
        config.netapp_vserver = 'openstack'
        return config

    @mock.patch.object(client_base.Client, 'get_ontapi_version',
                       mock.MagicMock(return_value=(1, 20)))
    @mock.patch.object(na_utils, 'check_flags')
    @mock.patch.object(block_base.NetAppBlockStorageLibrary, 'do_setup')
    def test_do_setup(self, super_do_setup, mock_check_flags):
        context = mock.Mock()

        self.library.do_setup(context)

        super_do_setup.assert_called_once_with(context)
        self.assertEqual(1, mock_check_flags.call_count)

    def test_check_for_setup_error(self):
        super_check_for_setup_error = self.mock_object(
            block_base.NetAppBlockStorageLibrary, 'check_for_setup_error')
        mock_check_ssc_api_permissions = self.mock_object(
            ssc_cmode, 'check_ssc_api_permissions')
        mock_start_periodic_tasks = self.mock_object(
            self.library, '_start_periodic_tasks')

        self.library.check_for_setup_error()

        self.assertEqual(1, super_check_for_setup_error.call_count)
        mock_check_ssc_api_permissions.assert_called_once_with(
            self.library.zapi_client)
        self.assertEqual(1, mock_start_periodic_tasks.call_count)

    def test_find_mapped_lun_igroup(self):
        igroups = [fake.IGROUP1]
        self.zapi_client.get_igroup_by_initiators.return_value = igroups

        lun_maps = [{'initiator-group': fake.IGROUP1_NAME,
                     'lun-id': '1',
                     'vserver': fake.VSERVER_NAME}]
        self.zapi_client.get_lun_map.return_value = lun_maps

        (igroup, lun_id) = self.library._find_mapped_lun_igroup(
            fake.LUN_PATH, fake.FC_FORMATTED_INITIATORS)

        self.assertEqual(fake.IGROUP1_NAME, igroup)
        self.assertEqual('1', lun_id)

    def test_find_mapped_lun_igroup_initiator_mismatch(self):
        self.zapi_client.get_igroup_by_initiators.return_value = []

        lun_maps = [{'initiator-group': fake.IGROUP1_NAME,
                     'lun-id': '1',
                     'vserver': fake.VSERVER_NAME}]
        self.zapi_client.get_lun_map.return_value = lun_maps

        (igroup, lun_id) = self.library._find_mapped_lun_igroup(
            fake.LUN_PATH, fake.FC_FORMATTED_INITIATORS)

        self.assertIsNone(igroup)
        self.assertIsNone(lun_id)

    def test_find_mapped_lun_igroup_name_mismatch(self):
        igroups = [{'initiator-group-os-type': 'linux',
                    'initiator-group-type': 'fcp',
                    'initiator-group-name': 'igroup2'}]
        self.zapi_client.get_igroup_by_initiators.return_value = igroups

        lun_maps = [{'initiator-group': fake.IGROUP1_NAME,
                     'lun-id': '1',
                     'vserver': fake.VSERVER_NAME}]
        self.zapi_client.get_lun_map.return_value = lun_maps

        (igroup, lun_id) = self.library._find_mapped_lun_igroup(
            fake.LUN_PATH, fake.FC_FORMATTED_INITIATORS)

        self.assertIsNone(igroup)
        self.assertIsNone(lun_id)

    def test_find_mapped_lun_igroup_no_igroup_prefix(self):
        igroups = [{'initiator-group-os-type': 'linux',
                    'initiator-group-type': 'fcp',
                    'initiator-group-name': 'igroup2'}]
        self.zapi_client.get_igroup_by_initiators.return_value = igroups

        lun_maps = [{'initiator-group': 'igroup2',
                     'lun-id': '1',
                     'vserver': fake.VSERVER_NAME}]
        self.zapi_client.get_lun_map.return_value = lun_maps

        (igroup, lun_id) = self.library._find_mapped_lun_igroup(
            fake.LUN_PATH, fake.FC_FORMATTED_INITIATORS)

        self.assertIsNone(igroup)
        self.assertIsNone(lun_id)

    def test_clone_lun_zero_block_count(self):
        """Test for when clone lun is not passed a block count."""

        self.library._get_lun_attr = mock.Mock(return_value={'Volume':
                                                             'fakeLUN'})
        self.library.zapi_client = mock.Mock()
        self.library.zapi_client.get_lun_by_args.return_value = [
            mock.Mock(spec=netapp_api.NaElement)]
        lun = fake.FAKE_LUN
        self.library._get_lun_by_args = mock.Mock(return_value=[lun])
        self.library._add_lun_to_table = mock.Mock()
        self.library._update_stale_vols = mock.Mock()

        self.library._clone_lun('fakeLUN', 'newFakeLUN', 'false')

        self.library.zapi_client.clone_lun.assert_called_once_with(
            'fakeLUN', 'fakeLUN', 'newFakeLUN', 'false', block_count=0,
            dest_block=0, src_block=0, qos_policy_group_name=None)

    def test_clone_lun_no_space_reservation(self):
        """Test for when space_reservation is not passed."""

        self.library._get_lun_attr = mock.Mock(return_value={'Volume':
                                                             'fakeLUN'})
        self.library.zapi_client = mock.Mock()
        self.library.lun_space_reservation = 'false'
        self.library.zapi_client.get_lun_by_args.return_value = [
            mock.Mock(spec=netapp_api.NaElement)]
        lun = fake.FAKE_LUN
        self.library._get_lun_by_args = mock.Mock(return_value=[lun])
        self.library._add_lun_to_table = mock.Mock()
        self.library._update_stale_vols = mock.Mock()

        self.library._clone_lun('fakeLUN', 'newFakeLUN')

        self.library.zapi_client.clone_lun.assert_called_once_with(
            'fakeLUN', 'fakeLUN', 'newFakeLUN', 'false', block_count=0,
            dest_block=0, src_block=0, qos_policy_group_name=None)

    def test_get_fc_target_wwpns(self):
        ports = [fake.FC_FORMATTED_TARGET_WWPNS[0],
                 fake.FC_FORMATTED_TARGET_WWPNS[1]]
        self.zapi_client.get_fc_target_wwpns.return_value = ports

        result = self.library._get_fc_target_wwpns()

        self.assertSetEqual(set(ports), set(result))

    @mock.patch.object(ssc_cmode, 'refresh_cluster_ssc', mock.Mock())
    @mock.patch.object(block_cmode.NetAppBlockStorageCmodeLibrary,
                       '_get_pool_stats', mock.Mock())
    def test_vol_stats_calls_provide_ems(self):
        self.library.zapi_client.provide_ems = mock.Mock()

        self.library.get_volume_stats(refresh=True)

        self.assertEqual(1, self.library.zapi_client.provide_ems.call_count)

    def test_create_lun(self):
        self.library._update_stale_vols = mock.Mock()

        self.library._create_lun(fake.VOLUME_ID, fake.LUN_ID,
                                 fake.LUN_SIZE, fake.LUN_METADATA)

        self.library.zapi_client.create_lun.assert_called_once_with(
            fake.VOLUME_ID, fake.LUN_ID, fake.LUN_SIZE, fake.LUN_METADATA,
            None)
        self.assertEqual(1, self.library._update_stale_vols.call_count)

    @mock.patch.object(ssc_cmode, 'get_volumes_for_specs')
    @mock.patch.object(ssc_cmode, 'get_cluster_latest_ssc')
    def test_check_volume_type_for_lun_fail(self, get_ssc, get_vols):
        self.library.ssc_vols = ['vol']
        fake_extra_specs = {'specs': 's'}
        get_vols.return_value = [ssc_cmode.NetAppVolume(name='name',
                                                        vserver='vs')]
        mock_lun = block_base.NetAppLun('handle', 'name', '1',
                                        {'Volume': 'fake', 'Path': '/vol/lun'})
        self.assertRaises(exception.ManageExistingVolumeTypeMismatch,
                          self.library._check_volume_type_for_lun,
                          {'vol': 'vol'}, mock_lun, {'ref': 'ref'},
                          fake_extra_specs)
        get_vols.assert_called_with(['vol'], {'specs': 's'})
        self.assertEqual(1, get_ssc.call_count)

    def test_get_preferred_target_from_list(self):
        target_details_list = fake.ISCSI_TARGET_DETAILS_LIST
        operational_addresses = [
            target['address']
            for target in target_details_list[2:]]
        self.zapi_client.get_operational_network_interface_addresses = (
            mock.Mock(return_value=operational_addresses))

        result = self.library._get_preferred_target_from_list(
            target_details_list)

        self.assertEqual(target_details_list[2], result)

    def test_get_pool_stats_no_volumes(self):

        self.library.ssc_vols = []

        result = self.library._get_pool_stats()

        self.assertListEqual([], result)

    @ddt.data({'thin': True, 'netapp_lun_space_reservation': 'enabled'},
              {'thin': True, 'netapp_lun_space_reservation': 'disabled'},
              {'thin': False, 'netapp_lun_space_reservation': 'enabled'},
              {'thin': False, 'netapp_lun_space_reservation': 'disabled'})
    @ddt.unpack
    def test_get_pool_stats(self, thin, netapp_lun_space_reservation):

        class test_volume(object):
            self.id = None
            self.aggr = None

        test_volume = test_volume()
        test_volume.id = {'vserver': 'openstack', 'name': 'vola'}
        test_volume.aggr = {
            'disk_type': 'SSD',
            'ha_policy': 'cfo',
            'junction': '/vola',
            'name': 'aggr1',
            'raid_type': 'raiddp'
        }
        test_volume.space = {
            'size_total_bytes': '10737418240',
            'space-guarantee': 'file',
            'size_avl_bytes': '2147483648',
            'space-guarantee-enabled': False,
            'thin_provisioned': False
        }
        test_volume.sis = {'dedup': False, 'compression': False}
        test_volume.state = {
            'status': 'online',
            'vserver_root': False,
            'junction_active': True
        }
        test_volume.qos = {'qos_policy_group': None}

        ssc_map = {
            'mirrored': {},
            'dedup': {},
            'compression': {},
            'thin': {test_volume if thin else None},
            'all': [test_volume]
        }
        self.library.ssc_vols = ssc_map
        self.library.reserved_percentage = 5
        self.library.max_over_subscription_ratio = 10
        self.library.configuration.netapp_lun_space_reservation = (
            netapp_lun_space_reservation)

        netapp_thin = 'true' if thin else 'false'
        netapp_thick = 'false' if thin else 'true'

        thick = not thin and (netapp_lun_space_reservation == 'enabled')

        result = self.library._get_pool_stats()

        expected = [{'pool_name': 'vola',
                     'netapp_unmirrored': 'true',
                     'QoS_support': True,
                     'thin_provisioned_support': not thick,
                     'thick_provisioned_support': thick,
                     'provisioned_capacity_gb': 8.0,
                     'netapp_thick_provisioned': netapp_thick,
                     'netapp_nocompression': 'true',
                     'free_capacity_gb': 2.0,
                     'netapp_thin_provisioned': netapp_thin,
                     'total_capacity_gb': 10.0,
                     'netapp_compression': 'false',
                     'netapp_mirrored': 'false',
                     'netapp_dedup': 'false',
                     'reserved_percentage': 5,
                     'max_over_subscription_ratio': 10.0,
                     'netapp_raid_type': 'raiddp',
                     'netapp_disk_type': 'SSD',
                     'netapp_nodedup': 'true'}]

        self.assertEqual(expected, result)

    def test_delete_volume(self):
        self.mock_object(block_base.NetAppLun, 'get_metadata_property',
                         mock.Mock(return_value=fake.POOL_NAME))
        self.mock_object(self.library, '_update_stale_vols')
        self.mock_object(na_utils, 'get_valid_qos_policy_group_info',
                         mock.Mock(
                             return_value=fake.QOS_POLICY_GROUP_INFO))
        self.mock_object(self.library, '_mark_qos_policy_group_for_deletion')

        self.library.delete_volume(fake.VOLUME)

        self.assertEqual(1,
                         block_base.NetAppLun.get_metadata_property.call_count)
        block_base.NetAppBlockStorageLibrary.delete_volume\
            .assert_called_once_with(fake.VOLUME)
        na_utils.get_valid_qos_policy_group_info.assert_called_once_with(
            fake.VOLUME)
        self.library._mark_qos_policy_group_for_deletion\
            .assert_called_once_with(fake.QOS_POLICY_GROUP_INFO)
        self.assertEqual(1, self.library._update_stale_vols.call_count)

    def test_delete_volume_no_netapp_vol(self):
        self.mock_object(block_base.NetAppLun, 'get_metadata_property',
                         mock.Mock(return_value=None))
        self.mock_object(self.library, '_update_stale_vols')
        self.mock_object(na_utils, 'get_valid_qos_policy_group_info',
                         mock.Mock(
                             return_value=fake.QOS_POLICY_GROUP_INFO))
        self.mock_object(self.library, '_mark_qos_policy_group_for_deletion')

        self.library.delete_volume(fake.VOLUME)

        block_base.NetAppLun.get_metadata_property.assert_called_once_with(
            'Volume')
        block_base.NetAppBlockStorageLibrary.delete_volume\
            .assert_called_once_with(fake.VOLUME)
        self.library._mark_qos_policy_group_for_deletion\
            .assert_called_once_with(fake.QOS_POLICY_GROUP_INFO)
        self.assertEqual(0, self.library._update_stale_vols.call_count)

    def test_delete_volume_get_valid_qos_policy_group_info_exception(self):
        self.mock_object(block_base.NetAppLun, 'get_metadata_property',
                         mock.Mock(return_value=fake.NETAPP_VOLUME))
        self.mock_object(self.library, '_update_stale_vols')
        self.mock_object(na_utils, 'get_valid_qos_policy_group_info',
                         mock.Mock(side_effect=exception.Invalid))
        self.mock_object(self.library, '_mark_qos_policy_group_for_deletion')

        self.library.delete_volume(fake.VOLUME)

        block_base.NetAppLun.get_metadata_property.assert_called_once_with(
            'Volume')
        block_base.NetAppBlockStorageLibrary.delete_volume\
            .assert_called_once_with(fake.VOLUME)
        self.library._mark_qos_policy_group_for_deletion\
            .assert_called_once_with(None)
        self.assertEqual(1, self.library._update_stale_vols.call_count)

    def test_setup_qos_for_volume(self):
        self.mock_object(na_utils, 'get_valid_qos_policy_group_info',
                         mock.Mock(
                             return_value=fake.QOS_POLICY_GROUP_INFO))
        self.mock_object(self.zapi_client, 'provision_qos_policy_group')

        result = self.library._setup_qos_for_volume(fake.VOLUME,
                                                    fake.EXTRA_SPECS)

        self.assertEqual(fake.QOS_POLICY_GROUP_INFO, result)
        self.zapi_client.provision_qos_policy_group.\
            assert_called_once_with(fake.QOS_POLICY_GROUP_INFO)

    def test_setup_qos_for_volume_exception_path(self):
        self.mock_object(na_utils, 'get_valid_qos_policy_group_info',
                         mock.Mock(
                             side_effect=exception.Invalid))
        self.mock_object(self.zapi_client, 'provision_qos_policy_group')

        self.assertRaises(exception.VolumeBackendAPIException,
                          self.library._setup_qos_for_volume, fake.VOLUME,
                          fake.EXTRA_SPECS)

        self.assertEqual(0,
                         self.zapi_client.
                         provision_qos_policy_group.call_count)

    def test_mark_qos_policy_group_for_deletion(self):
        self.mock_object(self.zapi_client,
                         'mark_qos_policy_group_for_deletion')

        self.library._mark_qos_policy_group_for_deletion(
            fake.QOS_POLICY_GROUP_INFO)

        self.zapi_client.mark_qos_policy_group_for_deletion\
            .assert_called_once_with(fake.QOS_POLICY_GROUP_INFO)

    def test_unmanage(self):
        self.mock_object(na_utils, 'get_valid_qos_policy_group_info',
                         mock.Mock(return_value=fake.QOS_POLICY_GROUP_INFO))
        self.mock_object(self.library, '_mark_qos_policy_group_for_deletion')
        self.mock_object(block_base.NetAppBlockStorageLibrary, 'unmanage')

        self.library.unmanage(fake.VOLUME)

        na_utils.get_valid_qos_policy_group_info.assert_called_once_with(
            fake.VOLUME)
        self.library._mark_qos_policy_group_for_deletion\
            .assert_called_once_with(fake.QOS_POLICY_GROUP_INFO)
        block_base.NetAppBlockStorageLibrary.unmanage.assert_called_once_with(
            fake.VOLUME)

    def test_unmanage_w_invalid_qos_policy(self):
        self.mock_object(na_utils, 'get_valid_qos_policy_group_info',
                         mock.Mock(side_effect=exception.Invalid))
        self.mock_object(self.library, '_mark_qos_policy_group_for_deletion')
        self.mock_object(block_base.NetAppBlockStorageLibrary, 'unmanage')

        self.library.unmanage(fake.VOLUME)

        na_utils.get_valid_qos_policy_group_info.assert_called_once_with(
            fake.VOLUME)
        self.library._mark_qos_policy_group_for_deletion\
            .assert_called_once_with(None)
        block_base.NetAppBlockStorageLibrary.unmanage.assert_called_once_with(
            fake.VOLUME)

    def test_manage_existing_lun_same_name(self):
        mock_lun = block_base.NetAppLun('handle', 'name', '1',
                                        {'Path': '/vol/vol1/name'})
        self.library._get_existing_vol_with_manage_ref = mock.Mock(
            return_value=mock_lun)
        self.mock_object(na_utils, 'get_volume_extra_specs')
        self.mock_object(na_utils, 'log_extra_spec_warnings')
        self.library._check_volume_type_for_lun = mock.Mock()
        self.library._setup_qos_for_volume = mock.Mock()
        self.mock_object(na_utils, 'get_qos_policy_group_name_from_info',
                         mock.Mock(return_value=fake.QOS_POLICY_GROUP_NAME))
        self.library._add_lun_to_table = mock.Mock()
        self.zapi_client.move_lun = mock.Mock()
        mock_set_lun_qos_policy_group = self.mock_object(
            self.zapi_client, 'set_lun_qos_policy_group')

        self.library.manage_existing({'name': 'name'}, {'ref': 'ref'})

        self.library._get_existing_vol_with_manage_ref.assert_called_once_with(
            {'ref': 'ref'})
        self.assertEqual(1, self.library._check_volume_type_for_lun.call_count)
        self.assertEqual(1, self.library._add_lun_to_table.call_count)
        self.assertEqual(0, self.zapi_client.move_lun.call_count)
        self.assertEqual(1, mock_set_lun_qos_policy_group.call_count)

    def test_manage_existing_lun_new_path(self):
        mock_lun = block_base.NetAppLun(
            'handle', 'name', '1', {'Path': '/vol/vol1/name'})
        self.library._get_existing_vol_with_manage_ref = mock.Mock(
            return_value=mock_lun)
        self.mock_object(na_utils, 'get_volume_extra_specs')
        self.mock_object(na_utils, 'log_extra_spec_warnings')
        self.library._check_volume_type_for_lun = mock.Mock()
        self.library._add_lun_to_table = mock.Mock()
        self.zapi_client.move_lun = mock.Mock()

        self.library.manage_existing({'name': 'volume'}, {'ref': 'ref'})

        self.assertEqual(
            2, self.library._get_existing_vol_with_manage_ref.call_count)
        self.assertEqual(1, self.library._check_volume_type_for_lun.call_count)
        self.assertEqual(1, self.library._add_lun_to_table.call_count)
        self.zapi_client.move_lun.assert_called_once_with(
            '/vol/vol1/name', '/vol/vol1/volume')

    def test_start_periodic_tasks(self):

        mock_remove_unused_qos_policy_groups = self.mock_object(
            self.zapi_client,
            'remove_unused_qos_policy_groups')

        harvest_qos_periodic_task = mock.Mock()
        mock_loopingcall = self.mock_object(
            loopingcall,
            'FixedIntervalLoopingCall',
            mock.Mock(side_effect=[harvest_qos_periodic_task]))

        self.library._start_periodic_tasks()

        mock_loopingcall.assert_has_calls([
            mock.call(mock_remove_unused_qos_policy_groups)])
        self.assertTrue(harvest_qos_periodic_task.start.called)


text:
no_licenses
=====

An OTP application

Build
-----

    $ rebar3 compile


text:
title: Namespace Renamer
categories: .Net
tags:
date: 2015-04-14 16:27:00 +11:00
---

All the way [back in 2009][0] I wrote a little console application that assisted with renaming a Visual Studio project and/or namespace. It was handy in that it took into consideration the solution being under source control with TFS. Over the years I have been using TF source control less and Git more. On the odd occasion I have also used this script for solutions that are not bound in source control.

I have refactored the script to take out the TF integration, remove the logging that was just causing a whole lot of white noise and to add in a few more out of the box exclusions for directories to skip in the process.

<!--more-->

```csharp
namespace Neovolve.NamespaceRenamer
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Reflection;
    using System.Text;
    using System.Text.RegularExpressions;

    internal class Program
    {
        /// <summary>
        ///     The name to find.
        /// </summary>
        private const string FindName = "Solution.Project.OldNamespace";

        /// <summary>
        ///     The name to replace.
        /// </summary>
        private const string ReplaceName = "Solution.Project.NewNamespace";

        /// <summary>
        ///     The root path from which changes are made.
        /// </summary>
        private const string RootPath = @"D:\Repos\Solution.Project";

        /// <summary>
        ///     Stores the directories that should not be processed
        /// </summary>
        private static readonly List<string> DirectoryExclusions = new List<string>
        {
            "bin",
            "obj",
            "$tf",
            ".git",
            "Packages",
            "References",
            "TestResults"
        };

        /// <summary>
        ///     Stores the file extensions that should not be processed.
        /// </summary>
        private static readonly List<string> FileExtensionExclusions = new List<string>
        {
            ".dll",
            ".exe",
            ".pdb",
            ".suo"
        };

        private static readonly string LogPath = Assembly.GetExecutingAssembly().Location + "."
                                                 + DateTime.Now.ToString("yyyyMMdd-hhmmss") + ".log";

        private static Regex _expression;

        private static Regex DetermineFindReplaceExpression(string findText, string replaceText)
        {
            string pattern;

            if (replaceText.EndsWith(findText, StringComparison.Ordinal))
            {
                // The rename is to add a prefix
                string prefix = replaceText.Substring(0, replaceText.Length - findText.Length);
                pattern = "(?<!" + Regex.Escape(prefix) + ")" + Regex.Escape(FindName);

                WriteMessage("Rename with new prefix detected");
            }
            else if (replaceText.StartsWith(findText, StringComparison.Ordinal))
            {
                // The rename is to add a suffix
                string suffix = replaceText.Substring(findText.Length);
                pattern = Regex.Escape(FindName) + "(?!" + Regex.Escape(suffix) + ")";

                WriteMessage("Rename with new suffix detected");
            }
            else if (replaceText.Contains(findText))
            {
                // The rename is to add a prefix and a suffix
                string prefix = replaceText.Substring(0, replaceText.IndexOf(findText));
                string suffix = replaceText.Substring(replaceText.LastIndexOf(findText) + findText.Length);
                pattern = "(?<!" + Regex.Escape(prefix) + ")" + Regex.Escape(FindName) + "(?!" + Regex.Escape(suffix) +
                          ")";

                WriteMessage("Rename with new prefix and suffix detected");
            }
            else
            {
                // The replace text is not contained within the find text
                pattern = Regex.Escape(FindName);

                WriteMessage("Rename with no suffix or prefix detected");
            }

            return new Regex(pattern, RegexOptions.Compiled);
        }

        private static void Main(string[] args)
        {
            _expression = DetermineFindReplaceExpression(FindName, ReplaceName);

            WriteMessage("Processed started at " + DateTime.Now);

            RunRename(new DirectoryInfo(RootPath));

            WriteMessage("Processed completed at " + DateTime.Now);
            WriteMessage("Log file located at " + LogPath);

            Console.ReadKey();
        }

        private static void RenameDirectory(DirectoryInfo directory)
        {
            WriteMessage("Renaming directory " + directory.FullName);

            Debug.Assert(directory.Parent != null, "No parent directory available for the directory");

            string newName = _expression.Replace(directory.Name, ReplaceName);
            string newPath = Path.Combine(directory.Parent.FullName, newName);

            directory.MoveTo(newPath);
        }

        private static void RenameFile(FileInfo file)
        {
            WriteMessage("Renaming file " + file.FullName);

            Debug.Assert(file.Directory != null, "No parent directory available for the file");

            string newName = _expression.Replace(file.Name, ReplaceName);
            string newPath = Path.Combine(file.Directory.FullName, newName);

            file.MoveTo(newPath);
        }

        private static void RunRename(DirectoryInfo directory)
        {
            if (DirectoryExclusions.Contains(directory.Name))
            {
                WriteMessage("Skipping excluded directory " + directory.FullName);

                return;
            }

            // Check if the directory is not excluded
            foreach (DirectoryInfo childDirectory in directory.GetDirectories())
            {
                // Recurse into child directories
                // Recurse first before changing the current directory results in a bottom up change
                RunRename(childDirectory);
            }

            foreach (FileInfo file in directory.GetFiles())
            {
                if (FileExtensionExclusions.Contains(file.Extension))
                {
                    WriteMessage("Skipping " + file.FullName + " due to file extension exclusion");

                    continue;
                }

                // Load the contents
                string contents = File.ReadAllText(file.FullName, Encoding.UTF8);

                if (_expression.IsMatch(contents))
                {
                    contents = _expression.Replace(contents, ReplaceName);

                    WriteMessage("Updating file contents for " + file.FullName);

                    // Ensure that the file is not read-only
                    // This will occur when the file was not under source control
                    if (file.IsReadOnly)
                    {
                        file.IsReadOnly = false;
                    }

                    File.WriteAllText(file.FullName, contents, Encoding.UTF8);
                }

                // Check if the file needs to be renamed
                if (_expression.IsMatch(file.Name))
                {
                    RenameFile(file);
                }
            }

            if (_expression.IsMatch(directory.Name))
            {
                RenameDirectory(directory);
            }
        }

        private static void WriteMessage(string message)
        {
            Console.WriteLine(message);
            File.AppendAllText(LogPath, message + Environment.NewLine);
        }
    }
}
```

All you have to do is drop this into a new console project, change the three field values and off you go.

[0]: /2009/10/06/changing-a-tfs-bound-solution-namespace/


text:
/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

/**
 * @file apihelp.c
 * @brief Support functions for the test code of FSL SHW API
 */

#include "apihelp.h"

/**
 * Compare two strings.  Print debug information if they do not compare.
 *
 * @param   model   The correct version of @a result.
 * @param   result  An output string from a test.
 * @param   length  The number of bytes of @a model, @a result to compare.
 * @param   name    A NUL-terminated string which is the name of the string
 *                  being compared.
 *
 * @return zero if the strings compare, non-zero otherwise.
 */
int compare_result(const uint8_t* model, const uint8_t* result,
                   uint32_t length, const char* name)
{
    int failed = 1;
    uint32_t i;

    for (i = 0; i < length; i++) {
        if (model[i] != result[i]) {
            break;
        }
    }
    if (i != length) {
        /* Try to dump relevant portions of data */
        uint32_t start_offset = (i > 10) ? (i - 4) : 0;
        uint32_t dump_count = (length > MAX_DUMP) ? MAX_DUMP : length;

        if ((start_offset + dump_count) > length) {
            dump_count = length - start_offset;
        }

        printf("Comparison of %s differs at offset %u of %u\n", name, i,
               length);

        printf("Good: %p (%d) ", model, start_offset);
        for (i = start_offset; i < (dump_count + start_offset); i++) {
            printf("%02x ", model[i]);
        }
        printf("\nBad:  %p (%d) ", result, start_offset);
        for (i = start_offset; i < (dump_count + start_offset); i++) {
            printf("%02x ", result[i]);
        }
        printf("\n");
    } else {
        failed = 0;
    }

    return failed;
}
#if defined(__KERNEL__)
EXPORT_SYMBOL(compare_result);
#endif

/**
 * Return a (non-allocated) string containing an interpretation of
 * an FSL SHW API error code.
 *
 * @param  code    The error code to interpret.
 *
 * @return  The associated English interpretation of the code.
 */
char* fsl_error_string(fsl_shw_return_t code)
{
    char* str;

    switch (code) {
    case FSL_RETURN_OK_S:
        str = "No error";
        break;
    case FSL_RETURN_ERROR_S:
        str = "Error";
        break;
    case FSL_RETURN_NO_RESOURCE_S:
        str = "No resource";
        break;
    case FSL_RETURN_BAD_ALGORITHM_S:
        str = "Bad algorithm";
        break;
    case FSL_RETURN_BAD_MODE_S:
        str = "Bad mode";
        break;
    case FSL_RETURN_BAD_FLAG_S:
        str = "Bad flag";
        break;
    case FSL_RETURN_BAD_KEY_LENGTH_S:
        str = "Bad key length";
        break;
    case FSL_RETURN_BAD_KEY_PARITY_S:
        str = "Bad key parity";
        break;
    case FSL_RETURN_BAD_DATA_LENGTH_S:
        str = "Bad data length";
        break;
    case FSL_RETURN_AUTH_FAILED_S:
        str = "Authentication failed";
        break;
    case FSL_RETURN_MEMORY_ERROR_S:
        str = "Memory error";
        break;
    case FSL_RETURN_INTERNAL_ERROR_S:
        str = "Internal error";
        break;
    default:
        str = "unknown value";
        break;
    }

    return str;
}
#if defined(__KERNEL__)
EXPORT_SYMBOL(fsl_error_string);
#endif

unsigned get_hash_size(fsl_shw_hash_alg_t alg)
{
    unsigned size;

     switch (alg) {
     case FSL_HASH_ALG_MD5:
         size = 16;
         break;
     case FSL_HASH_ALG_SHA1:
         size = 20;
         break;
     case FSL_HASH_ALG_SHA224:
         size = 28;
         break;
     case FSL_HASH_ALG_SHA256:
         size = 32;
         break;
     default:
         /* error ! */
         size = 1;
         break;
     }

     return size;
}
#if defined(__KERNEL__)
EXPORT_SYMBOL(get_hash_size);
#endif


text:
module Metasploit::Cache::Author::Persister
  # Maps {Metasploit::Cache::Author#name} to {Metasploit::Cache::Author} using pre-existing {Metasploit::Cache::Author}
  # matching `name_set`; otherwise, supplying newly created {Metasploit::Cache::Author}s.
  #
  # @param existing_name_set [Set<String>] Set of {Metasploit::Cache::Author#name} to preload
  # @return [Hash{String => Metasploit::Cache::Author}]
  def self.by_name(existing_name_set:)
    Metasploit::Cache::Persister::AttributeSet.existing_by_attribute_value(
        attribute: :name,
        scope: Metasploit::Cache::Author,
        value_set: existing_name_set
    ).tap { |hash|
      hash.default_proc = Metasploit::Cache::Persister.create_unique_proc(
          Metasploit::Cache::Author,
          :name
      )
    }
  end
end


text:
date: 2022-03-31T09:30:34-04:00
description: "A research program at Google Cloud, notable for the State of DevOps reports"
tags: [ "software-engineering", "management", "data-analysis", "google-cloud" ]
title: "DevOps Research and Assessment (DORA)"
---

# DevOps Research and Assessment (DORA)

**DevOps Research and Assessment (DORA)** is a research program at Google Cloud that uses behavioral science to investigate the practices, capabilities, and organizational techniques employed by high performance technology teams to impact organizational outcomes. DORA is most notable for it's annual State of DevOps Reports and for having identified the [four key metrics](dora-metrics.md) thought to indicate the performance of [software development](software-engineering.md) teams.

## DORA Resources

* [Official DORA website](https://www.devops-research.com/research.html)


text:
A minimal Elasticsearch client

## Using the API

First you create a client, specifying a host and then use it.

```go
package main

import (
        "fmt"
        "github.com/infospace/gopherlastic"
)

type Something struct {
        SomeValue  string `json:"someValue"`
        OtherThing string `json:"otherThing"`
}

func main() {
        doc := &Soemthing{
                SomeValue: "some",
                OtherThing: "thingy",
        }

        client := gopherlastic.NewClient(r.Host)
        putResponse, err := client.PutDocument("some-index", "something", "1", doc)

        if err != nil {
                fmt.Println("Problem storing document:", err)
                return
        }

        fmt.Println("Saved document:", putResponse)
}
```

There's also a WIP Search interface.


text:
<?php

namespace App\Model\Services;

use App\Model\Entities\User;
use Kdyby\Doctrine\EntityManager;
use Nette;
use Nette\Object;
use Nette\Security\Passwords;

/**
 * Performs user authentication.
 *
 * Class AuthenticationService
 * @package App\Model\Services
 */
class AuthenticationService extends Object implements Nette\Security\IAuthenticator
{
    /** @var EntityManager */
    private $em;

    /** @var \Kdyby\Doctrine\EntityDao */
    private $usersDao;

    /**
     * @param EntityManager $entityManager
     */
    public function __construct(EntityManager $entityManager)
    {
        $this->em = $entityManager;
        $this->usersDao = $entityManager->getRepository(User::getClassName());
    }

    /**
     * Try to authenticate the user.
     *
     * @param array $credentials
     * @return Nette\Security\Identity|Nette\Security\IIdentity
     * @throws Nette\Security\AuthenticationException
     */
    public function authenticate(array $credentials)
    {
        list($username, $password) = $credentials;

        /** @var User $row */
        $row = $this->usersDao->findOneBy(array(
            'username' => $username,
            'active' => TRUE
        ));

        if (!$row) {
            throw new Nette\Security\AuthenticationException('Špatný login.', self::IDENTITY_NOT_FOUND);
        } elseif (!Passwords::verify($password, $row->getPassword())) {
            throw new Nette\Security\AuthenticationException('Špatné heslo.', self::INVALID_CREDENTIAL);
        } elseif (Passwords::needsRehash($row->getPassword())) {
            $row->update(array(
                'password' => Passwords::hash($password),
            ));
        } elseif ($row->isBanned()) {
            throw new Nette\Security\AuthenticationException('Byl jsi zabanován. Napiš adminovi na ' . $row->getBan() . '.', self::NOT_APPROVED);
        }

        $arr = array(
            'username' => $row->getUsername(),
            'id' => $row->getId(),
            'role' => $row->getRole()
        );

        return new Nette\Security\Identity($arr['id'], $arr['role'], $arr);
    }
}


text:
namespace OleksiiOnSoftware.Services.Blog.Import.Utils
{
    using System;
    using System.Text;
    using System.Text.RegularExpressions;

    public static class LinkUtils
    {
        /// <summary>
        /// Match patterns like 00-not-set, 55-custom-category-name
        /// </summary>
        private static string LinkRegExp = @"[0-9]*-(.*)";

        public static string MakeFriendly(string url)
        {
            var segments = url.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
            var sb = new StringBuilder();
            foreach (var segment in segments)
            {
                var matches = new Regex(LinkRegExp).Matches(segment);
                if (matches.Count == 0)
                {
                    return null;
                }

                sb.AppendFormat("{0}_", matches[0].Groups[1].Value);
            }

            var newUrl = sb.ToString().Trim('_');
            return newUrl;
        }
    }
}


text:
#include "QmitkMaskStampWidget.h"

#include <mitkLabelSetImage.h>
#include <mitkRenderingManager.h>
#include <mitkToolManagerProvider.h>

#include <QMessageBox>

QmitkMaskStampWidget::QmitkMaskStampWidget(QWidget *parent, const char * /*name*/)
  : QWidget(parent), m_ToolManager(NULL), m_DataStorage(NULL)
{
  m_Controls.setupUi(this);
  m_Controls.m_InformationWidget->hide();

  m_ToolManager = mitk::ToolManagerProvider::GetInstance()->GetToolManager();
  assert(m_ToolManager);
  m_ToolManager->ActivateTool(-1);

  mitk::TNodePredicateDataType<mitk::Image>::Pointer isImage = mitk::TNodePredicateDataType<mitk::Image>::New();
  mitk::NodePredicateProperty::Pointer isBinary =
    mitk::NodePredicateProperty::New("binary", mitk::BoolProperty::New(true));
  mitk::NodePredicateAnd::Pointer isMask = mitk::NodePredicateAnd::New(isBinary, isImage);

  mitk::NodePredicateAnd::Pointer maskPredicate = mitk::NodePredicateAnd::New();
  maskPredicate->AddPredicate(isMask);
  maskPredicate->AddPredicate(mitk::NodePredicateNot::New(mitk::NodePredicateProperty::New("helper object")));

  m_Controls.m_cbMaskNodeSelector->SetPredicate(maskPredicate);

  connect(m_Controls.m_pbStamp, SIGNAL(clicked()), this, SLOT(OnStamp()));
  connect(m_Controls.m_cbShowInformation, SIGNAL(toggled(bool)), this, SLOT(OnShowInformation(bool)));
  m_Controls.m_InformationWidget->hide();
}

QmitkMaskStampWidget::~QmitkMaskStampWidget()
{
}

void QmitkMaskStampWidget::SetDataStorage(mitk::DataStorage *storage)
{
  m_DataStorage = storage;
  m_Controls.m_cbMaskNodeSelector->SetDataStorage(m_DataStorage);
}

void QmitkMaskStampWidget::OnStamp()
{
  mitk::DataNode *maskNode = m_Controls.m_cbMaskNodeSelector->GetSelectedNode();

  if (!maskNode)
  {
    QMessageBox::information(this, "Mask Stamp", "Please load and select a mask before starting some action.");
    return;
  }

  mitk::Image *mask = dynamic_cast<mitk::Image *>(maskNode->GetData());
  if (!mask)
  {
    QMessageBox::information(this, "Mask Stamp", "Please load and select a mask before starting some action.");
    return;
  }

  mitk::DataNode *workingNode = m_ToolManager->GetWorkingData(0);

  if (!workingNode)
  {
    QMessageBox::information(this, "Mask Stamp", "Please load and select a segmentation before starting some action.");
    return;
  }

  mitk::LabelSetImage *workingImage = dynamic_cast<mitk::LabelSetImage *>(workingNode->GetData());

  if (!workingImage)
  {
    QMessageBox::information(this, "Mask Stamp", "Please load and select a segmentation before starting some action.");
    return;
  }

  QApplication::setOverrideCursor(QCursor(Qt::BusyCursor));
  try
  {
    workingImage->MaskStamp(mask, m_Controls.m_chkOverwrite->isChecked());
  }
  catch (mitk::Exception &e)
  {
    QApplication::restoreOverrideCursor();
    MITK_ERROR << "Exception caught: " << e.GetDescription();
    QMessageBox::information(this, "Mask Stamp", "Could not stamp the selected mask.\n See error log for details.\n");
    return;
  }

  QApplication::restoreOverrideCursor();

  maskNode->SetVisibility(false);

  mitk::RenderingManager::GetInstance()->RequestUpdateAll();
}

void QmitkMaskStampWidget::OnShowInformation(bool on)
{
  if (on)
    m_Controls.m_InformationWidget->show();
  else
    m_Controls.m_InformationWidget->hide();
}


text:
"""
This module is the brain of the Chris Jones Bot. It handles queries and constructs responses
"""
import json
import re
import random
import pdb
import requests
from fuzzywuzzy import process

import elastic
from elastic import ES_URL

from google_nlp_api import GoogleNlp
from QueryAnalyzer import QueryAnalyzer
from default_query import DefaultQuery
from default_query import PersonThoughtsQuery
from default_query import LocationQuery
from theater_query import TheaterQuery
from sentiment_query import SentimentQuery
from show_query import ShowQuery

class ChrisJones:
    """
    This is the main class of the Chris Jones Bot
    """
    def __init__(self):
        self.query_analyzer = QueryAnalyzer()
        self.sentiment_selector = SentimentQuery()
        print 'ChrisJones activated'

    def respond(self, query):
        """
        Main wrapper function for Chris Jones bot. Takes a query and returns a response.
        args:
            query (string): A text query from the user

        return:
            response (string): A string (possibly with markdown formatting)
        """
        # Get query annotations
        annotated_query = self.query_analyzer.annotate(query)
        print annotated_query.get_framework()

        # TODO - change uses of `query` to annotated_query.query to
        # cut back on the number of arguments we have to pass around

        # Route to correct query handler
        if (query == 'how do you like your comedy'):
            print 'comedy query'
            return DefaultQuery().generate_response(query, annotated_query)
        elif (query in ['do you have a favorite actor',
                        'do you have a favorite director',
                        'do you have a favorite actress',
                        'do you have a favorite playwright',
                        'do you have a favorite show',
                        'do you have a favorite theater',
                        'do you have a favorite theatre',
                        'who is your favorite actor',
                        'who is your favorite director',
                        'who is your favorite actress',
                        'who is your favorite playwright',
                        'what is your favorite show',
                        'what is your favorite theater',
                        'what is your favorite theatre']):
            return PersonThoughtsQuery().generate_response_favorite_person(query, annotated_query)

        elif any(re.search(i, query) is not None for i in ['like', 'dislike', 'love', 'hate']):
            # TODO - Determine a more satisfying way to kick off this handler, perhaps it should just be more specific
            # Sentiment Aggregation query handler
            print 'Sentiment Query'
            router = {('do you ' + d + ' ' + t):
                      (lambda x, y: self.sentiment_selector.generate_response(x, y))
                      for t in ['THEATER', 'SHOW', 'GENRE', 'PERSON']
                      for d in ['like', 'dislike', 'hate', 'love']}

            return self.call_handler(router, query, annotated_query)

        elif len(annotated_query.genres) > 0:
            # Genre query handler
            # 'how do you like your GENRE',
            # Write better handler when we get more genre questions
            print 'Genre Query'
            return DefaultQuery().generate_response(query, annotated_query)

        elif len(annotated_query.theaters) > 0:
            # Theater-related questions
            print 'Theater Query'
            router = {
                'what was your favorite show at THEATER': lambda x, y: TheaterQuery().generate_response(x, y),
                'How has THEATER changed over time': lambda x, y: TheaterQuery().generate_response(x, y),
                'I want to go to THEATER. Do you think it is good': lambda x, y: TheaterQuery().generate_response(x, y)
            }
            # Find the closest question type and use it to access handler
            return self.call_handler(router, query, annotated_query)

        elif len(annotated_query.people) > 0 and len(annotated_query.shows) == 0:
            # People-related questions
            print 'People Query'
            router = {
                'what was PERSON best performance': lambda x, y:
                    PersonThoughtsQuery().generate_response_best_performance(x, y),
                'do you think PERSON is a good NOUN': lambda x, y:
                    PersonThoughtsQuery().generate_response_good_noun(x, y),
                'what do you think of PERSON': lambda x, y: PersonThoughtsQuery().generate_response(x, y)
            }
            # Find the closest question type and use it to access handler
            return self.call_handler(router, query, annotated_query)

        elif len(annotated_query.shows) > 0:
            # Show related question types
            print 'Show Query'
            router = {
                'what did you think of SHOW': lambda x, y: self.sentiment_selector.generate_response(x, y),
                'what do you think is the best SHOW right now': lambda x, y:
                    ShowQuery().generate_response_best_show(x, y),
                'what do you think of PERSON in SHOW': lambda x, y: ShowQuery().generate_response_person_in_show(x, y)
            }
            # Find the closest question type and use it to access handler
            return self.call_handler(router, query, annotated_query)

        elif any(re.search(i, query) is not None for i in ['Chicago', 'chicago', 'New York', 'NYC']):
            # Location and/or Chicago-based questions
            print 'Location/Chicago Query'
            router = {
                'what embodies the essence of chicago theater': lambda x, y: LocationQuery().chicago_essence(x, y),
                'how is chicago different from New York?': lambda x, y: LocationQuery().ny_v_chicago(x, y)
            }
            # Find the closest question type and use it to access handler
            return self.call_handler(router, query, annotated_query)

        else:
            print 'Default Query'
            # What do you think is good NOUN
            return DefaultQuery().generate_response(query, annotated_query)

    def call_handler(self, router, query, annotated_query):
        """
        Helper function to use with query router dictionary switch statements

        args:
            router (dictionary): a dictionary with question frameworks as keys and implementations as values
            query (string): a string with the user's query
            annotated_query (AnnotatedQuery): an AnnotatedQuery corresponding to the user's query
        return:
            response (string): a response string, perhaps with markdown formatting
        """
        question_type = process.extractOne(annotated_query.get_framework(), router.keys())[0]
        return router[question_type](query, annotated_query)

if __name__ == '__main__':
    # Work on query routing now
    cj = ChrisJones()
    query = 'How has the Goodman Theatre changed over time'

    qa = QueryAnalyzer()
    annotated_query = qa.get_keywords(query)
    print cj.route_query(query, annotated_query)


text:
package com.whai.blog.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.whai.blog.dao.IArticleDao;
import com.whai.blog.model.Article;

@Component("articleDao")
public class ArticleDaoImpl implements IArticleDao {

	@Resource(name = "hibernateTemplate")
	private HibernateTemplate template;

	@Override
	public List<Article> getList() {
		return template.find("from Article");
	}

	@Override
	public int insert(Article article) {
		int x = (Integer) template.save(article);
		return x;
	}

	@Override
	public List<Article> getTheArticle(int articleId) {
		return template.find("from Article art where art.id="+articleId);
	}

	@Override
	public void delete(int articleId) {
		Article article = (Article)template.load(Article.class, articleId);
		template.delete(article);
	}

	@Override
	public void update(Article article) {
		template.update(article);
	}
}


text:
<?php

namespace QATools\QATools\PageObject\ElementLocator;

use QATools\QATools\PageObject\Exception\AnnotationException;
use Behat\Mink\Element\NodeElement;
use mindplay\annotations\AnnotationManager;
use QATools\QATools\PageObject\Annotation\FindByAnnotation;
use QATools\QATools\PageObject\ISearchContext;
use QATools\QATools\PageObject\Property;

/**
 * Class, that locates WebElements.
 *
 * @method \Mockery\Expectation shouldReceive(string $name)
 */
class DefaultElementLocator implements IElementLocator
{

	/**
	 * Search context.
	 *
	 * @var ISearchContext
	 */
	protected $searchContext;

	/**
	 * Annotation manager.
	 *
	 * @var AnnotationManager
	 */
	protected $annotationManager;

	/**
	 * Property.
	 *
	 * @var Property
	 */
	protected $property;

	/**
	 * Creates a new element locator.
	 *
	 * @param Property          $property           Property.
	 * @param ISearchContext    $search_context     The context to use when finding the element.
	 * @param AnnotationManager $annotation_manager Annotation manager.
	 */
	public function __construct(Property $property, ISearchContext $search_context, AnnotationManager $annotation_manager)
	{
		$this->property = $property;
		$this->searchContext = $search_context;
		$this->annotationManager = $annotation_manager;
	}

	/**
	 * Returns search context in use.
	 *
	 * @return ISearchContext
	 */
	public function getSearchContext()
	{
		return $this->searchContext;
	}

	/**
	 * Find the element.
	 *
	 * @return NodeElement|null
	 */
	public function find()
	{
		$items = $this->findAll();

		return count($items) ? current($items) : null;
	}

	/**
	 * Find the element list.
	 *
	 * @return NodeElement[]
	 */
	public function findAll()
	{
		return $this->searchContext->findAll('se', $this->getSelector());
	}

	/**
	 * Returns final selector to be used for element locating.
	 *
	 * @return array
	 * @throws AnnotationException When required @find-by annotation is missing.
	 */
	protected function getSelector()
	{
		/* @var $annotations FindByAnnotation[] */
		$annotations = $this->property->getAnnotationsFromPropertyOrClass('@find-by');

		if ( $annotations && ($annotations[0] instanceof FindByAnnotation) ) {
			$selector = $annotations[0]->getSelector();
		}
		else {
			$selector = array();
		}

		if ( !$selector ) {
			$parameters = array((string)$this->property, $this->property->getDataType());
			$message = '@find-by must be specified in the property "%s" DocBlock or in class "%s" DocBlock';

			throw new AnnotationException(vsprintf($message, $parameters), AnnotationException::TYPE_REQUIRED);
		}

		return $selector;
	}

	/**
	 * Returns string representation of a locator.
	 *
	 * @return string
	 */
	public function __toString()
	{
		return var_export(array('se' => $this->getSelector()), true);
	}

}