phantomscript = os.path.join(os.path.dirname(__file__),<EOL>'<STR_LIT>')<EOL>directory = kwargs.get('<STR_LIT>', '<STR_LIT>')<EOL>image_name = kwargs.get('<STR_LIT>', None) or _image_name_from_url(url)<EOL>ext = kwargs.get('<STR_LIT>', '<STR_LIT>').lower()<EOL>save_path = os.path.join(directory, image_name) + '<STR_LIT:.>' + ext<EOL>crop_to_visible = kwargs.get('<STR_LIT>', False)<EOL>cmd_args = [<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>phantomscript,<EOL>url,<EOL>'<STR_LIT>',<EOL>str(kwargs['<STR_LIT:width>']),<EOL>'<STR_LIT>',<EOL>str(kwargs['<STR_LIT>']),<EOL>'<STR_LIT>',<EOL>str(kwargs['<STR_LIT>']),<EOL>'<STR_LIT>',<EOL>directory,<EOL>'<STR_LIT>',<EOL>ext,<EOL>'<STR_LIT>',<EOL>str(image_name),<EOL>]<EOL>if crop_to_visible:<EOL><INDENT>cmd_args.append('<STR_LIT>')<EOL><DEDENT>output = subprocess.Popen(cmd_args,<EOL>stdout=subprocess.PIPE).communicate()[<NUM_LIT:0>]<EOL>return Screenshot(save_path, directory, image_name + '<STR_LIT:.>' + ext, ext)<EOL>

Call PhantomJS with the specified flags and options.


find = r'<STR_LIT>'<EOL>replace = '<STR_LIT:_>'<EOL>return re.sub(find, replace, url).strip('<STR_LIT:_>')<EOL>


device = heimdallDevice(kwargs.get('<STR_LIT>', None))<EOL>kwargs['<STR_LIT:width>'] = kwargs.get('<STR_LIT:width>', None) or device.width<EOL>kwargs['<STR_LIT>'] = kwargs.get('<STR_LIT>', None) or device.height<EOL>kwargs['<STR_LIT>'] = kwargs.get('<STR_LIT>', None) or device.user_agent<EOL>screenshot_image = screenshot(url, **kwargs)<EOL>if kwargs.get('<STR_LIT>'):<EOL><INDENT>image = Image.open(screenshot_image.path)<EOL>image.save(screenshot_image.path, optimize=True)<EOL><DEDENT>return screenshot_image<EOL>

Parse the options, set defaults and then fire up PhantomJS.


return self._backend.height()<EOL>

Returns the height of the wallet.

:rtype: int


def transfer(self, address, amount,<EOL>priority=prio.NORMAL, payment_id=None, unlock_time=<NUM_LIT:0>,<EOL>relay=True):

return self.accounts[<NUM_LIT:0>].transfer(<EOL>address,<EOL>amount,<EOL>priority=priority,<EOL>payment_id=payment_id,<EOL>unlock_time=unlock_time,<EOL>relay=relay)<EOL>

Sends a transfer from the default account. Returns a list of resulting transactions.

:param address: destination :class:`Address <monero.address.Address>` or subtype
:param amount: amount to send
:param priority: transaction priority, implies fee. The priority can be a number
            from 1 to 4 (unimportant, normal, elevated, priority) or a constant
            from `monero.prio`.
:param payment_id: ID for the payment (must be None if
                :class:`IntegratedAddress <monero.address.IntegratedAddress>`
                is used as the destination)
:param unlock_time: the extra unlock delay
:param relay: if `True`, the wallet will relay the transaction(s) to the network
                immediately; when `False`, it will only return the transaction(s)
                so they might be broadcasted later
:rtype: list of :class:`Transaction <monero.transaction.Transaction>`


return self._backend.export_key_images()<EOL>

Exports signed key images as a list of dicts.

:rtype: [dict, dict, ...]


return self.accounts[<NUM_LIT:0>].new_address(label=label)<EOL>

Creates a new address in the default account.

:rtype: :class:`SubAddress <monero.address.SubAddress>`


return self.accounts[<NUM_LIT:0>].addresses()[<NUM_LIT:0>]<EOL>

Returns wallet's master address.

:rtype: :class:`Address <monero.address.Address>`


acc, addr = self._backend.new_account(label=label)<EOL>assert acc.index == len(self.accounts)<EOL>self.accounts.append(acc)<EOL>return acc<EOL>

Creates new account, appends it to the :class:`Wallet`'s account list and returns it.

:param label: account label as `str`
:rtype: :class:`Account`


return self._backend.seed()<EOL>


return self.accounts[<NUM_LIT:0>].addresses()<EOL>

Returns all addresses of the default account.

:rtype: list of :class:`Address <monero.address.Address>` and
        :class:`SubAddress <monero.address.SubAddress>`


return self._backend.balances(account=self.index)<EOL>

Returns a tuple of balance and unlocked balance.

:rtype: (Decimal, Decimal)


def transfer_multiple(self, destinations,<EOL>priority=prio.NORMAL, payment_id=None, unlock_time=<NUM_LIT:0>,<EOL>relay=True):

return self._backend.transfer(<EOL>destinations,<EOL>priority,<EOL>payment_id,<EOL>unlock_time,<EOL>account=self.index,<EOL>relay=relay)<EOL>

Sends a batch of transfers. Returns a list of resulting transactions.

:param destinations: a list of destination and amount pairs:
            [(:class:`Address <monero.address.Address>`, `Decimal`), ...]
:param priority: transaction priority, implies fee. The priority can be a number
            from 1 to 4 (unimportant, normal, elevated, priority) or a constant
            from `monero.prio`.
:param payment_id: ID for the payment (must be None if
                :class:`IntegratedAddress <monero.address.IntegratedAddress>`
                is used as the destination)
:param unlock_time: the extra unlock delay
:param relay: if `True`, the wallet will relay the transaction(s) to the network
                immediately; when `False`, it will only return the transaction(s)
                so they might be broadcasted later
:rtype: list of :class:`Transaction <monero.transaction.Transaction>`


return self._backend.addresses(account=self.index)<EOL>

Returns all addresses of the account.

:rtype: list


return self._backend.new_address(account=self.index, label=label)<EOL>

Creates a new address.

:param label: address label as `str`
:rtype: :class:`SubAddress <monero.address.SubAddress>`


def send_transaction(self, tx, relay=True):

return self._backend.send_transaction(tx.blob, relay=relay)<EOL>

Sends a transaction generated by a :class:`Wallet <monero.wallet.Wallet>`.

:param tx: :class:`Transaction <monero.transaction.Transaction>`
:param relay: whether to relay the transaction to peers. If `False`, the daemon will have
        to mine the transaction itself in order to have it included in the blockchain.


return self._backend.info()['<STR_LIT>']<EOL>

Return daemon's chain height.

:rtype: int


return hexlify(self._decoded[<NUM_LIT:1>:<NUM_LIT>]).decode()<EOL>

Returns public spend key.

        :rtype: str


return ed25519.public_from_secret_hex(key) == self.view_key()<EOL>

Checks if private view key matches this address.

        :rtype: bool


prefix = <NUM_LIT> if self.is_testnet() else <NUM_LIT> if self.is_stagenet() else <NUM_LIT><EOL>data = bytearray([prefix]) + self._decoded[<NUM_LIT:1>:<NUM_LIT>]<EOL>checksum = keccak_256(data).digest()[:<NUM_LIT:4>]<EOL>return Address(base58.encode(hexlify(data + checksum)))<EOL>

Returns the base address without payment id.
        :rtype: :class:`Address`


return ed25519.public_from_secret_hex(key) == self.spend_key()<EOL>

Checks if private spend key matches this address.

        :rtype: bool


return self._decoded[<NUM_LIT:0>] == self._valid_netbytes[<NUM_LIT:0>]<EOL>

Returns `True` if the address belongs to mainnet.

        :rtype: bool


if net not in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>")<EOL><DEDENT>netbyte = <NUM_LIT> if net == '<STR_LIT>' else <NUM_LIT> if net == '<STR_LIT>' else <NUM_LIT><EOL>data = "<STR_LIT>".format(netbyte, self.public_spend_key(), self.public_view_key())<EOL>h = keccak_256()<EOL>h.update(unhexlify(data))<EOL>checksum = h.hexdigest()<EOL>return address(base58.encode(data + checksum[<NUM_LIT:0>:<NUM_LIT:8>]))<EOL>

Returns the master :class:`Address <monero.address.Address>` represented by the seed.

        :param net: the network, one of 'mainnet', 'testnet', 'stagenet'. Default is 'mainnet'.

        :rtype: :class:`Address <monero.address.Address>`


self.phrase = self.word_list.encode(self.hex)<EOL>

Convert hexadecimal string to mnemonic word representation with checksum.


def __init__(self, phrase_or_hex="<STR_LIT>", wordlist="<STR_LIT>"):

self.phrase = "<STR_LIT>" <EOL>self.hex = "<STR_LIT>" <EOL>self.word_list = wordlists.get_wordlist(wordlist)<EOL>self._ed_pub_spend_key = None<EOL>self._ed_pub_view_key = None<EOL>if phrase_or_hex:<EOL><INDENT>seed_split = phrase_or_hex.split("<STR_LIT:U+0020>")<EOL>if len(seed_split) >= <NUM_LIT>:<EOL><INDENT>self.phrase = phrase_or_hex<EOL>if len(seed_split) == <NUM_LIT>:<EOL><INDENT>self._validate_checksum()<EOL><DEDENT>self._decode_seed()<EOL><DEDENT>elif len(seed_split) >= <NUM_LIT:12>:<EOL><INDENT>self.phrase = phrase_or_hex<EOL>if len(seed_split) == <NUM_LIT>:<EOL><INDENT>self._validate_checksum()<EOL><DEDENT>self._decode_seed()<EOL><DEDENT>elif len(seed_split) == <NUM_LIT:1>:<EOL><INDENT>if not len(phrase_or_hex) % <NUM_LIT:8> == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>".format(hex=phrase_or_hex))<EOL><DEDENT>self.hex = phrase_or_hex<EOL>self._encode_seed()<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>".format(arg=phrase_or_hex))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self.hex = generate_hex()<EOL>self._encode_seed()<EOL><DEDENT>

If user supplied a seed string to the class, break it down and determine
        if it's hexadecimal or mnemonic word string. Gather the values and store them.
        If no seed is passed, automatically generate a new one from local system randomness.

        :rtype: :class:`Seed <monero.seed.Seed>`


<EOL>import xmlsec<EOL>signature_node = xmlsec.tree.find_node(xml, xmlsec.Node.SIGNATURE)<EOL>if signature_node is None:<EOL><INDENT>return False<EOL><DEDENT>ctx = xmlsec.SignatureContext()<EOL>ctx.register_id(xml)<EOL>for assertion in xml.xpath("<STR_LIT>"):<EOL><INDENT>ctx.register_id(assertion)<EOL><DEDENT>key = None<EOL>for fmt in [<EOL>xmlsec.KeyFormat.PEM,<EOL>xmlsec.KeyFormat.CERT_PEM]:<EOL><INDENT>stream.seek(<NUM_LIT:0>)<EOL>try:<EOL><INDENT>key = xmlsec.Key.from_memory(stream, fmt)<EOL>break<EOL><DEDENT>except ValueError:  <EOL><INDENT>pass<EOL><DEDENT><DEDENT>ctx.key = key<EOL>try:<EOL><INDENT>ctx.verify(signature_node)<EOL>return True<EOL><DEDENT>except Exception:<EOL><INDENT>return False<EOL><DEDENT>

Verify the signaure of an XML document with the given certificate.
Returns `True` if the document is signed with a valid signature.
Returns `False` if the document is not signed or if the signature is
invalid.

:param lxml.etree._Element xml: The document to sign
:param file stream: The private key to sign the document with

:rtype: Boolean


<EOL>import xmlsec<EOL>from saml.schema.base import _element_registry<EOL>element = _element_registry.get(xml.tag)<EOL>signature_node = xmlsec.template.create(<EOL>xml,<EOL>xmlsec.Transform.EXCL_C14N,<EOL>xmlsec.Transform.RSA_SHA1)<EOL>xml.insert(element.meta.signature_index, signature_node)<EOL>ref = xmlsec.template.add_reference(<EOL>signature_node, xmlsec.Transform.SHA1)<EOL>xmlsec.template.add_transform(ref, xmlsec.Transform.ENVELOPED)<EOL>ctx = xmlsec.SignatureContext()<EOL>key = xmlsec.Key.from_memory(stream, xmlsec.KeyFormat.PEM, password)<EOL>ctx.key = key<EOL>ctx.sign(signature_node)<EOL>

Sign an XML document with the given private key file. This will add a
<Signature> element to the document.

:param lxml.etree._Element xml: The document to sign
:param file stream: The private key to sign the document with
:param str password: The password used to access the private key

:rtype: None

Example usage:
::
    from saml import schema
    from lxml import etree

    document = schema.AuthenticationRequest()
    xml_document = document.serialize()
    with open('my_key_file.pem', 'r+') as stream:
        sign(xml_document, stream)

    print etree.tostring(xml_document)

Produces the following XML document:

.. code-block:: xml

    <samlp:AuthnRequest
        xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
        xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
        Version="2.0" ID="_6087de0b111b44349a70ff40191a4c0c"
        IssueInstant="2015-03-16T21:06:39Z">
        <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
            <SignedInfo>
                <CanonicalizationMethod
                    Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
                    <SignatureMethod
                        Algorithm="http://www.w3.org/2000/
                        09/xmldsig#rsa-sha1"/>
                        <Reference>
                            <Transforms>
                                <Transform
                                    Algorithm="http://www.w3.org/2000/
                                    09/xmldsig#enveloped-signature"/>
                            </Transforms>
                            <DigestMethod
                                Algorithm="http://www.w3.org/2000/
                                09/xmldsig#sha1"/>
                                <DigestValue>
                                    94O1FOjRE4JQYVDqStkYzne9StQ=
                                </DigestValue>
                        </Reference>
            </SignedInfo>
            <SignatureValue>
                aFYRRjtB3bDyLLJzLZmsn0K4SXmOpFYJ+8R8D31VojgiF37FOElbE56UFbm8BAjn
                l2AixrUGXP4djxoxxnfBD/reYw5yVuIVXlMxKec784nF2V4GyrfwJOKaNmlVPkq5
                c8SI+EkKJ02mwiail0Zvjb9FzwvlYD+osMSXvJXVqnGHQDVFlhwbBRRVB6t44/M3
                TzC4mLSVhuvcpsm4GTQSpGkHP7HvweKN/OTc0aTy8Kh/YUrImwnUCii+J0EW4nGg
                71eZyq/IiSPnTD09WDHsWe3g29kpicZXqrQCWeLE2zfVKtyxxs7PyEmodH19jXyz
                wh9hQ8t6PFO47Ros5aV0bw==
            </SignatureValue>
        </Signature>
    </samlp:AuthnRequest>


def __init__(self, meta, name, data, bases):

<EOL>self.name = meta.get('<STR_LIT:name>')<EOL>if self.name is None:<EOL><INDENT>self.name = pascalize(name)<EOL><DEDENT>self.namespace = meta.get('<STR_LIT>')<EOL>self.signature_index = meta.get('<STR_LIT>', <NUM_LIT:1>)<EOL>

Initializes the options object and defaults configuration not
specified.


<EOL>return self._serialize_item(self)<EOL>

Serializes the data in the instance state as an
XML representation.


<EOL>attributes, elements = OrderedDict(), []<EOL>nsmap = dict([self.meta.namespace])<EOL>for name, item in self._items.items():<EOL><INDENT>if isinstance(item, Attribute):<EOL><INDENT>attributes[name] = item.prepare(self)<EOL><DEDENT>elif isinstance(item, Element):<EOL><INDENT>nsmap.update([item.namespace])<EOL>elements.append(item)<EOL><DEDENT><DEDENT>return attributes, elements, nsmap<EOL>

Prepare the date in the instance state for serialization.


return codecs.lookup(name).name<EOL>


try:<EOL><INDENT>return os.fspath(p)<EOL><DEDENT>except AttributeError:<EOL><INDENT>return p<EOL><DEDENT>


if PY3:<EOL><INDENT>bytes_ = map(lambda i: bytes([i]), range(<NUM_LIT:0>, <NUM_LIT>))<EOL><DEDENT>else:<EOL><INDENT>bytes_ = map(chr, range(<NUM_LIT:0>, <NUM_LIT>))<EOL><DEDENT>for byte in bytes_:<EOL><INDENT>try:<EOL><INDENT>byte.decode(encoding)<EOL><DEDENT>except UnicodeDecodeError:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>return True<EOL>

Whether the encoding can decode all byte values (e.g. latin1)


<EOL>def tp(s='<STR_LIT>'):<EOL><INDENT>return _str_to_path(s, result_type)<EOL><DEDENT>if os.name != '<STR_LIT>':<EOL><INDENT>return tp(os.sep)<EOL><DEDENT>sep = sampled_from([os.sep, os.altsep or os.sep]).map(tp)<EOL>name = _filename(result_type)<EOL>char = characters(min_codepoint=ord("<STR_LIT:A>"), max_codepoint=ord("<STR_LIT:z>")).map(<EOL>lambda c: tp(str(c)))<EOL>relative = sep<EOL>drive = builds(lambda *x: tp().join(x), char, just(tp('<STR_LIT::>')), sep)<EOL>extended = builds(<EOL>lambda *x: tp().join(x), sep, sep, just(tp('<STR_LIT:?>')), sep, drive)<EOL>network = one_of([<EOL>builds(lambda *x: tp().join(x), sep, sep, name, sep, name, sep),<EOL>builds(lambda *x: tp().join(x),<EOL>sep, sep, just(tp('<STR_LIT:?>')), sep, name, sep, name, sep),<EOL>builds(lambda *x: tp().join(x),<EOL>sep, sep, just(tp('<STR_LIT:?>')), sep, just(tp('<STR_LIT>')), sep, name, sep,<EOL>name, sep),<EOL>builds(lambda *x: tp().join(x),<EOL>sep, sep, just(tp('<STR_LIT:.>')), sep, name, sep),<EOL>])<EOL>final = one_of(relative, drive, extended, network)<EOL>return draw(final)<EOL>


assert isinstance(s, str)<EOL>if isinstance(s, bytes) and result_type is text_type:<EOL><INDENT>return s.decode('<STR_LIT:ascii>')<EOL><DEDENT>elif isinstance(s, text_type) and result_type is bytes:<EOL><INDENT>return s.encode('<STR_LIT:ascii>')<EOL><DEDENT>return s<EOL>

Given an ASCII str, returns a path of the given type.


@classmethod<EOL><INDENT>def deserialize(cls, serializer, s_state, **kwargs):<DEDENT>

return serializer.deserialize_workflow(s_state, **kwargs)<EOL>

Deserializes a Workflow instance using the provided serializer.

:type  serializer: :class:`SpiffWorkflow.serializer.base.Serializer`
:param serializer: The serializer to use.
:type  s_state: object
:param s_state: The serialized workflow.
:type  kwargs: dict
:param kwargs: Passed to the serializer.
:rtype:  Workflow
:returns: The workflow instance.


mask = Task.NOT_FINISHED_MASK<EOL>iter = Task.Iterator(self.task_tree, mask)<EOL>try:<EOL><INDENT>next(iter)<EOL><DEDENT>except StopIteration:<EOL><INDENT>return True<EOL><DEDENT>return False<EOL>

Returns True if the entire Workflow is completed, False otherwise.

:rtype: bool
:return: Whether the workflow is completed.


def __init__(self, workflow_spec, deserializing=False, **kwargs):

assert workflow_spec is not None<EOL>LOG.debug("<STR_LIT>" % self.__str__())<EOL>self.spec = workflow_spec<EOL>self.data = {}<EOL>self.outer_workflow = kwargs.get('<STR_LIT>', self)<EOL>self.locks = {}<EOL>self.last_task = None<EOL>if deserializing:<EOL><INDENT>assert '<STR_LIT>' in workflow_spec.task_specs<EOL>root = workflow_spec.task_specs['<STR_LIT>']  <EOL><DEDENT>else:<EOL><INDENT>if '<STR_LIT>' in workflow_spec.task_specs:<EOL><INDENT>root = workflow_spec.task_specs['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>root = specs.Simple(workflow_spec, '<STR_LIT>')<EOL><DEDENT><DEDENT>self.task_tree = Task(self, root)<EOL>self.success = True<EOL>self.debug = False<EOL>self.completed_event = Event()<EOL>self.task_tree.state = Task.COMPLETED<EOL>start = self.task_tree._add_child(self.spec.start, state=Task.FUTURE)<EOL>self.spec.start._predict(start)<EOL>if '<STR_LIT>' not in kwargs:<EOL><INDENT>start.task_spec._update(start)<EOL><DEDENT>

Constructor.

:type workflow_spec: specs.WorkflowSpec
:param workflow_spec: The workflow specification.
:type deserializing: bool
:param deserializing: set to true when deserializing to avoid
  generating tasks twice (and associated problems with multiple
  hierarchies of tasks)


Like :meth:`get_dump`, but prints the output to the terminal instead
of returning it.


return [task for task in self.get_tasks()<EOL>if task.task_spec.name == name]<EOL>

Returns all tasks whose spec has the given name.

:type name: str
:param name: The name of a task spec.
:rtype: Task
:return: The task that relates to the spec with the given name.


if task_id is None:<EOL><INDENT>raise WorkflowException(self.spec, '<STR_LIT>')<EOL><DEDENT>for task in self.task_tree:<EOL><INDENT>if task.id == task_id:<EOL><INDENT>return task.complete()<EOL><DEDENT><DEDENT>msg = '<STR_LIT>' % task_id<EOL>raise WorkflowException(self.spec, msg)<EOL>

Runs the task with the given id.

:type  task_id: integer
:param task_id: The id of the Task object.


self.success = success<EOL>cancel = []<EOL>mask = Task.NOT_FINISHED_MASK<EOL>for task in Task.Iterator(self.task_tree, mask):<EOL><INDENT>cancel.append(task)<EOL><DEDENT>for task in cancel:<EOL><INDENT>task.cancel()<EOL><DEDENT>

Cancels all open tasks in the workflow.

:type  success: bool
:param success: Whether the Workflow should be marked as successfully
                completed.


return self.task_tree.get_dump()<EOL>

Returns a complete dump of the current internal task tree for
debugging.

:rtype:  str
:returns: The debug information.


assert not self.read_only<EOL>engine_steps = list(<EOL>[t for t in self.get_tasks(Task.READY)<EOL>if self._is_engine_task(t.task_spec)])<EOL>while engine_steps:<EOL><INDENT>for task in engine_steps:<EOL><INDENT>task.complete()<EOL><DEDENT>engine_steps = list(<EOL>[t for t in self.get_tasks(Task.READY)<EOL>if self._is_engine_task(t.task_spec)])<EOL><DEDENT>

Execute any READY tasks that are engine specific (for example, gateways
or script tasks). This is done in a loop, so it will keep completing
those tasks until there are only READY User tasks, or WAITING tasks
left.


return self.get_tasks(Task.WAITING)<EOL>


def deserialize_workflow_spec(self, s_state, filename=None):

if isinstance(s_state, (str, bytes)):<EOL><INDENT>s_state = BytesIO(s_state)<EOL><DEDENT>package_zip = zipfile.ZipFile(<EOL>s_state, "<STR_LIT:r>", compression=zipfile.ZIP_DEFLATED)<EOL>config = configparser.ConfigParser()<EOL>ini_fp = TextIOWrapper(<EOL>package_zip.open(Packager.METADATA_FILE), encoding="<STR_LIT>")<EOL>try:<EOL><INDENT>config.read_file(ini_fp)<EOL><DEDENT>finally:<EOL><INDENT>ini_fp.close()<EOL><DEDENT>parser_class = BpmnParser<EOL>try:<EOL><INDENT>parser_class_module = config.get(<EOL>'<STR_LIT>', '<STR_LIT>', fallback=None)<EOL><DEDENT>except TypeError:<EOL><INDENT>parser_class_module = config.get(<EOL>'<STR_LIT>', '<STR_LIT>', None)<EOL><DEDENT>if parser_class_module:<EOL><INDENT>mod = __import__(parser_class_module, fromlist=[<EOL>config.get('<STR_LIT>', '<STR_LIT>')])<EOL>parser_class = getattr(mod, config.get('<STR_LIT>', '<STR_LIT>'))<EOL><DEDENT>parser = parser_class()<EOL>for info in package_zip.infolist():<EOL><INDENT>parts = os.path.split(info.filename)<EOL>if (len(parts) == <NUM_LIT:2> and<EOL>not parts[<NUM_LIT:0>] and parts[<NUM_LIT:1>].lower().endswith('<STR_LIT>')):<EOL><INDENT>try:<EOL><INDENT>svg = package_zip.read(info.filename[:-<NUM_LIT:5>] + '<STR_LIT>')<EOL><DEDENT>except KeyError:<EOL><INDENT>svg = None<EOL><DEDENT>bpmn_fp = package_zip.open(info)<EOL>try:<EOL><INDENT>bpmn = ET.parse(bpmn_fp)<EOL><DEDENT>finally:<EOL><INDENT>bpmn_fp.close()<EOL><DEDENT>parser.add_bpmn_xml(<EOL>bpmn, svg=svg,<EOL>filename='<STR_LIT>' % (filename, info.filename))<EOL><DEDENT><DEDENT>return parser.get_spec(config.get('<STR_LIT>', '<STR_LIT>'))<EOL>

:param s_state: a byte-string with the contents of the packaged
workflow archive, or a file-like object.

:param filename: the name of the package file.


group = OptionGroup(parser, "<STR_LIT>",<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>group.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>",<EOL>help="<STR_LIT>")<EOL>group.add_option(<EOL>"<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>",<EOL>help="<STR_LIT>")<EOL>parser.add_option_group(group)<EOL>


parser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>",<EOL>help="<STR_LIT>")<EOL>parser.add_option("<STR_LIT>", "<STR_LIT>", dest="<STR_LIT>",<EOL>help="<STR_LIT>")<EOL>parser.add_option("<STR_LIT:-c>", "<STR_LIT>", dest="<STR_LIT>",<EOL>help="<STR_LIT>")<EOL>parser.add_option(<EOL>"<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store_true>",<EOL>dest="<STR_LIT>", default=False,<EOL>help="<STR_LIT>")<EOL>group = OptionGroup(parser, "<STR_LIT>",<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>group.add_option("<STR_LIT>", dest="<STR_LIT>",<EOL>help="<STR_LIT>")<EOL>parser.add_option_group(group)<EOL>


def package_for_editor_signavio(self, spec, filename):

signavio_file = filename[:-len('<STR_LIT>')] + '<STR_LIT>'<EOL>if os.path.exists(signavio_file):<EOL><INDENT>self.write_file_to_package_zip(<EOL>"<STR_LIT>" + self._get_zip_path(signavio_file), signavio_file)<EOL>f = open(signavio_file, '<STR_LIT:r>')<EOL>try:<EOL><INDENT>signavio_tree = ET.parse(f)<EOL><DEDENT>finally:<EOL><INDENT>f.close()<EOL><DEDENT>svg_node = one(signavio_tree.findall('<STR_LIT>'))<EOL>self.write_to_package_zip("<STR_LIT>" % spec.name, svg_node.text)<EOL><DEDENT>

Adds the SVG files to the archive for this BPMN file.


def __init__(self, package_file, entry_point_process, meta_data=None,<EOL>editor=None):

self.package_file = package_file<EOL>self.entry_point_process = entry_point_process<EOL>self.parser = self.PARSER_CLASS()<EOL>self.meta_data = meta_data or []<EOL>self.input_files = []<EOL>self.input_path_prefix = None<EOL>self.editor = editor<EOL>self.manifest = {}<EOL>

Constructor.

:param package_file: a file-like object where the contents of the
package must be written to

:param entry_point_process: the name or ID of the entry point process

:param meta_data: A list of meta-data tuples to include in the
metadata.ini file (in addition to the standard ones)

:param editor: The name of the editor used to create the source BPMN /
SVG files. This activates additional hook method calls. (optional)


self.input_files += filenames<EOL>

Add all filenames in the given list to the packager's set.


def _fix_call_activities_signavio(self, bpmn, filename):

for node in xpath_eval(bpmn)("<STR_LIT>"):<EOL><INDENT>calledElement = node.get('<STR_LIT>', None)<EOL>if not calledElement:<EOL><INDENT>signavioMetaData = xpath_eval(node, extra_ns={<EOL>'<STR_LIT>': SIGNAVIO_NS})(<EOL>'<STR_LIT>')<EOL>if not signavioMetaData:<EOL><INDENT>raise ValidationException(<EOL>'<STR_LIT>',<EOL>node=node, filename=filename)<EOL><DEDENT>subprocess_reference = one(signavioMetaData).get('<STR_LIT>')<EOL>matches = []<EOL>for b in list(self.bpmn.values()):<EOL><INDENT>for p in xpath_eval(b)("<STR_LIT>"):<EOL><INDENT>if (p.get('<STR_LIT:name>', p.get('<STR_LIT:id>', None)) ==<EOL>subprocess_reference):<EOL><INDENT>matches.append(p)<EOL><DEDENT><DEDENT><DEDENT>if not matches:<EOL><INDENT>raise ValidationException(<EOL>"<STR_LIT>" %<EOL>subprocess_reference, node=node, filename=filename)<EOL><DEDENT>if len(matches) != <NUM_LIT:1>:<EOL><INDENT>raise ValidationException(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % subprocess_reference, node=node,<EOL>filename=filename)<EOL><DEDENT>node.set('<STR_LIT>', matches[<NUM_LIT:0>].get('<STR_LIT:id>'))<EOL><DEDENT><DEDENT>

Signavio produces slightly invalid BPMN for call activity nodes... It
is supposed to put a reference to the id of the called process in to
the calledElement attribute. Instead it stores a string (which is the
name of the process - not its ID, in our interpretation) in an
extension tag.

This code gets the name of the 'subprocess reference', finds a process
with a matching name, and sets the calledElement attribute to the id of
the process.


return OptionParser(<EOL>usage=("<STR_LIT>"<EOL>"<STR_LIT>"),<EOL>version="<STR_LIT>" % (cls.get_version()))<EOL>


pass<EOL>

Called when a task enters the CANCELLED state.

A subclass may override this method to do work when this happens.


return iter(list(self.outgoing_sequence_flows_by_id.values()))<EOL>

Returns a list of outgoing sequences. Some may be None.


def __init__(self, wf_spec, name, lane=None, **kwargs):

super(BpmnSpecMixin, self).__init__(wf_spec, name, **kwargs)<EOL>self.outgoing_sequence_flows = {}<EOL>self.outgoing_sequence_flows_by_id = {}<EOL>self.lane = lane<EOL>self.documentation = None<EOL>

Constructor.

:param lane: Indicates the name of the lane that this task belongs to
(optional).


def connect_outgoing(self, taskspec, sequence_flow_id, sequence_flow_name,<EOL>documentation):

self.connect(taskspec)<EOL>s = SequenceFlow(<EOL>sequence_flow_id, sequence_flow_name, documentation, taskspec)<EOL>self.outgoing_sequence_flows[taskspec.name] = s<EOL>self.outgoing_sequence_flows_by_id[sequence_flow_id] = s<EOL>

Connect this task spec to the indicated child.

:param sequence_flow_id: The ID of the connecting sequenceFlow node.

:param sequence_flow_name: The name of the connecting sequenceFlow
node.


return self.outgoing_sequence_flows_by_id[id]<EOL>

Returns the outgoing SequenceFlow with the specified ID.


pass<EOL>

Called when a task enters the WAITING state.

A subclass may override this method to do work when this happens.


def connect_outgoing_if(self, condition, taskspec, sequence_flow_id,<EOL>sequence_flow_name, documentation):

self.connect_if(_BpmnCondition(condition), taskspec)<EOL>s = SequenceFlow(<EOL>sequence_flow_id, sequence_flow_name, documentation, taskspec)<EOL>self.outgoing_sequence_flows[taskspec.name] = s<EOL>self.outgoing_sequence_flows_by_id[sequence_flow_id] = s<EOL>

Connect this task spec to the indicated child, if the condition
evaluates to true. This should only be called if the task has a
connect_if method (e.g. ExclusiveGateway).

:param sequence_flow_id: The ID of the connecting sequenceFlow node.

:param sequence_flow_name: The name of the connecting sequenceFlow
node.


def __init__(self, wf_spec, name, event_definition=None, **kwargs):

super(IntermediateCatchEvent, self).__init__(wf_spec, name, **kwargs)<EOL>self.event_definition = event_definition<EOL>

Constructor.

:param event_definition: the EventDefinition that we must wait for.


dt = my_task.workflow.script_engine.evaluate(my_task, self.dateTime)<EOL>if dt is None:<EOL><INDENT>return False<EOL><DEDENT>if dt.tzinfo:<EOL><INDENT>tz = dt.tzinfo<EOL>now = tz.fromutc(datetime.datetime.utcnow().replace(tzinfo=tz))<EOL><DEDENT>else:<EOL><INDENT>now = datetime.datetime.now()<EOL><DEDENT>return now > dt<EOL>

The Timer is considered to have fired if the evaluated dateTime
expression is before datetime.datetime.now()


return self.wf_class<EOL>

Returns the workflow class to instantiate for the sub workflow


done = set()<EOL>lanes = set()<EOL>def recursive_find(task_spec):<EOL><INDENT>if task_spec in done:<EOL><INDENT>return<EOL><DEDENT>done.add(task_spec)<EOL>if hasattr(task_spec, '<STR_LIT>') and task_spec.lane:<EOL><INDENT>lanes.add(task_spec.lane)<EOL><DEDENT>if hasattr(task_spec, '<STR_LIT>'):<EOL><INDENT>recursive_find(task_spec.spec.start)<EOL><DEDENT>for t in task_spec.outputs:<EOL><INDENT>recursive_find(t)<EOL><DEDENT><DEDENT>recursive_find(self.start)<EOL>return lanes<EOL>

Returns a set of the distinct lane names used in the process (including
called activities)


done = set()<EOL>specs = [self]<EOL>def recursive_find(task_spec):<EOL><INDENT>if task_spec in done:<EOL><INDENT>return<EOL><DEDENT>done.add(task_spec)<EOL>if hasattr(task_spec, '<STR_LIT>'):<EOL><INDENT>specs.append(task_spec.spec)<EOL>recursive_find(task_spec.spec.start)<EOL><DEDENT>for t in task_spec.outputs:<EOL><INDENT>recursive_find(t)<EOL><DEDENT><DEDENT>recursive_find(self.start)<EOL>return specs<EOL>

Get the specs for all processes (including called ones), in depth first
order.


if isinstance(expression, Operator):<EOL><INDENT>return expression._matches(task)<EOL><DEDENT>else:<EOL><INDENT>return self._eval(task, expression, **task.data)<EOL><DEDENT>

Evaluate the given expression, within the context of the given task and
return the result.


return one(self.xpath('<STR_LIT>')).text<EOL>

Gets the script content from the node. A subclass can override this
method, if the script needs to be pre-parsed. The result of this call
will be passed to the Script Engine for execution.


timeDate = first(self.xpath('<STR_LIT>'))<EOL>return TimerEventDefinition(<EOL>self.node.get('<STR_LIT:name>', timeDate.text),<EOL>self.parser.parse_condition(<EOL>timeDate.text, None, None, None, None, self))<EOL>

Parse the timerEventDefinition node and return an instance of
TimerEventDefinition

This currently only supports the timeDate node for specifying an expiry
time for the timer.


messageRef = first(self.xpath('<STR_LIT>'))<EOL>message = messageRef.get(<EOL>'<STR_LIT:name>') if messageRef is not None else self.node.get('<STR_LIT:name>')<EOL>return MessageEventDefinition(message)<EOL>

Parse the messageEventDefinition node and return an instance of
MessageEventDefinition


def add_bpmn_xml(self, bpmn, svg=None, filename=None):

xpath = xpath_eval(bpmn)<EOL>processes = xpath('<STR_LIT>')<EOL>for process in processes:<EOL><INDENT>process_parser = self.PROCESS_PARSER_CLASS(<EOL>self, process, svg, filename=filename, doc_xpath=xpath)<EOL>if process_parser.get_id() in self.process_parsers:<EOL><INDENT>raise ValidationException(<EOL>'<STR_LIT>', node=process, filename=filename)<EOL><DEDENT>if process_parser.get_name() in self.process_parsers_by_name:<EOL><INDENT>raise ValidationException(<EOL>'<STR_LIT>', node=process, filename=filename)<EOL><DEDENT>self.process_parsers[process_parser.get_id()] = process_parser<EOL>self.process_parsers_by_name[<EOL>process_parser.get_name()] = process_parser<EOL><DEDENT>

Add the given lxml representation of the BPMN file to the parser's set.

:param svg: Optionally, provide the text data for the SVG of the BPMN
  file
:param filename: Optionally, provide the source filename.


if process_id_or_name in self.process_parsers_by_name:<EOL><INDENT>return self.process_parsers_by_name[process_id_or_name]<EOL><DEDENT>else:<EOL><INDENT>return self.process_parsers[process_id_or_name]<EOL><DEDENT>

Returns the ProcessParser for the given process ID or name. It matches
by name first.


Add all filenames matching the provided pattern (e.g. *.bpmn) to the
parser's set.


self.process_parsers = {}<EOL>self.process_parsers_by_name = {}<EOL>


if len(nodes) >= <NUM_LIT:1>:<EOL><INDENT>return nodes[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

Return the first node in the given list, or None, if the list is empty.


return '<STR_LIT>' % (BPMN_MODEL_NS, tag)<EOL>

Return the full tag name including namespace for the given BPMN tag. In
other words, the name with namespace
http://www.omg.org/spec/BPMN/20100524/MODEL


if node.get('<STR_LIT:id>') in self.parsed_nodes:<EOL><INDENT>return self.parsed_nodes[node.get('<STR_LIT:id>')]<EOL><DEDENT>(node_parser, spec_class) = self.parser._get_parser_class(node.tag)<EOL>if not node_parser or not spec_class:<EOL><INDENT>raise ValidationException(<EOL>"<STR_LIT>",<EOL>node=node, filename=self.filename)<EOL><DEDENT>np = node_parser(self, spec_class, node)<EOL>task_spec = np.parse_node()<EOL>return task_spec<EOL>

Parses the specified child task node, and returns the task spec. This
can be called by a TaskParser instance, that is owned by this
ProcessParser.


def connect_outgoing(self, outgoing_task, outgoing_task_node,<EOL>sequence_flow_node, is_default):

Connects this task to the indicating outgoing task, with the details in
the sequence flow. A subclass can override this method to get extra
information from the node.


return self.process_parser.get_lane(self.get_id())<EOL>

Return the name of the lane that contains this task


if self.parent is None:<EOL><INDENT>return False<EOL><DEDENT>if self.parent == parent:<EOL><INDENT>return True<EOL><DEDENT>return self.parent._is_descendant_of(parent)<EOL>

Returns True if parent is in the list of ancestors, returns False
otherwise.

:type  parent: Task
:param parent: The parent that is searched in the ancestors.
:rtype:  bool
:returns: Whether the parent was found.


tasks = []<EOL>if self.task_spec == task_spec:<EOL><INDENT>tasks.append(self)<EOL><DEDENT>for child in self:<EOL><INDENT>if child.task_spec != task_spec:<EOL><INDENT>continue<EOL><DEDENT>tasks.append(child)<EOL><DEDENT>return tasks<EOL>

Returns any descendants that have the given task spec assigned.

:type  task_spec: TaskSpec
:param task_spec: The wanted task spec.
:rtype:  list(Task)
:returns: The tasks objects that are attached to the given task spec.


if self.parent is None:<EOL><INDENT>return self<EOL><DEDENT>if self.parent.task_spec == task_spec:<EOL><INDENT>return self.parent<EOL><DEDENT>return self.parent._find_ancestor(task_spec)<EOL>

Returns the ancestor that has the given task spec assigned.
If no such ancestor was found, the root task is returned.

:type  task_spec: TaskSpec
:param task_spec: The wanted task spec.
:rtype:  Task
:returns: The ancestor.


return self.state<EOL>


def _sync_children(self, task_specs, state=MAYBE):

LOG.debug("<STR_LIT>" % self.get_name())<EOL>if task_specs is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>add = task_specs[:]<EOL>remove = []<EOL>for child in self.children:<EOL><INDENT>if child.triggered:<EOL><INDENT>continue<EOL><DEDENT>if child.task_spec in add:<EOL><INDENT>add.remove(child.task_spec)<EOL>continue<EOL><DEDENT>if child._is_definite():<EOL><INDENT>raise WorkflowException(self.task_spec,<EOL>'<STR_LIT>' %<EOL>repr(child))<EOL><DEDENT>remove.append(child)<EOL><DEDENT>for child in remove:<EOL><INDENT>self.children.remove(child)<EOL><DEDENT>for task_spec in add:<EOL><INDENT>self._add_child(task_spec, state)<EOL><DEDENT>

This method syncs up the task's children with the given list of task
specs. In other words::

    - Add one child for each given TaskSpec, unless that child already
      exists.
    - Remove all children for which there is no spec in the given list,
      unless it is a "triggered" task.

.. note::

   It is an error if the task has a non-predicted child that is
   not given in the TaskSpecs.

:type  task_specs: list(TaskSpec)
:param task_specs: The list of task specs that may become children.
:type  state: integer
:param state: The bitmask of states for the new children.


if self.parent is None:<EOL><INDENT>return self<EOL><DEDENT>if self.parent.task_spec == parent_task_spec:<EOL><INDENT>return self<EOL><DEDENT>return self.parent._find_child_of(parent_task_spec)<EOL>

Returns the ancestor that has a task with the given task spec
as a parent.
If no such ancestor was found, the root task is returned.

:type  parent_task_spec: TaskSpec
:param parent_task_spec: The wanted ancestor.
:rtype:  Task
:returns: The child of the given ancestor.


self.task_spec._on_trigger(self, *args)<EOL>

If recursive is True, the state is applied to the tree recursively.


return self.data.get(name, default)<EOL>

Returns the value of the data field with the given name, or the given
default value if the data field does not exist.

:type  name: str
:param name: A data field name.
:type  default: obj
:param default: Return this value if the data field does not exist.
:rtype:  obj
:returns: The value of the data field


assert child is not None<EOL>self.children.append(child)<EOL>

Called by another Task to let us know that a child was added.


self.__class__.thread_id_pool += <NUM_LIT:1><EOL>self.thread_id = self.__class__.thread_id_pool<EOL>if not recursive:<EOL><INDENT>return self.thread_id<EOL><DEDENT>for child in self:<EOL><INDENT>child.thread_id = self.thread_id<EOL><DEDENT>return self.thread_id<EOL>

Assigns a new thread id to the task.

:type  recursive: bool
:param recursive: Whether to assign the id to children recursively.
:rtype:  bool
:returns: The new thread id.


state_name = []<EOL>for state, name in list(self.state_names.items()):<EOL><INDENT>if self._has_state(state):<EOL><INDENT>state_name.append(name)<EOL><DEDENT><DEDENT>return '<STR_LIT:|>'.join(state_name)<EOL>

Returns a textual representation of this Task's state.


if self.parent is None:<EOL><INDENT>return self<EOL><DEDENT>return self.parent._get_root()<EOL>


if not right_attribute and not right:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>assert left_attribute is not None<EOL>self.left_attribute = left_attribute<EOL>self.right_attribute = right_attribute<EOL>self.right = right<EOL>

Constructor.

:type  left_attribute: str
:param left_attribute: The name of the attribute to which the value
                       is assigned.
:type  right: object
:param right: A static value that, when given, is assigned to
              left_attribute.
:type  right_attribute: str
:param right_attribute: When given, the attribute with the given
                        name is used as the source (instead of the
                        static value).
:type  kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.TaskSpec`.


@classmethod<EOL><INDENT>def deserialize(cls, serializer, s_state):<DEDENT>

return serializer.deserialize_pathattrib(s_state)<EOL>

Serializes the instance using the provided serializer.

:type  serializer: :class:`SpiffWorkflow.serializer.base.Serializer`
:param serializer: The serializer to use.
:rtype:  object
:returns: The serialized object.


Operator.__init__(self, left, right)<EOL>


return serializer.deserialize_assign(s_state)<EOL>


return serializer.serialize_pathattrib(self)<EOL>


method = self.get_function()<EOL>if method is None:<EOL><INDENT>raise DeadMethodCalled('<STR_LIT>' + self.name)<EOL><DEDENT>method(*args, **kwargs)<EOL>

Proxied to the underlying function or method. Raises
:class:`DeadMethodCalled` if the referenced function is dead.

:rtype:  object :returns: Whatever the referenced function returned.


try:<EOL><INDENT>function.__func__<EOL><DEDENT>except AttributeError:<EOL><INDENT>return _WeakMethodFree(function, callback)<EOL><DEDENT>return _WeakMethodBound(function, callback)<EOL>

Returns a weak reference to the given method or function.
If the callback argument is not None, it is called as soon
as the referenced function is garbage deleted.

:type  function: callable
:param function: The function to reference.
:type  callback: callable
:param callback: Called when the function dies.


self.name = name<EOL>self.callback = callback<EOL>

Constructor. Do not use directly, use :class:`ref()` instead.


raise NotImplementedError()<EOL>

Returns the referenced method/function if it is still alive.
Returns None otherwise.

:rtype:  callable|None
:returns: The referenced function if it is still alive.


def connect(self, callback, *args, **kwargs):

if self.is_connected(callback):<EOL><INDENT>raise AttributeError('<STR_LIT>')<EOL><DEDENT>if self.hard_subscribers is None:<EOL><INDENT>self.hard_subscribers = []<EOL><DEDENT>self.hard_subscribers.append((callback, args, kwargs))<EOL>

Connects the event with the given callback.
When the signal is emitted, the callback is invoked.

.. note::

    The signal handler is stored with a hard reference, so you
    need to make sure to call :class:`disconnect()` if you want the
    handler
    to be garbage collected.

:type  callback: object
:param callback: The callback function.
:type  args: tuple
:param args: Optional arguments passed to the callback.
:type  kwargs: dict
:param kwargs: Optional keyword arguments passed to the callback.


def listen(self, callback, *args, **kwargs):

if self.lock is None:<EOL><INDENT>self.lock = Lock()<EOL><DEDENT>with self.lock:<EOL><INDENT>if self.is_connected(callback):<EOL><INDENT>raise AttributeError('<STR_LIT>')<EOL><DEDENT>if self.weak_subscribers is None:<EOL><INDENT>self.weak_subscribers = []<EOL><DEDENT>ref = weakmethod.ref(callback, self._try_disconnect)<EOL>self.weak_subscribers.append((ref, args, kwargs))<EOL><DEDENT>return ref<EOL>

Like :class:`connect()`, but uses a weak reference instead of a
normal reference.
The signal is automatically disconnected as soon as the handler
is garbage collected.

.. note::

    Storing signal handlers as weak references means that if
    your handler is a local function, it may be garbage collected. To
    prevent this, use :class:`connect()` instead.

:type  callback: object
:param callback: The callback function.
:type  args: tuple
:param args: Optional arguments passed to the callback.
:type  kwargs: dict
:param kwargs: Optional keyword arguments passed to the callback.
:rtype:  :class:`Exscript.util.weakmethod.WeakMethod`
:returns: The newly created weak reference to the callback.


return self.emit(*args, **kwargs)<EOL>


elem = etree.Element('<STR_LIT>')<EOL>return self.serialize_value_list(elem, op.args)<EOL>

Serializer for :meth:`SpiffWorkflow.operators.NotEqual`.

Example::

    <not-equals>
        <value>text</value>
        <value><attribute>foobar</attribute></value>
        <value><path>foobar</path></value>
    </not-equals>


def serialize_value_map(self, map_elem, thedict):

for key, value in sorted((str(k), v) for (k, v) in thedict.items()):<EOL><INDENT>var_elem = SubElement(map_elem, '<STR_LIT>')<EOL>SubElement(var_elem, '<STR_LIT:name>').text = str(key)<EOL>value_elem = SubElement(var_elem, '<STR_LIT:value>')<EOL>self.serialize_value(value_elem, value)<EOL><DEDENT>return map_elem<EOL>

Serializes a dictionary of key/value pairs, where the values are
either strings, or Attrib, or PathAttrib objects.

Example::

    <variable>
        <name>foo</name>
        <value>text</value>
    </variable>
    <variable>
        <name>foo2</name>
        <value><attribute>foobar</attribute></value>
    </variable>


def serialize_task_spec(self, spec, elem):

if spec.id is not None:<EOL><INDENT>SubElement(elem, '<STR_LIT:id>').text = str(spec.id)<EOL><DEDENT>SubElement(elem, '<STR_LIT:name>').text = spec.name<EOL>if spec.description:<EOL><INDENT>SubElement(elem, '<STR_LIT:description>').text = spec.description<EOL><DEDENT>if spec.manual:<EOL><INDENT>SubElement(elem, '<STR_LIT>')<EOL><DEDENT>if spec.internal:<EOL><INDENT>SubElement(elem, '<STR_LIT>')<EOL><DEDENT>SubElement(elem, '<STR_LIT>').text = str(spec.lookahead)<EOL>inputs = [t.name for t in spec.inputs]<EOL>outputs = [t.name for t in spec.outputs]<EOL>self.serialize_value_list(SubElement(elem, '<STR_LIT>'), inputs)<EOL>self.serialize_value_list(SubElement(elem, '<STR_LIT>'), outputs)<EOL>self.serialize_value_map(SubElement(elem, '<STR_LIT:data>'), spec.data)<EOL>self.serialize_value_map(SubElement(elem, '<STR_LIT>'), spec.defines)<EOL>self.serialize_value_list(SubElement(elem, '<STR_LIT>'),<EOL>spec.pre_assign)<EOL>self.serialize_value_list(SubElement(elem, '<STR_LIT>'),<EOL>spec.post_assign)<EOL>return elem<EOL>

Serializes common attributes of :meth:`SpiffWorkflow.specs.TaskSpec`.


Serializer for :meth:`SpiffWorkflow.operators.NotEqual`.

Example::

    <matches>
        <value>text</value>
        <value><attribute>foobar</attribute></value>
    </matches>


def serialize_value(self, parent_elem, value):

if isinstance(value, (str, int)) or type(value).__name__ == '<STR_LIT:str>':<EOL><INDENT>parent_elem.text = str(value)<EOL><DEDENT>elif value is None:<EOL><INDENT>parent_elem.text = None<EOL><DEDENT>else:<EOL><INDENT>parent_elem.append(value.serialize(self))<EOL><DEDENT>

Serializes str, Attrib, or PathAttrib objects.

Example::

    <attribute>foobar</attribute>


Serializer for :meth:`SpiffWorkflow.operators.Equal`.

Example::

    <equals>
        <value>text</value>
        <value><attribute>foobar</attribute></value>
        <value><path>foobar</path></value>
    </equals>


elem = etree.Element('<STR_LIT:path>')<EOL>elem.text = op.path<EOL>return elem<EOL>

Serializer for :meth:`SpiffWorkflow.operators.PathAttrib`.

Example::

    <path>foobar</path>


def deserialize_condition(self, workflow, start_node):

<EOL>condition = None<EOL>spec_name = None<EOL>for node in start_node.childNodes:<EOL><INDENT>if node.nodeType != minidom.Node.ELEMENT_NODE:<EOL><INDENT>continue<EOL><DEDENT>if node.nodeName.lower() == '<STR_LIT>':<EOL><INDENT>if spec_name is not None:<EOL><INDENT>_exc('<STR_LIT>' % spec_name)<EOL><DEDENT>if node.firstChild is None:<EOL><INDENT>_exc('<STR_LIT>')<EOL><DEDENT>spec_name = node.firstChild.nodeValue<EOL><DEDENT>elif node.nodeName.lower() in _op_map:<EOL><INDENT>if condition is not None:<EOL><INDENT>_exc('<STR_LIT>')<EOL><DEDENT>condition = self.deserialize_logical(node)<EOL><DEDENT>else:<EOL><INDENT>_exc('<STR_LIT>' % node.nodeName)<EOL><DEDENT><DEDENT>if condition is None:<EOL><INDENT>_exc('<STR_LIT>')<EOL><DEDENT>if spec_name is None:<EOL><INDENT>_exc('<STR_LIT>' % start_node.nodeName)<EOL><DEDENT>return condition, spec_name<EOL>

Reads the conditional statement from the given node.

workflow -- the workflow with which the concurrence is associated
start_node -- the xml structure (xml.dom.minidom.Node)


times = int(valueof(my_task, self.times, <NUM_LIT:1>)) + self.queued<EOL>for i in range(times):<EOL><INDENT>for task_name in self.context:<EOL><INDENT>task = my_task.workflow.get_task_spec_from_name(task_name)<EOL>task._on_trigger(my_task)<EOL><DEDENT><DEDENT>self.queued = <NUM_LIT:0><EOL>TaskSpec._on_complete_hook(self, my_task)<EOL>

A hook into _on_complete() that does the task specific work.

:type  my_task: Task
:param my_task: A task in which this method is executed.
:rtype:  bool
:returns: True on success, False otherwise.


@classmethod<EOL><INDENT>def deserialize(cls, serializer, wf_spec, s_state, **kwargs):<DEDENT>

return serializer.deserialize_trigger(wf_spec,<EOL>s_state,<EOL>**kwargs)<EOL>

Deserializes the trigger using the provided serializer.


def __init__(self, wf_spec, name, context, times=<NUM_LIT:1>, **kwargs):

assert wf_spec is not None<EOL>assert name is not None<EOL>assert context is not None<EOL>assert isinstance(context, list)<EOL>TaskSpec.__init__(self, wf_spec, name, **kwargs)<EOL>self.context = context<EOL>self.times = times<EOL>self.queued = <NUM_LIT:0><EOL>

Constructor.

:type  wf_spec: WorkflowSpec
:param wf_spec: A reference to the workflow specification.
:type  name: str
:param name: The name of the task spec.
:type  context: list(str)
:param context: A list of the names of tasks that are to be triggered.
:type  times: int or :class:`SpiffWorkflow.operators.Term`
:param times: The number of signals before the trigger fires.
:type  kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.TaskSpec`.


for task in my_task.workflow.task_tree._find_any(self):<EOL><INDENT>if task.thread_id != my_task.thread_id:<EOL><INDENT>continue<EOL><DEDENT>self._do_join(task)<EOL><DEDENT>

May be called to fire the Join before the incoming branches are
completed.


<EOL>if my_task._has_state(Task.COMPLETED):<EOL><INDENT>return True, None<EOL><DEDENT>if my_task._has_state(Task.READY):<EOL><INDENT>return True, None<EOL><DEDENT>if self.split_task is None:<EOL><INDENT>return self._check_threshold_unstructured(my_task, force)<EOL><DEDENT>return self._check_threshold_structured(my_task, force)<EOL>

Checks whether the preconditions for going to READY state are met.
Returns True if the threshold was reached, False otherwise.
Also returns the list of tasks that yet need to be completed.


<EOL>if '<STR_LIT>' in my_task.internal_data:<EOL><INDENT>history = my_task._get_internal_data('<STR_LIT>', [])<EOL>history.append(my_task._get_internal_data('<STR_LIT>'))<EOL>del my_task.internal_data['<STR_LIT>']<EOL>my_task._set_internal_data(task_history=history)<EOL><DEDENT>if '<STR_LIT>' in my_task.internal_data:<EOL><INDENT>del my_task.internal_data['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT:error>' in my_task.internal_data:<EOL><INDENT>del my_task.internal_data['<STR_LIT:error>']<EOL><DEDENT>if hasattr(my_task, '<STR_LIT>'):<EOL><INDENT>delattr(my_task, '<STR_LIT>')<EOL><DEDENT>if hasattr(my_task, '<STR_LIT>'):<EOL><INDENT>delattr(my_task, '<STR_LIT>')<EOL><DEDENT>


if not my_task._has_state(Task.WAITING):<EOL><INDENT>raise WorkflowException(my_task, "<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if my_task._get_internal_data('<STR_LIT>') is not None:<EOL><INDENT>if not hasattr(my_task, '<STR_LIT>'):<EOL><INDENT>task_id = my_task._get_internal_data('<STR_LIT>')<EOL>my_task.async_call = default_app.AsyncResult(task_id)<EOL>my_task.deserialized = True<EOL>my_task.async_call.state  <EOL><DEDENT>async_call = my_task.async_call<EOL>if async_call.state == '<STR_LIT>':<EOL><INDENT>pass<EOL><DEDENT>elif async_call.state in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>async_call.revoke()<EOL>LOG.info("<STR_LIT>" % (<EOL>async_call.state, async_call))<EOL><DEDENT>elif async_call.state == '<STR_LIT>':<EOL><INDENT>LOG.warning("<STR_LIT>"<EOL>"<STR_LIT>" % async_call)<EOL><DEDENT>self._clear_celery_task_data(my_task)<EOL><DEDENT>return self._start(my_task)<EOL>


if (not hasattr(my_task, '<STR_LIT>')) or my_task.subprocess is None:<EOL><INDENT>my_task.subprocess = subprocess.Popen(self.args,<EOL>stderr=subprocess.STDOUT,<EOL>stdout=subprocess.PIPE)<EOL><DEDENT>if my_task.subprocess:<EOL><INDENT>my_task.subprocess.poll()<EOL>if my_task.subprocess.returncode is None:<EOL><INDENT>return False<EOL><DEDENT>else:<EOL><INDENT>results = my_task.subprocess.communicate()<EOL>my_task.results = results<EOL>return True<EOL><DEDENT><DEDENT>return False<EOL>

Returns False when successfully fired, True otherwise


def __init__(self, wf_spec, name, args=None, **kwargs):

assert wf_spec is not None<EOL>assert name is not None<EOL>TaskSpec.__init__(self, wf_spec, name, **kwargs)<EOL>self.args = args<EOL>

Constructor.

:type  wf_spec: WorkflowSpec
:param wf_spec: A reference to the workflow specification.
:type  name: str
:param name: The name of the task spec.
:type  args: list
:param args: args to pass to process (first arg is the command).
:type  kwargs: dict
:param kwargs: kwargs to pass-through to TaskSpec initializer.


<EOL>my_task = self._find_my_task(task_spec)<EOL>if my_task._has_state(Task.COMPLETED):<EOL><INDENT>state = Task.READY<EOL><DEDENT>else:<EOL><INDENT>state = Task.FUTURE<EOL><DEDENT>for output in self.outputs:<EOL><INDENT>new_task = my_task._add_child(output, state)<EOL>new_task.triggered = True<EOL>output._predict(new_task)<EOL><DEDENT>

May be called after execute() was already completed to create an
additional outbound task.


def __init__(self, wf_spec, name, success=False, **kwargs):

TaskSpec.__init__(self, wf_spec, name, **kwargs)<EOL>self.cancel_successfully = success<EOL>

Constructor.

:type  wf_spec: WorkflowSpec
:param wf_spec: A reference to the workflow specification.
:type  name: str
:param name: The name of the task spec.
:type  success: bool
:param success: Whether to cancel successfully or unsuccessfully.
:type  kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.TaskSpec`.


raise WorkflowException(self, '<STR_LIT>')<EOL>

Called by the previous task to let us know that it exists.


assert split_task is not None<EOL>Join.__init__(self, wf_spec, name, split_task, **kwargs)<EOL>

Constructor.

:type  wf_spec: :class:`SpiffWorkflow.specs.WorkflowSpec`
:param wf_spec: A reference to the parent (usually a workflow).
:type  name: string
:param name: A name for the task.
:type  split_task: str
:param split_task: The name of the task spec that was previously
                   used to split the branch.
:type  kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.Join`.


def __init__(self, wf_spec, name, mutex, **kwargs):

assert mutex is not None<EOL>TaskSpec.__init__(self, wf_spec, name, **kwargs)<EOL>self.mutex = mutex<EOL>

Constructor.

:type  wf_spec: WorkflowSpec
:param wf_spec: A reference to the workflow specification.
:type  name: str
:param name: The name of the task spec.
:type  mutex: str
:param mutex: The name of the mutex that should be acquired.
:type  kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.TaskSpec`.


<EOL>for child in my_task.children:<EOL><INDENT>child.task_spec._update(child)<EOL><DEDENT>

A hook into _on_complete() that does the task specific work.

:type  my_task: Task
:param my_task: The associated task in the task tree.
:rtype:  bool
:returns: True on success, False otherwise.


def __init__(self, wf_spec, name, **kwargs):

assert wf_spec is not None<EOL>assert name is not None<EOL>self._wf_spec = wf_spec<EOL>self.id = None<EOL>self.name = str(name)<EOL>self.description = kwargs.get('<STR_LIT:description>', '<STR_LIT>')<EOL>self.inputs = []<EOL>self.outputs = []<EOL>self.manual = kwargs.get('<STR_LIT>', False)<EOL>self.internal = False  <EOL>self.data = kwargs.get('<STR_LIT:data>',        {})<EOL>self.defines = kwargs.get('<STR_LIT>',     {})<EOL>self.pre_assign = kwargs.get('<STR_LIT>',  [])<EOL>self.post_assign = kwargs.get('<STR_LIT>', [])<EOL>self.locks = kwargs.get('<STR_LIT>',        [])<EOL>self.lookahead = <NUM_LIT:2>  <EOL>self.entered_event = Event()<EOL>self.reached_event = Event()<EOL>self.ready_event = Event()<EOL>self.completed_event = Event()<EOL>self.cancelled_event = Event()<EOL>self.finished_event = Event()<EOL>self._wf_spec._add_notify(self)<EOL>self.data.update(self.defines)<EOL>assert self.id is not None<EOL>

Constructor.

The difference between the assignment of a data value using
the data argument versus pre_assign and post_assign is that
changes made using data are task-local, i.e. they are
not visible to other tasks.
Similarly, "defines" are spec data fields that, once defined, can
no longer be modified.

:type  wf_spec: WorkflowSpec
:param wf_spec: A reference to the workflow specification that owns it.
:type  name: string
:param name: A name for the task.
:type  lock: list(str)
:param lock: A list of mutex names. The mutex is acquired
             on entry of execute() and released on leave of
             execute().
:type  manual: bool
:param manual: Whether this task requires a manual action to complete.
:type  data: dict((str, object))
:param data: name/value pairs
:type  defines: dict((str, object))
:param defines: name/value pairs
:type  pre_assign: list((str, object))
:param pre_assign: a list of name/value pairs
:type  post_assign: list((str, object))
:param post_assign: a list of name/value pairs


self.cancelled_event.emit(my_task.workflow, my_task)<EOL>

May be called by another task to cancel the operation before it was
completed.

:type  my_task: Task
:param my_task: The associated task in the task tree.


raise NotImplementedError("<STR_LIT>")<EOL>

May be called by another task to trigger a task-specific
event.

:type  my_task: Task
:param my_task: The associated task in the task tree.
:rtype:  boolean
:returns: True on success, False otherwise.


self.inputs.append(taskspec)<EOL>

Called by the previous task to let us know that it exists.

:type  taskspec: TaskSpec
:param taskspec: The task by which this method is executed.


my_task._inherit_data()<EOL>self._update_hook(my_task)<EOL>

Called whenever any event happens that may affect the
state of this task in the workflow. For example, if a predecessor
completes it makes sure to call this method so we can react.


def _get_activated_tasks(self, my_task, destination):

return my_task.children<EOL>

Returns the list of tasks that were activated in the previous
call of execute(). Only returns tasks that point towards the
destination task, i.e. those which have destination as a
descendant.

:type  my_task: Task
:param my_task: The associated task in the task tree.
:type  destination: Task
:param destination: The destination task.


self.outputs.append(taskspec)<EOL>taskspec._connect_notify(self)<EOL>

Connect the *following* task to this one. In other words, the
given task is added as an output task.

:type  taskspec: TaskSpec
:param taskspec: The new output task.


def _predict(self, my_task, seen=None, looked_ahead=<NUM_LIT:0>):

if my_task._is_finished():<EOL><INDENT>return<EOL><DEDENT>if seen is None:<EOL><INDENT>seen = []<EOL><DEDENT>elif self in seen:<EOL><INDENT>return<EOL><DEDENT>if not my_task._is_finished():<EOL><INDENT>self._predict_hook(my_task)<EOL><DEDENT>if not my_task._is_definite():<EOL><INDENT>if looked_ahead + <NUM_LIT:1> >= self.lookahead:<EOL><INDENT>return<EOL><DEDENT>seen.append(self)<EOL><DEDENT>for child in my_task.children:<EOL><INDENT>child.task_spec._predict(child, seen[:], looked_ahead + <NUM_LIT:1>)<EOL><DEDENT>

Updates the branch such that all possible future routes are added.

Should NOT be overwritten! Instead, overwrite _predict_hook().

:type  my_task: Task
:param my_task: The associated task in the task tree.
:type  seen: list[taskspec]
:param seen: A list of already visited tasks.
:type  looked_ahead: integer
:param looked_ahead: The depth of the predicted path so far.


pass<EOL>

A hook into _on_ready() that does the task specific work.

:type  my_task: Task
:param my_task: The associated task in the task tree.


taskspec.connect(self)<EOL>

Make this task follow the provided one. In other words, this task is
added to the given task outputs.

This is an alias to connect, just easier to understand when reading
code - ex: my_task.follow(the_other_task)
Adding it after being confused by .connect one times too many!

:type  taskspec: TaskSpec
:param taskspec: The task to follow.


pass<EOL>


<EOL>outputs = []<EOL>for condition, output in self.cond_task_specs:<EOL><INDENT>if self.choice is not None and output not in self.choice:<EOL><INDENT>continue<EOL><DEDENT>if condition is None:<EOL><INDENT>outputs.append(self._wf_spec.get_task_spec_from_name(output))<EOL>continue<EOL><DEDENT>if not condition._matches(my_task):<EOL><INDENT>continue<EOL><DEDENT>outputs.append(self._wf_spec.get_task_spec_from_name(output))<EOL><DEDENT>my_task._sync_children(outputs, Task.FUTURE)<EOL>for child in my_task.children:<EOL><INDENT>child.task_spec._update(child)<EOL><DEDENT>

Runs the task. Should not be called directly.
Returns True if completed, False otherwise.


self.choice = choice<EOL>

Lets a caller narrow down the choice by using a Choose trigger.


def connect_if(self, condition, task_spec):

assert task_spec is not None<EOL>self.outputs.append(task_spec)<EOL>self.cond_task_specs.append((condition, task_spec.name))<EOL>task_spec._connect_notify(self)<EOL>

Connects a taskspec that is executed if the condition DOES match.

condition -- a condition (Condition)
taskspec -- the conditional task spec


return self.connect_if(None, task_spec)<EOL>

Convenience wrapper around connect_if() where condition is set to None.


super(MultiChoice, self).__init__(wf_spec, name, **kwargs)<EOL>self.cond_task_specs = []<EOL>self.choice = None<EOL>

Constructor.

:type  wf_spec: WorkflowSpec
:param wf_spec: A reference to the workflow specification.
:type  name: str
:param name: The name of the task spec.
:type  kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.TaskSpec`.


self.thread_starter.outputs.append(task_spec)<EOL>task_spec._connect_notify(self.thread_starter)<EOL>

Connect the *following* task to this one. In other words, the
given task is added as an output task.

task -- the task to connect to.


return my_task.children<EOL>

Returns the list of threads that were activated in the previous
call of execute().

my_task -- the task of this TaskSpec


assert self.default_task_spec is None<EOL>self.outputs.append(task_spec)<EOL>self.default_task_spec = task_spec.name<EOL>task_spec._connect_notify(self)<EOL>

Connects the task spec that is executed if no other condition
matches.

:type  task_spec: TaskSpec
:param task_spec: The following task spec.


def __init__(self, name=None, filename=None, nostart=False):

self.name = name or '<STR_LIT>'<EOL>self.description = '<STR_LIT>'<EOL>self.file = filename<EOL>self.task_specs = dict()<EOL>self.start = None<EOL>if not nostart:<EOL><INDENT>self.start = StartTask(self)<EOL><DEDENT>


WorkflowException.__init__(self, task.task_spec, error)<EOL>self.task = task<EOL>

Exception initialization.

:param sender: the task that threw the exception
:type sender: Task
:param error: a human readable error message
:type error: string


Exception.__init__(self, '<STR_LIT>' % (sender.name, error))<EOL>self.sender = sender<EOL>

Standard exception class.

:param sender: the task spec that threw the exception
:type sender: TaskSpec
:param error: a human readable error message
:type error: string


out = {}<EOL>stack = [(convert_path(where), '<STR_LIT>', package, only_in_packages)]<EOL>while stack:<EOL><INDENT>where, prefix, package, only_in_packages = stack.pop(<NUM_LIT:0>)<EOL>for name in os.listdir(where):<EOL><INDENT>fn = os.path.join(where, name)<EOL>if os.path.isdir(fn):<EOL><INDENT>bad_name = False<EOL>for pattern in exclude_directories:<EOL><INDENT>if (fnmatchcase(name, pattern)<EOL>or fn.lower() == pattern.lower()):<EOL><INDENT>bad_name = True<EOL>if show_ignored:<EOL><INDENT>sys.stderr.write(<EOL>"<STR_LIT>"<EOL>% (fn, pattern))<EOL><DEDENT>break<EOL><DEDENT><DEDENT>if bad_name:<EOL><INDENT>continue<EOL><DEDENT>if os.path.isfile(os.path.join(fn, '<STR_LIT>')):<EOL><INDENT>if not package:<EOL><INDENT>new_package = name<EOL><DEDENT>else:<EOL><INDENT>new_package = package + '<STR_LIT:.>' + name<EOL><DEDENT>stack.append((fn, '<STR_LIT>', new_package, False))<EOL><DEDENT>else:<EOL><INDENT>stack.append(<EOL>(fn, prefix + name + '<STR_LIT:/>', package, only_in_packages)<EOL>)<EOL><DEDENT><DEDENT>elif package or not only_in_packages:<EOL><INDENT>bad_name = False<EOL>for pattern in exclude:<EOL><INDENT>if (fnmatchcase(name, pattern)<EOL>or fn.lower() == pattern.lower()):<EOL><INDENT>bad_name = True<EOL>if show_ignored:<EOL><INDENT>sys.stderr.write(<EOL>"<STR_LIT>"<EOL>% (fn, pattern))<EOL><DEDENT>break<EOL><DEDENT><DEDENT>if bad_name:<EOL><INDENT>continue<EOL><DEDENT>out.setdefault(package, []).append(prefix + name)<EOL><DEDENT><DEDENT><DEDENT>return out<EOL>

Return a dictionary suitable for use in ``package_data``
in a distutils ``setup.py`` file.

The dictionary looks like::

    {'package': [files]}

Where ``files`` is a list of all the files in that package that
don't match anything in ``exclude``.

If ``only_in_packages`` is true, then top-level directories that
are not packages won't be included (but directories under packages
will).

Directories matching any pattern in ``exclude_directories`` will
be ignored; by default directories with leading ``.``, ``CVS``,
and ``_darcs`` will be ignored.

If ``show_ignored`` is true, then all the files that aren't
included in package data are shown on stderr (for debugging
purposes).

Note patterns use wildcards, or can be exact paths (including
leading ``./``), and all searching is case-insensitive.

This function is by Ian Bicking.


<EOL>if getattr(request, '<STR_LIT>', False):<EOL><INDENT>return response<EOL><DEDENT>duration = None<EOL>if getattr(request, '<STR_LIT>', None):<EOL><INDENT>duration = monotonic() - request._tracy_start_time<EOL><DEDENT>trace_id = None<EOL>if getattr(request, '<STR_LIT>', None):<EOL><INDENT>trace_id = request._tracy_id<EOL>response.headers[trace_header_id] = trace_id<EOL><DEDENT>trace_client = None<EOL>if getattr(request, '<STR_LIT>', None):<EOL><INDENT>trace_client = request._tracy_client<EOL><DEDENT>d = {'<STR_LIT>': response.status_code,<EOL>'<STR_LIT:url>': request.base_url,<EOL>'<STR_LIT>': request.remote_addr,<EOL>'<STR_LIT>': trace_client,<EOL>'<STR_LIT>': trace_id,<EOL>'<STR_LIT>': duration}<EOL>logger.info(None, extra=d)<EOL>return response<EOL>

Calculates the request duration, and adds a transaction
        ID to the header.


@blueprint.route('<STR_LIT>', methods=['<STR_LIT:GET>', '<STR_LIT:POST>'])<EOL>@register_breadcrumb(blueprint, '<STR_LIT>', _('<STR_LIT>'))<EOL>@login_required<EOL>def new():

form = GroupForm(request.form)<EOL>if form.validate_on_submit():<EOL><INDENT>try:<EOL><INDENT>group = Group.create(admins=[current_user], **form.data)<EOL>flash(_('<STR_LIT>', name=group.name), '<STR_LIT:success>')<EOL>return redirect(url_for("<STR_LIT>"))<EOL><DEDENT>except IntegrityError:<EOL><INDENT>flash(_('<STR_LIT>'), '<STR_LIT:error>')<EOL><DEDENT><DEDENT>return render_template(<EOL>"<STR_LIT>",<EOL>form=form,<EOL>)<EOL>


@blueprint.route('<STR_LIT>', methods=['<STR_LIT:GET>', '<STR_LIT:POST>'])<EOL>@login_required<EOL>@register_breadcrumb(blueprint, '<STR_LIT>', _('<STR_LIT>'))<EOL>def new_member(group_id):

group = Group.query.get_or_404(group_id)<EOL>if group.can_invite_others(current_user):<EOL><INDENT>form = NewMemberForm()<EOL>if form.validate_on_submit():<EOL><INDENT>emails = filter(None, form.data['<STR_LIT>'].splitlines())<EOL>group.invite_by_emails(emails)<EOL>flash(_('<STR_LIT>'), '<STR_LIT:success>')<EOL>return redirect(url_for('<STR_LIT>', group_id=group.id))<EOL><DEDENT>return render_template(<EOL>"<STR_LIT>",<EOL>group=group,<EOL>form=form<EOL>)<EOL><DEDENT>flash(<EOL>_(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>',<EOL>group_name=group.name<EOL>),<EOL>'<STR_LIT:error>'<EOL>)<EOL>return redirect(url_for('<STR_LIT>'))<EOL>


group = Group.query.get_or_404(group_id)<EOL>if group.can_edit(current_user):<EOL><INDENT>try:<EOL><INDENT>group.delete()<EOL><DEDENT>except Exception as e:<EOL><INDENT>flash(str(e), "<STR_LIT:error>")<EOL>return redirect(url_for("<STR_LIT>"))<EOL><DEDENT>flash(_('<STR_LIT>',<EOL>group_name=group.name), '<STR_LIT:success>')<EOL>return redirect(url_for("<STR_LIT>"))<EOL><DEDENT>flash(<EOL>_(<EOL>'<STR_LIT>',<EOL>group_name=group.name<EOL>),<EOL>'<STR_LIT:error>'<EOL>)<EOL>return redirect(url_for("<STR_LIT>"))<EOL>


membership = Membership.query.get_or_404((user_id, group_id))<EOL>group = membership.group<EOL>if group.can_edit(current_user):<EOL><INDENT>try:<EOL><INDENT>membership.accept()<EOL><DEDENT>except Exception as e:<EOL><INDENT>flash(str(e), '<STR_LIT:error>')<EOL>return redirect(url_for('<STR_LIT>', group_id=membership.group.id))<EOL><DEDENT>flash(_('<STR_LIT>',<EOL>user=membership.user.email,<EOL>name=membership.group.name), '<STR_LIT:success>')<EOL>return redirect(url_for('<STR_LIT>', group_id=membership.group.id))<EOL><DEDENT>flash(<EOL>_(<EOL>'<STR_LIT>',<EOL>group_name=group.name<EOL>),<EOL>'<STR_LIT:error>'<EOL>)<EOL>return redirect(url_for('<STR_LIT>'))<EOL>


group = Group.query.get_or_404(group_id)<EOL>form = GroupForm(request.form, obj=group)<EOL>if form.validate_on_submit():<EOL><INDENT>if group.can_edit(current_user):<EOL><INDENT>try:<EOL><INDENT>group.update(**form.data)<EOL>flash(_('<STR_LIT>', name=group.name),<EOL>'<STR_LIT:success>')<EOL><DEDENT>except Exception as e:<EOL><INDENT>flash(str(e), '<STR_LIT:error>')<EOL>return render_template(<EOL>"<STR_LIT>",<EOL>form=form,<EOL>group=group,<EOL>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>flash(<EOL>_(<EOL>'<STR_LIT>',<EOL>group_name=group.name<EOL>),<EOL>'<STR_LIT:error>'<EOL>)<EOL><DEDENT><DEDENT>return render_template(<EOL>"<STR_LIT>",<EOL>form=form,<EOL>group=group,<EOL>)<EOL>


app.config.setdefault(<EOL>"<STR_LIT>",<EOL>app.config.get("<STR_LIT>",<EOL>"<STR_LIT>"))<EOL>


self.init_config(app)<EOL>app.register_blueprint(blueprint)<EOL>app.extensions['<STR_LIT>'] = self<EOL>


if app:<EOL><INDENT>self.init_app(app)<EOL><DEDENT>


self.validate_data(form, field)<EOL>emails_org = field.data<EOL>emails = filter(None, emails_org.splitlines())<EOL>for email in emails:<EOL><INDENT>try:<EOL><INDENT>field.data = email<EOL>self.validate_email(form, field)<EOL><DEDENT>except (ValidationError, StopValidation):<EOL><INDENT>raise ValidationError('<STR_LIT>' + email)<EOL><DEDENT>finally:<EOL><INDENT>field.data = emails_org<EOL><DEDENT><DEDENT>


html = "<STR_LIT>"<EOL>for subfield in field:<EOL><INDENT>label = subfield.label.text<EOL>if (field.default == subfield.data):<EOL><INDENT>subfield.checked = True<EOL><DEDENT>else:<EOL><INDENT>subfield.checked = False<EOL><DEDENT>description = self.descriptions.get(subfield.data, "<STR_LIT>")<EOL>html += ('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>') % (subfield(), label, description)<EOL><DEDENT>return HTMLString(html)<EOL>


return GroupAdmin.delete(self, admin)<EOL>

Remove an admin from group (independent of membership state).

        :param admin: Admin to be removed from group.


try:<EOL><INDENT>return cls.query.filter_by(name=name).one()<EOL><DEDENT>except NoResultFound:<EOL><INDENT>return None<EOL><DEDENT>

Query group by a group name.

        :param name: Name of a group to search for.
        :returns: Group object or None.


@classmethod<EOL><INDENT>def delete(cls, group, user):<DEDENT>

with db.session.begin_nested():<EOL><INDENT>cls.query.filter_by(group=group, user_id=user.get_id()).delete()<EOL><DEDENT>


@classmethod<EOL><INDENT>def get(cls, group, user):<DEDENT>

try:<EOL><INDENT>m = cls.query.filter_by(user_id=user.get_id(), group=group).one()<EOL>return m<EOL><DEDENT>except Exception:<EOL><INDENT>return None<EOL><DEDENT>

Get membership for given user and group.

        :param group: Group object.
        :param user: User object.
        :returns: Membership or None.


return Membership.query_by_group(self).count()<EOL>

Determine members count.

        :returns: Number of memberships.


def update(self, name=None, description=None, privacy_policy=None,<EOL>subscription_policy=None, is_managed=None):

with db.session.begin_nested():<EOL><INDENT>if name is not None:<EOL><INDENT>self.name = name<EOL><DEDENT>if description is not None:<EOL><INDENT>self.description = description<EOL><DEDENT>if (<EOL>privacy_policy is not None and<EOL>PrivacyPolicy.validate(privacy_policy)<EOL>):<EOL><INDENT>self.privacy_policy = privacy_policy<EOL><DEDENT>if (<EOL>subscription_policy is not None and<EOL>SubscriptionPolicy.validate(subscription_policy)<EOL>):<EOL><INDENT>self.subscription_policy = subscription_policy<EOL><DEDENT>if is_managed is not None:<EOL><INDENT>self.is_managed = is_managed<EOL><DEDENT>db.session.merge(self)<EOL><DEDENT>return self<EOL>

Update group.

        :param name: Name of group.
        :param description: Description of group.
        :param privacy_policy: PrivacyPolicy
        :param subscription_policy: SubscriptionPolicy
        :returns: Updated group


@classmethod<EOL><INDENT>def create(cls, name=None, description='<STR_LIT>', privacy_policy=None,<EOL>subscription_policy=None, is_managed=False, admins=None):<DEDENT>

assert name<EOL>assert privacy_policy is None or PrivacyPolicy.validate(privacy_policy)<EOL>assert subscription_policy is None orSubscriptionPolicy.validate(subscription_policy)<EOL>assert admins is None or isinstance(admins, list)<EOL>with db.session.begin_nested():<EOL><INDENT>obj = cls(<EOL>name=name,<EOL>description=description,<EOL>privacy_policy=privacy_policy,<EOL>subscription_policy=subscription_policy,<EOL>is_managed=is_managed,<EOL>)<EOL>db.session.add(obj)<EOL>for a in admins or []:<EOL><INDENT>db.session.add(GroupAdmin(<EOL>group=obj, admin_id=a.get_id(),<EOL>admin_type=resolve_admin_type(a)))<EOL><DEDENT><DEDENT>return obj<EOL>

Create a new group.

        :param name: Name of group. Required and must be unique.
        :param description: Description of group. Default: ``''``
        :param privacy_policy: PrivacyPolicy
        :param subscription_policy: SubscriptionPolicy
        :param admins: list of user and/or group objects. Default: ``[]``
        :returns: Newly created group
        :raises: IntegrityError: if group with given name already exists


Delete a group and all associated memberships.


@classmethod<EOL><INDENT>def query_requests(cls, admin, eager=False):<DEDENT>

<EOL>if hasattr(admin, '<STR_LIT>') and admin.is_superadmin:<EOL><INDENT>q1 = GroupAdmin.query.with_entities(<EOL>GroupAdmin.group_id)<EOL><DEDENT>else:<EOL><INDENT>q1 = GroupAdmin.query_by_admin(admin).with_entities(<EOL>GroupAdmin.group_id)<EOL><DEDENT>q2 = Membership.query.filter(<EOL>Membership.state == MembershipState.PENDING_ADMIN,<EOL>Membership.id_group.in_(q1),<EOL>)<EOL>q3 = Membership.query_by_user(<EOL>user=admin, state=MembershipState.ACTIVE<EOL>).with_entities(Membership.id_group)<EOL>q4 = GroupAdmin.query.filter(<EOL>GroupAdmin.admin_type == '<STR_LIT>', GroupAdmin.admin_id.in_(q3)<EOL>).with_entities(GroupAdmin.group_id)<EOL>q5 = Membership.query.filter(<EOL>Membership.state == MembershipState.PENDING_ADMIN,<EOL>Membership.id_group.in_(q4))<EOL>query = q2.union(q5)<EOL>return query<EOL>


if self.subscription_policy == SubscriptionPolicy.OPEN:<EOL><INDENT>return self.add_member(user)<EOL><DEDENT>elif self.subscription_policy == SubscriptionPolicy.APPROVAL:<EOL><INDENT>return self.add_member(user, state=MembershipState.PENDING_ADMIN)<EOL><DEDENT>elif self.subscription_policy == SubscriptionPolicy.CLOSED:<EOL><INDENT>return None<EOL><DEDENT>

Subscribe a user to a group (done by users).

        Wrapper around ``add_member()`` which checks subscription policy.

        :param user: User to subscribe.
        :returns: Newly created Membership or None.


@classmethod<EOL><INDENT>def search(cls, query, q):<DEDENT>

query = query.join(User).filter(<EOL>User.email.like('<STR_LIT>'.format(q)),<EOL>)<EOL>return query<EOL>

Modify query as so include only specific members.

        :param query: Query object.
        :param str q: Search string.
        :returs: Query object.


return cls.query.filter_by(<EOL>admin_type=resolve_admin_type(admin), admin_id=admin.get_id())<EOL>

Get all groups for for a specific admin.


if self.is_managed:<EOL><INDENT>return False<EOL><DEDENT>elif self.is_admin(user):<EOL><INDENT>return True<EOL><DEDENT>elif self.subscription_policy != SubscriptionPolicy.CLOSED:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT>

Determine if user can invite people to a group.

        Be aware that this check is independent from the people (users) which
        are going to be invited. The checked user is the one who invites
        someone, NOT who is going to be invited.

        :param user: User to be checked.
        :returns: True or False.


assert isinstance(names, list)<EOL>return cls.query.filter(cls.name.in_(names))<EOL>

Query group by a list of group names.

        :param list names: List of the group names.
        :returns: Query object.


@classmethod<EOL><INDENT>def get(cls, group, admin):<DEDENT>

try:<EOL><INDENT>ga = cls.query.filter_by(<EOL>group=group, admin_id=admin.get_id(),<EOL>admin_type=resolve_admin_type(admin)).one()<EOL>return ga<EOL><DEDENT>except Exception:<EOL><INDENT>return None<EOL><DEDENT>


return policy in [cls.OPEN, cls.APPROVAL, cls.CLOSED]<EOL>


if cls.validate(policy):<EOL><INDENT>return cls.descriptions[policy]<EOL><DEDENT>


@classmethod<EOL><INDENT>def order(cls, query, field, s):<DEDENT>

if s == '<STR_LIT>':<EOL><INDENT>query = query.order_by(asc(field))<EOL><DEDENT>elif s == '<STR_LIT>':<EOL><INDENT>query = query.order_by(desc(field))<EOL><DEDENT>return query<EOL>

Modify query as so to order the results.

        :param query: Query object.
        :param str s: Orderinig: ``asc`` or ``desc``.
        :returs: Query object.


assert emails is None or isinstance(emails, list)<EOL>results = []<EOL>for email in emails:<EOL><INDENT>try:<EOL><INDENT>user = User.query.filter_by(email=email).one()<EOL>results.append(self.invite(user))<EOL><DEDENT>except NoResultFound:<EOL><INDENT>results.append(None)<EOL><DEDENT><DEDENT>return results<EOL>

Invite users to a group by emails.

        :param list emails: Emails of users that shall be invited.
        :returns list: Newly created Memberships or Nones.


return GroupAdmin.create(self, admin)<EOL>

Invite an admin to a group.

        :param admin: Object to be added as an admin.
        :returns: GroupAdmin object.


@classmethod<EOL><INDENT>def query_by_user(cls, user, **kwargs):<DEDENT>

return cls._filter(<EOL>cls.query.filter_by(user_id=user.get_id()),<EOL>**kwargs<EOL>)<EOL>


'''<STR_LIT>'''<EOL>'''<STR_LIT>'''<EOL>global tmp_dir<EOL>tmp_dir = os.path.join('<STR_LIT>', str(uuid.uuid4()))<EOL>if not os.path.exists(tmp_dir):<EOL><INDENT>os.makedirs(tmp_dir)<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>" % tmp_dir)<EOL>return<EOL><DEDENT>

create a unique tmp dir to hold the downloaded local files


global port_test<EOL>command = '<STR_LIT>' % port_test<EOL>os.system(command)<EOL>time.sleep(<NUM_LIT:1>)<EOL>


global tmp_dir<EOL>shutil.rmtree(tmp_dir)<EOL>


global tmp_dir<EOL>tmp_dir = os.path.join('<STR_LIT>', '<STR_LIT>' + '<STR_LIT:.>' + str(uuid.uuid4()))   <EOL>if not os.path.exists(tmp_dir):<EOL><INDENT>os.makedirs(tmp_dir)<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>" % tmp_dir)<EOL>return<EOL><DEDENT>

create a unique tmp dir to hold the downloaded local files
if the tmp_dir grenerated already exists, then simply return
the user simply try again to generate another unique tmp dir
:return:


if isinstance(metrix, TimeSeries):<EOL><INDENT>return metrix<EOL><DEDENT>if isinstance(metrix, dict):<EOL><INDENT>return TimeSeries(metrix)<EOL><DEDENT>return TimeSeries(utils.read_csv(metrix))<EOL>

Load time series.
:param timeseries: a TimeSeries, a dictionary or a path to a csv file(str).
:return TimeSeries: a TimeSeries object.


output = defaultdict(list)<EOL>output_by_name = defaultdict(list)<EOL>scores = self.anomaly_detector.get_all_scores()<EOL>if self.anomalies:<EOL><INDENT>for anomaly in self.anomalies:<EOL><INDENT>metrix_scores = scores<EOL>start_t, end_t = anomaly.get_time_window()<EOL>t = anomaly.exact_timestamp<EOL>room = (end_t - start_t) / <NUM_LIT:2><EOL>if not room:<EOL><INDENT>room = <NUM_LIT:30><EOL><DEDENT>extended_start_t = start_t - room<EOL>extended_end_t = end_t + room<EOL>metrix_scores_cropped = metrix_scores.crop(extended_start_t, extended_end_t)<EOL>while len(metrix_scores_cropped) < <NUM_LIT:2>:<EOL><INDENT>extended_start_t = extended_start_t - room<EOL>extended_end_t = extended_end_t + room<EOL>metrix_scores_cropped = metrix_scores.crop(extended_start_t, extended_end_t)<EOL><DEDENT>for entry in self.related_metrices:<EOL><INDENT>try:<EOL><INDENT>entry_correlation_result = Correlator(self.metrix, entry, time_period=(extended_start_t, extended_end_t),<EOL>use_anomaly_score=True).get_correlation_result()<EOL>record = extended_start_t, extended_end_t, entry_correlation_result.__dict__, entry<EOL>record_by_name = extended_start_t, extended_end_t, entry_correlation_result.__dict__<EOL>output[t].append(record)<EOL>output_by_name[entry].append(record_by_name)<EOL><DEDENT>except exceptions.NotEnoughDataPoints:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT><DEDENT>self.output = output<EOL>self.output_by_name = output_by_name<EOL>

Analyzes if a matrix has anomalies.
If any anomaly is found, determine if the matrix correlates with any other matrixes.
To be implemented.


def __init__(self, metrix, related_metrices):

self.metrix = self._load(metrix)<EOL>self.anomaly_detector = AnomalyDetector(metrix)<EOL>self.related_metrices = related_metrices<EOL>self.anomalies = self.anomaly_detector.get_anomalies()<EOL>self._analyze()<EOL>

Initializer
:param metrix: a TimeSeries, a dictionary or a path to a csv file(str)
:param list related_metrixes: a list of time series.


return AnomalyDetector(time_series, score_only=True).get_all_scores()<EOL>

Get anomaly scores of a time series.
:param TimeSeries time_series: a time_series.


def __init__(self, time_series_a, time_series_b, time_period=None, use_anomaly_score=False, algorithm_name=None, algorithm_params=None):

self.time_series_a = self._load(time_series_a)<EOL>self.time_series_b = self._load(time_series_b)<EOL>if use_anomaly_score:<EOL><INDENT>self.time_series_a = self._get_anomaly_scores(self.time_series_a)<EOL>self.time_series_b = self._get_anomaly_scores(self.time_series_b)<EOL><DEDENT>if time_period:<EOL><INDENT>start_p, end_p = time_period<EOL>try:<EOL><INDENT>self.time_series_a = self.time_series_a.crop(start_p, end_p)<EOL>self.time_series_b = self.time_series_b.crop(start_p, end_p)<EOL><DEDENT>except ValueError:<EOL><INDENT>raise exceptions.NotEnoughDataPoints<EOL><DEDENT><DEDENT>self._sanity_check()<EOL>self.algorithm_params = {'<STR_LIT>': self.time_series_a, '<STR_LIT>': self.time_series_b}<EOL>self._get_algorithm_and_params(algorithm_name, algorithm_params)<EOL>self._correlate()<EOL>

Initializer
:param time_series_a: a TimeSeries, a dictionary or a path to a csv file(str).
:param time_series_b: a TimeSeries, a dictionary or a path to a csv file(str).
:param time_period: a tuple (start, end) representing a data period for considering correlation.
:param str algorithm_name: name of the algorithm to use.
:param dict algorithm_params: additional params for the specific algorithm.


def _get_algorithm_and_params(self, algorithm_name, algorithm_params):

algorithm_name = algorithm_name or CORRELATOR_ALGORITHM<EOL>try:<EOL><INDENT>self.algorithm = correlator_algorithms[algorithm_name]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise exceptions.AlgorithmNotFound('<STR_LIT>' + str(algorithm_name) + '<STR_LIT>')<EOL><DEDENT>if algorithm_params:<EOL><INDENT>if not isinstance(algorithm_params, dict):<EOL><INDENT>raise exceptions.InvalidDataFormat('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>self.algorithm_params = dict(algorithm_params.items() + self.algorithm_params.items())<EOL><DEDENT><DEDENT>

Get the specific algorithm and merge the algorithm params.
:param str algorithm: name of the algorithm to use.
:param dict algorithm_params: additional params for the specific algorithm.


return self.correlation_result if self.correlation_result.coefficient >= threshold else False<EOL>

Compare with a threshold to determine whether two timeseries correlate to each other.
:return: a CorrelationResult object if two time series correlate otherwise false.


if isinstance(time_series, TimeSeries):<EOL><INDENT>return time_series<EOL><DEDENT>if isinstance(time_series, dict):<EOL><INDENT>return TimeSeries(time_series)<EOL><DEDENT>return TimeSeries(utils.read_csv(time_series))<EOL>

Load time series into a TimeSeries object.
:param timeseries: a TimeSeries, a dictionary or a path to a csv file(str).
:return TimeSeries: a TimeSeries object.


if len(self.time_series_a) < <NUM_LIT:2> or len(self.time_series_b) < <NUM_LIT:2>:<EOL><INDENT>raise exceptions.NotEnoughDataPoints('<STR_LIT>')<EOL><DEDENT>

Check if the time series have more than two data points.


return getattr(self, '<STR_LIT>', [])<EOL>

Get anomalies.
:return list: a list of Anomaly objects.


return getattr(self, '<STR_LIT>', None)<EOL>

Get anomaly scores.
:return: a TimeSeries object represents anomaly scores.


try:<EOL><INDENT>algorithm = anomaly_detector_algorithms[algorithm_name]<EOL>return algorithm<EOL><DEDENT>except KeyError:<EOL><INDENT>raise exceptions.AlgorithmNotFound('<STR_LIT>' + str(algorithm_name) + '<STR_LIT>')<EOL><DEDENT>

Get the specific algorithm.
:param str algorithm_name: name of the algorithm to use(file name).
:return: algorithm object.


derivatives = []<EOL>for i, (timestamp, value) in enumerate(self.time_series_items):<EOL><INDENT>if i > <NUM_LIT:0>:<EOL><INDENT>pre_item = self.time_series_items[i - <NUM_LIT:1>]<EOL>pre_timestamp = pre_item[<NUM_LIT:0>]<EOL>pre_value = pre_item[<NUM_LIT:1>]<EOL>td = timestamp - pre_timestamp<EOL>derivative = (value - pre_value) / td if td != <NUM_LIT:0> else value - pre_value<EOL>derivative = abs(derivative)<EOL>derivatives.append(derivative)<EOL><DEDENT><DEDENT>if derivatives:<EOL><INDENT>derivatives.insert(<NUM_LIT:0>, derivatives[<NUM_LIT:0>])<EOL><DEDENT>self.derivatives = derivatives<EOL>


anom_scores_ema = self.exp_avg_detector.run()<EOL>anom_scores_deri = self.derivative_detector.run()<EOL>anom_scores = {}<EOL>for timestamp in anom_scores_ema.timestamps:<EOL><INDENT>anom_scores[timestamp] = max(anom_scores_ema[timestamp],<EOL>anom_scores_ema[timestamp] * DEFAULT_DETECTOR_EMA_WEIGHT + anom_scores_deri[timestamp] * (<NUM_LIT:1> - DEFAULT_DETECTOR_EMA_WEIGHT))<EOL>if anom_scores_ema[timestamp] > DEFAULT_DETECTOR_EMA_SIGNIFICANT:<EOL><INDENT>anom_scores[timestamp] = max(anom_scores[timestamp], anom_scores_deri[timestamp])<EOL><DEDENT><DEDENT>self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))<EOL>

Set anomaly scores using a weighted sum.


anom_scores = {}<EOL>for i, (timestamp, value) in enumerate(self.time_series.items()):<EOL><INDENT>baseline_value = self.baseline_time_series[i]<EOL>if baseline_value > <NUM_LIT:0>:<EOL><INDENT>diff_percent = <NUM_LIT:100> * (value - baseline_value) / baseline_value<EOL><DEDENT>elif value > <NUM_LIT:0>:<EOL><INDENT>diff_percent = <NUM_LIT><EOL><DEDENT>else:<EOL><INDENT>diff_percent = <NUM_LIT:0.0><EOL><DEDENT>anom_scores[timestamp] = <NUM_LIT:0.0><EOL>if self.percent_threshold_upper and diff_percent > <NUM_LIT:0> and diff_percent > self.percent_threshold_upper:<EOL><INDENT>anom_scores[timestamp] = diff_percent<EOL><DEDENT>if self.percent_threshold_lower and diff_percent < <NUM_LIT:0> and diff_percent < self.percent_threshold_lower:<EOL><INDENT>anom_scores[timestamp] = -<NUM_LIT:1> * diff_percent<EOL><DEDENT><DEDENT>self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))<EOL>

Compute anomaly scores for the time series
This algorithm just takes the diff of threshold with current value as anomaly score


raise NotImplementedError<EOL>

Compute anomaly scores for the time series.


return self.anom_scores<EOL>

Get anomaly scores for the time series.
:return TimeSeries: a TimeSeries representation of the anomaly scores.


self._set_scores()<EOL>return self.anom_scores<EOL>

Run the algorithm to get anomalies.
return list: a list of Anomaly objects.


lag_dicts = {}<EOL>fut_dicts = {}<EOL>length = self.time_series_length<EOL>lws = self.lag_window_size<EOL>fws = self.future_window_size<EOL>chunk_size = self.chunk_size<EOL>for i in range(length):<EOL><INDENT>if i < lws or i > length - fws:<EOL><INDENT>lag_dicts[i] = None<EOL><DEDENT>else:<EOL><INDENT>if lag_dicts[i - <NUM_LIT:1>] is None:<EOL><INDENT>lag_dict = self._construct_SAX_chunk_dict(self.sax[i - lws: i])<EOL>lag_dicts[i] = lag_dict<EOL>lw_leave_chunk = self.sax[<NUM_LIT:0>:chunk_size]<EOL>lw_enter_chunk = self.sax[i - chunk_size + <NUM_LIT:1>: i + <NUM_LIT:1>]<EOL>fut_dict = self._construct_SAX_chunk_dict(self.sax[i: i + fws])<EOL>fut_dicts[i] = fut_dict<EOL>fw_leave_chunk = self.sax[i: i + chunk_size]<EOL>fw_enter_chunk = self.sax[i + fws + <NUM_LIT:1> - chunk_size: i + fws + <NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>lag_dict = copy(lag_dicts[i - <NUM_LIT:1>])<EOL>lag_dict[lw_leave_chunk] -= <NUM_LIT:1><EOL>lag_dict[lw_enter_chunk] += <NUM_LIT:1><EOL>lag_dicts[i] = lag_dict<EOL>fut_dict = copy(fut_dicts[i - <NUM_LIT:1>])<EOL>fut_dict[fw_leave_chunk] -= <NUM_LIT:1><EOL>fut_dict[fw_enter_chunk] += <NUM_LIT:1><EOL>fut_dicts[i] = fut_dict<EOL>lw_leave_chunk = self.sax[i - lws: i - lws + chunk_size]<EOL>lw_enter_chunk = self.sax[i - chunk_size + <NUM_LIT:1>: i + <NUM_LIT:1>]<EOL>fw_leave_chunk = self.sax[i: i + chunk_size]<EOL>fw_enter_chunk = self.sax[i + fws + <NUM_LIT:1> - chunk_size: i + fws + <NUM_LIT:1>]<EOL><DEDENT><DEDENT><DEDENT>self.lag_dicts = lag_dicts<EOL>self.fut_dicts = fut_dicts<EOL>

Construct the chunk dicts for lagging window and future window at each index.
 e.g: Suppose we have a SAX sequence as '1234567890', both window sizes are 3, and the chunk size is 2.
 The first index that has a lagging window is 3. For index equals 3, the lagging window has sequence '123',
 the chunk to leave lagging window(lw_leave_chunk) is '12', and the chunk to enter lagging window(lw_enter_chunk) is '34'.
 Therefore, given chunk dicts at i, to compute chunk dicts at i+1, simply decrement the count for lw_leave_chunk,
 and increment the count for lw_enter_chunk from chunk dicts at i. Same method applies to future window as well.


sections = {}<EOL>self.value_min = self.time_series.min()<EOL>self.value_max = self.time_series.max()<EOL>section_height = (self.value_max - self.value_min) / self.precision<EOL>for section_number in range(self.precision):<EOL><INDENT>sections[section_number] = self.value_min + section_number * section_height<EOL><DEDENT>self.sax = '<STR_LIT>'.join(self._generate_SAX_single(sections, value) for value in self.time_series.values)<EOL>

Generate SAX representation for all values of the time series.


def __init__(self, time_series, baseline_time_series=None, precision=None, lag_window_size=None,<EOL>future_window_size=None, chunk_size=None):

super(BitmapDetector, self).__init__(self.__class__.__name__, time_series, baseline_time_series)<EOL>self.precision = precision if precision and precision > <NUM_LIT:0> else DEFAULT_BITMAP_PRECISION<EOL>self.chunk_size = chunk_size if chunk_size and chunk_size > <NUM_LIT:0> else DEFAULT_BITMAP_CHUNK_SIZE<EOL>if lag_window_size:<EOL><INDENT>self.lag_window_size = lag_window_size<EOL><DEDENT>else:<EOL><INDENT>self.lag_window_size = int(self.time_series_length * DEFAULT_BITMAP_LAGGING_WINDOW_SIZE_PCT)<EOL><DEDENT>if future_window_size:<EOL><INDENT>self.future_window_size = future_window_size<EOL><DEDENT>else:<EOL><INDENT>self.future_window_size = int(self.time_series_length * DEFAULT_BITMAP_LEADING_WINDOW_SIZE_PCT)<EOL><DEDENT>self._sanity_check()<EOL>

Initializer
:param TimeSeries time_series: a TimeSeries object.
:param TimeSeries baseline_time_series: baseline TimeSeries.
:param int precision: how many sections to categorize values.
:param int lag_window_size: lagging window size.
:param int future_window_size: future window size.
:param int chunk_size: chunk size.


frequency = defaultdict(int)<EOL>chunk_size = self.chunk_size<EOL>length = len(sax)<EOL>for i in range(length):<EOL><INDENT>if i + chunk_size <= length:<EOL><INDENT>chunk = sax[i: i + chunk_size]<EOL>frequency[chunk] += <NUM_LIT:1><EOL><DEDENT><DEDENT>return frequency<EOL>

Form a chunk frequency dictionary from a SAX representation.
:param str sax: a SAX representation.
:return dict: frequency dictionary for chunks in the SAX representation.


lag_window_chunk_dict = self.lag_dicts[i]<EOL>future_window_chunk_dict = self.fut_dicts[i]<EOL>score = <NUM_LIT:0><EOL>for chunk in lag_window_chunk_dict:<EOL><INDENT>if chunk in future_window_chunk_dict:<EOL><INDENT>score += math.pow(future_window_chunk_dict[chunk] - lag_window_chunk_dict[chunk], <NUM_LIT:2>)<EOL><DEDENT>else:<EOL><INDENT>score += math.pow(lag_window_chunk_dict[chunk], <NUM_LIT:2>)<EOL><DEDENT><DEDENT>for chunk in future_window_chunk_dict:<EOL><INDENT>if chunk not in lag_window_chunk_dict:<EOL><INDENT>score += math.pow(future_window_chunk_dict[chunk], <NUM_LIT:2>)<EOL><DEDENT><DEDENT>return score<EOL>

Compute distance difference between two windows' chunk frequencies,
which is then marked as the anomaly score of the data point on the window boundary in the middle.
:param int i: index of the data point between two windows.
:return float: the anomaly score.


def _find_first_bigger(self, timestamps, target, lower_bound, upper_bound):

while lower_bound < upper_bound:<EOL><INDENT>pos = lower_bound + (upper_bound - lower_bound) / <NUM_LIT:2><EOL>if timestamps[pos] > target:<EOL><INDENT>upper_bound = pos<EOL><DEDENT>else:<EOL><INDENT>lower_bound = pos + <NUM_LIT:1><EOL><DEDENT><DEDENT>return pos<EOL>

Find the first element in timestamps whose value is bigger than target.
param list values: list of timestamps(epoch number).
param target: target value.
param lower_bound: lower bound for binary search.
param upper_bound: upper bound for binary search.


def __init__(self, time_series_a, time_series_b, max_shift_seconds=None, shift_impact=None):

super(CrossCorrelator, self).__init__(self.__class__.__name__, time_series_a, time_series_b)<EOL>self.shift_impact = shift_impact or DEFAULT_SHIFT_IMPACT<EOL>if max_shift_seconds is not None:<EOL><INDENT>self.max_shift_milliseconds = max_shift_seconds<EOL><DEDENT>else:<EOL><INDENT>self.max_shift_milliseconds = DEFAULT_ALLOWED_SHIFT_SECONDS * <NUM_LIT:1000><EOL><DEDENT>

Initializer
:param TimeSeries time_series_a: TimeSeries a.
:param TimeSeries time_series_b: TimeSeries b.
:param int max_shift_milliseconds: allowed maximal shift seconds.
:param time_period: if given, correlate the data inside the time period only.


raise NotImplementedError<EOL>


return self.correlation_result<EOL>

Get correlation result.
:return CorrelationResult: a CorrelationResult object represents the correlation result.


def __init__(self, class_name, time_series_a, time_series_b):

self.class_name = class_name<EOL>self.time_series_a = time_series_a<EOL>self.time_series_b = time_series_b<EOL>

Initializer
:param class_name: name of extended class.
:param TimeSeries time_series_a: TimeSeries a.
:param TimeSeries time_series_b: TimeSeries b.


self._detect_correlation()<EOL>return self.correlation_result<EOL>

Execute algorithm.
:return CorrelationResult: a CorrelationResult object represents the correlation result.


Get root causes.
:return dict: a dict represents root causes for each anomaly.


try:<EOL><INDENT>t = float(t_str)<EOL>return t<EOL><DEDENT>except:<EOL><INDENT>for format in constants.TIMESTAMP_STR_FORMATS:<EOL><INDENT>try:<EOL><INDENT>t = datetime.datetime.strptime(t_str, format)<EOL>return float(time.mktime(t.utctimetuple()) * <NUM_LIT> + t.microsecond / <NUM_LIT>)<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>raise exceptions.InvalidDataFormat<EOL>

Covert a timestamp string to an epoch number.
:param str t_str: a timestamp string.
:return int: epoch number of the timestamp.


data = {}<EOL>if not isinstance(csv_name, (str, unicode)):<EOL><INDENT>raise exceptions.InvalidDataFormat('<STR_LIT>')<EOL><DEDENT>with open(csv_name, '<STR_LIT:r>') as csv_data:<EOL><INDENT>reader = csv.reader(csv_data, delimiter='<STR_LIT:U+002C>', quotechar='<STR_LIT:|>')<EOL>for row in reader:<EOL><INDENT>try:<EOL><INDENT>key = to_epoch(row[<NUM_LIT:0>])<EOL>value = float(row[<NUM_LIT:1>])<EOL>data[key] = value<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>return data<EOL>

Read data from a csv file into a dictionary.
:param str csv_name: path to a csv file.
:return dict: a dictionary represents the data in file.


def __init__(self, start_timestamp, end_timestamp, anomaly_score, exact_timestamp):

self.start_timestamp = start_timestamp<EOL>self.end_timestamp = end_timestamp<EOL>self.anomaly_score = anomaly_score<EOL>self.exact_timestamp = exact_timestamp<EOL>

Construct an anomaly object.
:param:start_timestamp: start time of the anomaly period.
:param:end_timestamp: end time of the anomaly period.
:param:anomly_score: the score of the anomaly.
:param:exact_timestamp: the timestamp within the period where the anomaly likely happened.


def _generic_binary_op(self, other, op):

output = {}<EOL>if isinstance(other, TimeSeries):<EOL><INDENT>for key, value in self.items():<EOL><INDENT>if key in other:<EOL><INDENT>try:<EOL><INDENT>result = op(value, other[key])<EOL>if result is NotImplemented:<EOL><INDENT>other_type = type(other[key])<EOL>other_op = vars(other_type).get(op.__name__)<EOL>if other_op:<EOL><INDENT>output[key] = other_op(other_type(value), other[key])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>output[key] = result<EOL><DEDENT><DEDENT>except ZeroDivisionError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>for key, value in self.items():<EOL><INDENT>try:<EOL><INDENT>result = op(value, other)<EOL>if result is NotImplemented:<EOL><INDENT>other_type = type(other)<EOL>other_op = vars(other_type).get(op.__name__)<EOL>if other_op:<EOL><INDENT>output[key] = other_op(other_type(value), other)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>output[key] = result<EOL><DEDENT><DEDENT>except ZeroDivisionError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT><DEDENT>if output:<EOL><INDENT>return TimeSeries(output)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>

Perform the method operation specified in the op parameter on the values
within the instance's time series values and either another time series
or a constant number value.

:param other: Time series of values or a constant number to use in calculations with instance's time series.
:param func op: The method to perform the calculation between the values.
:return: :class:`TimeSeries` object.


return numpy.asscalar(numpy.percentile(self.values, n)) if self.values else default<EOL>

Calculate the Nth Percentile value over the time series.

:param int n: Integer value of the percentile to calculate.
:param default: Value to return as a default should the calculation not be possible.
:return: Float representing the Nth percentile value or `None`.


return numpy.asscalar(numpy.max(self.values)) if self.values else default<EOL>

Calculate the maximum value over the time series.

:param default: Value to return as a default should the calculation not be possible.
:return: Float representing the maximum value or `None`.


return numpy.asscalar(numpy.sum(self.values)) if self.values else default<EOL>

Calculate the sum of all the values in the times series.

:param default: Value to return as a default should the calculation not be possible.
:return: Float representing the sum or `None`.