text:
__version__ = '0.1'

__all__ = ['opmap', 'opname', 'opcodes',
           'cmp_op', 'hasarg', 'hasname', 'hasjrel', 'hasjabs',
           'hasjump', 'haslocal', 'hascompare', 'hasfree', 'hascode',
           'hasflow', 'getse',
           'Opcode', 'SetLineno', 'Label', 'isopcode', 'Code',
           'CodeList', 'printcodelist']

import opcode
from dis import findlabels
import new
from array import array
import operator
import itertools
import sys
import warnings
from cStringIO import StringIO

######################################################################
# Define opcodes and information about them

python_version = '.'.join(str(x) for x in sys.version_info[:2])
if python_version not in ('2.4', '2.5'):
    warnings.warn('byteplay supports only Python versions 2.4 and 2.5')

class Opcode(int):
    """An int which represents an opcode - has a nicer repr."""
    def __repr__(self):
        return opname[self]
    __str__ = __repr__

class CodeList(list):
    """A list for storing opcode tuples - has a nicer __str__."""
    def __str__(self):
        f = StringIO()
        printcodelist(self, f)
        return f.getvalue()

opmap = dict((name.replace('+', '_'), Opcode(code))
             for name, code in opcode.opmap.iteritems()
             if name != 'EXTENDED_ARG')
opname = dict((code, name) for name, code in opmap.iteritems())
opcodes = set(opname)

def globalize_opcodes():
    for name, code in opmap.iteritems():
        globals()[name] = code
        __all__.append(name)
globalize_opcodes()

cmp_op = opcode.cmp_op

hasarg = set(x for x in opcodes if x >= opcode.HAVE_ARGUMENT)
hasconst = set(Opcode(x) for x in opcode.hasconst)
hasname = set(Opcode(x) for x in opcode.hasname)
hasjrel = set(Opcode(x) for x in opcode.hasjrel)
hasjabs = set(Opcode(x) for x in opcode.hasjabs)
hasjump = hasjrel.union(hasjabs)
haslocal = set(Opcode(x) for x in opcode.haslocal)
hascompare = set(Opcode(x) for x in opcode.hascompare)
hasfree = set(Opcode(x) for x in opcode.hasfree)
hascode = set([MAKE_FUNCTION, MAKE_CLOSURE])

class _se:
    """Quick way of defining static stack effects of opcodes"""
    # Taken from assembler.py by Phillip J. Eby
    NOP = 0

    POP_TOP   = 1,0
    ROT_TWO   = 2,2
    ROT_THREE = 3,3
    ROT_FOUR  = 4,4
    DUP_TOP   = 1,2

    UNARY_POSITIVE = UNARY_NEGATIVE = UNARY_NOT = UNARY_CONVERT = \
        UNARY_INVERT = GET_ITER = LOAD_ATTR = IMPORT_NAME = 1,1

    IMPORT_FROM = 1,2

    BINARY_POWER = BINARY_MULTIPLY = BINARY_DIVIDE = BINARY_FLOOR_DIVIDE = \
        BINARY_TRUE_DIVIDE = BINARY_MODULO = BINARY_ADD = BINARY_SUBTRACT = \
        BINARY_SUBSCR = BINARY_LSHIFT = BINARY_RSHIFT = BINARY_AND = \
        BINARY_XOR = BINARY_OR = COMPARE_OP = 2,1

    INPLACE_POWER = INPLACE_MULTIPLY = INPLACE_DIVIDE = \
        INPLACE_FLOOR_DIVIDE = INPLACE_TRUE_DIVIDE = INPLACE_MODULO = \
        INPLACE_ADD = INPLACE_SUBTRACT = INPLACE_LSHIFT = INPLACE_RSHIFT = \
        INPLACE_AND = INPLACE_XOR = INPLACE_OR = 2,1

    SLICE_0, SLICE_1, SLICE_2, SLICE_3 = \
        (1,1),(2,1),(2,1),(3,1)
    STORE_SLICE_0, STORE_SLICE_1, STORE_SLICE_2, STORE_SLICE_3 = \
        (2,0),(3,0),(3,0),(4,0)
    DELETE_SLICE_0, DELETE_SLICE_1, DELETE_SLICE_2, DELETE_SLICE_3 = \
        (1,0),(2,0),(2,0),(3,0)

    STORE_SUBSCR = 3,0
    DELETE_SUBSCR = STORE_ATTR = 2,0
    DELETE_ATTR = STORE_DEREF = 1,0
    PRINT_NEWLINE = 0,0
    PRINT_EXPR = PRINT_ITEM = PRINT_NEWLINE_TO = IMPORT_STAR = 1,0
    STORE_NAME = STORE_GLOBAL = STORE_FAST = 1,0
    PRINT_ITEM_TO = LIST_APPEND = 2,0

    LOAD_LOCALS = LOAD_CONST = LOAD_NAME = LOAD_GLOBAL = LOAD_FAST = \
        LOAD_CLOSURE = LOAD_DEREF = BUILD_MAP = 0,1

    DELETE_FAST = DELETE_GLOBAL = DELETE_NAME = 0,0

    EXEC_STMT = 3,0
    BUILD_CLASS = 3,1

    if python_version == '2.4':
        YIELD_VALUE = 1,0
        IMPORT_NAME = 1,1
    else:
        YIELD_VALUE = 1,1
        IMPORT_NAME = 2,1

_se = dict((op, getattr(_se, opname[op]))
           for op in opcodes
           if hasattr(_se, opname[op]))

hasflow = opcodes - set(_se) - \
          set([CALL_FUNCTION, CALL_FUNCTION_VAR, CALL_FUNCTION_KW,
               CALL_FUNCTION_VAR_KW, BUILD_TUPLE, BUILD_LIST,
               UNPACK_SEQUENCE, BUILD_SLICE, DUP_TOPX,
               RAISE_VARARGS, MAKE_FUNCTION, MAKE_CLOSURE])

def getse(op, arg=None):
    """Get the stack effect of an opcode, as a (pop, push) tuple.

    If an arg is needed and is not given, a ValueError is raised.
    If op isn't a simple opcode, that is, the flow doesn't always continue
    to the next opcode, a ValueError is raised.
    """
    try:
        return _se[op]
    except KeyError:
        # Continue to opcodes with an effect that depends on arg
        pass

    if arg is None:
        raise ValueError, "Opcode stack behaviour depends on arg"

    def get_func_tup(arg, nextra):
        if arg > 0xFFFF:
            raise ValueError, "Can only split a two-byte argument"
        return (nextra + 1 + (arg & 0xFF) + 2*((arg >> 8) & 0xFF),
                1)

    if op == CALL_FUNCTION:
        return get_func_tup(arg, 0)
    elif op == CALL_FUNCTION_VAR:
        return get_func_tup(arg, 1)
    elif op == CALL_FUNCTION_KW:
        return get_func_tup(arg, 1)
    elif op == CALL_FUNCTION_VAR_KW:
        return get_func_tup(arg, 2)

    elif op == BUILD_TUPLE:
        return arg, 1
    elif op == BUILD_LIST:
        return arg, 1
    elif op == UNPACK_SEQUENCE:
        return 1, arg
    elif op == BUILD_SLICE:
        return arg, 1
    elif op == DUP_TOPX:
        return arg, arg*2
    elif op == RAISE_VARARGS:
        return 1+arg, 1
    elif op == MAKE_FUNCTION:
        return 1+arg, 1
    elif op == MAKE_CLOSURE:
        if python_version == '2.4':
            raise ValueError, "The stack effect of MAKE_CLOSURE depends on TOS"
        else:
            return 2+arg, 1
    else:
        raise ValueError, "The opcode %r isn't recognized or has a special "\
              "flow control" % op

class SetLinenoType(object):
    def __repr__(self):
        return 'SetLineno'
SetLineno = SetLinenoType()

class Label(object):
    pass

def isopcode(obj):
    """Return whether obj is an opcode - not SetLineno or Label"""
    return obj is not SetLineno and not isinstance(obj, Label)

# Flags from code.h
CO_OPTIMIZED              = 0x0001      # use LOAD/STORE_FAST instead of _NAME
CO_NEWLOCALS              = 0x0002      # only cleared for module/exec code
CO_VARARGS                = 0x0004
CO_VARKEYWORDS            = 0x0008
CO_NESTED                 = 0x0010      # ???
CO_GENERATOR              = 0x0020
CO_NOFREE                 = 0x0040      # set if no free or cell vars
CO_GENERATOR_ALLOWED      = 0x1000      # unused
# The future flags are only used on code generation, so we can ignore them.
# (It does cause some warnings, though.)
CO_FUTURE_DIVISION        = 0x2000
CO_FUTURE_ABSOLUTE_IMPORT = 0x4000
CO_FUTURE_WITH_STATEMENT  = 0x8000

######################################################################
# Define the Code class

class Code(object):
    """An object which holds all the information which a Python code object
    holds, but in an easy-to-play-with representation.

    The attributes are:

    Affecting action
    ----------------
    code - list of 2-tuples: the code
    freevars - list of strings: the free vars of the code (those are names
               of variables created in outer functions and used in the function)
    args - list of strings: the arguments of the code
    varargs - boolean: Does args end with a '*args' argument
    varkwargs - boolean: Does args end with a '**kwargs' argument
    newlocals - boolean: Should a new local namespace be created.
                (True in functions, False for module and exec code)

    Not affecting action
    --------------------
    name - string: the name of the code (co_name)
    filename - string: the file name of the code (co_filename)
    firstlineno - int: the first line number (co_firstlineno)
    docstring - string or None: the docstring (the first item of co_consts,
                if it's str or unicode)

    code is a list of 2-tuples. The first item is an opcode, or SetLineno, or a
    Label instance. The second item is the argument, if applicable, or None.
    code can be a CodeList instance, which will produce nicer output when
    being printed.
    """
    def __init__(self, code, freevars, args, varargs, varkwargs, newlocals,
                 name, filename, firstlineno, docstring):
        self.code = code
        self.freevars = freevars
        self.args = args
        self.varargs = varargs
        self.varkwargs = varkwargs
        self.newlocals = newlocals
        self.name = name
        self.filename = filename
        self.firstlineno = firstlineno
        self.docstring = docstring

    @staticmethod
    def _findlinestarts(code):
        """Find the offsets in a byte code which are start of lines in the
        source.

        Generate pairs (offset, lineno) as described in Python/compile.c.

        This is a modified version of dis.findlinestarts, which allows multiple
        "line starts" with the same line number.
        """
        byte_increments = [ord(c) for c in code.co_lnotab[0::2]]
        line_increments = [ord(c) for c in code.co_lnotab[1::2]]

        lineno = code.co_firstlineno
        addr = 0
        for byte_incr, line_incr in zip(byte_increments, line_increments):
            if byte_incr:
                yield (addr, lineno)
                addr += byte_incr
            lineno += line_incr
        yield (addr, lineno)

    @classmethod
    def from_code(cls, co):
        """Disassemble a Python code object into a Code object."""
        co_code = co.co_code
        labels = dict((addr, Label()) for addr in findlabels(co_code))
        linestarts = dict(cls._findlinestarts(co))
        cellfree = co.co_cellvars + co.co_freevars

        code = CodeList()
        n = len(co_code)
        i = 0
        extended_arg = 0
        while i < n:
            op = Opcode(ord(co_code[i]))
            if i in labels:
                code.append((labels[i], None))
            if i in linestarts:
                code.append((SetLineno, linestarts[i]))
            i += 1
            if op in hascode:
                lastop, lastarg = code[-1]
                if lastop != LOAD_CONST:
                    raise ValueError, \
                          "%s should be preceded by LOAD_CONST code" % op
                code[-1] = (LOAD_CONST, Code.from_code(lastarg))
            if op not in hasarg:
                code.append((op, None))
            else:
                arg = ord(co_code[i]) + ord(co_code[i+1])*256 + extended_arg
                extended_arg = 0
                i += 2
                if op == opcode.EXTENDED_ARG:
                    extended_arg = arg << 16
                elif op in hasconst:
                    code.append((op, co.co_consts[arg]))
                elif op in hasname:
                    code.append((op, co.co_names[arg]))
                elif op in hasjabs:
                    code.append((op, labels[arg]))
                elif op in hasjrel:
                    code.append((op, labels[i + arg]))
                elif op in haslocal:
                    code.append((op, co.co_varnames[arg]))
                elif op in hascompare:
                    code.append((op, cmp_op[arg]))
                elif op in hasfree:
                    code.append((op, cellfree[arg]))
                else:
                    code.append((op, arg))

        varargs = bool(co.co_flags & CO_VARARGS)
        varkwargs = bool(co.co_flags & CO_VARKEYWORDS)
        newlocals = bool(co.co_flags & CO_NEWLOCALS)
        args = co.co_varnames[:co.co_argcount + varargs + varkwargs]
        if co.co_consts and isinstance(co.co_consts[0], basestring):
            docstring = co.co_consts[0]
        else:
            docstring = None
        return cls(code = code,
                   freevars = co.co_freevars,
                   args = args,
                   varargs = varargs,
                   varkwargs = varkwargs,
                   newlocals = newlocals,
                   name = co.co_name,
                   filename = co.co_filename,
                   firstlineno = co.co_firstlineno,
                   docstring = docstring,
                   )

    def __eq__(self, other):
        if (self.freevars != other.freevars or
            self.args != other.args or
            self.varargs != other.varargs or
            self.varkwargs != other.varkwargs or
            self.newlocals != other.newlocals or
            self.name != other.name or
            self.filename != other.filename or
            self.firstlineno != other.firstlineno or
            self.docstring != other.docstring or
            len(self.code) != len(other.code)
            ):
            return False

        # Compare code. This isn't trivial because labels should be matching,
        # not equal.
        labelmapping = {}
        for (op1, arg1), (op2, arg2) in itertools.izip(self.code, other.code):
            if isinstance(op1, Label):
                if labelmapping.setdefault(op1, op2) is not op2:
                    return False
            else:
                if op1 != op2:
                    return False
                if op1 in hasjump:
                    if labelmapping.setdefault(arg1, arg2) is not arg2:
                        return False
                elif op1 in hasarg:
                    if arg1 != arg2:
                        return False
        return True

    def _compute_flags(self):
        opcodes = set(op for op, arg in self.code if isopcode(op))

        optimized = (STORE_NAME not in opcodes and
                     LOAD_NAME not in opcodes and
                     DELETE_NAME not in opcodes)
        generator = (YIELD_VALUE in opcodes)
        nofree = not (opcodes.intersection(hasfree))

        flags = 0
        if optimized: flags |= CO_OPTIMIZED
        if self.newlocals: flags |= CO_NEWLOCALS
        if self.varargs: flags |= CO_VARARGS
        if self.varkwargs: flags |= CO_VARKEYWORDS
        if generator: flags |= CO_GENERATOR
        if nofree: flags |= CO_NOFREE
        return flags

    def _compute_stacksize(self):
        """Get a code list, compute its maximal stack usage."""
        # This is done by scanning the code, and computing for each opcode
        # the stack state at the opcode.
        code = self.code

        # A mapping from labels to their positions in the code list
        label_pos = dict((op, pos)
                         for pos, (op, arg) in enumerate(code)
                         if isinstance(op, Label))

        # sf_targets are the targets of SETUP_FINALLY opcodes. They are recorded
        # because they have special stack behaviour. If an exception was raised
        # in the block pushed by a SETUP_FINALLY opcode, the block is popped
        # and 3 objects are pushed. On return or continue, the block is popped
        # and 2 objects are pushed. If nothing happened, the block is popped by
        # a POP_BLOCK opcode and 1 object is pushed by a (LOAD_CONST, None)
        # operation.
        #
        # Our solution is to record the stack state of SETUP_FINALLY targets
        # as having 3 objects pushed, which is the maximum. However, to make
        # stack recording consistent, the get_next_stacks function will always
        # yield the stack state of the target as if 1 object was pushed, but
        # this will be corrected in the actual stack recording.

        sf_targets = set(label_pos[arg]
                         for op, arg in code
                         if op == SETUP_FINALLY)

        # What we compute - for each opcode, its stack state, as an n-tuple.
        # n is the number of blocks pushed. For each block, we record the number
        # of objects pushed.
        stacks = [None] * len(code)

        def get_next_stacks(pos, curstack):
            """Get a code position and the stack state before the operation
            was done, and yield pairs (pos, curstack) for the next positions
            to be explored - those are the positions to which you can get
            from the given (pos, curstack).

            If the given position was already explored, nothing will be yielded.
            """
            op, arg = code[pos]

            if isinstance(op, Label):
                # We should check if we already reached a node only if it is
                # a label.
                if pos in sf_targets:
                    curstack = curstack[:-1] + (curstack[-1] + 2,)
                if stacks[pos] is None:
                    stacks[pos] = curstack
                else:
                    if stacks[pos] != curstack:
                        raise ValueError, "Inconsistent code"
                    return

            def newstack(n):
                # Return a new stack, modified by adding n elements to the last
                # block
                if curstack[-1] + n < 0:
                    raise ValueError, "Popped a non-existing element"
                return curstack[:-1] + (curstack[-1]+n,)

            if not isopcode(op):
                # label or SetLineno - just continue to next line
                yield pos+1, curstack

            elif op in (STOP_CODE, RETURN_VALUE, RAISE_VARARGS):
                # No place in particular to continue to
                pass

            elif op == MAKE_CLOSURE and python_version == '2.4':
                # This is only relevant in Python 2.4 - in Python 2.5 the stack
                # effect of MAKE_CLOSURE can be calculated from the arg.
                # In Python 2.4, it depends on the number of freevars of TOS,
                # which should be a code object.
                if pos == 0:
                    raise ValueError, \
                          "MAKE_CLOSURE can't be the first opcode"
                lastop, lastarg = code[pos-1]
                if lastop != LOAD_CONST:
                    raise ValueError, \
                          "MAKE_CLOSURE should come after a LOAD_CONST op"
                try:
                    nextrapops = len(lastarg.freevars)
                except AttributeError:
                    try:
                        nextrapops = len(lastarg.co_freevars)
                    except AttributeError:
                        raise ValueError, \
                              "MAKE_CLOSURE preceding const should "\
                              "be a code or a Code object"

                yield pos+1, newstack(-arg-nextrapops)

            elif op not in hasflow:
                # Simple change of stack
                pop, push = getse(op, arg)
                yield pos+1, newstack(push - pop)

            elif op in (JUMP_FORWARD, JUMP_ABSOLUTE):
                # One possibility for a jump
                yield label_pos[arg], curstack

            elif op in (JUMP_IF_FALSE, JUMP_IF_TRUE):
                # Two possibilities for a jump
                yield label_pos[arg], curstack
                yield pos+1, curstack

            elif op == FOR_ITER:
                # FOR_ITER pushes next(TOS) on success, and pops TOS and jumps
                # on failure
                yield label_pos[arg], newstack(-1)
                yield pos+1, newstack(1)

            elif op == BREAK_LOOP:
                # BREAK_LOOP jumps to a place specified on block creation, so
                # it is ignored here
                pass

            elif op == CONTINUE_LOOP:
                # CONTINUE_LOOP jumps to the beginning of a loop which should
                # already ave been discovered, but we verify anyway.
                # It pops a block.
                yield label_pos[arg], curstack[:-1]

            elif op == SETUP_LOOP:
                # We continue with a new block.
                # On break, we jump to the label and return to current stack
                # state.
                yield pos+1, curstack + (0,)
                yield label_pos[arg], curstack

            elif op == SETUP_EXCEPT:
                # We continue with a new block.
                # On exception, we jump to the label with 3 extra objects on
                # stack
                yield pos+1, curstack + (0,)
                yield label_pos[arg], newstack(3)

            elif op == SETUP_FINALLY:
                # We continue with a new block.
                # On exception, we jump to the label with 3 extra objects on
                # stack, but to keep stack recording consistent, we behave as
                # if we add only 1 object. Extra 2 will be added to the actual
                # recording.
                yield pos+1, curstack + (0,)
                yield label_pos[arg], newstack(1)

            elif op == POP_BLOCK:
                # Just pop the block
                yield pos+1, curstack[:-1]

            elif op == END_FINALLY:
                # Since stack recording of SETUP_FINALLY targets is of 3 pushed
                # objects (as when an exception is raised), we pop 3 objects.
                yield pos+1, newstack(-3)

            elif op == WITH_CLEANUP:
                # Since WITH_CLEANUP is always found after SETUP_FINALLY
                # targets, and the stack recording is that of a raised
                # exception, we can simply pop 1 object and let END_FINALLY
                # pop the remaining 3.
                yield pos+1, newstack(-1)

            else:
                assert False, "Unhandled opcode: %r" % op

        # Now comes the calculation: open_positions holds positions which are
        # yet to be explored. In each step we take one open position, and
        # explore it by adding the positions to which you can get from it, to
        # open_positions. On the way, we update maxsize.
        # open_positions is a list of tuples: (pos, stack state)
        maxsize = 0
        open_positions = [(0, (0,))]
        while open_positions:
            pos, curstack = open_positions.pop()
            maxsize = max(maxsize, sum(curstack))
            open_positions.extend(get_next_stacks(pos, curstack))

        return maxsize

    def to_code(self):
        """Assemble a Python code object from a Code object."""
        co_argcount = len(self.args) - self.varargs - self.varkwargs
        co_stacksize = self._compute_stacksize()
        co_flags = self._compute_flags()

        co_consts = [self.docstring]
        co_names = []
        co_varnames = list(self.args)

        co_freevars = tuple(self.freevars)

        # We find all cellvars beforehand, for two reasons:
        # 1. We need the number of them to construct the numeric argument
        #    for ops in "hasfree".
        # 2. We need to put arguments which are cell vars in the beginning
        #    of co_cellvars
        cellvars = set(arg for op, arg in self.code
                       if isopcode(op) and op in hasfree
                       and arg not in co_freevars)
        co_cellvars = [x for x in self.args if x in cellvars]

        def index(seq, item, eq=operator.eq, can_append=True):
            """Find the index of item in a sequence and return it.
            If it is not found in the sequence, and can_append is True,
            it is appended to the sequence.

            eq is the equality operator to use.
            """
            for i, x in enumerate(seq):
                if eq(x, item):
                    return i
            else:
                if can_append:
                    seq.append(item)
                    return len(seq) - 1
                else:
                    raise IndexError, "Item not found"

        # List of tuples (pos, label) to be filled later
        jumps = []
        # A mapping from a label to its position
        label_pos = {}
        # Last SetLineno
        lastlineno = self.firstlineno
        lastlinepos = 0

        co_code = array('B')
        co_lnotab = array('B')
        for i, (op, arg) in enumerate(self.code):
            if isinstance(op, Label):
                label_pos[op] = len(co_code)

            elif op is SetLineno:
                incr_lineno = arg - lastlineno
                incr_pos = len(co_code) - lastlinepos
                lastlineno = arg
                lastlinepos = len(co_code)

                if incr_lineno == 0 and incr_pos == 0:
                    co_lnotab.append(0)
                    co_lnotab.append(0)
                else:
                    while incr_pos > 255:
                        co_lnotab.append(255)
                        co_lnotab.append(0)
                        incr_pos -= 255
                    while incr_lineno > 255:
                        co_lnotab.append(incr_pos)
                        co_lnotab.append(255)
                        incr_pos = 0
                        incr_lineno -= 255
                    if incr_pos or incr_lineno:
                        co_lnotab.append(incr_pos)
                        co_lnotab.append(incr_lineno)

            elif op == opcode.EXTENDED_ARG:
                raise ValueError, "EXTENDED_ARG not supported in Code objects"

            elif not op in hasarg:
                co_code.append(op)

            else:
                if op in hasconst:
                    if isinstance(arg, Code) and i < len(self.code)-1 and \
                       self.code[i+1][0] in hascode:
                        arg = arg.to_code()
                    arg = index(co_consts, arg, operator.is_)
                elif op in hasname:
                    arg = index(co_names, arg)
                elif op in hasjump:
                    # arg will be filled later
                    jumps.append((len(co_code), arg))
                    arg = 0
                elif op in haslocal:
                    arg = index(co_varnames, arg)
                elif op in hascompare:
                    arg = index(cmp_op, arg, can_append=False)
                elif op in hasfree:
                    try:
                        arg = index(co_freevars, arg, can_append=False) \
                              + len(cellvars)
                    except IndexError:
                        arg = index(co_cellvars, arg)
                else:
                    # arg is ok
                    pass

                if arg > 0xFFFF:
                    co_code.append(opcode.EXTENDED_ARG)
                    co_code.append((arg >> 16) & 0xFF)
                    co_code.append((arg >> 24) & 0xFF)
                co_code.append(op)
                co_code.append(arg & 0xFF)
                co_code.append((arg >> 8) & 0xFF)

        for pos, label in jumps:
            jump = label_pos[label]
            if co_code[pos] in hasjrel:
                jump -= pos+3
            if jump > 0xFFFF:
                raise NotImplementedError, "Extended jumps not implemented"
            co_code[pos+1] = jump & 0xFF
            co_code[pos+2] = (jump >> 8) & 0xFF

        co_code = co_code.tostring()
        co_lnotab = co_lnotab.tostring()

        co_consts = tuple(co_consts)
        co_names = tuple(co_names)
        co_varnames = tuple(co_varnames)
        co_nlocals = len(co_varnames)
        co_cellvars = tuple(co_cellvars)

        return new.code(co_argcount, co_nlocals, co_stacksize, co_flags,
                        co_code, co_consts, co_names, co_varnames,
                        self.filename, self.name, self.firstlineno, co_lnotab,
                        co_freevars, co_cellvars)

def printcodelist(codelist, to=sys.stdout):
    """Get a code list. Print it nicely."""

    labeldict = {}
    pendinglabels = []
    for i, (op, arg) in enumerate(codelist):
        if isinstance(op, Label):
            pendinglabels.append(op)
        elif op is SetLineno:
            pass
        else:
            while pendinglabels:
                labeldict[pendinglabels.pop()] = i

    lineno = None
    islabel = False
    for i, (op, arg) in enumerate(codelist):
        if op is SetLineno:
            lineno = arg
            print >> to
            continue

        if isinstance(op, Label):
            islabel = True
            continue

        if lineno is None:
            linenostr = ''
        else:
            linenostr = str(lineno)
            lineno = None

        if islabel:
            islabelstr = '>>'
            islabel = False
        else:
            islabelstr = ''

        if op in hasconst:
            argstr = repr(arg)
        elif op in hasjump:
            try:
                argstr = 'to ' + str(labeldict[arg])
            except KeyError:
                argstr = repr(arg)
        elif op in hasarg:
            argstr = str(arg)
        else:
            argstr = ''

        print >> to, '%3s     %2s %4d %-20s %s' % (
            linenostr,
            islabelstr,
            i,
            op,
            argstr)

def install():
    """Install byteplay to automatically reassemble all functions when a module
    is imported.

    This is useful for testing.
    """
    import gc
    import __builtin__
    import sys
    import atexit

    orig_importer = __builtin__.__import__
    reassembled_ids = set()

    def reassemble_all_funcs():
        """Reassemble all function objects not reassembled before.

        We store ids of already reassembled functions, so a function won't
        be reassembled if another function with the same id was already
        reassembled.
        """
        funcs = [x for x in gc.get_objects()
                 if isinstance(x, new.function)
                 and id(x) not in reassembled_ids
                 ]
        for f in funcs:
            f.func_code = Code.from_code(f.func_code).to_code()
            reassembled_ids.add(id(f))

    def patched_importer(*args, **kwargs):
        prevlen = len(sys.modules)
        r = orig_importer(*args, **kwargs)
        curlen = len(sys.modules)
        if curlen > prevlen:
            reassemble_all_funcs()
        return r

    def print_howmany_reassembled():
        print >> sys.stderr, "Reassembled %d functions." % len(reassembled_ids)

    __builtin__.__import__ = patched_importer
    atexit.register(print_howmany_reassembled)
    reassemble_all_funcs()

# To test byteplay, do this:
#
# >>> import byteplay
# >>> byteplay.install()
# >>> from test.regrtest import main
# >>> main()
#
# It may raise some FutureWarnings, but that's expected.
# Tip: before doing this, check to see what tests fail even without
# doing byteplay.install()...


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title>Uses of Package org.apache.hadoop.hbase.client (HBase 0.98.7-hadoop2 API)</title>
<meta name="date" content="2014-10-08">
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev

Next

Frames

No Frames

All Classes

Uses of Package
org.apache.hadoop.hbase.client

Packages that use org.apache.hadoop.hbase.client

Package
Description

org.apache.hadoop.hbase

org.apache.hadoop.hbase.client

Provides HBase Client

org.apache.hadoop.hbase.mapred

Provides HBase MapReduce
Input/OutputFormats, a table indexing MapReduce job, and utility

org.apache.hadoop.hbase.mapreduce

Provides HBase MapReduce
Input/OutputFormats, a table indexing MapReduce job, and utility

org.apache.hadoop.hbase.rest.client

Classes in org.apache.hadoop.hbase.client used by org.apache.hadoop.hbase

Class and Description

Durability

Enum describing the durability guarantees for tables and Mutations
 Note that the items must be sorted in order of increasing durability

Result

Single row result of a Get or Scan query.

Classes in org.apache.hadoop.hbase.client used by org.apache.hadoop.hbase.client

Class and Description

Append

Performs Append operations on a single row.

Attributes

ClientScanner

Implements the scanner interface for the HBase client.

Delete

Used to perform Delete operations on a single row.

Durability

Enum describing the durability guarantees for tables and Mutations
 Note that the items must be sorted in order of increasing durability

Get

Used to perform Get operations on a single row.

HConnection

A cluster connection.

HTableInterface

Used to communicate with a single HBase table.

HTableInterfaceFactory

Defines methods to create new HTableInterface.

Increment

Used to perform Increment operations on a single row.

IsolationLevel

Specify Isolation levels in Scan operations.

Mutation

Operation

Superclass for any type that maps to a potentially application-level query.

OperationWithAttributes

Put

Used to perform Put operations for a single row.

Query

Result

Single row result of a Get or Scan query.

ResultScanner

Interface for client-side scanning.

RetriesExhaustedException

Exception thrown by HTable methods when an attempt to do something (like
 commit changes) fails after a bunch of retries.

RetriesExhaustedWithDetailsException

This subclass of RetriesExhaustedException
 is thrown when we have more information about which rows were causing which
 exceptions on what servers.

ReversedClientScanner

A reversed client scanner which support backward scanning

Row

Has a row.

RowMutations

Performs multiple mutations atomically on a single row.

Scan

Used to perform Scan operations.

Classes in org.apache.hadoop.hbase.client used by org.apache.hadoop.hbase.mapred

Class and Description

Put

Used to perform Put operations for a single row.

Result

Single row result of a Get or Scan query.

Classes in org.apache.hadoop.hbase.client used by org.apache.hadoop.hbase.mapreduce

Class and Description

HConnection

A cluster connection.

HTable

Used to communicate with a single HBase table.

Mutation

Put

Used to perform Put operations for a single row.

Result

Single row result of a Get or Scan query.

Scan

Used to perform Scan operations.

Classes in org.apache.hadoop.hbase.client used by org.apache.hadoop.hbase.rest.client

Class and Description

Append

Performs Append operations on a single row.

Delete

Used to perform Delete operations on a single row.

Durability

Enum describing the durability guarantees for tables and Mutations
 Note that the items must be sorted in order of increasing durability

Get

Used to perform Get operations on a single row.

HTableInterface

Used to communicate with a single HBase table.

Increment

Used to perform Increment operations on a single row.

Put

Used to perform Put operations for a single row.

Result

Single row result of a Get or Scan query.

ResultScanner

Interface for client-side scanning.

Row

Has a row.

RowMutations

Performs multiple mutations atomically on a single row.

Scan

Used to perform Scan operations.

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev

Next

Frames

No Frames

All Classes

Copyright © 2014 The Apache Software Foundation. All rights reserved.

redpajama_set_name: RedPajamaGithub


text:
module MediaWiktory::Wikipedia
  module Modules
    # Get deleted revision information.
    #
    # The "submodule" (MediaWiki API term) is included in action after setting some param, providing
    # additional tweaking for this param. Example (for {MediaWiktory::Wikipedia::Actions::Query} and
    # its submodules):
    #
    # ```ruby
    # api.query             # returns Actions::Query
    #    .prop(:revisions)  # adds prop=revisions to action URL, and includes Modules::Revisions into action
    #    .limit(10)         # method of Modules::Revisions, adds rvlimit=10 to URL
    # ```
    #
    # All submodule's parameters are documented as its public methods, see below.
    #
    module Deletedrevisions

      # Which properties to get for each revision:
      #
      # @param values [Array<String>] Allowed values: "ids" (The ID of the revision), "flags" (Revision flags (minor)), "timestamp" (The timestamp of the revision), "user" (User that made the revision), "userid" (User ID of the revision creator), "size" (Length (bytes) of the revision), "sha1" (SHA-1 (base 16) of the revision), "contentmodel" (Content model ID of the revision), "comment" (Comment by the user for the revision), "parsedcomment" (Parsed comment by the user for the revision), "content" (Text of the revision), "tags" (Tags for the revision), "parsetree" (The XML parse tree of revision content (requires content model wikitext)).
      # @return [self]
      def prop(*values)
        values.inject(self) { |res, val| res._prop(val) or fail ArgumentError, "Unknown value for prop: #{val}" }
      end

      # @private
      def _prop(value)
        defined?(super) && super || ["ids", "flags", "timestamp", "user", "userid", "size", "sha1", "contentmodel", "comment", "parsedcomment", "content", "tags", "parsetree"].include?(value.to_s) && merge(drvprop: value.to_s, replace: false)
      end

      # Limit how many revisions will be returned.
      #
      # @param value [Integer, "max"]
      # @return [self]
      def limit(value)
        merge(drvlimit: value.to_s)
      end

      # Expand templates in revision content (requires drvprop=content).
      #
      # @return [self]
      def expandtemplates()
        merge(drvexpandtemplates: 'true')
      end

      # Generate XML parse tree for revision content (requires drvprop=content; replaced by drvprop=parsetree).
      #
      # @return [self]
      def generatexml()
        merge(drvgeneratexml: 'true')
      end

      # Parse revision content (requires drvprop=content). For performance reasons, if this option is used, drvlimit is enforced to 1.
      #
      # @return [self]
      def parse()
        merge(drvparse: 'true')
      end

      # Only retrieve the content of this section number.
      #
      # @param value [String]
      # @return [self]
      def section(value)
        merge(drvsection: value.to_s)
      end

      # Revision ID to diff each revision to. Use prev, next and cur for the previous, next and current revision respectively.
      #
      # @param value [String]
      # @return [self]
      def diffto(value)
        merge(drvdiffto: value.to_s)
      end

      # Text to diff each revision to. Only diffs a limited number of revisions. Overrides drvdiffto. If drvsection is set, only that section will be diffed against this text.
      #
      # @param value [String]
      # @return [self]
      def difftotext(value)
        merge(drvdifftotext: value.to_s)
      end

      # Perform a pre-save transform on the text before diffing it. Only valid when used with drvdifftotext.
      #
      # @return [self]
      def difftotextpst()
        merge(drvdifftotextpst: 'true')
      end

      # Serialization format used for drvdifftotext and expected for output of content.
      #
      # @param value [String] One of "application/json", "text/x-wiki", "text/javascript", "text/css", "text/plain".
      # @return [self]
      def contentformat(value)
        _contentformat(value) or fail ArgumentError, "Unknown value for contentformat: #{value}"
      end

      # @private
      def _contentformat(value)
        defined?(super) && super || ["application/json", "text/x-wiki", "text/javascript", "text/css", "text/plain"].include?(value.to_s) && merge(drvcontentformat: value.to_s)
      end

      # The timestamp to start enumerating from. Ignored when processing a list of revision IDs.
      #
      # @param value [Time]
      # @return [self]
      def start(value)
        merge(drvstart: value.iso8601)
      end

      # The timestamp to stop enumerating at. Ignored when processing a list of revision IDs.
      #
      # @param value [Time]
      # @return [self]
      def end(value)
        merge(drvend: value.iso8601)
      end

      # In which direction to enumerate:
      #
      # @param value [String] One of "newer" (List oldest first. Note: drvstart has to be before drvend), "older" (List newest first (default). Note: drvstart has to be later than drvend).
      # @return [self]
      def dir(value)
        _dir(value) or fail ArgumentError, "Unknown value for dir: #{value}"
      end

      # @private
      def _dir(value)
        defined?(super) && super || ["newer", "older"].include?(value.to_s) && merge(drvdir: value.to_s)
      end

      # Only list revisions tagged with this tag.
      #
      # @param value [String]
      # @return [self]
      def tag(value)
        merge(drvtag: value.to_s)
      end

      # Only list revisions by this user.
      #
      # @param value [String]
      # @return [self]
      def user(value)
        merge(drvuser: value.to_s)
      end

      # Don't list revisions by this user.
      #
      # @param value [String]
      # @return [self]
      def excludeuser(value)
        merge(drvexcludeuser: value.to_s)
      end

      # When more results are available, use this to continue.
      #
      # @param value [String]
      # @return [self]
      def continue(value)
        merge(drvcontinue: value.to_s)
      end
    end
  end
end


text: docker build -t fhem-rpi-jessie .


text:
package org.apache.struts2.tiles;

import org.apache.tiles.TilesApplicationContext;
import org.apache.tiles.TilesException;
import org.apache.tiles.context.TilesContextFactory;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.definition.DefinitionsFactory;
import org.apache.tiles.factory.TilesContainerFactory;
import org.apache.tiles.impl.BasicTilesContainer;
import org.apache.tiles.preparer.PreparerFactory;

import java.util.Map;

public class StrutsTilesContainerFactory extends TilesContainerFactory {

    @Override
    protected void storeContainerDependencies(Object context, Map<String, String> initParameters, Map<String, String> configuration, BasicTilesContainer container) throws TilesException {
        TilesContextFactory contextFactory =
            (TilesContextFactory) createFactory(configuration,
                CONTEXT_FACTORY_INIT_PARAM);

        contextFactory = new StrutsTilesContextFactory(contextFactory);

        DefinitionsFactory defsFactory =
            (DefinitionsFactory) createFactory(configuration,
                DEFINITIONS_FACTORY_INIT_PARAM);

        PreparerFactory prepFactory =
            (PreparerFactory) createFactory(configuration,
                PREPARER_FACTORY_INIT_PARAM);

        contextFactory.init(configuration);
        TilesApplicationContext tilesContext =
            contextFactory.createApplicationContext(context);

        container.setDefinitionsFactory(defsFactory);
        container.setContextFactory(contextFactory);
        container.setPreparerFactory(prepFactory);
        container.setApplicationContext(tilesContext);
    }

    /**
     * Wrapper factory, used to decorate the TilesRequestContext with a
     * FreemarkerResult aware version.
     *
     */
    class StrutsTilesContextFactory implements TilesContextFactory {

        private TilesContextFactory factory;

        public StrutsTilesContextFactory(TilesContextFactory factory) {
            this.factory = factory;
        }

        public void init(Map<String, String> map) {
            factory.init(map);
        }

        public TilesApplicationContext createApplicationContext(Object context) {
            return factory.createApplicationContext(context);
        }

        public TilesRequestContext createRequestContext(
                TilesApplicationContext tilesApplicationContext,
                Object... requestItems) {
            TilesRequestContext context = factory.createRequestContext(tilesApplicationContext, requestItems);
            return new StrutsTilesRequestContext(context);
        }
    }
}


text:
package org.apache.axis2.jaxws.sample;

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

import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.axis2.jaxws.TestLogger;
import org.apache.axis2.jaxws.framework.AbstractTestCase;
import org.apache.axis2.jaxws.sample.dlwminArrays.IGenericService;
import org.apache.axis2.jaxws.sample.dlwminArrays.WSUser;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Holder;
import javax.xml.ws.Service;

/**
 *
 * Tests to verify Document/Literal Wrapped Minimal Scenarios
 * Document/Literal Wrapped is a JAX-WS style.
 * "Minimal" indicates that no wrapper beans are associated with the JAX-WS method.
 * In most enterprise scenarios, wrapper beans are packaged with the JAX-WS application.
 */
public class DLWMinArrayTests extends AbstractTestCase {

    private static final String NAMESPACE = "http://apache.org/axis2/jaxws/sample/dlwminArrays";
    private static final QName QNAME_SERVICE = new QName(
            NAMESPACE, "GenericService");
    private static final QName QNAME_PORT = new QName(
            NAMESPACE, "GenericServicePort");
    private static final String URL_ENDPOINT = "http://localhost:6060/axis2/services/GenericService.GenericServicePort";

    private static String FIRST = "first";
    private static String SECOND = "second";

    public static Test suite() {
        return getTestSetup(new TestSuite(DLWMinArrayTests.class));
    }

    private IGenericService getProxy(String action) {
        Service service = Service.create(QNAME_SERVICE);
        IGenericService proxy = service.getPort(QNAME_PORT, IGenericService.class);
        BindingProvider p = (BindingProvider) proxy;
        p.getRequestContext().put(
                BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
        p.getRequestContext().put(
                BindingProvider.SOAPACTION_URI_PROPERTY, action);
        p.getRequestContext().put(
                BindingProvider.ENDPOINT_ADDRESS_PROPERTY, URL_ENDPOINT);
        return proxy;
    }

    /**
     * Test sayHello method
     */
    public void testHello() throws Exception {

        IGenericService proxy = getProxy("sayHello");

        String me = "Scheu";
        String response = proxy.sayHello(me);
        assertTrue("Hello Scheu".equals(response));

        // Try the call again
        response = proxy.sayHello(me);
        assertTrue("Hello Scheu".equals(response));
    }

    /**
     * Test method that returns a String[]
     */
    public void testGetSimpleArray() throws Exception {

        IGenericService proxy = getProxy("getSimpleArray");

        String[] response = proxy.getSimpleArray();
        assertTrue(response != null);
        assertTrue("Expected 2, Received " + response.length, response.length == 2);
        assertTrue("The first response is: " + response[0], response.length == 2);
        assertTrue(FIRST.equals(response[0]));
        assertTrue(SECOND.equals(response[1]));

        // Try the call again
        response = proxy.getSimpleArray();
        assertTrue(response != null);
        assertTrue(response.length == 2);
        assertTrue(FIRST.equals(response[0]));
        assertTrue(SECOND.equals(response[1]));
    }

    /**
     * Test method that returns a List<String>
     */
    public void testGetSimpleList() throws Exception {

        IGenericService proxy = getProxy("getSimpleList");

        List<String> response = proxy.getSimpleList();
        assertTrue(response != null);
        assertTrue(response.size() == 2);
        assertTrue(FIRST.equals(response.get(0)));
        assertTrue(SECOND.equals(response.get(1)));

        // Try the call again
        response = proxy.getSimpleList();
        assertTrue(response != null);
        assertTrue(response.size() == 2);
        assertTrue(FIRST.equals(response.get(0)));
        assertTrue(SECOND.equals(response.get(1)));
    }

    /**
     * Test method that returns a bean array (WSUser[])
     */
    public void testGetComplexArray() throws Exception {

        IGenericService proxy = getProxy("getComplexArray");

        WSUser[] response = proxy.getComplexArray();
        assertTrue(response != null);
        assertTrue("Expected 2, Received " + response.length, response.length == 2);
        assertTrue(FIRST.equals(response[0].getUserID()));
        assertTrue(SECOND.equals(response[1].getUserID()));

        // Try the call again
        response = proxy.getComplexArray();
        assertTrue(response != null);
        assertTrue(response.length == 2);
        assertTrue(FIRST.equals(response[0].getUserID()));
        assertTrue(SECOND.equals(response[1].getUserID()));
    }

    /**
     * Test method that returns a List of beans (List<WSUser>)
     */
    public void testGetComplexList() throws Exception {

        IGenericService proxy = getProxy("getComplexList");

        List<WSUser> response = proxy.getComplexList();
        assertTrue(response != null);
        assertTrue(response.size() == 2);
        assertTrue(FIRST.equals(response.get(0).getUserID()));
        assertTrue(SECOND.equals(response.get(1).getUserID()));

        // Try the call again
        response = proxy.getComplexList();
        assertTrue(response != null);
        assertTrue(response.size() == 2);
        assertTrue(FIRST.equals(response.get(0).getUserID()));
        assertTrue(SECOND.equals(response.get(1).getUserID()));
    }

    /**
     * Test method that echos a List of beans (List<WSUser>)
     * Two items are echo'd.
     */
    public void testEchoComplexList2() throws Exception {

        IGenericService proxy = getProxy("echoComplexList");

        List<WSUser> in = new ArrayList<WSUser>();
        WSUser wsUser = new WSUser();
        wsUser.setUserID("Hello");
        in.add(wsUser);
        wsUser = new WSUser();
        wsUser.setUserID("World");
        in.add(wsUser);

        List<WSUser> response = proxy.echoComplexList(in);
        assertTrue(response != null);
        assertTrue(response.size() == 2);
        assertTrue("Hello".equals(response.get(0).getUserID()));
        assertTrue("World".equals(response.get(1).getUserID()));

        // Try the call again
        response = proxy.echoComplexList(in);
        assertTrue(response != null);
        assertTrue(response.size() == 2);
        assertTrue("Hello".equals(response.get(0).getUserID()));
        assertTrue("World".equals(response.get(1).getUserID()));
    }

    /**
     * Test method that echos a List of beans (List<WSUser>)
     * One item is echo'd.
     */
    public void testEchoComplexList1() throws Exception {

        IGenericService proxy = getProxy("echoComplexList");

        List<WSUser> in = new ArrayList<WSUser>();
        WSUser wsUser = new WSUser();
        wsUser.setUserID("Hello");
        in.add(wsUser);

        List<WSUser> response = proxy.echoComplexList(in);
        assertTrue(response != null);
        assertTrue(response.size() == 1);
        assertTrue("Hello".equals(response.get(0).getUserID()));

        // Try the call again
        response = proxy.echoComplexList(in);
        assertTrue(response != null);
        assertTrue(response.size() == 1);
        assertTrue("Hello".equals(response.get(0).getUserID()));
    }

    /**
     * Test method that echos a List of beans (List<WSUser>)
     * The list contains no items.
     */
    public void testEchoComplexList0() throws Exception {

        IGenericService proxy = getProxy("echoComplexList");

        List<WSUser> in = new ArrayList<WSUser>();

        List<WSUser> response = proxy.echoComplexList(in);
        assertTrue(response != null);
        assertTrue(response.size() == 0);

        // Try the call again
        response = proxy.echoComplexList(in);
        assertTrue(response != null);
        assertTrue(response.size() == 0);

    }

    /**
     * Test method that echos a List of beans (List<WSUser>)
     * The list contains no items.
     */
    public void testEchoComplexListNull() throws Exception {

        IGenericService proxy = getProxy("echoComplexList");

        // There really is no discernible difference between
        // an empty array and null over the wire.  Sometimes users
        // will pass in a null on the client or server.

        List<WSUser> response = proxy.echoComplexList(null);
        assertTrue(response != null);
        assertTrue(response.size() == 0);

        // Try the call again
        response = proxy.echoComplexList(null);
        assertTrue(response != null);
        assertTrue(response.size() == 0);

        // Now try force the server to return a null argument
        List<WSUser> in = new ArrayList<WSUser>();
        WSUser wsUser = new WSUser();
        wsUser.setUserID("FORCENULL");
        in.add(wsUser);

        response = proxy.echoComplexList(in);
        assertTrue(response != null);
        assertTrue(response.size() == 0);

        // Try the call again
        response = proxy.echoComplexList(in);
        assertTrue(response != null);
        assertTrue(response.size() == 0);

    }

    /**
     * Test method that echos a List of beans (List<WSUser>)
     * and echos a List<String> as an inout parameter.
     * 2 WSUsers are echo'd
     * 2 Strings are echo'd
     */
    public void testEcho22() throws Exception {

        IGenericService proxy = getProxy("echo");

        List<WSUser> in = new ArrayList<WSUser>();
        WSUser wsUser = new WSUser();
        wsUser.setUserID("James Bond");
        in.add(wsUser);
        wsUser = new WSUser();
        wsUser.setUserID("Dr. Evil");
        in.add(wsUser);

        List<String> id_in = new ArrayList<String>();
        id_in.add("jbond");
        id_in.add("evil");
        Holder<List<String>> inout = new Holder<List<String>>(id_in);

        List<WSUser> response = proxy.echo(in, inout);
        assertTrue(response != null);
        assertTrue(response.size() == 2);
        assertTrue("James Bond".equals(response.get(0).getUserID()));
        assertTrue("Dr. Evil".equals(response.get(1).getUserID()));
        List<String> id_out = inout.value;
        assertTrue(id_out.size() == 2);
        assertTrue("JBOND".equals(id_out.get(0)));
        assertTrue("EVIL".equals(id_out.get(1)));

        // Try the call again
        inout = new Holder<List<String>>(id_in);
        response = proxy.echo(in, inout);
        assertTrue(response != null);
        assertTrue(response.size() == 2);
        assertTrue("James Bond".equals(response.get(0).getUserID()));
        assertTrue("Dr. Evil".equals(response.get(1).getUserID()));
        id_out = inout.value;
        assertTrue(id_out.size() == 2);
        assertTrue("JBOND".equals(id_out.get(0)));
        assertTrue("EVIL".equals(id_out.get(1)));
    }

    /**
     * Test method that echos a List of beans (List<WSUser>)
     * and echos a List<String> as an inout parameter.
     * 1 WSUsers is echo'd
     * 1 Strings is echo'd
     */
    public void testEcho11() throws Exception {

        IGenericService proxy = getProxy("echo");

        List<WSUser> in = new ArrayList<WSUser>();
        WSUser wsUser = new WSUser();
        wsUser.setUserID("James Bond");
        in.add(wsUser);

        List<String> id_in = new ArrayList<String>();
        id_in.add("jbond");
        Holder<List<String>> inout = new Holder<List<String>>(id_in);

        List<WSUser> response = proxy.echo(in, inout);
        assertTrue(response != null);
        assertTrue(response.size() == 1);
        assertTrue("James Bond".equals(response.get(0).getUserID()));
        List<String> id_out = inout.value;
        assertTrue(id_out.size() == 1);
        assertTrue("JBOND".equals(id_out.get(0)));

        // Try the call again
        inout = new Holder<List<String>>(id_in);
        response = proxy.echo(in, inout);
        assertTrue(response != null);
        assertTrue(response.size() == 1);
        assertTrue("James Bond".equals(response.get(0).getUserID()));
        id_out = inout.value;
        assertTrue(id_out.size() == 1);
        assertTrue("JBOND".equals(id_out.get(0)));
    }

    /**
     * Test method that echos a List of beans (List<WSUser>)
     * and echos a List<String> as an inout parameter.
     * 1 WSUsers is echo'd
     * 0 Strings are echo'd
     */
    public void testEcho10() throws Exception {

        IGenericService proxy = getProxy("echo");

        List<WSUser> in = new ArrayList<WSUser>();
        WSUser wsUser = new WSUser();
        wsUser.setUserID("James Bond");
        in.add(wsUser);

        List<String> id_in = new ArrayList<String>();
        Holder<List<String>> inout = new Holder<List<String>>(id_in);

        List<WSUser> response = proxy.echo(in, inout);
        assertTrue(response != null);
        assertTrue(response.size() == 1);
        assertTrue("James Bond".equals(response.get(0).getUserID()));
        List<String> id_out = inout.value;
        assertTrue(id_out.size() == 0);

        // Try the call again
        inout = new Holder<List<String>>(id_in);
        response = proxy.echo(in, inout);
        assertTrue(response != null);
        assertTrue(response.size() == 1);
        assertTrue("James Bond".equals(response.get(0).getUserID()));
        id_out = inout.value;
        assertTrue(id_out.size() == 0);
    }

    /**
     * Test method that echos a List of beans (List<WSUser>)
     * and echos a List<String> as an inout parameter.
     * 0 WSUsers are echo'd
     * 1 Strings is echo'd
     */
    public void testEcho01() throws Exception {

        IGenericService proxy = getProxy("echo");

        List<WSUser> in = new ArrayList<WSUser>();

        List<String> id_in = new ArrayList<String>();
        id_in.add("jbond");
        Holder<List<String>> inout = new Holder<List<String>>(id_in);

        List<WSUser> response = proxy.echo(in, inout);
        assertTrue(response != null);
        assertTrue(response.size() == 0);
        List<String> id_out = inout.value;
        assertTrue(id_out.size() == 1);
        assertTrue("JBOND".equals(id_out.get(0)));

        // Try the call again
        inout = new Holder<List<String>>(id_in);
        response = proxy.echo(in, inout);
        assertTrue(response != null);
        assertTrue(response.size() == 0);
        id_out = inout.value;
        assertTrue(id_out.size() == 1);
        assertTrue("JBOND".equals(id_out.get(0)));
    }

    /**
     * Test method that echos a List of beans (List<WSUser>)
     * and echos a List<String> as an inout parameter.
     * 0 WSUsers are echo'd
     * 0 Strings are echo'd
     */
    public void testEcho00() throws Exception {

        IGenericService proxy = getProxy("echo");

        List<WSUser> in = new ArrayList<WSUser>();

        List<String> id_in = new ArrayList<String>();
        Holder<List<String>> inout = new Holder<List<String>>(id_in);

        List<WSUser> response = proxy.echo(in, inout);
        assertTrue(response != null);
        assertTrue(response.size() == 0);
        List<String> id_out = inout.value;
        assertTrue(id_out.size() == 0);

        // Try the call again
        inout = new Holder<List<String>>(id_in);
        response = proxy.echo(in, inout);
        assertTrue(response != null);
        assertTrue(response.size() == 0);
        id_out = inout.value;
        assertTrue(id_out.size() == 0);
    }
}


text:
<?php

declare(strict_types=1);

namespace Chubbyphp\Tests\Serialization\Unit\ServiceFactory;

use Chubbyphp\Mock\Call;
use Chubbyphp\Mock\MockByCallsTrait;
use Chubbyphp\Validation\Mapping\ValidationMappingProviderRegistryInterface;
use Chubbyphp\Validation\ServiceFactory\ValidatorFactory;
use Chubbyphp\Validation\ValidatorInterface;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

/**
 * @covers \Chubbyphp\Validation\ServiceFactory\ValidatorFactory
 *
 * @internal
 */
final class ValidatorFactoryTest extends TestCase
{
    use MockByCallsTrait;

    public function testInvoke(): void
    {
        /** @var ValidationMappingProviderRegistryInterface $validationMappingProviderRegistry */
        $validationMappingProviderRegistry = $this->getMockByCalls(ValidationMappingProviderRegistryInterface::class);

        /** @var ContainerInterface $container */
        $container = $this->getMockByCalls(ContainerInterface::class, [
            Call::create('has')->with(ValidationMappingProviderRegistryInterface::class)->willReturn(true),
            Call::create('get')
                ->with(ValidationMappingProviderRegistryInterface::class)
                ->willReturn($validationMappingProviderRegistry),
            Call::create('has')->with(LoggerInterface::class)->willReturn(false),
        ]);

        $factory = new ValidatorFactory();

        $service = $factory($container);

        self::assertInstanceOf(ValidatorInterface::class, $service);
    }

    public function testCallStatic(): void
    {
        /** @var ValidationMappingProviderRegistryInterface $validationMappingProviderRegistry */
        $validationMappingProviderRegistry = $this->getMockByCalls(ValidationMappingProviderRegistryInterface::class);

        /** @var ContainerInterface $container */
        $container = $this->getMockByCalls(ContainerInterface::class, [
            Call::create('has')->with(ValidationMappingProviderRegistryInterface::class.'default')->willReturn(true),
            Call::create('get')
                ->with(ValidationMappingProviderRegistryInterface::class.'default')
                ->willReturn($validationMappingProviderRegistry),
            Call::create('has')->with(LoggerInterface::class)->willReturn(false),
        ]);

        $factory = [ValidatorFactory::class, 'default'];

        $service = $factory($container);

        self::assertInstanceOf(ValidatorInterface::class, $service);
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<!--
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-->
<config id="sharpdx-dmanip" xmlns="urn:SharpGen.Config">
  <depends>sharpdx</depends>

  <namespace>SharpDX.DirectManipulation</namespace>
  <assembly>SharpDX.DirectManipulation</assembly>

  <include file="directmanipulation.h" attach="true"/>

  <naming />

  <extension>
    <context>sharpdx-dmanip-ext</context>
    <context>directmanipulation</context>

		<const from-guid="CLSID_VerticalIndicatorContent" class="SharpDX.DirectManipulation.Manager" type="System.Guid" name="VerticalIndicatorContent" visibility="public static readonly">new System.Guid("$1")</const>
		<const from-guid="CLSID_HorizontalIndicatorContent" class="SharpDX.DirectManipulation.Manager" type="System.Guid" name="HorizontalIndicatorContent" visibility="public static readonly">new System.Guid("$1")</const>
		<const from-guid="CLSID_VirtualViewportContent" class="SharpDX.DirectManipulation.Manager" type="System.Guid" name="VirtualViewportContent" visibility="public static readonly">new System.Guid("$1")</const>

		<const from-guid="CLSID_DragDropConfigurationBehavior" class="SharpDX.DirectManipulation.Manager2" type="System.Guid" name="DragDropConfigurationBehavior" visibility="public static readonly">new System.Guid("$1")</const>
		<const from-guid="CLSID_AutoScrollBehavior" class="SharpDX.DirectManipulation.Manager2" type="System.Guid" name="AutoScrollBehavior" visibility="public static readonly">new System.Guid("$1")</const>

		<const from-macro="DIRECTMANIPULATION_KEYBOARDFOCUS" class="SharpDX.DirectManipulation.PointerId" type="int" name="KeyboardFocus" />
		<const from-macro="DIRECTMANIPULATION_MOUSEFOCUS" class="SharpDX.DirectManipulation.PointerId" type="int" name="MouseFocus" />
		<const from-macro="DIRECTMANIPULATION_MINIMUM_ZOOM" class="SharpDX.DirectManipulation.PointerId" type="double" name="MinimumZoom" />

		<create class="DManip" />
		<create class="PointerId" visibility="public" />
    <context-clear />
  </extension>

  <bindings>
  </bindings>

  <mapping>
    <context>sharpdx-dmanip-ext</context>
    <context>directmanipulation</context>

    <!--
    // *****************************************************************
    // DManip Enumerations
    // *****************************************************************
    -->
    <map enum="DIRECTMANIPULATION_(.*)" name-tmp="$1" />

		<map enum="DIRECTMANIPULATION_HORIZONTALALIGNMENT" name-tmp="HorizontalAlignment" />
		<map enum="DIRECTMANIPULATION_VERTICALALIGNMENT" name-tmp="VerticalAlignment" />
		<map enum="DIRECTMANIPULATION_AUTOSCROLL_CONFIGURATION" name-tmp="AutoScrollConfiguration" />
		<map enum="DIRECTMANIPULATION_HITTEST_TYPE" name-tmp="HitTestType" />
		<map enum="DIRECTMANIPULATION_SNAPPOINT_COORDINATE" name-tmp="SnapPointCoordinate" />
		<map enum="DIRECTMANIPULATION_SNAPPOINT_TYPE" name-tmp="SnapPointType" />

		<map enum-item="DIRECTMANIPULATION_VIEWPORT_OPTIONS_AUTODISABLE" name-tmp="AutoDisable" />
		<map enum-item="DIRECTMANIPULATION_VIEWPORT_OPTIONS_MANUALUPDATE" name-tmp="ManualUpdate" />
		<map enum-item="DIRECTMANIPULATION_VIEWPORT_OPTIONS_EXPLICITHITTEST" name-tmp="ExplicitHitTest" />

		<map enum-item="DIRECTMANIPULATION_(HORIZONTAL|VERTICAL)ALIGNMENT_UNLOCKCENTER" name-tmp="UnlockCenter" />
		<map enum-item="DIRECTMANIPULATION_VIEWPORT_OPTIONS_EXPLICITHITTEST" name-tmp="ExplicitHitTest" />

		<map enum-item="DIRECTMANIPULATION_MOTION_TYPES_(TRANSLATE|CENTER)(.)" name-tmp="Translate$2" />

    <!--
    // *****************************************************************
    // DManip Structures
    // *****************************************************************
    -->

    <!--
    // *****************************************************************
    // DManip Interfaces
    // *****************************************************************
    -->
    <map interface="IDirectManipulation(.+)" name-tmp="$1" />

		<map interface="IDirectManipulationCompositor" callback-visibility="public" callback="true" callback-dual="true" />
    <map method="IDirectManipulationCompositor::.*" keep-implement-public="true" property="false" />

		<map method="IDirectManipulationContent::GetContentTransform" visibility="internal" />
		<map method="IDirectManipulationContent::GetOutputTransform" visibility="internal" />
		<map method="IDirectManipulationContent::SyncContentTransform" visibility="internal" />
		<map method="IDirectManipulationManager::CreateViewport" visibility="internal" />
		<map method="IDirectManipulationManager::CreateContent" visibility="internal" />
		<map method="IDirectManipulationManager2::CreateBehavior" visibility="internal" />

    <!--
    // *****************************************************************
    // DManip Functions
    // *****************************************************************
    -->

    <context-clear />
  </mapping>
</config>


text:
// https://github.com/apache/metamodel/blob/master/core/src/main/java/org/apache/metamodel/util/Predicate.java

namespace org.apache.metamodel.util
{
    /**
     * A predicate is a special type of {@link Func}, used typically for
     * inclusion/exclusion criteria.
     *
     * @param <E>
     */
    public interface Predicate<E> : NFunc<E, bool>
    {
    }
} // org.apache.metamodel.util NAMESPACE


text:
package biz.webgate.xpages.base;

import java.lang.reflect.Method;
import java.util.Date;

public class BaseDateBinder {

	public BaseDateBinder() {
		super();
	}

	public Date getValue(Object objCurrent, String strJavaField) {
		try {
			Method mt = objCurrent.getClass().getMethod("get" + strJavaField);
			return (Date) mt.invoke(objCurrent);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

}


text:
mysqlutilities
==============

Box's MySQL Utilities

This is a collection for various helpful scripts we've developed at box to improve MySQL. We have the following categories:

* Tcollectors For OpenTSDB; scripts for collection metrics from mysql.  Many of these can be adapted to other systems
* Nagios plugins;  Alerts for various mysql metrics

## Copyright and License

Copyright 2014 Box, Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


text:
package org.bigmouth.dao.datastore.base;

import com.google.inject.AbstractModule;

public class GuiceConfigModule extends AbstractModule{

	@Override
	protected void configure() {

	}

}


text:
module FabricRequirements {

  export var SshConfigController = controller("SshConfigController", ["$scope", "jolokia", "$templateCache", ($scope, jolokia, $templateCache) => {

    $scope.forms = {
      sshConfig: {

      }
    };

    $scope.tableTemplate = '';

    if (!$scope.requirements.sshConfiguration) {
      $scope.requirements.sshConfiguration = Fabric.createSshConfiguration();
    }
    if (!$scope.requirements.sshConfiguration.hosts) {
      $scope.requirements.sshConfiguration.hosts = <Array<Fabric.SshHostConfiguration>> [];
    }

    $scope.gridConfig = Forms.createFormGridConfiguration();

    $scope.$watch("requirements.sshConfiguration.hosts", (newValue, oldValue) => {
      if (newValue !== oldValue) {
        $scope.requirements.$dirty = true;
      }
    }, true);

    $scope.$watch("forms.sshConfig.$dirty", (newValue) => {
      if (newValue) {
        $scope.requirements.$dirty = true;
      }
    });

    $scope.noop = () => {};

    Fabric.getDtoSchema(undefined, "io.fabric8.api.SshConfiguration", jolokia, (sshConfigurationSchema) => {
      Fabric.getDtoSchema(undefined, "io.fabric8.api.SshHostConfiguration", jolokia, (hostConfigurationSchema) => {

        // Override these elements since they're passwords
        ['defaultPassword', 'defaultPassPhrase'].forEach((s) => {
          Core.pathSet(sshConfigurationSchema, ['properties', s, 'type'], 'password');
        });
        ['password', 'passPhrase'].forEach((s) => {
          Core.pathSet(hostConfigurationSchema, ['properties', s, 'type'], 'password');
        });

        // Order the form elements nicely
        sshConfigurationSchema['tabs'] = {
          'Defaults': ['defaultUsername', 'defaultPassword', 'defaultPort', 'defaultPrivateKeyFile', 'defaultPassPhrase', 'defaultPath', '*']
        };

        // We don't want the form plugin to handle this guy
        delete sshConfigurationSchema.properties.hosts;

        $scope.gridConfig.rowSchema = hostConfigurationSchema;

        // Order the columns in the hosts config nicely
        $scope.gridConfig.rowName = "host";
        $scope.gridConfig.heading = true;
        $scope.gridConfig.noDataTemplate = $templateCache.get('noDataTemplate');
        $scope.gridConfig.rowSchema.columnOrder = ['hostName', 'port', 'username', 'password', 'privateKeyFile', 'passPhrase', 'path', 'preferredAddress', 'tags'];
        $scope.gridConfig.rows = $scope.requirements.sshConfiguration.hosts;
        $scope.gridConfig.onAdd = () => {
          var answer = Fabric.createSshHostConfiguration();
          answer.hostName = 'New Host';
          return answer;
        };

        $scope.formConfig = sshConfigurationSchema;

        $scope.tableTemplate = $templateCache.get('tableTemplate');
        Core.$apply($scope);
      });
    });
  }]);
}


text:
package api

import (
	"encoding/json"
	"net/http"

	log "github.com/Sirupsen/logrus"
	"github.com/shipyard/shipyard/auth"
	"github.com/shipyard/shipyard/auth/ldap"
	"github.com/shipyard/shipyard/controller/manager"
)

func (a *Api) login(w http.ResponseWriter, r *http.Request) {
	var creds *Credentials
	if err := json.NewDecoder(r.Body).Decode(&creds); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	loginSuccessful, err := a.manager.Authenticate(creds.Username, creds.Password)
	if err != nil {
		log.Errorf("error during login for %s from %s: %s", creds.Username, r.RemoteAddr, err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if !loginSuccessful {
		log.Warnf("invalid login for %s from %s", creds.Username, r.RemoteAddr)
		http.Error(w, "invalid username/password", http.StatusForbidden)
		return
	}

	// check for ldap and autocreate for users
	if a.manager.GetAuthenticator().Name() == "ldap" {
		if a.manager.GetAuthenticator().(*ldap.LdapAuthenticator).AutocreateUsers {
			defaultAccessLevel := a.manager.GetAuthenticator().(*ldap.LdapAuthenticator).DefaultAccessLevel
			log.Debug("ldap: checking for existing user account and creating if necessary")
			// give default users readonly access to containers
			acct := &auth.Account{
				Username: creds.Username,
				Roles:    []string{defaultAccessLevel},
			}

			// check for existing account
			if _, err := a.manager.Account(creds.Username); err != nil {
				if err == manager.ErrAccountDoesNotExist {
					log.Debugf("autocreating user for ldap: username=%s access=%s", creds.Username, defaultAccessLevel)
					if err := a.manager.SaveAccount(acct); err != nil {
						log.Errorf("error autocreating ldap user %s: %s", creds.Username, err)
						http.Error(w, err.Error(), http.StatusInternalServerError)
						return
					}
				} else {
					log.Errorf("error checking user for autocreate: %s", err)
					http.Error(w, err.Error(), http.StatusInternalServerError)
					return
				}
			}

		}
	}

	// return token
	token, err := a.manager.NewAuthToken(creds.Username, r.UserAgent())
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if err := json.NewEncoder(w).Encode(token); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

func (a *Api) changePassword(w http.ResponseWriter, r *http.Request) {
	session, _ := a.manager.Store().Get(r, a.manager.StoreKey())
	var creds *Credentials
	if err := json.NewDecoder(r.Body).Decode(&creds); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	username := session.Values["username"].(string)
	if username == "" {
		http.Error(w, "unauthorized", http.StatusInternalServerError)
		return
	}
	if err := a.manager.ChangePassword(username, creds.Password); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}


text:
Puppet module for managing glusterfs.

Unlike other glusterfs modules, this one uses custom types and providers to
avoid needing multiple runs to set up interdependent resources and to
gracefully handle missing peers.

This is particularly useful when provisioning VMs with vagrant.

## WARNING

This is pretty much just a prototype at the moment. Its functionality is quite
limited and it hasn't been tested on a wide variety of environments. It meets
our fairly basic needs, but we wouldn't recommend using it in production
without hammering on it a bit first.

## Usage

The `gluster` class manages the installation and running of the
`glusterfs-server` service. Repo and package management is only implemented for
Ubuntu, however.

```puppet
class { 'gluster':
  package_ensure => 'latest',
  service_ensure => 'running',
}
```

For a full list of options see the [manifest source](manifests/init.pp).

In most cases the defaults should be suitable and a simple `include gluster`
will suffice.

### `gluster::client`

Hosts that only need a client can use `gluster::client`.

```puppet
class { 'gluster::client':
  ensure => 'latest',
}
```

### `gluster_peer`

The `gluster_peer` resource sets up peer relationships between the nodes by
running `gluster peer probe` as necessary. If a peer can't be reached, the
resource will log a warning and pretend success to avoid unnecessary failures
during cluster bootstrapping.

```puppet
gluster_peer { ['gfs1.local', 'gfs2.local']:
    ensure => present,
}
```

Peers may be removed by setting `ensure` to `absent`.

```puppet
gluster_peer { oldgfs1.local':
    ensure => absent,
}
```

To avoid continually probing localhost, `gluster_peer` ignores peers that match
the `fqdn` and `hostname` facts. Other peer names can be ignored with the
`local_peer_aliases` param, which can be useful if a host has multiple names.

```puppet
gluster_peer { ['gfs1.local', 'gfs2.local']:
    ensure             => present,
    local_peer_aliases => [$gfs_host_alias],
}
```

The `gluster_peer` resource sets up peer relationships between the nodes by
running `gluster peer probe` as necessary.

### `gluster_volume`

The `gluster_volume` resource manages a glusterfs volume. If any hosts
mentioned in the `bricks` list aren't active peers, the resource will log a
message and pretend to be in sync to avoid unnecessary failures during cluster
bootstrapping.

`Gluster_peer` resources for all hosts mentioned in the `bricks` list will be
autorequired, as will `File` resources for the parent directories of brick
paths on the local peer.

```
gluster_volume { 'volume1':
    bricks => [
        'gfs1.local:/data/brick1',
        'gfs2.local:/data/brick1',
    ],
}
```

As with `gluster_peer`, aliases for the local peer can be provided.

```
gluster_volume { 'volume1':
    local_peer_aliases => [$gfs_host_alias],
    bricks             => [
        'gfs1.local:/data/brick1',
        'gfs2.local:/data/brick1',
    ],
}
```

Currently, the only volume type parameter accepted is `replica`. (We expect to
support the others in the future.)

```
gluster_volume { 'volume1':
    replica => 2,
    bricks  => [
        'gfs1.local:/data/brick1',
        'gfs2.local:/data/brick1',
    ],
}
```

The `ensure` parameter accepts the values `present`, `absent`, and `stopped`.
`present` indicates that the volume should be running, `absent` indicates that
the volume should not exist, and `stopped` indicates that the volume should
exist but not be running.


text:
<?hh

namespace RobLoach\DesignPatterns;

/**
 * Create families of objects without specifying their concrete classes.
 */
class AbstractFactory<T>
{
    /**
     * Instantiate a new object of the given T type.
     */
    public function create(): T
    {
        // @todo Hack does not currently support creating new instances of T.
        //return new T();
        throw new \Exception('Hack does not currently support creating new instances of T.');
    }
}


text:
package org.openas2.partner;

import org.openas2.OpenAS2Exception;

public interface RefreshablePartnershipFactory {
    public void refresh() throws OpenAS2Exception;
}


text:
layout: post
title: Advanced JavaScript - new
---

Since I just wrote up a [blog post on the this keyword](https://jamesnvk.github.io/2017/02/16/advanced-js-this/), I figured I would round up some knowledge on new. The new keyword does have some built in ties to this that I want to illuminate, but I also want to clarify some misconceptions about new in the world of JavaScript.

One common misconception in JavaScript is that when we are calling new we are instantiating a new object from some sort of class. Instead, what we are really doing here is *constructing* a brand new object, then attaching whatever properties our *function constructor* has. For example:

![Alt text](/assets/ss22.png)

Notice that the f in foo is lowercase. This is to illustrate the point that even though we typically see function constructors start with a capital letter, that is simply just a convention to let other developers know that the function is meant to be used as a constructor.

So what exactly happened when we called new foo()? Just like the [4 rules of this](https://jamesnvk.github.io/2017/02/16/advanced-js-this/), new has some rules as well. Can you guess how many rules there are for new? If you said 4, you are correct! Let us run through what happens when we invoke a function with new in front of it.

> 1. A brand new object is created (or constructed) out of thin air.
> 2. That object is attached to the function's *prototype*.
> 3. That object is set as the this binding for the function call.
> 4. Unless the function has a return value of an alternate object, it implicitly returns this (the newly constructed object)

Just like this, new isn't some magical class oriented keyword. Understanding the core functions and principles of JavaScript can really open up the language in many ways.

______________
If you like what you read you can check out my previous posts listed in my [archive](https://jamesnvk.github.io/archives/). Or you can check out my github here.


text:
vtree has a notion of a thunk build into the virtual tree data structure. A virtual tree can either be a virtual node, a virtual text node, a widget or a thunk.

A thunk is an unevaluated virtual node.The purpose of it is to make the virtual tree data structure lazy, i.e. to create a new complex virtual tree data structure you do not have to build the entire thing top to bottom.

Being lazy on it's own adds little value without also being able to add caching.

The problem we are solving here is that creating 5000 virtual nodes from top to bottom every time something changes is expensive on GC churn. There are two solutions to this problem, you either make subsections of the virtual tree lazy and only evaluate them if you need to evaluate them or you do subtree re-rendering.

As a brief aside, the subtree re-rendering technique (which is not implemented) says that recreating the entire virtual tree from the top to the bottom when anything changes is a bad idea. Instead if something changes we should try and only recreate the component that has changed (and it's children). This is not implemented because having a state <-> component mapping is hard to do whilst keeping purity, immutability & referential transparency.

So thunks, allow us to only evaluate subsets of the virtual tree if they really need to be evaluated. Imagine that only one text node has really changed, and imagine this text node is 10 vnodes deep in the tree, i.e. has 10 parents. Ideally we only evaluate that node and it's 10 parents, the other 4989 nodes in the virtual tree do not have to be constantly recreated or constantly diffed at 60 frames per second.

So how do thunks work.

A thunk is an object that looks like

```js
{
  type: "Thunk",
  render: function (previous) {
    return h('div', 'some text')
  },
  vnode: null
}
```
It's important to note that `render` is only allowed to return one vnode ever. So for every thunk instance we will only call `render()` once then cache the result on `thunk.vnode` and access `thunk.vnode` in the future. This means there is zero cache invalidation and this is because we assume a thunk is just an unevaluated vnode and any thunk instance only ever evaluates to a single vnode.

When we see a thunk, if `thunk.vnode` exists we use that and don't call `.render()`. If `thunk.vnode` does not exist we call `.render(previousThunk)`. i.e. if we are doing a diff and we see a thunk object in the previous tree and a thunk object in the current tree we will call the current thunk with the previous thunk.

The purpose of invoking a thunk instance in the current tree with the equivelant thunk instance in the previous tree is so that the implementor of the thunk can implement a better caching technique.

For example

```js
{
  type: "Thunk",
  isTextThunk: true,
  render: function (previous) {
    if (previous.text === this.text && previous.isTextThunk) {
      return prev.vnode
    }

    return h('div', this.text)
  },
  text: 'some text',
  vnode: null
}
```

Here we use the fact that all our thunk instances that `isTextThunk` will have a text property and the only thing that makes this thunk dynamic is the text. This means that if the previous thunk has the same text we know that the previous vnode is deep equivelant to the thunk we would have created so we can return it instead.

This safes us a small amount of computation because we do not have to call `h()`. Also because we are return the previous vnode we know that the vtree/diff algorithm will go `prevVnode === currVnode` i.e. returning the same vnode instance means the diff algorithm can short circuit and does not have to recursively check stuff.

Now imagine you wrapped a subtree containing 1000 virtual nodes behind a thunk, you would save having to recreate them if nothing changed and you would save having to diff them because diff can short circuit on strict equality of the root virtual node for that subtree.


text:
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>

#include "pc_api_internal.h"
#include "stringbuffer.h"

/** Convert XML string token to type interpretation number */
static const char *
pc_interpretation_string(uint32_t interp)
{
	if ( interp < NUM_INTERPRETATIONS )
		return INTERPRETATION_STRINGS[interp];
	else
		return "unknown";
}

/** Convert XML string token to type interpretation number */
static int
pc_interpretation_number(const char *str)
{
	if ( str[0] == 'i' || str[0] == 'I' )
	{
		if ( str[3] == '8' )
			return PC_INT8;
		if ( str[3] == '1' )
			return PC_INT16;
		if ( str[3] == '3' )
			return PC_INT32;
		if ( str[3] == '6' )
			return PC_INT64;
	}
	else if ( str[0] == 'u' || str[0] == 'U' )
	{
		if ( str[4] == '8' )
			return PC_UINT8;
		if ( str[4] == '1' )
			return PC_UINT16;
		if ( str[4] == '3' )
			return PC_UINT32;
		if ( str[4] == '6' )
			return PC_UINT64;
	}
	else if ( str[0] == 'd' || str[0] == 'D' )
	{
		return PC_DOUBLE;
	}
	else if ( str[0] == 'f' || str[0] == 'F' )
	{
		return PC_FLOAT;
	}
	else
		return PC_UNKNOWN;

	return PC_UNKNOWN;
}

static int
pc_compression_number(const char *str)
{
	if ( ! str )
		return PC_NONE;

	if ( (str[0] == 'd' || str[0] == 'D') &&
	        (strcasecmp(str, "dimensional") == 0) )
	{
		return PC_DIMENSIONAL;
	}

	if ( (str[0] == 'g' || str[0] == 'G') &&
	        (strcasecmp(str, "ght") == 0) )
	{
		return PC_GHT;
	}

	if ( (str[0] == 'n' || str[0] == 'N') &&
	        (strcasecmp(str, "none") == 0) )
	{
		return PC_NONE;
	}

	return PC_NONE;
}

/** Convert type interpretation number size in bytes */
size_t
pc_interpretation_size(uint32_t interp)
{
	if ( interp < NUM_INTERPRETATIONS )
	{
		return INTERPRETATION_SIZES[interp];
	}
	else
	{
		pcerror("pc_interpretation_size: invalid interpretation");
		return 0;
	}
}

/** Allocate clean memory for a PCDIMENSION struct */
static PCDIMENSION*
pc_dimension_new()
{
	PCDIMENSION *pcd = pcalloc(sizeof(PCDIMENSION));
	/* Default scaling value is 1! */
	pcd->scale = 1.0;
	return pcd;
}

static PCDIMENSION*
pc_dimension_clone(const PCDIMENSION *dim)
{
	PCDIMENSION *pcd = pc_dimension_new();
	/* Copy all the inline data */
	memcpy(pcd, dim, sizeof(PCDIMENSION));
	/* Copy the referenced data */
	pcd->name = pcstrdup(dim->name);
	pcd->description = pcstrdup(dim->description);
	return pcd;
}

/** Release the memory behind the PCDIMENSION struct */
static void
pc_dimension_free(PCDIMENSION *pcd)
{
	/* Assumption: No memory in the dimension is owned somewhere else */
	if ( pcd->description )
		pcfree(pcd->description);
	if ( pcd->name )
		pcfree(pcd->name);
	pcfree(pcd);
}

static PCSCHEMA*
pc_schema_new(uint32_t ndims)
{
	PCSCHEMA *pcs = pcalloc(sizeof(PCSCHEMA));
	pcs->dims = pcalloc(sizeof(PCDIMENSION*) * ndims);
	pcs->namehash = create_string_hashtable();
	pcs->ndims = ndims;
	pcs->x_position = -1;
	pcs->y_position = -1;
	return pcs;
}

/** Complete the byte offsets of dimensions from the ordered sizes */
static void
pc_schema_calculate_byteoffsets(PCSCHEMA *pcs)
{
	int i;
	size_t byteoffset = 0;
	for ( i = 0; i < pcs->ndims; i++ )
	{
		if ( pcs->dims[i] )
		{
			pcs->dims[i]->byteoffset = byteoffset;
			pcs->dims[i]->size = pc_interpretation_size(pcs->dims[i]->interpretation);
			byteoffset += pcs->dims[i]->size;
		}
	}
	pcs->size = byteoffset;
}

void
pc_schema_set_dimension(PCSCHEMA *s, PCDIMENSION *d)
{
	s->dims[d->position] = d;
	hashtable_insert(s->namehash, d->name, d);
	pc_schema_calculate_byteoffsets(s);
}

PCSCHEMA*
pc_schema_clone(const PCSCHEMA *s)
{
	int i;
	PCSCHEMA *pcs = pc_schema_new(s->ndims);
	pcs->pcid = s->pcid;
	pcs->srid = s->srid;
	pcs->x_position = s->x_position;
	pcs->y_position = s->y_position;
	pcs->compression = s->compression;
	for ( i = 0; i < pcs->ndims; i++ )
	{
		if ( s->dims[i] )
		{
			pc_schema_set_dimension(pcs, pc_dimension_clone(s->dims[i]));
		}
	}
	pc_schema_calculate_byteoffsets(pcs);
	return pcs;
}

/** Release the memory behind the PCSCHEMA struct */
void
pc_schema_free(PCSCHEMA *pcs)
{
	int i;

	for ( i = 0; i < pcs->ndims; i++ )
	{
		if ( pcs->dims[i] )
		{
			pc_dimension_free(pcs->dims[i]);
			pcs->dims[i] = 0;
		}
	}
	pcfree(pcs->dims);

	if ( pcs->namehash )
		hashtable_destroy(pcs->namehash, 0);

	pcfree(pcs);
}

/** Convert a PCSCHEMA to a human-readable JSON string */
char *
pc_schema_to_json(const PCSCHEMA *pcs)
{
	int i;
	char *str;
	stringbuffer_t *sb = stringbuffer_create();
	stringbuffer_append(sb, "{");

	if ( pcs->pcid )
		stringbuffer_aprintf(sb, "\"pcid\" : %d,\n", pcs->pcid);
	if ( pcs->srid )
		stringbuffer_aprintf(sb, "\"srid\" : %d,\n", pcs->srid);
	if ( pcs->compression )
		stringbuffer_aprintf(sb, "\"compression\" : %d,\n", pcs->compression);

	if ( pcs->ndims )
	{

		stringbuffer_append(sb, "\"dims\" : [\n");

		for ( i = 0; i < pcs->ndims; i++ )
		{
			if ( pcs->dims[i] )
			{
				PCDIMENSION *d = pcs->dims[i];

				if ( i ) stringbuffer_append(sb, ",");
				stringbuffer_append(sb, "\n { \n");

				if ( d->name )
					stringbuffer_aprintf(sb, "  \"name\" : \"%s\",\n", d->name);
				if ( d->description )
					stringbuffer_aprintf(sb, "  \"description\" : \"%s\",\n", d->description);

				stringbuffer_aprintf(sb, "  \"size\" : %d,\n", d->size);
				stringbuffer_aprintf(sb, "  \"byteoffset\" : %d,\n", d->byteoffset);
				stringbuffer_aprintf(sb, "  \"scale\" : %g,\n", d->scale);
				stringbuffer_aprintf(sb, "  \"interpretation\" : \"%s\",\n", pc_interpretation_string(d->interpretation));
				stringbuffer_aprintf(sb, "  \"offset\" : %g,\n", d->offset);

				stringbuffer_aprintf(sb, "  \"active\" : %d\n", d->active);
				stringbuffer_append(sb, " }");
			}
		}
		stringbuffer_append(sb, "\n]\n");
	}
	stringbuffer_append(sb, "}\n");
	str = stringbuffer_getstringcopy(sb);
	stringbuffer_destroy(sb);
	return str;
}

void pc_schema_check_xy(PCSCHEMA *s)
{
	int i;
	for ( i = 0; i < s->ndims; i++ )
	{
		char *dimname = s->dims[i]->name;
		if ( strcasecmp(dimname, "X") == 0 ||
		        strcasecmp(dimname, "Longitude") == 0 ||
		        strcasecmp(dimname, "Lon") == 0 )
		{
			s->x_position = i;
			continue;
		}
		if ( strcasecmp(dimname, "Y") == 0 ||
		        strcasecmp(dimname, "Latitude") == 0 ||
		        strcasecmp(dimname, "Lat") == 0 )
		{
			s->y_position = i;
			continue;
		}
	}

	if ( s->x_position < 0 )
		pcerror("pc_schema_check_xy: invalid x_position '%d'", s->x_position);

	if ( s->y_position < 0 )
		pcerror("pc_schema_check_xy: invalid y_position '%d'", s->y_position);
}

static char *
xml_node_get_content(xmlNodePtr node)
{
	xmlNodePtr cur = node->children;
	if ( cur )
	{
		do
		{
			if ( cur->type == XML_TEXT_NODE )
			{
				return (char*)(cur->content);
			}
		}
		while ( (cur = cur->next) );
	}
	return "";
}

/** Population a PCSCHEMA struct from the XML representation */
int
pc_schema_from_xml(const char *xml_str, PCSCHEMA **schema)
{
	xmlDocPtr xml_doc = NULL;
	xmlNodePtr xml_root = NULL;
	xmlNsPtr xml_ns = NULL;
	xmlXPathContextPtr xpath_ctx;
	xmlXPathObjectPtr xpath_obj;
	xmlNodeSetPtr nodes;
	PCSCHEMA *s = NULL;
	const char *xml_ptr = xml_str;

	/* Roll forward to start of XML string */
	while( (*xml_ptr != '\0') && (*xml_ptr != '<') )
	{
		xml_ptr++;
	}

	size_t xml_size = strlen(xml_ptr);
	static xmlChar *xpath_str = (xmlChar*)("/pc:PointCloudSchema/pc:dimension");
	static xmlChar *xpath_metadata_str = (xmlChar*)("/pc:PointCloudSchema/pc:metadata/Metadata");

	/* Parse XML doc */
	*schema = NULL;
	xmlInitParser();
	xml_doc = xmlReadMemory(xml_ptr, xml_size, NULL, NULL, 0);
	if ( ! xml_doc )
	{
		xmlCleanupParser();
		pcwarn("unable to parse schema XML");
		return PC_FAILURE;
	}

	/* Capture the namespace */
	xml_root = xmlDocGetRootElement(xml_doc);
	if ( xml_root->ns )
		xml_ns = xml_root->ns;

	/* Create xpath evaluation context */
	xpath_ctx = xmlXPathNewContext(xml_doc);
	if( ! xpath_ctx )
	{
		xmlFreeDoc(xml_doc);
		xmlCleanupParser();
		pcwarn("unable to create new XPath context to read schema XML");
		return PC_FAILURE;
	}

	/* Register the root namespace if there is one */
	if ( xml_ns )
		xmlXPathRegisterNs(xpath_ctx, (xmlChar*)"pc", xml_ns->href);

	/* Evaluate xpath expression */
	xpath_obj = xmlXPathEvalExpression(xpath_str, xpath_ctx);
	if( ! xpath_obj )
	{
		xmlXPathFreeContext(xpath_ctx);
		xmlFreeDoc(xml_doc);
		xmlCleanupParser();
		pcwarn("unable to evaluate xpath expression \"%s\" against schema XML", xpath_str);
		return PC_FAILURE;
	}

	/* Iterate on the dimensions we found */
	if ( (nodes = xpath_obj->nodesetval) )
	{
		int ndims = nodes->nodeNr;
		int i;
		s = pc_schema_new(ndims);
		*schema = s;

		for ( i = 0; i < ndims; i++ )
		{
			/* This is a "dimension" */
			if( nodes->nodeTab[i]->type == XML_ELEMENT_NODE )
			{
				xmlNodePtr cur = nodes->nodeTab[i];
				xmlNodePtr child;
				PCDIMENSION *d = pc_dimension_new();
				char xydim = 0;

				/* These are the values of the dimension */
				for ( child = cur->children; child; child = child->next )
				{
					if( child->type == XML_ELEMENT_NODE )
					{
						char *content = (char*)(child->children->content);
						char *name = (char*)(child->name);
						if ( strcmp(name, "name") == 0 )
						{
							if ( strcasecmp(content, "X") == 0 ||
							        strcasecmp(content, "Longitude") == 0 ||
							        strcasecmp(content, "Lon") == 0 )
							{
								xydim = 'x';
							}
							if ( strcasecmp(content, "Y") == 0 ||
							        strcasecmp(content, "Latitude") == 0 ||
							        strcasecmp(content, "Lat") == 0 )
							{
								xydim = 'y';
							}
							d->name = pcstrdup(content);
						}
						else if ( strcmp(name, "description") == 0 )
							d->description = pcstrdup(content);
						else if ( strcmp(name, "size") == 0 )
							d->size = atoi(content);
						else if ( strcmp(name, "active") == 0 )
							d->active = atoi(content);
						else if ( strcmp(name, "position") == 0 )
							d->position = atoi(content) - 1;
						else if ( strcmp(name, "interpretation") == 0 )
							d->interpretation = pc_interpretation_number(content);
						else if ( strcmp(name, "scale") == 0 )
							d->scale = atof(content);
						else if ( strcmp(name, "offset") == 0 )
							d->offset = atof(content);
						else if ( strcmp(name, "uuid") == 0 )
							/* Ignore this tag for now */ {}
						else if ( strcmp(name, "parent_uuid") == 0 )
							/* Ignore this tag for now */ {}
						else
							pcinfo("unhandled schema type element \"%s\" encountered", name);
					}
				}

				/* Convert interprestation to size */
				d->size = pc_interpretation_size(d->interpretation);

				/* Store the dimension in the schema */
				if ( d->position < ndims )
				{
					if ( s->dims[d->position] )
					{
						xmlXPathFreeObject(xpath_obj);
						xmlXPathFreeContext(xpath_ctx);
						xmlFreeDoc(xml_doc);
						xmlCleanupParser();
						pc_schema_free(s);
						pcwarn("schema dimension at position \"%d\" is declared twice", d->position + 1, ndims);
						return PC_FAILURE;
					}
					if ( xydim == 'x' )
					{
						s->x_position = d->position;
					}
					if ( xydim == 'y' )
					{
						s->y_position = d->position;
					}
					pc_schema_set_dimension(s, d);
				}
				else
				{
					xmlXPathFreeObject(xpath_obj);
					xmlXPathFreeContext(xpath_ctx);
					xmlFreeDoc(xml_doc);
					xmlCleanupParser();
					pc_schema_free(s);
					pcwarn("schema dimension states position \"%d\", but number of XML dimensions is \"%d\"", d->position + 1, ndims);
					return PC_FAILURE;
				}
			}
		}

		/* Complete the byte offsets of dimensions from the ordered sizes */
		pc_schema_calculate_byteoffsets(s);
		/* Check X/Y positions */
		pc_schema_check_xy(s);
	}

	xmlXPathFreeObject(xpath_obj);

	/* SEARCH FOR METADATA ENTRIES */
	xpath_obj = xmlXPathEvalExpression(xpath_metadata_str, xpath_ctx);
	if( ! xpath_obj )
	{
		xmlXPathFreeContext(xpath_ctx);
		xmlFreeDoc(xml_doc);
		xmlCleanupParser();
		pcwarn("unable to evaluate xpath expression \"%s\" against schema XML", xpath_metadata_str);
		return PC_FAILURE;
	}

	/* Iterate on the <Metadata> we find */
	if ( (nodes = xpath_obj->nodesetval) )
	{
		int i;

		for ( i = 0; i < nodes->nodeNr; i++ )
		{
			char *metadata_name = "";
			char *metadata_value = "";
			/* Read the metadata name and value from the node */
			/* <Metadata name="somename">somevalue</Metadata> */
			xmlNodePtr cur = nodes->nodeTab[i];
			if( cur->type == XML_ELEMENT_NODE && strcmp((char*)(cur->name), "Metadata") == 0 )
			{
				metadata_name = (char*)xmlGetProp(cur, (xmlChar*)"name");
				metadata_value = xml_node_get_content(cur);
			}

			/* Store the compression type on the schema */
			if ( strcmp(metadata_name, "compression") == 0 )
			{
				int compression = pc_compression_number(metadata_value);
				if ( compression >= 0 )
				{
					s->compression = compression;
				}
			}
			xmlFree(metadata_name);
		}
	}

	xmlXPathFreeObject(xpath_obj);

	xmlXPathFreeContext(xpath_ctx);
	xmlFreeDoc(xml_doc);
	xmlCleanupParser();

	return PC_SUCCESS;
}

uint32_t
pc_schema_is_valid(const PCSCHEMA *s)
{
	int i;

	if ( s->x_position < 0 )
	{
		pcwarn("schema does not include an X coordinate");
		return PC_FALSE;
	}

	if ( s->y_position < 0 )
	{
		pcwarn("schema does not include a Y coordinate");
		return PC_FALSE;
	}

	if ( ! s->ndims )
	{
		pcwarn("schema has no dimensions");
		return PC_FALSE;
	}

	for ( i = 0; i < s->ndims; i++ )
	{
		if ( ! s->dims[i] )
		{
			pcwarn("schema is missing a dimension at position %d", i);
			return PC_FALSE;
		}
	}

	return PC_TRUE;
}

PCDIMENSION *
pc_schema_get_dimension(const PCSCHEMA *s, uint32_t dim)
{
	if ( s && s->ndims > dim )
	{
		return s->dims[dim];
	}
	return NULL;
}

PCDIMENSION *
pc_schema_get_dimension_by_name(const PCSCHEMA *s, const char *name)
{
	if ( ! ( s && s->namehash ) )
		return NULL;

	return hashtable_search(s->namehash, name);
}

size_t
pc_schema_get_size(const PCSCHEMA *s)
{
	return s->size;
}