if len(self.factory.windowUnsubscribe[self.addr]) == self._window:<EOL><INDENT>raise MQTTWindowError("<STR_LIT>", self._window)<EOL><DEDENT>if not isinstance(request.topics, list):<EOL><INDENT>raise TopicTypeError(type(topic))<EOL><DEDENT>


cnx = self.addr<EOL>N = min(self._window - len(self.factory.windowPublish[cnx]), len(self.factory.queuePublishTx[cnx]))<EOL>for i in range(<NUM_LIT:0>,N):<EOL><INDENT>request = self.factory.queuePublishTx[cnx].popleft()<EOL>if request.msgId:   <EOL><INDENT>self.factory.windowPublish[cnx][request.msgId] = request<EOL><DEDENT>self._retryPublish(request, dup)<EOL><DEDENT>

Refills the Publisher transmission window from the queue


request.encoded[<NUM_LIT:0>] |=  (dup << <NUM_LIT:3>)   <EOL>request.dup = dup<EOL>if request.interval:    <EOL><INDENT>request.alarm = self.callLater(request.interval(len(request.encoded)), self._publishError, request)<EOL><DEDENT>if request.msgId is None:<EOL><INDENT>log.debug("<STR_LIT>", packet="<STR_LIT>", request=request, dup=dup)<EOL><DEDENT>else:<EOL><INDENT>log.debug("<STR_LIT>", packet="<STR_LIT>", request=request, dup=dup)<EOL><DEDENT>self.transport.write(str(request.encoded) if PY2 else bytes(request.encoded))<EOL>


if self._version == v31:<EOL><INDENT>reply.encoded[<NUM_LIT:0>] |=  (dup << <NUM_LIT:3>)   <EOL>reply.dup = dup<EOL><DEDENT>reply.alarm = self.callLater(reply.interval(), self._pubrelError, reply)<EOL>log.debug("<STR_LIT>", packet="<STR_LIT>", reply=reply, dup=dup)<EOL>self.transport.write(str(reply.encoded) if PY2 else bytes(reply.encoded))<EOL>


request.retries += <NUM_LIT:1><EOL>log.error("<STR_LIT>", packet="<STR_LIT>", request=request, timeout="<STR_LIT>")<EOL>self._retryPublish(request, dup=True)<EOL>


log.error("<STR_LIT>", packet="<STR_LIT>", timeout="<STR_LIT>")<EOL>self._retryRelease(reply, dup=True)<EOL>


if not ( <NUM_LIT:0><= request.qos < <NUM_LIT:3>):<EOL><INDENT>raise QoSValueError("<STR_LIT>",request.qos)<EOL><DEDENT>


<EOL>for _, reply in self.factory.windowPubRelease[self.addr].items():<EOL><INDENT>self._retryRelease(reply, dup=True)<EOL><DEDENT>for _, request in self.factory.windowPublish[self.addr].items():<EOL><INDENT>self._retryPublish(request, dup=True)<EOL><DEDENT>

Tries to restore the session state upon a new MQTT connection made (publisher)


<EOL>for k in list(self.factory.windowPublish[self.addr]):<EOL><INDENT>request = self.factory.windowPublish[self.addr][k]<EOL>del self.factory.windowPublish[self.addr][k]<EOL>request.deferred.errback(reason)<EOL><DEDENT>for k in list(self.factory.windowPubRelease[self.addr]):<EOL><INDENT>request = self.factory.windowPubRelease[self.addr][k]<EOL>del self.factory.windowPubRelease[self.addr][k]<EOL>request.deferred.errback(reason)<EOL><DEDENT>

Purges the persistent state in the client


<EOL>for _, request in self.factory.windowSubscribe[self.addr].items():<EOL><INDENT>if request.alarm is not None:<EOL><INDENT>request.alarm.cancel()<EOL>request.alarm = None<EOL><DEDENT><DEDENT>for _, request in self.factory.windowUnsubscribe[self.addr].items():<EOL><INDENT>if request.alarm is not None:<EOL><INDENT>request.alarm.cancel()<EOL>request.alarm = None<EOL><DEDENT><DEDENT>for _, request in self.factory.windowPublish[self.addr].items():<EOL><INDENT>if request.alarm is not None:<EOL><INDENT>request.alarm.cancel()<EOL>request.alarm = None<EOL><DEDENT><DEDENT>for _, request in self.factory.windowPubRelease[self.addr].items():<EOL><INDENT>if request.alarm is not None:<EOL><INDENT>request.alarm.cancel()<EOL>request.alarm = None<EOL><DEDENT><DEDENT>if self._cleanStart:<EOL><INDENT>for k in list(self.factory.windowSubscribe[self.addr]):<EOL><INDENT>request = self.factory.windowSubscribe[self.addr][k]<EOL>del self.factory.windowSubscribe[self.addr][k]<EOL>request.deferred.errback(reason)<EOL><DEDENT>for k in list(self.factory.windowUnsubscribe[self.addr]):<EOL><INDENT>request = self.factory.windowUnsubscribe[self.addr][k]<EOL>del self.factory.windowUnsubscribe[self.addr][k]<EOL>request.deferred.errback(reason)<EOL><DEDENT>self._purgeSession(reason)<EOL><DEDENT>


def __init__(self, initial=<NUM_LIT:2>, maxDelay=<NUM_LIT>, factor=<NUM_LIT:2>):

self.initial  = initial<EOL>self.factor   = factor<EOL>self.maxDelay = max(initial, maxDelay)<EOL>self._value   = self.initial<EOL>


self._value *= self.factor<EOL>self._value = min(self._value, self.maxDelay)<EOL>return self._value + random.random()<EOL>

Call the interval to produce a new delay time


def __init__(self, initial=<NUM_LIT:2>,  factor=<NUM_LIT:2>, bandwith=<NUM_LIT:1>):

self.initial  = initial<EOL>self.factor   = factor<EOL>self.bandwith = bandwith<EOL>self._k       = <NUM_LIT:1><EOL>self._value   = self.initial<EOL>


self._value = self.initial + (self._k*size)/self.bandwith<EOL>self._k    *= self.factor<EOL>return self._value + random.random()<EOL>

Call the interval to produce a new delay time taking into account the bandwith


self.id = (self.id + <NUM_LIT:1>) % <NUM_LIT><EOL>self.id = self.id or <NUM_LIT:1>   <EOL>return self.id<EOL>

Produce ids for Protocol packets, outliving their sessions


global logLevelFilterPredicate<EOL>observers = []<EOL>if console:<EOL><INDENT>observers.append( FilteringLogObserver(observer=textFileLogObserver(sys.stdout),  <EOL>predicates=[logLevelFilterPredicate] ))<EOL><DEDENT>if filepath is not None and filepath != "<STR_LIT>":<EOL><INDENT>observers.append( FilteringLogObserver(observer=textFileLogObserver(open(filepath,'<STR_LIT:a>')), <EOL>predicates=[logLevelFilterPredicate] ))<EOL><DEDENT>globalLogBeginner.beginLoggingTo(observers)<EOL>

Starts the global Twisted logger subsystem with maybe
stdout and/or a file specified in the config file


level = LogLevel.levelWithName(levelStr)<EOL>logLevelFilterPredicate.setLogLevelForNamespace(namespace=namespace, level=level)<EOL>

Set a new log level for a given namespace
LevelStr is: 'critical', 'error', 'warn', 'info', 'debug'


self.protocol                 = protocol<EOL>self.protocol.onDisconnection = self.onDisconnection<EOL>self.protocol.setWindowSize(<NUM_LIT:3>) <EOL>self.task = task.LoopingCall(self.publish)<EOL>self.task.start(<NUM_LIT>, now=False)<EOL>try:<EOL><INDENT>yield self.protocol.connect("<STR_LIT>", keepalive=<NUM_LIT>)<EOL><DEDENT>except Exception as e:<EOL><INDENT>log.error("<STR_LIT>", <EOL>broker=BROKER, excp=e)<EOL><DEDENT>else:<EOL><INDENT>log.info("<STR_LIT>", broker=BROKER)<EOL><DEDENT>


log.debug("<STR_LIT>", r=reason)<EOL>self.whenConnected().addCallback(self.connectToBroker)<EOL>

get notfied of disconnections
and get a deferred for a new protocol object (next retry)


self.protocol                 = protocol<EOL>self.protocol.onPublish       = self.onPublish<EOL>self.protocol.onDisconnection = self.onDisconnection<EOL>self.protocol.setWindowSize(<NUM_LIT:3>) <EOL>try:<EOL><INDENT>yield self.protocol.connect("<STR_LIT>", keepalive=<NUM_LIT>)<EOL>yield self.subscribe()<EOL><DEDENT>except Exception as e:<EOL><INDENT>log.error("<STR_LIT>", <EOL>broker=BROKER, excp=e)<EOL><DEDENT>else:<EOL><INDENT>log.info("<STR_LIT>", broker=BROKER)<EOL><DEDENT>


def onPublish(self, topic, payload, qos, dup, retain, msgId):

log.debug("<STR_LIT>", payload=payload)<EOL>

Callback Receiving messages from publisher


self.protocol                 = protocol<EOL>self.protocol.onPublish       = self.onPublish<EOL>self.protocol.onDisconnection = self.onDisconnection<EOL>self.protocol.setWindowSize(<NUM_LIT:3>)<EOL>self.task = task.LoopingCall(self.publish)<EOL>self.task.start(<NUM_LIT>, now=False) <EOL>try:<EOL><INDENT>yield self.protocol.connect("<STR_LIT>", keepalive=<NUM_LIT>)<EOL>yield self.subscribe()<EOL><DEDENT>except Exception as e:<EOL><INDENT>log.error("<STR_LIT>", <EOL>broker=BROKER, excp=e)<EOL><DEDENT>else:<EOL><INDENT>log.info("<STR_LIT>", broker=BROKER)<EOL><DEDENT>


def build_callback_url(request, urlname, message):

location = reverse(urlname, kwargs={"<STR_LIT>": message.pk})<EOL>callback_domain = getattr(settings, "<STR_LIT>", None)<EOL>if callback_domain:<EOL><INDENT>url = "<STR_LIT>".format(<EOL>"<STR_LIT>" if getattr(settings, "<STR_LIT>", False) else "<STR_LIT:http>",<EOL>callback_domain,<EOL>location<EOL>)<EOL><DEDENT>elif request is not None:<EOL><INDENT>url = request.build_absolute_uri(location)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>return url<EOL>

Build Twilio callback url for confirming message delivery status

:type message: OutgoingSMS


def send_sms(request, to_number, body, callback_urlname="<STR_LIT>"):

client = TwilioRestClient(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)<EOL>from_number = settings.TWILIO_PHONE_NUMBER<EOL>message = OutgoingSMS.objects.create(<EOL>from_number=from_number,<EOL>to_number=to_number,<EOL>body=body,<EOL>)<EOL>status_callback = None<EOL>if callback_urlname:<EOL><INDENT>status_callback = build_callback_url(request, callback_urlname, message)<EOL><DEDENT>logger.debug("<STR_LIT>",<EOL>to_number, status_callback, body)<EOL>if not getattr(settings, "<STR_LIT>", False):<EOL><INDENT>sent = client.sms.messages.create(<EOL>to=to_number,<EOL>from_=from_number,<EOL>body=body,<EOL>status_callback=status_callback<EOL>)<EOL>logger.debug("<STR_LIT>", sent.__dict__)<EOL>message.sms_sid = sent.sid<EOL>message.account_sid = sent.account_sid<EOL>message.status = sent.status<EOL>message.to_parsed = sent.to<EOL>if sent.price:<EOL><INDENT>message.price = Decimal(force_text(sent.price))<EOL>message.price_unit = sent.price_unit<EOL><DEDENT>message.sent_at = sent.date_created<EOL>message.save(update_fields=[<EOL>"<STR_LIT>", "<STR_LIT>", "<STR_LIT:status>", "<STR_LIT>",<EOL>"<STR_LIT>", "<STR_LIT>", "<STR_LIT>"<EOL>])<EOL><DEDENT>else:<EOL><INDENT>logger.info("<STR_LIT>", from_number, to_number, body)<EOL><DEDENT>return message<EOL>

Create :class:`OutgoingSMS` object and send SMS using Twilio.


@csrf_exempt<EOL>@wraps(f)<EOL>def decorator(request, *args, **kwargs):<EOL><INDENT>if request.method != "<STR_LIT:POST>":<EOL><INDENT>logger.error("<STR_LIT>", extra={"<STR_LIT>": request})<EOL>return HttpResponseNotAllowed(request.method)<EOL><DEDENT>if not getattr(settings, "<STR_LIT>"):<EOL><INDENT>try:<EOL><INDENT>validator = RequestValidator(settings.TWILIO_AUTH_TOKEN)<EOL>url = request.build_absolute_uri()<EOL>if "<STR_LIT>" in request.META:<EOL><INDENT>protocol = "<STR_LIT>" if request.META["<STR_LIT>"] == "<STR_LIT>" else "<STR_LIT:http>"<EOL>url = "<STR_LIT>".format(<EOL>protocol, request.META["<STR_LIT>"], request.META["<STR_LIT>"]<EOL>)<EOL><DEDENT>signature = request.META["<STR_LIT>"]<EOL><DEDENT>except (AttributeError, KeyError) as e:<EOL><INDENT>logger.exception("<STR_LIT>", extra={"<STR_LIT>": request})<EOL>return HttpResponseForbidden("<STR_LIT>" % e)<EOL><DEDENT>if not validator.validate(url, request.POST, signature):<EOL><INDENT>logger.error(<EOL>"<STR_LIT>",<EOL>url, request.POST, signature, extra={"<STR_LIT>": request}<EOL>)<EOL>return HttpResponseForbidden("<STR_LIT>")<EOL><DEDENT><DEDENT>response = f(request, *args, **kwargs)<EOL>if isinstance(response, six.text_type):<EOL><INDENT>return HttpResponse(response, mimetype="<STR_LIT>")<EOL><DEDENT>elif isinstance(response, Verb):<EOL><INDENT>return HttpResponse(force_text(response), mimetype="<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>return response<EOL><DEDENT><DEDENT>return decorator<EOL>

This decorator provides several helpful shortcuts for writing Twilio
    views.

        - It ensures that only requests from Twilio are passed through. This
          helps protect you from forged requests.

        - It ensures your view is exempt from CSRF checks via Django's
          @csrf_exempt decorator. This is necessary for any view that accepts
          POST requests from outside the local domain (eg: Twilio's servers).

        - It allows your view to (optionally) return TwiML to pass back to
          Twilio's servers instead of building a ``HttpResponse`` object
          manually.

        - It allows your view to (optionally) return any ``twilio.Verb`` object
          instead of building a ``HttpResponse`` object manually.

    Usage::

        from twilio.twiml import Response

        @twilio_view
        def my_view(request):
            r = Response()
            r.sms("Thanks for the SMS message!")
            return r


@register.simple_tag(takes_context = True)<EOL>def current(context, urlName, className = '<STR_LIT>', **kwargs):

matches = pathMatches(context['<STR_LIT>'].path, urlName, **kwargs)<EOL>return className if matches else '<STR_LIT>'<EOL>

Return a class name (string) if the current URL matches the route name specified in ``className``.
If any URL keyword arguments are provided, they must be matched as well.

:param urlName: The route name that the current URL should match. Example: 'accounts:login'.
:param className: The string that is returned if the current URL matches the specified one.
:param kwargs: Any extra URL keyword arguments that must also be present in the current URL for it to match.
:returns: ``className`` if the current URL matches, or an empty string otherwise.


def pathMatches(path, urlName, **kwargs):

resolved = urlresolvers.resolve(path)<EOL>resolvedName = '<STR_LIT>'.format(r = resolved) if resolved.namespace else resolved.url_name<EOL>if urlName != resolvedName:<EOL><INDENT>return False<EOL><DEDENT>for key, value in kwargs.items():<EOL><INDENT>currentArg = resolved.kwargs.get(key)<EOL>if currentArg is None or str(value) != str(currentArg):<EOL><INDENT>return False<EOL><DEDENT><DEDENT>return True<EOL>

:param path: str
:param urlName: str
:returns: bool.


class MixinManager(models.Manager, mixinClass):<EOL><INDENT>class MixinQuerySet(models.query.QuerySet, mixinClass):<EOL><INDENT>pass<EOL><DEDENT>def get_queryset(self):<EOL><INDENT>return self.MixinQuerySet(self.model, using = self._db)<EOL><DEDENT><DEDENT>return MixinManager()<EOL>

Use as a class decorator to add extra methods to your model manager.
Example usage:

        class Article(django.db.models.Model):
                published = models.DateTimeField()
                ...

                @extendManager
                class objects(object):
                        def getPublished(self):
                                return self.filter(published__lte = django.utils.timezone.now()).order_by('-published')

        ...

        publishedArticles = Article.objects.getPublished()


def simple_decorator(View):<EOL><INDENT>View.dispatch = method_decorator(function_decorator)(View.dispatch)<EOL>return View<EOL><DEDENT>return simple_decorator<EOL>

Convert a function based decorator into a class based decorator usable
    on class based Views.

    Can't subclass the `View` as it breaks inheritance (super in particular),
    so we monkey-patch instead.

    Based on http://stackoverflow.com/a/8429311


if '<STR_LIT>' in kwargs:<EOL><INDENT>query = kwargs.pop('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>query = None<EOL><DEDENT>base = urlresolvers.reverse(view, args = args, kwargs = kwargs)<EOL>if query:<EOL><INDENT>return '<STR_LIT>'.format(base, django.utils.http.urlencode(query))<EOL><DEDENT>else:<EOL><INDENT>return base<EOL><DEDENT>

User-friendly reverse. Pass arguments and keyword arguments to Django's `reverse`
as `args` and `kwargs` arguments, respectively.

The special optional keyword argument `query` is a dictionary of query (or GET) parameters
that can be appended to the `reverse`d URL.

Example:

        reverse('products:category', categoryId = 5, query = {'page': 2})

is equivalent to

        django.core.urlresolvers.reverse('products:category', kwargs = {'categoryId': 5}) + '?page=2'


try:<EOL><INDENT>dynamodb_client.create_table(<EOL>AttributeDefinitions=[<EOL>{<EOL>'<STR_LIT>': DYNAMODB_RUNID_ATTRIBUTE,<EOL>'<STR_LIT>': '<STR_LIT:S>'<EOL>},<EOL>],<EOL>TableName=table_name,<EOL>KeySchema=[<EOL>{<EOL>'<STR_LIT>': DYNAMODB_RUNID_ATTRIBUTE,<EOL>'<STR_LIT>': '<STR_LIT>'<EOL>},<EOL>],<EOL>ProvisionedThroughput={<EOL>'<STR_LIT>': <NUM_LIT:5>,<EOL>'<STR_LIT>': <NUM_LIT:5><EOL>}<EOL>)<EOL>dynamodb_client.get_waiter('<STR_LIT>').wait(TableName=table_name)<EOL><DEDENT>except ClientError as e:<EOL><INDENT>if e.response['<STR_LIT>']['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>raise e<EOL><DEDENT><DEDENT>

Create DynamoDB table for run manifests

    Arguments:
    dynamodb_client - boto3 DynamoDB client (not service)
    table_name - string representing existing table name


listing_finished = False                 <EOL>run_ids_buffer = []<EOL>last_continuation_token = None<EOL>(bucket, prefix) = split_full_path(full_path)<EOL>while not listing_finished:<EOL><INDENT>options = clean_dict({<EOL>'<STR_LIT>': bucket,<EOL>'<STR_LIT>': prefix,<EOL>'<STR_LIT>': '<STR_LIT:/>',<EOL>'<STR_LIT>': last_continuation_token<EOL>})<EOL>response = s3_client.list_objects_v2(**options)<EOL>keys = [extract_run_id(key['<STR_LIT>']) for key<EOL>in response.get('<STR_LIT>', [])]<EOL>run_ids_buffer.extend([key for key in keys if key is not None])<EOL>last_continuation_token = response.get('<STR_LIT>', None)<EOL>if not response['<STR_LIT>']:<EOL><INDENT>listing_finished = True<EOL><DEDENT><DEDENT>non_archived_run_ids = [run_id for run_id in run_ids_buffer<EOL>if not is_glacier(s3_client, bucket, run_id)]<EOL>return non_archived_run_ids<EOL>

Return list of all run ids inside S3 folder. It does not respect
    S3 pagination (`MaxKeys`) and returns **all** keys from bucket
    and won't list any prefixes with object archived to AWS Glacier

    Arguments:
    s3_client - boto3 S3 client (not service)
    full_path - full valid S3 path to events (such as enriched-archive)
                example: s3://acme-events-bucket/main-pipeline/enriched-archive


if path.startswith('<STR_LIT>'):<EOL><INDENT>path = path[<NUM_LIT:5>:]<EOL><DEDENT>elif path.startswith('<STR_LIT>'):<EOL><INDENT>path = path[<NUM_LIT:6>:]<EOL><DEDENT>elif path.startswith('<STR_LIT>'):<EOL><INDENT>path = path[<NUM_LIT:6>:]<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>parts = path.split('<STR_LIT:/>')<EOL>bucket = parts[<NUM_LIT:0>]<EOL>path = '<STR_LIT:/>'.join(parts[<NUM_LIT:1>:])<EOL>return bucket, normalize_prefix(path)<EOL>

Return pair of bucket without protocol and path

    Arguments:
    path - valid S3 path, such as s3://somebucket/events

    >>> split_full_path('s3://mybucket/path-to-events')
    ('mybucket', 'path-to-events/')
    >>> split_full_path('s3://mybucket')
    ('mybucket', None)
    >>> split_full_path('s3n://snowplow-bucket/some/prefix/')
    ('snowplow-bucket', 'some/prefix/')


def is_glacier(s3_client, bucket, prefix):

response = s3_client.list_objects_v2(Bucket=bucket,<EOL>Prefix=prefix,<EOL>MaxKeys=<NUM_LIT:3>)  <EOL>for key in response['<STR_LIT>']:<EOL><INDENT>if key.get('<STR_LIT>', '<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL>

Check if prefix is archived in Glacier, by checking storage class of
    first object inside that prefix

    Arguments:
    s3_client - boto3 S3 client (not service)
    bucket - valid extracted bucket (without protocol and prefix)
             example: sowplow-events-data
    prefix - valid S3 prefix (usually, run_id)
             example: snowplow-archive/enriched/archive/


filename = key.split('<STR_LIT:/>')[-<NUM_LIT:2>]  <EOL>run_id = filename.lstrip('<STR_LIT>')<EOL>try:<EOL><INDENT>datetime.strptime(run_id, '<STR_LIT>')<EOL>return key<EOL><DEDENT>except ValueError:<EOL><INDENT>return None<EOL><DEDENT>

Extract date part from run id

    Arguments:
    key - full key name, such as shredded-archive/run=2012-12-11-01-31-33/
          (trailing slash is required)

    >>> extract_run_id('shredded-archive/run=2012-12-11-01-11-33/')
    'shredded-archive/run=2012-12-11-01-11-33/'
    >>> extract_run_id('shredded-archive/run=2012-12-11-01-11-33')
    >>> extract_run_id('shredded-archive/run=2012-13-11-01-11-33/')


if path is None or path is '<STR_LIT>' or path is '<STR_LIT:/>':<EOL><INDENT>return None<EOL><DEDENT>elif path.endswith('<STR_LIT:/>'):<EOL><INDENT>return path<EOL><DEDENT>else:<EOL><INDENT>return path + '<STR_LIT:/>'<EOL><DEDENT>

Add trailing slash to prefix if it is not present

    >>> normalize_prefix("somepath")
    'somepath/'
    >>> normalize_prefix("somepath/")
    'somepath/'


if sys.version_info[<NUM_LIT:0>] < <NUM_LIT:3>:<EOL><INDENT>return {k: v for k, v in dict.iteritems() if v is not None}<EOL><DEDENT>else:<EOL><INDENT>return {k: v for k, v in dict.items() if v is not None}<EOL><DEDENT>

Remove all keys with Nones as values

    >>> clean_dict({'key': None})
    {}
    >>> clean_dict({'empty_s': ''})
    {'empty_s': ''}


def add_to_manifest(dynamodb_client, table_name, run_id):

dynamodb_client.put_item(<EOL>TableName=table_name,<EOL>Item={<EOL>DYNAMODB_RUNID_ATTRIBUTE: {<EOL>'<STR_LIT:S>': run_id<EOL>}<EOL>}<EOL>)<EOL>

Add run_id into DynamoDB manifest table

    Arguments:
    dynamodb_client - boto3 DynamoDB client (not service)
    table_name - string representing existing table name
    run_id - string representing run_id to store


def is_in_manifest(dynamodb_client, table_name, run_id):

response = dynamodb_client.get_item(<EOL>TableName=table_name,<EOL>Key={<EOL>DYNAMODB_RUNID_ATTRIBUTE: {<EOL>'<STR_LIT:S>': run_id<EOL>}<EOL>}<EOL>)<EOL>return response.get('<STR_LIT>') is not None<EOL>

Check if run_id is stored in DynamoDB table.
    Return True if run_id is stored or False otherwise.

    Arguments:
    dynamodb_client - boto3 DynamoDB client (not service)
    table_name - string representing existing table name
    run_id - string representing run_id to store


match = re.match(SCHEMA_URI_REGEX, uri)<EOL>if match:<EOL><INDENT>return {<EOL>'<STR_LIT>': match.group(<NUM_LIT:1>),<EOL>'<STR_LIT:name>': match.group(<NUM_LIT:2>),<EOL>'<STR_LIT>': match.group(<NUM_LIT:3>),<EOL>'<STR_LIT:version>': match.group(<NUM_LIT:4>)<EOL>}<EOL><DEDENT>else:<EOL><INDENT>raise SnowplowEventTransformationException([<EOL>"<STR_LIT>".format(uri, SCHEMA_URI)<EOL>])<EOL><DEDENT>

Extracts Schema information from Iglu URI

>>> extract_schema("iglu:com.acme-corporation_underscore/event_name-dash/jsonschema/1-10-1")['vendor']
'com.acme-corporation_underscore'


schema_dict = extract_schema(schema)<EOL>snake_case_organization = schema_dict['<STR_LIT>'].replace('<STR_LIT:.>', '<STR_LIT:_>').lower()<EOL>snake_case_name = re.sub('<STR_LIT>', '<STR_LIT>', schema_dict['<STR_LIT:name>']).lower()<EOL>model = schema_dict['<STR_LIT:version>'].split('<STR_LIT:->')[<NUM_LIT:0>]<EOL>return "<STR_LIT>".format(prefix, snake_case_organization, snake_case_name, model)<EOL>

Create an Elasticsearch field name from a schema string


my_json = json.loads(contexts)<EOL>data = my_json['<STR_LIT:data>']<EOL>distinct_contexts = {}<EOL>for context in data:<EOL><INDENT>schema = fix_schema("<STR_LIT>", context['<STR_LIT>'])<EOL>inner_data = context['<STR_LIT:data>']<EOL>if schema not in distinct_contexts:<EOL><INDENT>distinct_contexts[schema] = [inner_data]<EOL><DEDENT>else:<EOL><INDENT>distinct_contexts[schema].append(inner_data)<EOL><DEDENT><DEDENT>output = []<EOL>for key in distinct_contexts:<EOL><INDENT>output.append((key, distinct_contexts[key]))<EOL><DEDENT>return output<EOL>

Convert a contexts JSON to an Elasticsearch-compatible list of key-value pairs
For example, the JSON

{
  "data": [
    {
      "data": {
        "unique": true
      },
      "schema": "iglu:com.acme/unduplicated/jsonschema/1-0-0"
    },
    {
      "data": {
        "value": 1
      },
      "schema": "iglu:com.acme/duplicated/jsonschema/1-0-0"
    },
    {
      "data": {
        "value": 2
      },
      "schema": "iglu:com.acme/duplicated/jsonschema/1-0-0"
    }
  ],
  "schema": "iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0"
}

would become

[
  ("context_com_acme_duplicated_1", [{"value": 1}, {"value": 2}]),
  ("context_com_acme_unduplicated_1", [{"unique": true}])
]


my_json = json.loads(unstruct)<EOL>data = my_json['<STR_LIT:data>']<EOL>schema = data['<STR_LIT>']<EOL>if '<STR_LIT:data>' in data:<EOL><INDENT>inner_data = data['<STR_LIT:data>']<EOL><DEDENT>else:<EOL><INDENT>raise SnowplowEventTransformationException(["<STR_LIT>"])<EOL><DEDENT>fixed_schema = fix_schema("<STR_LIT>", schema)<EOL>return [(fixed_schema, inner_data)]<EOL>

Convert an unstructured event JSON to a list containing one Elasticsearch-compatible key-value pair
For example, the JSON

{
  "data": {
    "data": {
      "key": "value"
    },
    "schema": "iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-1"
  },
  "schema": "iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0"
}

would become

[
  (
    "unstruct_com_snowplowanalytics_snowplow_link_click_1", {
      "key": "value"
    }
  )
]


def transform(line, known_fields=ENRICHED_EVENT_FIELD_TYPES, add_geolocation_data=True):

return jsonify_good_event(line.split('<STR_LIT:\t>'), known_fields, add_geolocation_data)<EOL>

Convert a Snowplow enriched event TSV into a JSON


def jsonify_good_event(event, known_fields=ENRICHED_EVENT_FIELD_TYPES, add_geolocation_data=True):

if len(event) != len(known_fields):<EOL><INDENT>raise SnowplowEventTransformationException(<EOL>["<STR_LIT>".format(len(known_fields), len(event))]<EOL>)<EOL><DEDENT>else:<EOL><INDENT>output = {}<EOL>errors = []<EOL>if add_geolocation_data and event[LATITUDE_INDEX] != '<STR_LIT>' and event[LONGITUDE_INDEX] != '<STR_LIT>':<EOL><INDENT>output['<STR_LIT>'] = event[LATITUDE_INDEX] + '<STR_LIT:U+002C>' + event[LONGITUDE_INDEX]<EOL><DEDENT>for i in range(len(event)):<EOL><INDENT>key = known_fields[i][<NUM_LIT:0>]<EOL>if event[i] != '<STR_LIT>':<EOL><INDENT>try:<EOL><INDENT>kvpairs = known_fields[i][<NUM_LIT:1>](key, event[i])<EOL>for kvpair in kvpairs:<EOL><INDENT>output[kvpair[<NUM_LIT:0>]] = kvpair[<NUM_LIT:1>]<EOL><DEDENT><DEDENT>except SnowplowEventTransformationException as sete:<EOL><INDENT>errors += sete.error_messages<EOL><DEDENT>except Exception as e:<EOL><INDENT>errors += ["<STR_LIT>".format(<EOL>known_fields[i][<NUM_LIT:0>],<EOL>event[i],<EOL>repr(e)<EOL>)]<EOL><DEDENT><DEDENT><DEDENT>if errors:<EOL><INDENT>raise SnowplowEventTransformationException(errors)<EOL><DEDENT>else:<EOL><INDENT>return output<EOL><DEDENT><DEDENT>

Convert a Snowplow enriched event in the form of an array of fields into a JSON


try:<EOL><INDENT>args.query = '<STR_LIT:U+0020>'.join(args.query).replace('<STR_LIT:?>', '<STR_LIT>')<EOL>so = SOSearch(args.query, args.tags)<EOL>result =  so.first_q().best_answer.code<EOL>if result != None:<EOL><INDENT>print(result)<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT><DEDENT>except NoResult as e:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>


parser = argparse.ArgumentParser(description='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', help="<STR_LIT>", type=str, nargs='<STR_LIT:+>')<EOL>parser.add_argument('<STR_LIT>','<STR_LIT>', help='<STR_LIT>')<EOL>args = parser.parse_args()<EOL>main(args)<EOL>


unittest.main()<EOL>


rooted_leaf_samples, _ = self.live_data_copy()<EOL>rooted_file_samples = {}<EOL>for root, counts in rooted_leaf_samples.items():<EOL><INDENT>cur = {}<EOL>for key, count in counts.items():<EOL><INDENT>code, lineno = key<EOL>cur.setdefault(code.co_filename, <NUM_LIT:0>)<EOL>cur[code.co_filename] += count<EOL><DEDENT>rooted_file_samples[root] = cur<EOL><DEDENT>return rooted_file_samples<EOL>

Get sample counts by file, and root thread function.
(Useful for answering quesitons like "what modules are hot?")


rooted_leaf_samples, _ = self.live_data_copy()<EOL>rooted_line_samples = {}<EOL>for root, counts in rooted_leaf_samples.items():<EOL><INDENT>cur = {}<EOL>for key, count in counts.items():<EOL><INDENT>code, lineno = key<EOL>if code.co_filename != filename:<EOL><INDENT>continue<EOL><DEDENT>cur[lineno] = count<EOL><DEDENT>rooted_line_samples[root] = cur<EOL><DEDENT>return rooted_line_samples<EOL>

Get sample counts by line, and root thread function.
(For one file, specified as a parameter.)
This is useful for generating "side-by-side" views of
source code and samples.


rooted_leaf_samples, _ = self.live_data_copy()<EOL>line_samples = {}<EOL>for _, counts in rooted_leaf_samples.items():<EOL><INDENT>for key, count in counts.items():<EOL><INDENT>line_samples.setdefault(key, <NUM_LIT:0>)<EOL>line_samples[key] += count<EOL><DEDENT><DEDENT>return sorted(<EOL>line_samples.items(), key=lambda v: v[<NUM_LIT:1>], reverse=True)<EOL>

Get lines sampled accross all threads, in order
from most to least sampled.


flame_map = {}<EOL>_, stack_counts = self.live_data_copy()<EOL>for stack, count in stack_counts.items():<EOL><INDENT>root = stack[-<NUM_LIT:2>].co_name<EOL>stack_elements = []<EOL>for i in range(len(stack)):<EOL><INDENT>if type(stack[i]) in (int, long):<EOL><INDENT>continue<EOL><DEDENT>code = stack[i]<EOL>stack_elements.append("<STR_LIT>".format(<EOL>root, code.co_filename, code.co_name))<EOL><DEDENT>flame_key = '<STR_LIT:;>'.join(stack_elements)<EOL>flame_map.setdefault(flame_key, <NUM_LIT:0>)<EOL>flame_map[flame_key] += count<EOL><DEDENT>return flame_map<EOL>

return sampled stacks in form suitable for inclusion in a
flame graph (https://github.com/brendangregg/FlameGraph)


if self.started:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>self.started = True<EOL>self.thread = threading.Thread(target=self._run)<EOL>self.thread.daemon = True<EOL>self.thread.start()<EOL>

start a background thread that will sample ~50x per second


return lib.lsl_protocol_version()<EOL>

Protocol version.

    The major version is protocol_version() / 100;
    The minor version is protocol_version() % 100;

    Clients with different minor versions are protocol-compatible with each
    other while clients with different major versions will refuse to work
    together.


return lib.lsl_library_version()<EOL>

Version of the underlying liblsl library.

    The major version is library_version() / 100;
    The minor version is library_version() % 100;


return lib.lsl_library_info().decode('<STR_LIT:utf-8>')<EOL>

Get a string containing library information. The format of the string shouldn't be used
    for anything important except giving a a debugging person a good idea which exact library
    version is used.


return lib.lsl_local_clock()<EOL>

Obtain a local system time stamp in seconds.

    The resolution is better than a milisecond. This reading can be used to
    assign time stamps to samples as they are being acquired.

    If the "age" of a sample is known at a particular time (e.g., from USB
    transmission delays), it can be used as an offset to lsl_local_clock() to
    obtain a better estimate of when a sample was actually captured. See
    StreamOutlet.push_sample() for a use case.


<EOL>buffer = (c_void_p*<NUM_LIT>)()<EOL>num_found = lib.lsl_resolve_all(byref(buffer), <NUM_LIT>, c_double(wait_time))<EOL>return [StreamInfo(handle=buffer[k]) for k in range(num_found)]<EOL>

Resolve all streams on the network.

    This function returns all currently available streams from any outlet on
    the network. The network is usually the subnet specified at the local
    router, but may also include a group of machines visible to each other via
    multicast packets (given that the network supports it), or list of
    hostnames. These details may optionally be customized by the experimenter
    in a configuration file (see Network Connectivity in the LSL wiki).

    Keyword arguments:
    wait_time -- The waiting time for the operation, in seconds, to search for
                 streams. Warning: If this is too short (<0.5s) only a subset
                 (or none) of the outlets that are present on the network may
                 be returned. (default 1.0)

    Returns a list of StreamInfo objects (with empty desc field), any of which
    can subsequently be used to open an inlet. The full description can be
    retrieved from the inlet.


def resolve_byprop(prop, value, minimum=<NUM_LIT:1>, timeout=FOREVER):

<EOL>buffer = (c_void_p*<NUM_LIT>)()<EOL>num_found = lib.lsl_resolve_byprop(byref(buffer), <NUM_LIT>,<EOL>c_char_p(str.encode(prop)),<EOL>c_char_p(str.encode(value)),<EOL>minimum,<EOL>c_double(timeout))<EOL>return [StreamInfo(handle=buffer[k]) for k in range(num_found)]<EOL>

Resolve all streams with a specific value for a given property.

    If the goal is to resolve a specific stream, this method is preferred over
    resolving all streams and then selecting the desired one.

    Keyword arguments:
    prop -- The StreamInfo property that should have a specific value (e.g.,
            "name", "type", "source_id", or "desc/manufaturer").
    value -- The string value that the property should have (e.g., "EEG" as
             the type property).
    minimum -- Return at least this many streams. (default 1)
    timeout -- Optionally a timeout of the operation, in seconds. If the
               timeout expires, less than the desired number of streams
               (possibly none) will be returned. (default FOREVER)

    Returns a list of matching StreamInfo objects (with empty desc field), any
    of which can subsequently be used to open an inlet.

    Example: results = resolve_Stream_byprop("type","EEG")


def resolve_bypred(predicate, minimum=<NUM_LIT:1>, timeout=FOREVER):

<EOL>buffer = (c_void_p*<NUM_LIT>)()<EOL>num_found = lib.lsl_resolve_bypred(byref(buffer), <NUM_LIT>,<EOL>c_char_p(str.encode(predicate)),<EOL>minimum,<EOL>c_double(timeout))<EOL>return [StreamInfo(handle=buffer[k]) for k in range(num_found)]<EOL>

Resolve all streams that match a given predicate.

    Advanced query that allows to impose more conditions on the retrieved
    streams; the given string is an XPath 1.0 predicate for the <description>
    node (omitting the surrounding []'s), see also
    http://en.wikipedia.org/w/index.php?title=XPath_1.0&oldid=474981951.

    Keyword arguments:
    predicate -- The predicate string, e.g. "name='BioSemi'" or
                "type='EEG' and starts-with(name,'BioSemi') and
                 count(description/desc/channels/channel)=32"
    minimum -- Return at least this many streams. (default 1)
    timeout -- Optionally a timeout of the operation, in seconds. If the
               timeout expires, less than the desired number of streams
               (possibly none) will be returned. (default FOREVER)

    Returns a list of matching StreamInfo objects (with empty desc field), any
    of which can subsequently be used to open an inlet.


if type(errcode) is c_int:<EOL><INDENT>errcode = errcode.value<EOL><DEDENT>if errcode == <NUM_LIT:0>:<EOL><INDENT>pass  <EOL><DEDENT>elif errcode == -<NUM_LIT:1>:<EOL><INDENT>raise TimeoutError("<STR_LIT>")<EOL><DEDENT>elif errcode == -<NUM_LIT:2>:<EOL><INDENT>raise LostError("<STR_LIT>")<EOL><DEDENT>elif errcode == -<NUM_LIT:3>:<EOL><INDENT>raise InvalidArgumentError("<STR_LIT>")<EOL><DEDENT>elif errcode == -<NUM_LIT:4>:<EOL><INDENT>raise InternalError("<STR_LIT>")<EOL><DEDENT>elif errcode < <NUM_LIT:0>: <EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>

Error handler function. Translates an error code into an exception.


def __init__(self, name='<STR_LIT>', type='<STR_LIT>', channel_count=<NUM_LIT:1>,<EOL>nominal_srate=IRREGULAR_RATE, channel_format=cf_float32,<EOL>source_id='<STR_LIT>', handle=None):

if handle is not None:<EOL><INDENT>self.obj = c_void_p(handle)<EOL><DEDENT>else:<EOL><INDENT>if isinstance(channel_format, str):<EOL><INDENT>channel_format = string2fmt[channel_format]<EOL><DEDENT>self.obj = lib.lsl_create_streaminfo(c_char_p(str.encode(name)),<EOL>c_char_p(str.encode(type)),<EOL>channel_count,<EOL>c_double(nominal_srate),<EOL>channel_format,<EOL>c_char_p(str.encode(source_id)))<EOL>self.obj = c_void_p(self.obj)<EOL>if not self.obj:<EOL><INDENT>raise RuntimeError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>

Construct a new StreamInfo object.

        Core stream information is specified here. Any remaining meta-data can
        be added later.

        Keyword arguments:
        name -- Name of the stream. Describes the device (or product series)
                that this stream makes available (for use by programs,
                experimenters or data analysts). Cannot be empty.
        type -- Content type of the stream. By convention LSL uses the content
                types defined in the XDF file format specification where
                applicable (https://github.com/sccn/xdf). The content type is the
                preferred way to find streams (as opposed to searching by name).
        channel_count -- Number of channels per sample. This stays constant for
                         the lifetime of the stream. (default 1)
        nominal_srate -- The sampling rate (in Hz) as advertised by the data
                         source, regular (otherwise set to IRREGULAR_RATE).
                         (default IRREGULAR_RATE)
        channel_format -- Format/type of each channel. If your channels have
                          different formats, consider supplying multiple
                          streams or use the largest type that can hold
                          them all (such as cf_double64). It is also allowed
                          to pass this as a string, without the cf_ prefix,
                          e.g., 'float32' (default cf_float32)
        source_id -- Unique identifier of the device or source of the data, if
                     available (such as the serial number). This is critical
                     for system robustness since it allows recipients to
                     recover from failure even after the serving app, device or
                     computer crashes (just by finding a stream with the same
                     source id on the network again). Therefore, it is highly
                     recommended to always try to provide whatever information
                     can uniquely identify the data source itself.
                     (default '')


<EOL>try:<EOL><INDENT>lib.lsl_destroy_streaminfo(self.obj)<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>

Destroy a previously created StreamInfo object.


return lib.lsl_get_name(self.obj).decode('<STR_LIT:utf-8>')<EOL>

Name of the stream.

        This is a human-readable name. For streams offered by device modules,
        it refers to the type of device or product series that is generating
        the data of the stream. If the source is an application, the name may
        be a more generic or specific identifier. Multiple streams with the
        same name can coexist, though potentially at the cost of ambiguity (for
        the recording app or experimenter).


return lib.lsl_get_type(self.obj).decode('<STR_LIT:utf-8>')<EOL>

Content type of the stream.

        The content type is a short string such as "EEG", "Gaze" which
        describes the content carried by the channel (if known). If a stream
        contains mixed content this value need not be assigned but may instead
        be stored in the description of channel types. To be useful to
        applications and automated processing systems using the recommended
        content types is preferred.


return lib.lsl_get_channel_count(self.obj)<EOL>

Number of channels of the stream.

        A stream has at least one channel; the channel count stays constant for
        all samples.


return lib.lsl_get_nominal_srate(self.obj)<EOL>

Sampling rate of the stream, according to the source (in Hz).

        If a stream is irregularly sampled, this should be set to
        IRREGULAR_RATE.

        Note that no data will be lost even if this sampling rate is incorrect
        or if a device has temporary hiccups, since all samples will be
        transmitted anyway (except for those dropped by the device itself).
        However, when the recording is imported into an application, a good
        data importer may correct such errors more accurately if the advertised
        sampling rate was close to the specs of the device.


return lib.lsl_get_channel_format(self.obj)<EOL>

Channel format of the stream.

        All channels in a stream have the same format. However, a device might
        offer multiple time-synched streams each with its own format.


return lib.lsl_get_source_id(self.obj).decode('<STR_LIT:utf-8>')<EOL>

Unique identifier of the stream's source, if available.

        The unique source (or device) identifier is an optional piece of
        information that, if available, allows that endpoints (such as the
        recording program) can re-acquire a stream automatically once it is
        back online.


return lib.lsl_get_version(self.obj)<EOL>

Protocol version used to deliver the stream.


return lib.lsl_get_created_at(self.obj)<EOL>

Creation time stamp of the stream.

        This is the time stamp when the stream was first created
        (as determined via local_clock() on the providing machine).


return lib.lsl_get_uid(self.obj).decode('<STR_LIT:utf-8>')<EOL>

Unique ID of the stream outlet instance (once assigned).

        This is a unique identifier of the stream outlet, and is guaranteed to
        be different across multiple instantiations of the same outlet (e.g.,
        after a re-start).


return lib.lsl_get_session_id(self.obj).decode('<STR_LIT:utf-8>')<EOL>

Session ID for the given stream.

        The session id is an optional human-assigned identifier of the
        recording session. While it is rarely used, it can be used to prevent
        concurrent recording activitites on the same sub-network (e.g., in
        multiple experiment areas) from seeing each other's streams
        (can be assigned in a configuration file read by liblsl, see also
        Network Connectivity in the LSL wiki).


return lib.lsl_get_hostname(self.obj).decode('<STR_LIT:utf-8>')<EOL>


return XMLElement(lib.lsl_get_desc(self.obj))<EOL>

Extended description of the stream.

        It is highly recommended that at least the channel labels are described
        here. See code examples on the LSL wiki. Other information, such
        as amplifier settings, measurement units if deviating from defaults,
        setup information, subject information, etc., can be specified here, as
        well. Meta-data recommendations follow the XDF file format project
        (github.com/sccn/xdf/wiki/Meta-Data or web search for: XDF meta-data).

        Important: if you use a stream content type for which meta-data
        recommendations exist, please try to lay out your meta-data in
        agreement with these recommendations for compatibility with other
        applications.


return lib.lsl_get_xml(self.obj).decode('<STR_LIT:utf-8>')<EOL>

Retrieve the entire stream_info in XML format.

        This yields an XML document (in string form) whose top-level element is
        <description>. The description element contains one element for each
        field of the stream_info class, including:
        a) the core elements <name>, <type>, <channel_count>, <nominal_srate>,
           <channel_format>, <source_id>
        b) the misc elements <version>, <created_at>, <uid>, <session_id>,
           <v4address>, <v4data_port>, <v4service_port>, <v6address>,
           <v6data_port>, <v6service_port>
        c) the extended description element <desc> with user-defined
           sub-elements.


def __init__(self, info, chunk_size=<NUM_LIT:0>, max_buffered=<NUM_LIT>):

self.obj = lib.lsl_create_outlet(info.obj, chunk_size, max_buffered)<EOL>self.obj = c_void_p(self.obj)<EOL>if not self.obj:<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>self.channel_format = info.channel_format()<EOL>self.channel_count = info.channel_count()<EOL>self.do_push_sample = fmt2push_sample[self.channel_format]<EOL>self.do_push_chunk = fmt2push_chunk[self.channel_format]<EOL>self.value_type = fmt2type[self.channel_format]<EOL>self.sample_type = self.value_type*self.channel_count<EOL>

Establish a new stream outlet. This makes the stream discoverable.

        Keyword arguments:
        description -- The StreamInfo object to describe this stream. Stays
                constant over the lifetime of the outlet.
        chunk_size --- Optionally the desired chunk granularity (in samples)
                       for transmission. If unspecified, each push operation
                       yields one chunk. Inlets can override this setting.
                       (default 0)
        max_buffered -- Optionally the maximum amount of data to buffer (in
                        seconds if there is a nominal sampling rate, otherwise
                        x100 in samples). The default is 6 minutes of data.
                        Note that, for high-bandwidth data, you will want to
                        use a lower value here to avoid running out of RAM.
                        (default 360)


<EOL>try:<EOL><INDENT>lib.lsl_destroy_outlet(self.obj)<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>

Destroy an outlet.

        The outlet will no longer be discoverable after destruction and all
        connected inlets will stop delivering data.


def push_sample(self, x, timestamp=<NUM_LIT:0.0>, pushthrough=True):

if len(x) == self.channel_count:<EOL><INDENT>if self.channel_format == cf_string:<EOL><INDENT>x = [v.encode('<STR_LIT:utf-8>') for v in x]<EOL><DEDENT>handle_error(self.do_push_sample(self.obj, self.sample_type(*x),<EOL>c_double(timestamp),<EOL>c_int(pushthrough)))<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>

Push a sample into the outlet.

        Each entry in the list corresponds to one channel.

        Keyword arguments:
        x -- A list of values to push (one per channel).
        timestamp -- Optionally the capture time of the sample, in agreement
                     with local_clock(); if omitted, the current
                     time is used. (default 0.0)
        pushthrough -- Whether to push the sample through to the receivers
                       instead of buffering it with subsequent samples.
                       Note that the chunk_size, if specified at outlet
                       construction, takes precedence over the pushthrough flag.
                       (default True)


def push_chunk(self, x, timestamp=<NUM_LIT:0.0>, pushthrough=True):

try:<EOL><INDENT>n_values = self.channel_count * len(x)<EOL>data_buff = (self.value_type * n_values).from_buffer(x)<EOL>handle_error(self.do_push_chunk(self.obj, data_buff,<EOL>c_long(n_values),<EOL>c_double(timestamp),<EOL>c_int(pushthrough)))<EOL><DEDENT>except TypeError:<EOL><INDENT>if len(x):<EOL><INDENT>if type(x[<NUM_LIT:0>]) is list:<EOL><INDENT>x = [v for sample in x for v in sample]<EOL><DEDENT>if self.channel_format == cf_string:<EOL><INDENT>x = [v.encode('<STR_LIT:utf-8>') for v in x]<EOL><DEDENT>if len(x) % self.channel_count == <NUM_LIT:0>:<EOL><INDENT>constructor = self.value_type*len(x)<EOL>handle_error(self.do_push_chunk(self.obj, constructor(*x),<EOL>c_long(len(x)),<EOL>c_double(timestamp),<EOL>c_int(pushthrough)))<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>

Push a list of samples into the outlet.

        samples -- A list of samples, either as a list of lists or a list of
                   multiplexed values.
        timestamp -- Optionally the capture time of the most recent sample, in
                     agreement with local_clock(); if omitted, the current
                     time is used. The time stamps of other samples are
                     automatically derived according to the sampling rate of
                     the stream. (default 0.0)
        pushthrough Whether to push the chunk through to the receivers instead
                    of buffering it with subsequent samples. Note that the
                    chunk_size, if specified at outlet construction, takes
                    precedence over the pushthrough flag. (default True)


return bool(lib.lsl_have_consumers(self.obj))<EOL>

Check whether consumers are currently registered.

        While it does not hurt, there is technically no reason to push samples
        if there is no consumer.


return bool(lib.lsl_wait_for_consumers(self.obj, c_double(timeout)))<EOL>

Wait until some consumer shows up (without wasting resources).

        Returns True if the wait was successful, False if the timeout expired.


def __init__(self, info, max_buflen=<NUM_LIT>, max_chunklen=<NUM_LIT:0>, recover=True, processing_flags=<NUM_LIT:0>):

if type(info) is list:<EOL><INDENT>raise TypeError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>self.obj = lib.lsl_create_inlet(info.obj, max_buflen, max_chunklen,<EOL>recover)<EOL>self.obj = c_void_p(self.obj)<EOL>if not self.obj: <EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>if processing_flags > <NUM_LIT:0>:<EOL><INDENT>handle_error(lib.lsl_set_postprocessing(self.obj, processing_flags))<EOL><DEDENT>self.channel_format = info.channel_format()<EOL>self.channel_count = info.channel_count()<EOL>self.do_pull_sample = fmt2pull_sample[self.channel_format]<EOL>self.do_pull_chunk = fmt2pull_chunk[self.channel_format]<EOL>self.value_type = fmt2type[self.channel_format]<EOL>self.sample_type = self.value_type*self.channel_count<EOL>self.sample = self.sample_type()<EOL>self.buffers = {}<EOL>

Construct a new stream inlet from a resolved stream description.

        Keyword arguments:
        description -- A resolved stream description object (as coming from one
                of the resolver functions). Note: the stream_inlet may also be
                constructed with a fully-specified stream_info, if the desired
                channel format and count is already known up-front, but this is
                strongly discouraged and should only ever be done if there is
                no time to resolve the stream up-front (e.g., due to
                limitations in the client program).
        max_buflen -- Optionally the maximum amount of data to buffer (in
                      seconds if there is a nominal sampling rate, otherwise
                      x100 in samples). Recording applications want to use a
                      fairly large buffer size here, while real-time
                      applications would only buffer as much as they need to
                      perform their next calculation. (default 360)
        max_chunklen -- Optionally the maximum size, in samples, at which
                        chunks are transmitted (the default corresponds to the
                        chunk sizes used by the sender). Recording programs
                        can use a generous size here (leaving it to the network
                        how to pack things), while real-time applications may
                        want a finer (perhaps 1-sample) granularity. If left
                        unspecified (=0), the sender determines the chunk
                        granularity. (default 0)
        recover -- Try to silently recover lost streams that are recoverable
                   (=those that that have a source_id set). In all other cases
                   (recover is False or the stream is not recoverable)
                   functions may throw a lost_error if the stream's source is
                   lost (e.g., due to an app or computer crash). (default True)


<EOL>try:<EOL><INDENT>lib.lsl_destroy_inlet(self.obj)<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>

Destructor. The inlet will automatically disconnect if destroyed.


errcode = c_int()<EOL>result = lib.lsl_get_fullinfo(self.obj, c_double(timeout),<EOL>byref(errcode))<EOL>handle_error(errcode)<EOL>return StreamInfo(handle=result)<EOL>

Retrieve the complete information of the given stream.

        This includes the extended description. Can be invoked at any time of
        the stream's lifetime.

        Keyword arguments:
        timeout -- Timeout of the operation. (default FOREVER)

        Throws a TimeoutError (if the timeout expires), or LostError (if the
        stream source has been lost).


errcode = c_int()<EOL>lib.lsl_open_stream(self.obj, c_double(timeout), byref(errcode))<EOL>handle_error(errcode)<EOL>

Subscribe to the data stream.

        All samples pushed in at the other end from this moment onwards will be
        queued and eventually be delivered in response to pull_sample() or
        pull_chunk() calls. Pulling a sample without some preceding open_stream
        is permitted (the stream will then be opened implicitly).

        Keyword arguments:
        timeout -- Optional timeout of the operation (default FOREVER).

        Throws a TimeoutError (if the timeout expires), or LostError (if the
        stream source has been lost).


Drop the current data stream.

        All samples that are still buffered or in flight will be dropped and
        transmission and buffering of data for this inlet will be stopped. If
        an application stops being interested in data from a source
        (temporarily or not) but keeps the outlet alive, it should call
        lsl_close_stream() to not waste unnecessary system and network
        resources.


errcode = c_int()<EOL>result = lib.lsl_time_correction(self.obj, c_double(timeout),<EOL>byref(errcode))<EOL>handle_error(errcode)<EOL>return result<EOL>

Retrieve an estimated time correction offset for the given stream.

        The first call to this function takes several miliseconds until a
        reliable first estimate is obtained. Subsequent calls are instantaneous
        (and rely on periodic background updates). The precision of these
        estimates should be below 1 ms (empirically within +/-0.2 ms).

        Keyword arguments:
        timeout -- Timeout to acquire the first time-correction estimate
                   (default FOREVER).

        Returns the current time correction estimate. This is the number that
        needs to be added to a time stamp that was remotely generated via
        local_clock() to map it into the local clock domain of this
        machine.

        Throws a TimeoutError (if the timeout expires), or LostError (if the
        stream source has been lost).


def pull_sample(self, timeout=FOREVER, sample=None):

<EOL>if type(timeout) is list:<EOL><INDENT>assign_to = timeout<EOL>timeout = sample if type(sample) is float else <NUM_LIT:0.0><EOL><DEDENT>else:<EOL><INDENT>assign_to = None<EOL><DEDENT>errcode = c_int()<EOL>timestamp = self.do_pull_sample(self.obj, byref(self.sample),<EOL>self.channel_count, c_double(timeout),<EOL>byref(errcode))<EOL>handle_error(errcode)<EOL>if timestamp:<EOL><INDENT>sample = [v for v in self.sample]<EOL>if self.channel_format == cf_string:<EOL><INDENT>sample = [v.decode('<STR_LIT:utf-8>') for v in sample]<EOL><DEDENT>if assign_to is not None:<EOL><INDENT>assign_to[:] = sample<EOL><DEDENT>return sample, timestamp<EOL><DEDENT>else:<EOL><INDENT>return None, None<EOL><DEDENT>

Pull a sample from the inlet and return it.

        Keyword arguments:
        timeout -- The timeout for this operation, if any. (default FOREVER)
                   If this is passed as 0.0, then the function returns only a
                   sample if one is buffered for immediate pickup.

        Returns a tuple (sample,timestamp) where sample is a list of channel
        values and timestamp is the capture time of the sample on the remote
        machine, or (None,None) if no new sample was available. To remap this
        time stamp to the local clock, add the value returned by
        .time_correction() to it.

        Throws a LostError if the stream source has been lost. Note that, if
        the timeout expires, no TimeoutError is thrown (because this case is
        not considered an error).


def pull_chunk(self, timeout=<NUM_LIT:0.0>, max_samples=<NUM_LIT>, dest_obj=None):

<EOL>num_channels = self.channel_count<EOL>max_values = max_samples*num_channels<EOL>if max_samples not in self.buffers:<EOL><INDENT>self.buffers[max_samples] = ((self.value_type*max_values)(),<EOL>(c_double*max_samples)())<EOL><DEDENT>if dest_obj is not None:<EOL><INDENT>data_buff = (self.value_type * max_values).from_buffer(dest_obj)<EOL><DEDENT>else:<EOL><INDENT>data_buff = self.buffers[max_samples][<NUM_LIT:0>]<EOL><DEDENT>ts_buff = self.buffers[max_samples][<NUM_LIT:1>]<EOL>errcode = c_int()<EOL>num_elements = self.do_pull_chunk(self.obj, byref(data_buff),<EOL>byref(ts_buff), max_values,<EOL>max_samples, c_double(timeout),<EOL>byref(errcode))<EOL>handle_error(errcode)<EOL>num_samples = num_elements/num_channels<EOL>if dest_obj is None:<EOL><INDENT>samples = [[data_buff[s*num_channels+c] for c in range(num_channels)]<EOL>for s in range(int(num_samples))]<EOL>if self.channel_format == cf_string:<EOL><INDENT>samples = [[v.decode('<STR_LIT:utf-8>') for v in s] for s in samples]<EOL>free_char_p_array_memory(data_buff, max_values)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>samples = None<EOL><DEDENT>timestamps = [ts_buff[s] for s in range(int(num_samples))]<EOL>return samples, timestamps<EOL>

Pull a chunk of samples from the inlet.

        Keyword arguments:
        timeout -- The timeout of the operation; if passed as 0.0, then only
                   samples available for immediate pickup will be returned.
                   (default 0.0)
        max_samples -- Maximum number of samples to return. (default
                       1024)
        dest_obj -- A Python object that supports the buffer interface.
                    If this is provided then the dest_obj will be updated in place
                    and the samples list returned by this method will be empty.
                    It is up to the caller to trim the buffer to the appropriate
                    number of samples.
                    A numpy buffer must be order='C'
                    (default None)

        Returns a tuple (samples,timestamps) where samples is a list of samples
        (each itself a list of values), and timestamps is a list of time-stamps.

        Throws a LostError if the stream source has been lost.


return lib.lsl_samples_available(self.obj)<EOL>

Query whether samples are currently available for immediate pickup.

        Note that it is not a good idea to use samples_available() to determine
        whether a pull_*() call would block: to be sure, set the pull timeout
        to 0.0 or an acceptably low value. If the underlying implementation
        supports it, the value will be the number of samples available
        (otherwise it will be 1 or 0).


return bool(lib.lsl_was_clock_reset(self.obj))<EOL>

Query whether the clock was potentially reset since the last call.

        This is rarely-used function is only needed for applications that
        combine multiple time_correction values to estimate precise clock
        drift if they should tolerate cases where the source machine was
        hot-swapped or restarted.


self.e = c_void_p(handle)<EOL>

Construct new XML element from existing handle.


return XMLElement(lib.lsl_first_child(self.e))<EOL>


return XMLElement(lib.lsl_last_child(self.e))<EOL>


return XMLElement(lib.lsl_child(self.e, str.encode(name)))<EOL>


if name is None:<EOL><INDENT>return XMLElement(lib.lsl_next_sibling(self.e))<EOL><DEDENT>else:<EOL><INDENT>return XMLElement(lib.lsl_next_sibling_n(self.e, str.encode(name)))<EOL><DEDENT>

Get the next sibling in the children list of the parent node.

        If a name is provided, the next sibling with the given name is returned.


if name is None:<EOL><INDENT>return XMLElement(lib.lsl_previous_sibling(self.e))<EOL><DEDENT>else:<EOL><INDENT>return XMLElement(lib.lsl_previous_sibling_n(self.e,<EOL>str.encode(name)))<EOL><DEDENT>

Get the previous sibling in the children list of the parent node.

        If a name is provided, the previous sibling with the given name is
        returned.


return XMLElement(lib.lsl_parent(self.e))<EOL>


return bool(lib.lsl_empty(self.e))<EOL>


return bool(lib.lsl_is_text(self.e))<EOL>

Whether this is a text body (instead of an XML element).

        True both for plain char data and CData.


return lib.lsl_name(self.e).decode('<STR_LIT:utf-8>')<EOL>


return lib.lsl_value(self.e).decode('<STR_LIT:utf-8>')<EOL>


if name is None:<EOL><INDENT>res = lib.lsl_child_value(self.e)<EOL><DEDENT>else:<EOL><INDENT>res = lib.lsl_child_value_n(self.e, str.encode(name))<EOL><DEDENT>return res.decode('<STR_LIT:utf-8>')<EOL>

Get child value (value of the first child that is text).

        If a name is provided, then the value of the first child with the
        given name is returned.


def append_child_value(self, name, value):

return XMLElement(lib.lsl_append_child_value(self.e,<EOL>str.encode(name),<EOL>str.encode(value)))<EOL>

Append a child node with a given name, which has a (nameless)
        plain-text child with the given text value.


def prepend_child_value(self, name, value):

return XMLElement(lib.lsl_prepend_child_value(self.e,<EOL>str.encode(name),<EOL>str.encode(value)))<EOL>

Prepend a child node with a given name, which has a (nameless)
        plain-text child with the given text value.


return XMLElement(lib.lsl_set_child_value(self.e,<EOL>str.encode(name),<EOL>str.encode(value)))<EOL>

Set the text value of the (nameless) plain-text child of a named
        child node.


return bool(lib.lsl_set_name(self.e, str.encode(name)))<EOL>

Set the element's name. Returns False if the node is empty.


return bool(lib.lsl_set_value(self.e, str.encode(value)))<EOL>

Set the element's value. Returns False if the node is empty.


return XMLElement(lib.lsl_append_child(self.e, str.encode(name)))<EOL>

Append a child element with the specified name.


return XMLElement(lib.lsl_prepend_child(self.e, str.encode(name)))<EOL>

Prepend a child element with the specified name.


return XMLElement(lib.lsl_append_copy(self.e, elem.e))<EOL>

Append a copy of the specified element as a child.


return XMLElement(lib.lsl_prepend_copy(self.e, elem.e))<EOL>

Prepend a copy of the specified element as a child.


if type(rhs) is XMLElement:<EOL><INDENT>lib.lsl_remove_child(self.e, rhs.e)<EOL><DEDENT>else:<EOL><INDENT>lib.lsl_remove_child_n(self.e, rhs)<EOL><DEDENT>

Remove a given child element, specified by name or as element.


def __init__(self, prop=None, value=None, pred=None, forget_after=<NUM_LIT>):

if pred is not None:<EOL><INDENT>if prop is not None or value is not None:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>self.obj = lib.lsl_create_continuous_resolver_bypred(str.encode(pred),<EOL>c_double(forget_after))<EOL><DEDENT>elif prop is not None and value is not None:<EOL><INDENT>self.obj = lib.lsl_create_continuous_resolver_byprop(str.encode(prop),<EOL>str.encode(value),<EOL>c_double(forget_after))<EOL><DEDENT>elif prop is not None or value is not None:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>self.obj = lib.lsl_create_continuous_resolver(c_double(forget_after))<EOL><DEDENT>self.obj = c_void_p(self.obj)<EOL>if not self.obj:<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>

Construct a new continuous_resolver.

        Keyword arguments:
        forget_after -- When a stream is no longer visible on the network
                        (e.g., because it was shut down), this is the time in
                        seconds after which it is no longer reported by the
                        resolver.


<EOL>try:<EOL><INDENT>lib.lsl_destroy_continuous_resolver(self.obj)<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>


<EOL>buffer = (c_void_p*<NUM_LIT>)()<EOL>num_found = lib.lsl_resolver_results(self.obj, byref(buffer), <NUM_LIT>)<EOL>return [StreamInfo(handle=buffer[k]) for k in range(num_found)]<EOL>

Obtain the set of currently present streams on the network.

        Returns a list of matching StreamInfo objects (with empty desc
        field), any of which can subsequently be used to open an inlet.


return (np.abs(x) ** <NUM_LIT>).mean()<EOL>

Mean value of signal `x` squared.
    :param x: Dynamic quantity.
    :returns: Mean squared of `x`.


<EOL>if x is not None:<EOL><INDENT>x = ms(x)<EOL><DEDENT>else:<EOL><INDENT>x = <NUM_LIT:1.0><EOL><DEDENT>return y * np.sqrt( x / ms(y) )<EOL>

normalize power in y to a (standard normal) white noise signal.
    Optionally normalize to power in signal `x`.
    #The mean power of a Gaussian with :math:`\\mu=0` and :math:`\\sigma=1` is 1.


<EOL>uneven = N % <NUM_LIT:2><EOL>X = np.random.randn(N // <NUM_LIT:2> + <NUM_LIT:1> + uneven) + <NUM_LIT> * np.random.randn(N // <NUM_LIT:2> + <NUM_LIT:1> + uneven)<EOL>S = np.sqrt(np.arange(len(X)) + <NUM_LIT:1.>)  <EOL>y = (irfft(X / S)).real<EOL>if uneven:<EOL><INDENT>y = y[:-<NUM_LIT:1>]<EOL><DEDENT>return normalize(y)<EOL>

Pink noise.

:param N: Amount of samples.

Pink noise has equal power in bands that are proportionally wide.
Power density decreases with 3 dB per octave.


def __init__(self, Fs=<NUM_LIT:2>**<NUM_LIT>, FreqBeta=<NUM_LIT>, AmpBeta=<NUM_LIT>, AmpNoise=<NUM_LIT>, NCyclesPerChunk=<NUM_LIT:4>,<EOL>channels=["<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"]):

<EOL>self.FreqBeta = FreqBeta<EOL>self.AmpBeta = AmpBeta                                          <EOL>self.AmpNoise = AmpNoise                                        <EOL>self.channels = channels<EOL>chunk_dur = NCyclesPerChunk / self.FreqBeta           <EOL>chunk_len = int(Fs * chunk_dur)                  <EOL>self.tvec = <NUM_LIT:1.0> * (np.arange(chunk_len) + <NUM_LIT:1>) / Fs     <EOL>self.pinkNoiseGen = PinkNoiseGenerator(nSampsPerBlock=chunk_len)<EOL>raw_info = StreamInfo(name='<STR_LIT>', type='<STR_LIT>',<EOL>channel_count=len(self.channels), nominal_srate=Fs,<EOL>channel_format='<STR_LIT>', source_id='<STR_LIT>')<EOL>raw_xml = raw_info.desc()<EOL>chans = raw_xml.append_child("<STR_LIT>")<EOL>for channame in self.channels:<EOL><INDENT>chn = chans.append_child("<STR_LIT>")<EOL>chn.append_child_value("<STR_LIT:label>", channame)<EOL>chn.append_child_value("<STR_LIT>", "<STR_LIT>")<EOL>chn.append_child_value("<STR_LIT:type>", "<STR_LIT>")<EOL><DEDENT>self.eeg_outlet = StreamOutlet(raw_info)<EOL>print("<STR_LIT>")<EOL>self.last_time = local_clock()<EOL>

:param Fs:              Sampling rate
:param FreqBeta:        Central frequency of beta band
:param AmpBeta:         Amplitude of beta (uV)
:param AmpNoise:        Amplitude of pink noise (uV)
:param NCyclesPerChunk: Minimum number of cycles of beta in a chunk.
:param channels:        List of channel names


def __init__(self, class_list=[<NUM_LIT:1>, <NUM_LIT:3>], classes_rand=True, target_list=[<NUM_LIT:1>, <NUM_LIT:2>], targets_rand=True):

stream_name = '<STR_LIT>'<EOL>stream_type = '<STR_LIT>'<EOL>outlet_info = StreamInfo(name=stream_name, type=stream_type,<EOL>channel_count=<NUM_LIT:1>, nominal_srate=<NUM_LIT:0>,<EOL>channel_format='<STR_LIT:string>',<EOL>source_id='<STR_LIT>')<EOL>outlet_xml = outlet_info.desc()<EOL>channels_xml = outlet_xml.append_child("<STR_LIT>")<EOL>chan_xml = channels_xml.append_child("<STR_LIT>")<EOL>chan_xml.append_child_value("<STR_LIT:label>", "<STR_LIT>")<EOL>chan_xml.append_child_value("<STR_LIT:type>", "<STR_LIT>")<EOL>self.outlet = StreamOutlet(outlet_info)<EOL>print("<STR_LIT>".format(stream_name, stream_type))<EOL>self.class_list = class_list<EOL>self.classes_rand = classes_rand<EOL>self.target_list = target_list<EOL>self.targets_rand = targets_rand<EOL>self.next_transition = -<NUM_LIT:1><EOL>self.in_phase = '<STR_LIT>'<EOL>self.trial_ix = <NUM_LIT:0><EOL>self.class_id = self.class_list[<NUM_LIT:0>]<EOL>self.target_id = self.target_list[<NUM_LIT:0>]<EOL>

:param class_list:  A list of integers comprising different class ids. Default: [1, 3]
:param classes_rand: If True, classes are chosen randomly from list. If False, the list is cycled. Default: True
:param target_list: A list of integers comprising different target ids. Default: [1, 2]
:param targets_rand: If True, targets are chosen randomly from list. If False, the list is cycled. Default: True


try:<EOL><INDENT>return current_app.extensions["<STR_LIT>"]<EOL><DEDENT>except (AttributeError, KeyError):<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>


if not hasattr(app, "<STR_LIT>"):  <EOL><INDENT>app.extensions = {}<EOL><DEDENT>app.extensions["<STR_LIT>"] = self<EOL>@app.before_request<EOL>def start_context(*a, **k):<EOL><INDENT>self.overrides.push(Override())<EOL>self.additional.push(Additional())<EOL><DEDENT>@app.after_request<EOL>def cleanup(response):<EOL><INDENT>self.clear_all_overrides()<EOL>self.clear_all_additional()<EOL>return response<EOL><DEDENT>

Initializes the Flask-Allows object against the provided application


def requires(self, *requirements, **opts):

identity = opts.get("<STR_LIT>")<EOL>on_fail = opts.get("<STR_LIT>")<EOL>throws = opts.get("<STR_LIT>")<EOL>def decorator(f):<EOL><INDENT>@wraps(f)<EOL>def allower(*args, **kwargs):<EOL><INDENT>result = self.run(<EOL>requirements,<EOL>identity=identity,<EOL>on_fail=on_fail,<EOL>throws=throws,<EOL>f_args=args,<EOL>f_kwargs=kwargs,<EOL>)<EOL>if result is not None:<EOL><INDENT>return result<EOL><DEDENT>return f(*args, **kwargs)<EOL><DEDENT>return allower<EOL><DEDENT>return decorator<EOL>

Decorator to enforce requirements on routes

:param requirements: Collection of requirements to impose on view
:param throws: Optional, keyword only. Exception to throw for this
    route, if provided it takes precedence over the exception stored
    on the instance
:param on_fail: Optional, keyword only. Value or function to use as
    the on_fail for this route, takes precedence over the on_fail
    configured on the instance.


self._identity_loader = f<EOL>return f<EOL>

Used to provide an identity loader after initialization of the
extension.

Can be used as a method::

    allows.identity_loader(lambda: a_user)

Or as a decorator::

    @allows.identity_loader
    def load_user():
        return a_user

If an identity loader is provided at initialization, this method
will overwrite it.

:param f: Callable to load the current user


def fulfill(self, requirements, identity=None):

identity = identity or self._identity_loader()<EOL>if self.additional.current:<EOL><INDENT>all_requirements = chain(iter(self.additional.current), requirements)<EOL><DEDENT>else:<EOL><INDENT>all_requirements = iter(requirements)<EOL><DEDENT>if self.overrides.current is not None:<EOL><INDENT>all_requirements = (<EOL>r for r in all_requirements if r not in self.overrides.current<EOL>)<EOL><DEDENT>return all(_call_requirement(r, identity, request) for r in all_requirements)<EOL>

Checks that the provided or current identity meets each requirement
passed to this method.

This method takes into account both additional and overridden
requirements, with overridden requirements taking precedence::

    allows.additional.push(Additional(Has('foo')))
    allows.overrides.push(Override(Has('foo')))

    allows.fulfill([], user_without_foo)  # return True

:param requirements: The requirements to check the identity against.
:param identity: Optional. Identity to use in place of the current
    identity.


while self.overrides.current is not None:<EOL><INDENT>self.overrides.pop()<EOL><DEDENT>

Helper method to remove all override contexts, this is called automatically
during the after request phase in Flask. However it is provided here
if override contexts need to be cleared independent of the application
context.

If an override context is found that originated from an OverrideManager
instance not controlled by the Allows object, a ``RuntimeError``
will be raised.


while self.additional.current is not None:<EOL><INDENT>self.additional.pop()<EOL><DEDENT>

Helper method to remove all additional contexts, this is called
automatically during the after request phase in Flask. However it is
provided here if additional contexts need to be cleared independent of
the request cycle.

If an additional context is found that originated from an
AdditionalManager instance not controlled by the Allows object, a
``RuntimeError`` will be raised.


throws = throws or self.throws<EOL>on_fail = _make_callable(on_fail) if on_fail is not None else self.on_fail<EOL>if not self.fulfill(requirements, identity):<EOL><INDENT>result = on_fail(*f_args, **f_kwargs)<EOL>if use_on_fail_return and result is not None:<EOL><INDENT>return result<EOL><DEDENT>raise throws<EOL><DEDENT>

Used to preform a full run of the requirements and the options given,
this method will invoke on_fail and/or throw the appropriate exception
type. Can be passed arguments to call on_fail with via f_args (which are
passed positionally) and f_kwargs (which are passed as keyword).

:param requirements: The requirements to check
:param identity: Optional. A specific identity to use for the check
:param throws: Optional. A specific exception to throw for this check
:param on_fail: Optional. A callback to invoke after failure,
    alternatively a value to return when failure happens
:param f_args: Positional arguments to pass to the on_fail callback
:param f_kwargs: Keyword arguments to pass to the on_fail callback
:param use_on_fail_return: Boolean (default True) flag to determine
    if the return value should be used. If true, the return value
    will be considered, else failure will always progress to
    exception raising.


identity = opts.get("<STR_LIT>")<EOL>on_fail = opts.get("<STR_LIT>")<EOL>throws = opts.get("<STR_LIT>")<EOL>def decorator(f):<EOL><INDENT>@wraps(f)<EOL>def allower(*args, **kwargs):<EOL><INDENT>result = allows.run(<EOL>requirements,<EOL>identity=identity,<EOL>on_fail=on_fail,<EOL>throws=throws,<EOL>f_args=args,<EOL>f_kwargs=kwargs,<EOL>)<EOL>if result is not None:<EOL><INDENT>return result<EOL><DEDENT>return f(*args, **kwargs)<EOL><DEDENT>return allower<EOL><DEDENT>return decorator<EOL>

Standalone decorator to apply requirements to routes, either function
handlers or class based views::

    @requires(MyRequirement())
    def a_view():
        pass

    class AView(View):
        decorators = [requires(MyRequirement())]

:param requirements: The requirements to apply to this route
:param throws: Optional. Exception or exception instance to throw if
    authorization fails.
:param on_fail: Optional. Value or function to use when authorization
    fails.
:param identity: Optional. An identity to use in place of the currently
    loaded identity.


f.__allows_exempt__ = True<EOL>return f<EOL>

Used to exempt a route handler from ambient requirement handling unless it
is explicitly decorated with a requirement runner::

    @bp.route('/')
    @exempt_from_requirements
    def greeting_area():
        return "Hello!"

To use with a class based view, apply it to the class level decorators
attribute::

    class SomeCBV(View):
        decorators = [exempt_from_requirements]

        def get(self):
            return "Hello!"

.. note::

    You cannot exempt individual methods of a class based view with this
    decorator, e.g. the follow will not work::

        class SomeCBV(MethodView):

            @exempt_from_requirements
            def get(self):
                return "Hello!"

    Any permissioning applied at the blueprint level would still affect
    this route.

:param f: The route handler to be decorated.

.. versionadded:: 0.7.0


def guard_entire(requirements, identity=None, throws=None, on_fail=None):

def guarder():<EOL><INDENT>if _should_run_requirements():<EOL><INDENT>return allows.run(<EOL>requirements,<EOL>identity=identity,<EOL>on_fail=on_fail,<EOL>throws=throws,<EOL>f_kwargs=request.view_args,<EOL>)<EOL><DEDENT>return None<EOL><DEDENT>return guarder<EOL>

Used to protect an entire blueprint with a set of requirements. If a route
handler inside the blueprint should be exempt, then it may be decorated
with the :func:`~flask_allows.views.exempt_from_requirements` decorator.

This function should be registered as a before_request handler on the
blueprint and provided with the requirements to guard the blueprint with::

    my_bp = Blueprint(__name__, 'namespace')
    my_bp.before_request(guard_entire(MustBeLoggedIn()))

``identity``, ``on_fail`` and ``throws`` may also be provided but are optional.
If on_fails returns a non-None result, that will be considered the return
value of the routing::

    from flask import flash, redirect

    def flash_and_redirect(message, level, endpoint):
        def _(*a, **k):
            flash(message, level)
            return redirect(endpoint)
        return _

    bp = Blueprint(__name__, 'namespace')
    bp.before_request(
        guard_entire(
            [MustBeLoggedIn()],
            on_fail=flash_and_redirect(
                "Please login in first",
                "warning",
                "login"
            )
        )
    )

``on_fail`` will also receive anything found in
``flask.request.view_args`` as keyword arguments.

If needed, this guard may be applied multiple times. This may be useful
if different conditions should result in different `on_fail` mechanisms
being invoked::

    bp = Blueprint(__name__, "admin_panel")
    bp.before_request(
        guard_entire(
            [MustBeLoggedIn()],
            on_fail=flash_and_redirect(
                "Please login in first",
                "warning",
                "login"
            )
        )
    )
    bp.before_request(
        guard_entire(
            [MustBeAdmin()],
            on_fail=flash_and_redirect(
                "You are not an admin.",
                "danger",
                "index"
            )
        )
    )

:param requirements: An iterable of requirements to apply to every request
    routed to the blueprint.
:param identity: Optional. The identity that should be used for fulfilling
    requirements on the blueprint level.
:param throws: Optional. Exception or exception type to be thrown if
    authorization fails.
:param on_fail: Optional. Value or function to use if authorization fails.

.. versionadded: 0.7.0


rv = _additional_ctx_stack.top<EOL>if rv is None:<EOL><INDENT>return None<EOL><DEDENT>return rv[<NUM_LIT:1>]<EOL>

Proxy to the currently added requirements


def push(self, additional, use_parent=False):

current = self.current<EOL>if use_parent and current:<EOL><INDENT>additional = current + additional<EOL><DEDENT>_additional_ctx_stack.push((self, additional))<EOL>

Binds an additional to the current context, optionally use the
current additionals in conjunction with this additional

If ``use_parent`` is true, a new additional is created from the
parent and child additionals rather than manipulating either
directly.


rv = _additional_ctx_stack.pop()<EOL>if rv is None or rv[<NUM_LIT:0>] is not self:<EOL><INDENT>raise RuntimeError(<EOL>"<STR_LIT>".format(rv, self)<EOL>)<EOL><DEDENT>

Pops the latest additional context.

If the additional context was pushed by a different additional manager,
a ``RuntimeError`` is raised.


try:<EOL><INDENT>return _additional_ctx_stack.top[<NUM_LIT:1>]<EOL><DEDENT>except TypeError:<EOL><INDENT>return None<EOL><DEDENT>

Returns the current additional context if set otherwise None


@contextmanager<EOL><INDENT>def additional(self, additional, use_parent=False):<DEDENT>

self.push(additional, use_parent)<EOL>yield self.current<EOL>self.pop()<EOL>

Allows temporarily pushing an additional context, yields the new context
into the following block.


@wraps(f)<EOL>def wrapper(user):<EOL><INDENT>return f(user, request)<EOL><DEDENT>return wrapper<EOL>

Helper decorator for transitioning to user-only requirements, this aids
in situations where the request may be marked optional and causes an
incorrect flow into user-only requirements.

This decorator causes the requirement to look like a user-only requirement
but passes the current request context internally to the requirement.

This decorator is intended only to assist during a transitionary phase
and will be removed in flask-allows 1.0

See: :issue:`20,27`


@abstractmethod<EOL><INDENT>def fulfill(self, user, request=None):<DEDENT>

return NotImplemented<EOL>

Abstract method called to verify the requirement against the current
user and request.

.. versionchanged:: 0.5.0
    Passing request is now deprecated, pending removal in version 1.0.0

:param user: The current identity
:param request: The current request.


return cls(*requirements, op=operator.and_, until=False)<EOL>

Short cut helper to construct a combinator that uses
:meth:`operator.and_` to reduce requirement results and stops
evaluating on the first False.

This is also exported at the module level as ``And``


return cls(*requirements, op=operator.or_, until=True)<EOL>

Short cut helper to construct a combinator that uses
:meth:`operator.or_` to reduce requirement results and stops evaluating
on the first True.

This is also exported at the module level as ``Or``


return cls(*requirements, negated=True)<EOL>

Shortcut helper to negate a requirement or requirements.

This is also exported at the module as ``Not``


rv = _override_ctx_stack.top<EOL>if rv is None:<EOL><INDENT>return None<EOL><DEDENT>return rv[<NUM_LIT:1>]<EOL>

Proxy to the currently pushed override context.


def add(self, requirement, *requirements):

self._requirements.update((requirement,) + requirements)<EOL>

Adds one or more requirements to the override context.


def remove(self, requirement, *requirements):

self._requirements.difference_update((requirement,) + requirements)<EOL>

Removes one or more requirements from the override context.


return requirement in self._requirements<EOL>

Checks if a particular requirement is current overridden. Can also
be used as ``in``::

    override = Override()
    override.add(is_admin)
    override.is_overridden(is_admin)  # True
    is_admin in override  # True


def push(self, override, use_parent=False):

current = self.current<EOL>if use_parent and current:<EOL><INDENT>override = current + override<EOL><DEDENT>_override_ctx_stack.push((self, override))<EOL>

Binds an override to the current context, optionally use the
current overrides in conjunction with this override

If ``use_parent`` is true, a new override is created from the
parent and child overrides rather than manipulating either
directly.


rv = _override_ctx_stack.pop()<EOL>if rv is None or rv[<NUM_LIT:0>] is not self:<EOL><INDENT>raise RuntimeError(<EOL>"<STR_LIT>".format(rv, self)<EOL>)<EOL><DEDENT>

Pops the latest override context.

If the override context was pushed by a different override manager,
a ``RuntimeError`` is raised.


try:<EOL><INDENT>return _override_ctx_stack.top[<NUM_LIT:1>]<EOL><DEDENT>except TypeError:<EOL><INDENT>return None<EOL><DEDENT>

Returns the current override context if set otherwise None


@contextmanager<EOL><INDENT>def override(self, override, use_parent=False):<DEDENT>

self.push(override, use_parent)<EOL>yield self.current<EOL>self.pop()<EOL>

Allows temporarily pushing an override context, yields the new context
into the following block.


@abc.abstractmethod<EOL><INDENT>def extract_concepts(self, sentences=None, ids=None,<EOL>filename=None, composite_phrase=<NUM_LIT:4>, <EOL>file_format='<STR_LIT>', word_sense_disambiguation=True):<DEDENT>

Extract concepts from a list of sentences using MetaMap.


@abc.abstractmethod<EOL><INDENT>def extract_concepts(self, sentences=None, ids=None, filename=None):<DEDENT>

return<EOL>

Extract concepts from a list of sentences using MetaMapLite.


def __init__(self, metamap_filename, version=None):

MetaMap.__init__(self, metamap_filename, version)<EOL>

Interface to MetaMap using subprocess. This creates a
            command line call to a specified metamap process.


if allow_acronym_variants and unique_acronym_variants:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if (sentences is not None and filename is not None) or(sentences is None and filename is None):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if file_format not in ['<STR_LIT>','<STR_LIT>']:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>input_file = None<EOL>if sentences is not None:<EOL><INDENT>input_file = tempfile.NamedTemporaryFile(mode="<STR_LIT:wb>", delete=False)<EOL><DEDENT>else:<EOL><INDENT>input_file = open(filename, '<STR_LIT:r>')<EOL><DEDENT>output_file = tempfile.NamedTemporaryFile(mode="<STR_LIT:r>", delete=False)<EOL>error = None<EOL>try:<EOL><INDENT>if sentences is not None:<EOL><INDENT>if ids is not None:<EOL><INDENT>for identifier, sentence in zip(ids, sentences):<EOL><INDENT>input_file.write('<STR_LIT>'.format(identifier, sentence).encode('<STR_LIT:utf8>'))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for sentence in sentences:<EOL><INDENT>input_file.write('<STR_LIT>'.format(sentence).encode('<STR_LIT:utf8>'))<EOL><DEDENT><DEDENT>input_file.flush()<EOL><DEDENT>command = [self.metamap_filename, '<STR_LIT>']<EOL>command.append('<STR_LIT>')<EOL>command.append(str(composite_phrase))<EOL>if mm_data_version is not False:<EOL><INDENT>if mm_data_version not in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>command.append('<STR_LIT>')<EOL>command.append(str(mm_data_version))<EOL><DEDENT>if word_sense_disambiguation:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if strict_model:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if relaxed_model:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if allow_large_n:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if allow_overmatches:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if allow_concept_gaps:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if term_processing:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if no_derivational_variants:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if derivational_variants:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if ignore_word_order:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if allow_acronym_variants:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if unique_acronym_variants:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if prefer_multiple_concepts:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if ignore_stop_phrases:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if compute_all_mappings:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>if len(exclude_sources) > <NUM_LIT:0>:<EOL><INDENT>command.append('<STR_LIT>')<EOL>command.append(str('<STR_LIT:U+002C>'.join(exclude_sources)))<EOL><DEDENT>if len(restrict_to_sources) > <NUM_LIT:0>:<EOL><INDENT>command.append('<STR_LIT>')<EOL>command.append(str('<STR_LIT:U+002C>'.join(restrict_to_sources)))<EOL><DEDENT>if len(restrict_to_sts) > <NUM_LIT:0>:<EOL><INDENT>command.append('<STR_LIT>')<EOL>command.append(str('<STR_LIT:U+002C>'.join(restrict_to_sts)))<EOL><DEDENT>if len(exclude_sts) > <NUM_LIT:0>:<EOL><INDENT>command.append('<STR_LIT>')<EOL>command.append(str('<STR_LIT:U+002C>'.join(exclude_sts)))<EOL><DEDENT>if ids is not None or (file_format == '<STR_LIT>' and<EOL>sentences is None):<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>command.append(input_file.name)<EOL>command.append(output_file.name)<EOL>metamap_process = subprocess.Popen(command, stdout=subprocess.PIPE)<EOL>while metamap_process.poll() is None:<EOL><INDENT>stdout = str(metamap_process.stdout.readline())<EOL>if '<STR_LIT>' in stdout:<EOL><INDENT>metamap_process.terminate()<EOL>error = stdout.rstrip()<EOL><DEDENT><DEDENT>output = str(output_file.read())<EOL><DEDENT>finally:<EOL><INDENT>if sentences is not None:<EOL><INDENT>os.remove(input_file.name)<EOL><DEDENT>else:<EOL><INDENT>input_file.close()<EOL><DEDENT>os.remove(output_file.name)<EOL><DEDENT>concepts = Corpus.load(output.splitlines())<EOL>return (concepts, error)<EOL>

extract_concepts takes a list of sentences and ids(optional)
            then returns a list of Concept objects extracted via
            MetaMap.

            Supported Options:
                Composite Phrase -Q
                Word Sense Disambiguation -y
                use strict model -A
                use relaxed model -C
                allow large N -l
                allow overmatches -o
                allow concept gaps -g
                term processing -z
                No Derivational Variants -d
                All Derivational Variants -D
                Ignore Word Order -i
                Allow Acronym Variants -a
                Unique Acronym Variants -u
                Prefer Multiple Concepts -Y
                Ignore Stop Phrases -K
                Compute All Mappings -b
                MM Data Version -V
                Exclude Sources -e
                Restrict to Sources -R
                Restrict to Semantic Types -J
                Exclude Semantic Types -k

            For information about the available options visit
            http://metamap.nlm.nih.gov/.

            Note: If an error is encountered the process will be closed
                  and whatever was processed, if anything, will be
                  returned along with the error found.


MetaMapLite.__init__(self, metamap_filename=metamap_filename)<EOL>


def extract_concepts(self, sentences=None, ids=None, filename=None,<EOL>restrict_to_sts=None, restrict_to_sources=None):

if (sentences is not None and filename is not None) or(sentences is None and filename is None):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>input_file = None<EOL>if sentences is not None:<EOL><INDENT>input_file = tempfile.NamedTemporaryFile(mode="<STR_LIT:wb>", delete=False)<EOL><DEDENT>else:<EOL><INDENT>input_file = open(filename, '<STR_LIT:r>')<EOL><DEDENT>output_file_name = None<EOL>error = None<EOL>try:<EOL><INDENT>if sentences is not None:<EOL><INDENT>if ids is not None:<EOL><INDENT>for identifier, sentence in zip(ids, sentences):<EOL><INDENT>input_file.write('<STR_LIT>'.format(identifier, sentence).encode('<STR_LIT:utf8>'))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for sentence in sentences:<EOL><INDENT>input_file.write('<STR_LIT>'.format(sentence).encode('<STR_LIT:utf8>'))<EOL><DEDENT><DEDENT>input_file.flush()<EOL><DEDENT>command = ["<STR_LIT>", os.path.join(self.metamap_filename, "<STR_LIT>")]<EOL>if restrict_to_sts:<EOL><INDENT>if isinstance(restrict_to_sts, str):<EOL><INDENT>restrict_to_sts = [restrict_to_sts]<EOL><DEDENT>if len(restrict_to_sts) > <NUM_LIT:0>:<EOL><INDENT>command.append('<STR_LIT>')<EOL>command.append(str('<STR_LIT:U+002C>'.join(restrict_to_sts)))<EOL><DEDENT><DEDENT>if restrict_to_sources:<EOL><INDENT>if isinstance(restrict_to_sources, str):<EOL><INDENT>restrict_to_sources = [restrict_to_sources]<EOL><DEDENT>if len(restrict_to_sources) > <NUM_LIT:0>:<EOL><INDENT>command.append('<STR_LIT>')<EOL>command.append(str('<STR_LIT:U+002C>'.join(restrict_to_sources)))<EOL><DEDENT><DEDENT>if ids is not None:<EOL><INDENT>command.append('<STR_LIT>')<EOL><DEDENT>command.append(input_file.name)<EOL>metamap_process = subprocess.Popen(command, stdout=subprocess.PIPE)<EOL>while metamap_process.poll() is None:<EOL><INDENT>stdout = str(metamap_process.stdout.readline())<EOL>if '<STR_LIT>' in stdout:<EOL><INDENT>metamap_process.terminate()<EOL>error = stdout.rstrip()<EOL><DEDENT><DEDENT>output_file_name, file_extension = os.path.splitext(input_file.name)<EOL>output_file_name += "<STR_LIT:.>" + "<STR_LIT>"<EOL>with open(output_file_name) as fd:<EOL><INDENT>output = fd.read()<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>if sentences is not None:<EOL><INDENT>os.remove(input_file.name)<EOL><DEDENT>else:<EOL><INDENT>input_file.close()<EOL><DEDENT>os.remove(output_file_name)<EOL><DEDENT>concepts = CorpusLite.load(output.splitlines())<EOL>return concepts, error<EOL>

extract_concepts takes a list of sentences and ids(optional)
            then returns a list of Concept objects extracted via
            MetaMapLite.

            Supported Options:
                Restrict to Semantic Types --restrict_to_sts
                Restrict to Sources --restrict_to_sources

            For information about the available options visit
            http://metamap.nlm.nih.gov/.

            Note: If an error is encountered the process will be closed
                  and whatever was processed, if anything, will be
                  returned along with the error found.


if short == '<STR_LIT:a>':<EOL><INDENT>return RDF.type<EOL><DEDENT>for prefix in sorted(self.__prefixes, key=lambda x: len(x), reverse=True):<EOL><INDENT>if short.startswith(prefix):<EOL><INDENT>return URIRef(short.replace(prefix + '<STR_LIT::>', self.__prefixes[prefix]))<EOL><DEDENT><DEDENT>return short<EOL>

Extend a prefixed uri with the help of a specific dictionary of prefixes
:param short: Prefixed uri to be extended
:return:


def _create_regex(self, line, intent_name):

try:<EOL><INDENT>return re.compile(self._create_intent_pattern(line, intent_name),<EOL>re.IGNORECASE)<EOL><DEDENT>except sre_constants.error as e:<EOL><INDENT>LOG.warning('<STR_LIT>'<EOL>'<STR_LIT>'.format(line, intent_name))<EOL>return None<EOL><DEDENT>

Create regex and return. If error occurs returns None.


lock = RLock()<EOL>def synchronized(func):<EOL><INDENT>@wraps(func)<EOL>def wrapper(*args, **kwargs):<EOL><INDENT>with lock:<EOL><INDENT>return func(*args, **kwargs)<EOL><DEDENT><DEDENT>return wrapper<EOL><DEDENT>class MonopriceSync(Monoprice):<EOL><INDENT>def __init__(self, port_url):<EOL><INDENT>self._port = serial.serial_for_url(port_url, do_not_open=True)<EOL>self._port.baudrate = <NUM_LIT><EOL>self._port.stopbits = serial.STOPBITS_ONE<EOL>self._port.bytesize = serial.EIGHTBITS<EOL>self._port.parity = serial.PARITY_NONE<EOL>self._port.timeout = TIMEOUT<EOL>self._port.write_timeout = TIMEOUT<EOL>self._port.open()<EOL><DEDENT>def _process_request(self, request: bytes, skip=<NUM_LIT:0>):<EOL><INDENT>"""<STR_LIT>"""<EOL>_LOGGER.debug('<STR_LIT>', request)<EOL>self._port.reset_output_buffer()<EOL>self._port.reset_input_buffer()<EOL>self._port.write(request)<EOL>self._port.flush()<EOL>result = bytearray()<EOL>while True:<EOL><INDENT>c = self._port.read(<NUM_LIT:1>)<EOL>if not c:<EOL><INDENT>raise serial.SerialTimeoutException(<EOL>'<STR_LIT>'.format([hex(a) for a in result]))<EOL><DEDENT>result += c<EOL>if len(result) > skip and result[-LEN_EOL:] == EOL:<EOL><INDENT>break<EOL><DEDENT><DEDENT>ret = bytes(result)<EOL>_LOGGER.debug('<STR_LIT>', ret)<EOL>return ret.decode('<STR_LIT:ascii>')<EOL><DEDENT>@synchronized<EOL>def zone_status(self, zone: int):<EOL><INDENT>return ZoneStatus.from_string(self._process_request(_format_zone_status_request(zone), skip=<NUM_LIT:6>))<EOL><DEDENT>@synchronized<EOL>def set_power(self, zone: int, power: bool):<EOL><INDENT>self._process_request(_format_set_power(zone, power))<EOL><DEDENT>@synchronized<EOL>def set_mute(self, zone: int, mute: bool):<EOL><INDENT>self._process_request(_format_set_mute(zone, mute))<EOL><DEDENT>@synchronized<EOL>def set_volume(self, zone: int, volume: int):<EOL><INDENT>self._process_request(_format_set_volume(zone, volume))<EOL><DEDENT>@synchronized<EOL>def set_treble(self, zone: int, treble: int):<EOL><INDENT>self._process_request(_format_set_treble(zone, treble))<EOL><DEDENT>@synchronized<EOL>def set_bass(self, zone: int, bass: int):<EOL><INDENT>self._process_request(_format_set_bass(zone, bass))<EOL><DEDENT>@synchronized<EOL>def set_balance(self, zone: int, balance: int):<EOL><INDENT>self._process_request(_format_set_balance(zone, balance))<EOL><DEDENT>@synchronized<EOL>def set_source(self, zone: int, source: int):<EOL><INDENT>self._process_request(_format_set_source(zone, source))<EOL><DEDENT>@synchronized<EOL>def restore_zone(self, status: ZoneStatus):<EOL><INDENT>self.set_power(status.zone, status.power)<EOL>self.set_mute(status.zone, status.mute)<EOL>self.set_volume(status.zone, status.volume)<EOL>self.set_treble(status.zone, status.treble)<EOL>self.set_bass(status.zone, status.bass)<EOL>self.set_balance(status.zone, status.balance)<EOL>self.set_source(status.zone, status.source)<EOL><DEDENT><DEDENT>return MonopriceSync(port_url)<EOL>

Return synchronous version of Monoprice interface
:param port_url: serial port, i.e. '/dev/ttyUSB0'
:return: synchronous implementation of Monoprice interface