text:
from bottle import route, Route, request, default_app, view, HTTPError, response

from redis import StrictRedis
from redis.utils import pipeline

import json
import uwsgi
import os
import logging
import requests

from config import get_config

from worker import get_cache_key, get_wait_key, get_queue_key
from worker import init_redis

application = None

ERROR_RESP = {'archived': False, 'queued': False, 'error': {'msg': 'unknown'}}

def init():
    """ Init the application and add routes """

    logging.basicConfig(format='%(asctime)s: [%(levelname)s]: %(message)s',
                        level=logging.DEBUG)

    global theconfig
    theconfig = get_config()

    global rc
    rc = init_redis(theconfig)

    app = default_app()

    return app

@route(['/', '/index.html', '/index.htm'])
@view('index')
def home():
    return {'archives': theconfig['archives'],
            'default_archive': theconfig.get('default_archive')}

def get_params():
    url = request.query.get('url')

    archive = request.query.get('archive')

    browser_type = request.query.get('browser', 'chrome')

    if not url:
        raise HTTPError(status=400, body='No url= specified')

    if archive not in theconfig['archives']:
        raise HTTPError(status=400, body='No archive {0}'.format(archive))

    if not url.startswith(('http://', 'https://')):
        url = 'http://' + url

    return browser_type, archive, url

@route('/archivepage')
def archive_page():
    browser_type, archive, url = get_params()

    response_key = get_cache_key(archive, browser_type, url)
    wait_key = get_wait_key(archive, browser_type, url)

    queue_key = get_queue_key(browser_type)

    result = None

    if not rc.exists(response_key):
        cmd = dict(request.query)
        cmd['url'] = url

        num = rc.incr('total_urls:' + browser_type)
        cmd['num'] = num

        cmd = json.dumps(cmd)

        with pipeline(rc) as pi:
            waiting_str = {'archived': False,
                           'queued': True,
                           'num': num}

            pi.set(response_key, json.dumps(waiting_str))
            pi.rpush(queue_key, cmd)

        rc.blpop(wait_key, theconfig['wait_timeout_secs'])

    result = rc.get(response_key)

    if result:
        result = json.loads(result)

        if 'queued' in result:
            result['queue_pos'] = 0
            front = rc.lindex(queue_key, 0)
            if front:
                front = json.loads(front)
                front_num = front.get('num', 0)

                # pos == 1 implies this url is next up
                # pos <= 0 implies this url was removed from queue and is being processed
                pos = result['num'] - front_num + 1
                result['queue_pos'] = pos
        else:
            result['ttl'] = rc.ttl(response_key)
    else:
        result = ERROR_RESP

    return result

@route('/download')
def download():
    browser_type, archive, url = get_params()

    response_key = get_cache_key(archive, browser_type, url)

    result = rc.get(response_key)
    if not result:
        raise HTTPError(status=404, body='Url Not Archived')

    result = json.loads(result)
    if not 'download_url' in result:
        raise HTTPError(status=404, body='Download Not Available')

    headers = {}
    session = result.get('download_session')

    if session:
        headers['Cookie'] = session

    r = requests.get(result['download_url'],
                     headers=headers,
                     stream=True)

    if r.status_code != 200:
        raise HTTPError(status=400, body='Invalid Download Result: {0} {1}'.format(r.status_code, r.reason))

    pass_headers = ('Content-Disposition', 'Content-Length', 'Content-Type')

    for h in pass_headers:
        response.set_header(h, r.headers.get(h))

    response.body = r.iter_content()
    return response

application = init()

domain: python


text:
#!/usr/bin/env python
#============================================================================
# Copyright (C) Microsoft Corporation, All rights reserved.
#============================================================================

import os
import imp
import re
import codecs

protocol = imp.load_source('protocol', '../protocol.py')
nxDSCLog = imp.load_source('nxDSCLog', '../nxDSCLog.py')

LG = nxDSCLog.DSCLog

# backwards compatibility with pre-multi-homing bundles
conf_path = '/etc/opt/microsoft/omsagent/conf/omsagent.conf'
omi_map_path = '/etc/opt/microsoft/omsagent/conf/omsagent.d/omi_mapping.json'
omi_map = None
multi_homed = None
non_mh_heartbeat_cmd = '/opt/microsoft/omsagent/bin/omsadmin.sh -b'
oms_restart_cmd = 'sudo /opt/microsoft/omsagent/bin/service_control restart'

def init_paths(WorkspaceID):
    global conf_path
    global omi_map_path
    global multi_homed

    omsagent_dir = '/etc/opt/microsoft/omsagent/'
    mh_conf_dir = omsagent_dir + WorkspaceID + '/conf'

    multi_homed = os.path.isdir(mh_conf_dir)

    if multi_homed:
        LG().Log('INFO', 'OMSAgent is multi-homed and resource is updating workspace ' + WorkspaceID)
        conf_path = mh_conf_dir + '/omsagent.conf'
        omi_map_path = mh_conf_dir + '/omsagent.d/omi_mapping.json'

def init_omi_map():
    global omi_map
    txt = codecs.open(omi_map_path, 'r', 'utf8').read()
    omi_map = eval(txt)

def init_vars(WorkspaceID, HeartbeatIntervalSeconds, PerfCounterObject):
    init_paths(WorkspaceID)
    init_omi_map()

    if WorkspaceID is not None:
        WorkspaceID = WorkspaceID.encode('ascii', 'ignore')
    else:
        WorkspaceID = ''
    if PerfCounterObject is not None:
        for perf in PerfCounterObject:
            new_perfs = []
            if len(perf['PerformanceCounter'].value):
                for perf_counter in perf['PerformanceCounter'].value:
                    new_perfs.append(perf_counter.encode('ascii', 'ignore'))
                perf['PerformanceCounter'] = new_perfs
            if perf['InstanceName'].value is None:
                perf['InstanceName'] = ''
            else:
                perf['InstanceName'] = perf[
                    'InstanceName'].value.encode('ascii', 'ignore')
            if perf['ObjectName'].value is None:
                perf['ObjectName'] = ''
            else:
                perf['ObjectName'] = perf[
                    'ObjectName'].value.encode('ascii', 'ignore')
            if perf['AllInstances'].value is None:
                perf['AllInstances'] = False
            else:
                if perf['AllInstances'].value.value == 1:
                    perf['AllInstances'] = True
                else:
                    perf['AllInstances'] = False
            perf['IntervalSeconds'] = perf['IntervalSeconds'].value.value

def Set_Marshall(Name, WorkspaceID, HeartbeatIntervalSeconds, PerfCounterObject):
    init_vars(WorkspaceID, HeartbeatIntervalSeconds, PerfCounterObject)
    return Set(WorkspaceID, HeartbeatIntervalSeconds, PerfCounterObject)

def Test_Marshall(Name, WorkspaceID, HeartbeatIntervalSeconds, PerfCounterObject):
    init_vars(WorkspaceID, HeartbeatIntervalSeconds, PerfCounterObject)
    return Test(HeartbeatIntervalSeconds, PerfCounterObject)

def Get_Marshall(Name, WorkspaceID, HeartbeatIntervalSeconds, PerfCounterObject):
    arg_names = list(locals().keys())
    init_vars(WorkspaceID, HeartbeatIntervalSeconds, PerfCounterObject)
    retval = 0
    NewHeartbeatIntervalSeconds, NewPerf = Get(
        HeartbeatIntervalSeconds, PerfCounterObject)
    for perf in NewPerf:
        if len(perf['PerformanceCounter']):
            perf['PerformanceCounter'] = protocol.MI_StringA(
                perf['PerformanceCounter'])
        perf['ObjectName'] = protocol.MI_String(perf['ObjectName'])
        perf['InstanceName'] = protocol.MI_String(perf['InstanceName'])
        perf['AllInstances'] = protocol.MI_Boolean(perf['AllInstances'])
        perf['IntervalSeconds'] = protocol.MI_Uint16(perf['IntervalSeconds'])
    PerfCounterObject = protocol.MI_InstanceA(NewPerf)
    HeartbeatIntervalSeconds = protocol.MI_Uint16(NewHeartbeatIntervalSeconds)
    WorkspaceID = protocol.MI_String(WorkspaceID)
    Name = protocol.MI_String(Name)
    retd = {}
    ld = locals()
    for k in arg_names:
        retd[k] = ld[k]
    return retval, retd

def Set(WorkspaceID, HeartbeatIntervalSeconds, PerfCounterObject):
    if Test(HeartbeatIntervalSeconds, PerfCounterObject) == [0]:
        return [0]
    if UpdateOMSAgentConf(WorkspaceID, HeartbeatIntervalSeconds, PerfCounterObject):
        return [0]
    else:
        return [-1]

def Test(HeartbeatIntervalSeconds, PerfCounterObject):
    prune_perfs(PerfCounterObject)
    NewHeartbeatIntervalSeconds, NewPerfs = ReadOMSAgentConf(
        HeartbeatIntervalSeconds, PerfCounterObject)
    if NewHeartbeatIntervalSeconds != HeartbeatIntervalSeconds:
        return [-1]
    PerfCounterObject.sort()
    for perf in PerfCounterObject:
        perf['PerformanceCounter'].sort()
        perf['AllInstances'] = True
    NewPerfs.sort()
    for perf in NewPerfs:
        perf['PerformanceCounter'].sort()
    if PerfCounterObject != NewPerfs:
            return [-1]
    return [0]

def Get(HeartbeatIntervalSeconds, PerfCounterObject):
    NewHeartbeatIntervalSeconds, NewPerf = ReadOMSAgentConf(
        HeartbeatIntervalSeconds, PerfCounterObject)
    return NewHeartbeatIntervalSeconds, NewPerf

def TranslatePerfs(object_name, perfs):
    d = {}
    for p in perfs:
        for cname in omi_map:
            for prop in cname['CimProperties']:
                if (p == prop['CounterName'] or p == prop['CimPropertyName']) and cname['ObjectName'] == object_name:
                    if cname['ObjectName'] not in d.keys():
                        d[cname['ObjectName']] = [p]
                    else:
                        d[cname['ObjectName']].append(p)
    return d

def ReadOMSAgentConf(HeartbeatIntervalSeconds, PerfCounterObject):
    txt = ''
    try:
        txt = codecs.open(conf_path, 'r', 'utf8').read().encode(
            'ascii', 'ignore')
        LG().Log('INFO', 'Read omsagent configuration ' + conf_path + '.')
    except:
        LG().Log(
            'ERROR', 'Unable to read omsagent configuration ' + conf_path + '.')
    heartbeat_srch_str = r'<source>.*?tag heartbeat.*?run_interval ([0-9]+[a-z])\n</source>\n'
    heartbeat_srch = re.compile(heartbeat_srch_str, re.M | re.S)
    m = heartbeat_srch.search(txt)
    if m is not None:
        interval = int(m.group(1)[:-1])
        if m.group(1)[-1:] == 'm':
            interval *= 60
    else:
        interval = None
    new_heartbeat = interval
    perf_src_srch_str = r'\n<source>\n  type oms_omi.*?object_name "(.*?)".*?instance_regex "(.*?)".*?counter_name_regex "(.*?)".*?interval ([0-9]+[a-z]).*?</source>\n'
    perf_src_srch = re.compile(perf_src_srch_str, re.M | re.S)
    new_perfobj = []
    sources = perf_src_srch.findall(txt)
    inst = ''
    interval = 0
    for source in sources:
        s_perf = []
        if len(source[2]):
            s_perf = source[2].strip('(').strip(')').split('|')
        object_name = source[0]
        interval = int(source[3][:-1])
        if source[3][-1:] == 'm':
            interval *= 60
        inst = source[1]
        inst = inst.replace('.*', '*')
        new_perfobj.append({'PerformanceCounter': s_perf, 'InstanceName': inst,
                           'IntervalSeconds': interval, 'AllInstances': True, 'ObjectName': object_name})
    return new_heartbeat, new_perfobj

def UpdateOMSAgentConf(WorkspaceID, HeartbeatIntervalSeconds, PerfCounterObject):
    if os.path.exists(conf_path):
        txt = codecs.open(conf_path, 'r', 'utf8').read().encode(
            'ascii', 'ignore')
        LG().Log('INFO', 'Read omsagent configuration ' + conf_path + '.')
    else:
        LG().Log(
            'INFO', 'No omsagent configuration file present.  Will create new  configuration file at ' + conf_path + '.')
        txt = ''
    heartbeat_srch_str = r'<source>.*?tag heartbeat.*?</source>\n'
    heartbeat_srch = re.compile(heartbeat_srch_str, re.M | re.S)
    heartbeat_cmd = non_mh_heartbeat_cmd
    if multi_homed:
        heartbeat_cmd = 'echo'
    heartbeat_src = '<source>\n  type exec\n  tag heartbeat.output\n  command ' + heartbeat_cmd + ' > /dev/null\n  format tsv\n  keys severity,message\n  run_interval ' + \
        str(HeartbeatIntervalSeconds) + 's\n</source>\n'
    txt = heartbeat_srch.sub(heartbeat_src, txt)
    d = {}
    perf_src_srch_str = r'\n<source>\n  type oms_omi.*?</source>\n'
    perf_src_srch = re.compile(perf_src_srch_str, re.M | re.S)
    for source in perf_src_srch.findall(txt):
        txt = txt.replace(source, '')
    new_source = ''
    for perf in PerfCounterObject:
        d = TranslatePerfs(perf['ObjectName'], perf['PerformanceCounter'])
        for k in d.keys():
            names = '(' + reduce(lambda x, y: x + '|' + y, d[k]) + ')'
            instances = re.sub(r'([><]|&gt|&lt)', '', perf['InstanceName'])
            instances = re.sub(r'([*])', '.*', instances)
            new_source += '\n<source>\n  type oms_omi\n  object_name "' + k + '"\n  instance_regex "' + instances + \
                '"\n  counter_name_regex "' + names + '"\n  interval ' + \
                str(perf['IntervalSeconds']) + 's\n</source>\n'
    m = heartbeat_srch.search(txt)
    if m is not None:
        i = m.end(0) + 1
        txt = txt[:i] + new_source + txt[i:]
    else:
        txt = new_source
    try:
        codecs.open(conf_path, 'w', 'utf8').write(txt)
        LG().Log(
            'INFO', 'Created omsagent configuration at ' + conf_path + '.')
    except:
        LG().Log(
            'ERROR', 'Unable to create omsagent configuration at ' + conf_path + '.')
        return False

    global oms_restart_cmd
    process_to_restart = 'omsagent'
    if multi_homed:
        restart_cmd += ' ' + WorkspaceID
        process_to_restart += '-' + WorkspaceID
    if os.system(restart_cmd) == 0:
        LG().Log('INFO', 'Successfully restarted ' + process_to_restart + '.')
    else:
        LG().Log('ERROR', 'Error restarting ' + process_to_restart + '.')
        return False
    return True

def rm_unicode(obj):
    if isinstance(obj, dict):
        d = {}
        for k, v in obj.iteritems():
            d[rm_unicode(k)] = rm_unicode(v)
        return d
    elif isinstance(obj, list):
        return [rm_unicode(i) for i in obj]
    elif isinstance(obj, unicode):
        return obj.encode('ascii', 'ignore')
    else:
        return obj

def prune_perfs(PerfCounterObject):
    l = len(PerfCounterObject)
    i = 0
    while i < l:
        d = TranslatePerfs(PerfCounterObject[i]['ObjectName'], PerfCounterObject[i]['PerformanceCounter'])
        if PerfCounterObject[i]['ObjectName'] in d.keys():
            for p in PerfCounterObject[i]['PerformanceCounter']:
                if p not in d[PerfCounterObject[i]['ObjectName']]:
                    LG().Log('INFO', 'No match for PerformanceCounter \'' \
                             + p + '\' in ' \
                             + repr(PerfCounterObject[i]['ObjectName']) + ' in omi_mapping.json, ignoring.')
                    PerfCounterObject[i]['PerformanceCounter'].remove(p)
            if len(PerfCounterObject[i]['PerformanceCounter']) == 0:
                PerfCounterObject.pop(i)
                l -= 1
                i -= 1
        else:
            LG().Log('INFO', 'No matches for ObjectName ' \
                     + repr(PerfCounterObject[i]['ObjectName']) + ' and PerformanceCounter ' \
                     + repr(PerfCounterObject[i]['PerformanceCounter']) + ' in omi_mapping.json, ignoring.')
            PerfCounterObject.pop(i)
            l -= 1
            i -= 1
        i += 1


text:
"""
    Cisco Intersight

    Cisco Intersight is a management platform delivered as a service with embedded analytics for your Cisco and 3rd party IT infrastructure. This platform offers an intelligent level of management that enables IT organizations to analyze, simplify, and automate their environments in more advanced ways than the prior generations of tools. Cisco Intersight provides an integrated and intuitive management experience for resources in the traditional data center as well as at the edge. With flexible deployment options to address complex security needs, getting started with Intersight is quick and easy. Cisco Intersight has deep integration with Cisco UCS and HyperFlex systems allowing for remote deployment, configuration, and ongoing maintenance. The model-based deployment works for a single system in a remote location or hundreds of systems in a data center and enables rapid, standardized configuration and deployment. It also streamlines maintaining those systems whether you are working with small or very large configurations. The Intersight OpenAPI document defines the complete set of properties that are returned in the HTTP response. From that perspective, a client can expect that no additional properties are returned, unless these properties are explicitly defined in the OpenAPI document. However, when a client uses an older version of the Intersight OpenAPI document, the server may send additional properties because the software is more recent than the client. In that case, the client may receive properties that it does not know about. Some generated SDKs perform a strict validation of the HTTP response body against the OpenAPI document.  # noqa: E501

    The version of the OpenAPI document: 1.0.9-4950
    Contact: intersight@cisco.com
    Generated by: https://openapi-generator.tech
"""

import re  # noqa: F401
import sys  # noqa: F401

from intersight.model_utils import (  # noqa: F401
    ApiTypeError,
    ModelComposed,
    ModelNormal,
    ModelSimple,
    cached_property,
    change_keys_js_to_python,
    convert_js_args_to_python_args,
    date,
    datetime,
    file_type,
    none_type,
    validate_get_composed_info,
)

def lazy_import():
    from intersight.model.storage_net_app_node_relationship import StorageNetAppNodeRelationship
    globals()['StorageNetAppNodeRelationship'] = StorageNetAppNodeRelationship

class StorageNetAppSensorAllOf(ModelNormal):
    """NOTE: This class is auto generated by OpenAPI Generator.
    Ref: https://openapi-generator.tech

    Do not edit the class manually.

    Attributes:
      allowed_values (dict): The key is the tuple path to the attribute
          and the for var_name this is (var_name,). The value is a dict
          with a capitalized key describing the allowed value and an allowed
          value. These dicts store the allowed enum values.
      attribute_map (dict): The key is attribute name
          and the value is json key in definition.
      discriminator_value_class_map (dict): A dict to go from the discriminator
          variable value to the discriminator class name.
      validations (dict): The key is the tuple path to the attribute
          and the for var_name this is (var_name,). The value is a dict
          that stores validations for max_length, min_length, max_items,
          min_items, exclusive_maximum, inclusive_maximum, exclusive_minimum,
          inclusive_minimum, and regex.
      additional_properties_type (tuple): A tuple of classes accepted
          as additional properties values.
    """

    allowed_values = {
        ('class_id',): {
            'STORAGE.NETAPPSENSOR': "storage.NetAppSensor",
        },
        ('object_type',): {
            'STORAGE.NETAPPSENSOR': "storage.NetAppSensor",
        },
    }

    validations = {
    }

    additional_properties_type = None

    _nullable = False

    @cached_property
    def openapi_types():
        """
        This must be a method because a model may have properties that are
        of type self, this must run after the class is loaded

        Returns
            openapi_types (dict): The key is attribute name
                and the value is attribute type.
        """
        lazy_import()
        return {
            'class_id': (str,),  # noqa: E501
            'object_type': (str,),  # noqa: E501
            'array_controller': (StorageNetAppNodeRelationship,),  # noqa: E501
        }

    @cached_property
    def discriminator():
        return None

    attribute_map = {
        'class_id': 'ClassId',  # noqa: E501
        'object_type': 'ObjectType',  # noqa: E501
        'array_controller': 'ArrayController',  # noqa: E501
    }

    _composed_schemas = {}

    required_properties = set([
        '_data_store',
        '_check_type',
        '_spec_property_naming',
        '_path_to_item',
        '_configuration',
        '_visited_composed_classes',
    ])

    @convert_js_args_to_python_args
    def __init__(self, *args, **kwargs):  # noqa: E501
        """StorageNetAppSensorAllOf - a model defined in OpenAPI

        Args:

        Keyword Args:
            class_id (str): The fully-qualified name of the instantiated, concrete type. This property is used as a discriminator to identify the type of the payload when marshaling and unmarshaling data.. defaults to "storage.NetAppSensor", must be one of ["storage.NetAppSensor", ]  # noqa: E501
            object_type (str): The fully-qualified name of the instantiated, concrete type. The value should be the same as the 'ClassId' property.. defaults to "storage.NetAppSensor", must be one of ["storage.NetAppSensor", ]  # noqa: E501
            _check_type (bool): if True, values for parameters in openapi_types
                                will be type checked and a TypeError will be
                                raised if the wrong type is input.
                                Defaults to True
            _path_to_item (tuple/list): This is a list of keys or values to
                                drill down to the model in received_data
                                when deserializing a response
            _spec_property_naming (bool): True if the variable names in the input data
                                are serialized names, as specified in the OpenAPI document.
                                False if the variable names in the input data
                                are pythonic names, e.g. snake case (default)
            _configuration (Configuration): the instance to use when
                                deserializing a file_type parameter.
                                If passed, type conversion is attempted
                                If omitted no type conversion is done.
            _visited_composed_classes (tuple): This stores a tuple of
                                classes that we have traveled through so that
                                if we see that class again we will not use its
                                discriminator again.
                                When traveling through a discriminator, the
                                composed schema that is
                                is traveled through is added to this set.
                                For example if Animal has a discriminator
                                petType and we pass in "Dog", and the class Dog
                                allOf includes Animal, we move through Animal
                                once using the discriminator, and pick Dog.
                                Then in Dog, we will make an instance of the
                                Animal class but this time we won't travel
                                through its discriminator because we passed in
                                _visited_composed_classes = (Animal,)
            array_controller (StorageNetAppNodeRelationship): [optional]  # noqa: E501
        """

        class_id = kwargs.get('class_id', "storage.NetAppSensor")
        object_type = kwargs.get('object_type', "storage.NetAppSensor")
        _check_type = kwargs.pop('_check_type', True)
        _spec_property_naming = kwargs.pop('_spec_property_naming', False)
        _path_to_item = kwargs.pop('_path_to_item', ())
        _configuration = kwargs.pop('_configuration', None)
        _visited_composed_classes = kwargs.pop('_visited_composed_classes', ())

        if args:
            raise ApiTypeError(
                "Invalid positional arguments=%s passed to %s. Remove those invalid positional arguments." % (
                    args,
                    self.__class__.__name__,
                ),
                path_to_item=_path_to_item,
                valid_classes=(self.__class__,),
            )

        self._data_store = {}
        self._check_type = _check_type
        self._spec_property_naming = _spec_property_naming
        self._path_to_item = _path_to_item
        self._configuration = _configuration
        self._visited_composed_classes = _visited_composed_classes + (self.__class__,)

        self.class_id = class_id
        self.object_type = object_type
        for var_name, var_value in kwargs.items():
            if var_name not in self.attribute_map and \
                        self._configuration is not None and \
                        self._configuration.discard_unknown_keys and \
                        self.additional_properties_type is None:
                # discard variable.
                continue
            setattr(self, var_name, var_value)


text:
from __future__ import print_function
from flask import Flask, Response
from pyzbar import pyzbar
from picamera.array import PiRGBArray
from picamera import PiCamera
from datetime import datetime

import numpy as np
import cv2
import time

camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
time.sleep(0.1)

app = Flask(__name__)

@app.route('/stream')
def stream():
    return Response(gen(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

def gen():
    while True:
        frame = get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

def get_frame():
    camera.capture(rawCapture, format="bgr", use_video_port=True)
    frame = rawCapture.array
    decoded_objs = decode(frame)
    frame = display(frame, decoded_objs)
    ret, jpeg = cv2.imencode('.jpg', frame)
    rawCapture.truncate(0)

    return jpeg.tobytes()

def decode(frame):
    decoded_objs = pyzbar.decode(frame, scan_locations=True)
    for obj in decoded_objs:
        print(datetime.now().strftime('%H:%M:%S.%f'))
        print('Type: ', obj.type)
        print('Data: ', obj.data)

    return decoded_objs

def display(frame, decoded_objs):
    for decoded_obj in decoded_objs:
        left, top, width, height = decoded_obj.rect
        frame = cv2.rectangle(frame,
                              (left, top),
                              (left + width, height + top),
                              (0, 255, 255), 2)
    return frame

if __name__ == '__main__':
    app.run(host="0.0.0.0", debug=False, threaded=True)


text:
import collections
from collections import defaultdict
import sys
import json
import random
from jsmin import jsmin
from io import StringIO
import numpy as np
import copy
import os

script_n = os.path.basename(__file__).split('.')[0]
script_n = script_n.split('_', 1)[1]

def to_ng(loc):
    return (int(loc[0]/4), int(loc[1]/4), int(loc[2]/40))

'''Load data'''
import compress_pickle
fname = 'gen_210518_setup01_v2_syndb_threshold_20_coalesced.gz'
grc_mfs_locs = compress_pickle.load(fname)

mfs_locs = defaultdict(list)
for grc in grc_mfs_locs:
    for mf in grc_mfs_locs[grc]:
        for syn in grc_mfs_locs[grc][mf]:
            mfs_locs[mf].append(syn['syn_loc0'])

# print(mfs_locs[mf]); asdf
asdff = (172644, 113468, 89)
asdfff = (137580, 101824, 369)

# white list for big boutons
whitelist = set([
    (172644, 113468, 89),
    (163520, 98364, 83),
    (113008, 109372, 1154),
    (70424, 116512, 71),
    (186536, 100020, 130),
    (86780, 110184, 81),
    (177992, 108528, 1164),
    (127368, 101716, 1143),
    (155036, 103252, 71),
    (97884, 104152, 1160),
    (109476, 104808, 76),
    (82936, 122484, 76),
    (113532, 104660, 1150),
    (78904, 115540, 1158),
    (190684, 91276, 1015),
    (160500, 99828, 1165),
    (109020, 115476, 74),
    (93516, 101476, 858),
    (126728, 104988, 86),
    (173456, 106376, 71),
    (197436, 95688, 898),
    (122752, 110608, 85),
    (122192, 119344, 70),
    (122396, 118840, 83),
    (204868, 103452, 145),
    (94212, 107860, 1137),
    (92360, 105844, 1162),
    (84704, 115452, 119),
    (54036, 105484, 394),
    (110624, 105800, 70),
    (170512, 99132, 107),
    (71200, 114308, 1123),
    (106588, 98692, 1160),
    (70164, 107908, 1015),
    (144772, 106812, 105),
    (asdff),
    (asdff),
    (asdff),
])

blacklist = set([
    (137580, 101824, 369),
    (127384, 115252, 746),
    (155268, 99276, 918),
    (182000, 91966, 716),
    (119828, 107400, 312),
    (171384, 94244, 573),
    (asdfff),
    (asdfff),
    (asdfff),
    (asdfff),
    (asdfff),
    (asdfff),
])

'''Cluster and extract locations of MF boutons'''
from sklearn.cluster import DBSCAN

mfs_bouton_locs = {}

'''if a bouton location has less than this many synapses then it won't be considered in order to reduce false positives'''
# bouton_synapse_threshold = 6  # safe for determining big bouton locations
bouton_synapse_threshold = 2
bouton_synapse_threshold = 3
bouton_synapse_threshold = 4  # 4 is a bit iffy, since it has some semi big boutons
bouton_synapse_threshold = 5
# bouton_synapse_threshold = 6  # this threshold has quite a bit of FPs

for mf in mfs_locs:
    dbscan = DBSCAN(eps=8000, min_samples=2)  # max dist set to 8um
    # dbscan = DBSCAN(eps=10000, min_samples=2)  # max dist set to 8um
    dbscan.fit(mfs_locs[mf])
    loc_by_label = defaultdict(list)
    for loc, label in zip(mfs_locs[mf], dbscan.labels_):
        loc_by_label[label].append(loc)
    mf_bouton_locs = []
    for label in loc_by_label:
        if len(loc_by_label[label]) <= bouton_synapse_threshold:
            whitelisted = False
            for loc in loc_by_label[label]:
                if to_ng(loc) in whitelist:
                    whitelisted = True
            if not whitelisted:
                if len(loc_by_label[label]) >= 2:
                    print(f'Ignoring {mf} due to insufficient synapses')
                    for loc in loc_by_label[label]:
                        print(to_ng(loc))
                    continue
        sum = [0, 0, 0]
        for loc in loc_by_label[label]:
            sum = [sum[0]+loc[0], sum[1]+loc[1], sum[2]+loc[2]]
        center = [
            int(sum[0]/len(loc_by_label[label])),
            int(sum[1]/len(loc_by_label[label])),
            int(sum[2]/len(loc_by_label[label])),
            ]
        mf_bouton_locs.append(center)
    mfs_bouton_locs[mf] = mf_bouton_locs
    # print(mf_bouton_locs)
    # for loc in mf_bouton_locs:
        # print([int(loc[0]/4), int(loc[1]/4), int(loc[2]/40)])

mfs_bouton_count = defaultdict(list)
for mf in mfs_bouton_locs:
    mfs_bouton_count[len(mfs_bouton_locs[mf])].append(mf)

for count in sorted(mfs_bouton_count.keys()):
    print(f'{count}: {mfs_bouton_count[count]}')

'''save mfs_bouton_locs'''
import compress_pickle
compress_pickle.dump((
    mfs_bouton_locs
    ), f"{script_n}.gz")

asdf

for loc in mfs_bouton_locs['mf_431']:
    print([int(loc[0]/4), int(loc[1]/4), int(loc[2]/40)])

for loc in mfs_locs['mf_41']:
    print([int(loc[0]/4), int(loc[1]/4), int(loc[2]/40)])


text:
import random as rd
import time as t
def Seed():
    rd.seed(int(str(t.time()).split(".")[0]))
Seed()
def Random():
    return rd.gauss(0,0.01)
def RandomZeroMask(Prob=0.1):
    r= rd.random()
    if r<Prob:
        return 0.0
    else:
        return 1.0


text:
from typing import Callable

import numpy as np

from manimlib.utils.bezier import bezier

def linear(t: float) -> float:
    return t

def smooth(t: float) -> float:
    # Zero first and second derivatives at t=0 and t=1.
    # Equivalent to bezier([0, 0, 0, 1, 1, 1])
    s = 1 - t
    return (t**3) * (10 * s * s + 5 * s * t + t * t)

def rush_into(t: float) -> float:
    return 2 * smooth(0.5 * t)

def rush_from(t: float) -> float:
    return 2 * smooth(0.5 * (t + 1)) - 1

def slow_into(t: float) -> float:
    return np.sqrt(1 - (1 - t) * (1 - t))

def double_smooth(t: float) -> float:
    if t < 0.5:
        return 0.5 * smooth(2 * t)
    else:
        return 0.5 * (1 + smooth(2 * t - 1))

def there_and_back(t: float) -> float:
    new_t = 2 * t if t < 0.5 else 2 * (1 - t)
    return smooth(new_t)

def there_and_back_with_pause(t: float, pause_ratio: float = 1. / 3) -> float:
    a = 1. / pause_ratio
    if t < 0.5 - pause_ratio / 2:
        return smooth(a * t)
    elif t < 0.5 + pause_ratio / 2:
        return 1
    else:
        return smooth(a - a * t)

def running_start(t: float, pull_factor: float = -0.5) -> float:
    return bezier([0, 0, pull_factor, pull_factor, 1, 1, 1])(t)

def not_quite_there(
    func: Callable[[float], float] = smooth,
    proportion: float = 0.7
) -> Callable[[float], float]:
    def result(t):
        return proportion * func(t)
    return result

def wiggle(t: float, wiggles: float = 2) -> float:
    return there_and_back(t) * np.sin(wiggles * np.pi * t)

def squish_rate_func(
    func: Callable[[float], float],
    a: float = 0.4,
    b: float = 0.6
) -> Callable[[float], float]:
    def result(t):
        if a == b:
            return a
        elif t < a:
            return func(0)
        elif t > b:
            return func(1)
        else:
            return func((t - a) / (b - a))

    return result

# Stylistically, should this take parameters (with default values)?
# Ultimately, the functionality is entirely subsumed by squish_rate_func,
# but it may be useful to have a nice name for with nice default params for
# "lingering", different from squish_rate_func's default params

def lingering(t: float) -> float:
    return squish_rate_func(lambda t: t, 0, 0.8)(t)

def exponential_decay(t: float, half_life: float = 0.1) -> float:
    # The half-life should be rather small to minimize
    # the cut-off error at the end
    return 1 - np.exp(-t / half_life)


text:
from typing import Literal

EventType = Literal['call', 'line', 'return']


text:
# Copyright 2011 OpenStack Foundation
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""RequestContext: context for requests that persist through all of patron."""

import copy

from keystoneclient import auth
from keystoneclient import service_catalog
from oslo_context import context
from oslo_log import log as logging
from oslo_utils import timeutils
import six

from patron import exception
from patron.i18n import _, _LW
from patron import policy

LOG = logging.getLogger(__name__)

class _ContextAuthPlugin(auth.BaseAuthPlugin):
    """A keystoneclient auth plugin that uses the values from the Context.

    Ideally we would use the plugin provided by auth_token middleware however
    this plugin isn't serialized yet so we construct one from the serialized
    auth data.
    """

    def __init__(self, auth_token, sc):
        super(_ContextAuthPlugin, self).__init__()

        self.auth_token = auth_token
        sc = {'serviceCatalog': sc}
        self.service_catalog = service_catalog.ServiceCatalogV2(sc)

    def get_token(self, *args, **kwargs):
        return self.auth_token

    def get_endpoint(self, session, service_type=None, interface=None,
                     region_name=None, service_name=None, **kwargs):
        return self.service_catalog.url_for(service_type=service_type,
                                            service_name=service_name,
                                            endpoint_type=interface,
                                            region_name=region_name)

class RequestContext(context.RequestContext):
    """Security context and request information.

    Represents the user taking a given action within the system.

    """

    def __init__(self, user_id=None, project_id=None,
                 is_admin=None, read_deleted="no",
                 roles=None, remote_address=None, timestamp=None,
                 request_id=None, auth_token=None, overwrite=True,
                 quota_class=None, user_name=None, project_name=None,
                 service_catalog=None, instance_lock_checked=False,
                 user_auth_plugin=None, **kwargs):
        """:param read_deleted: 'no' indicates deleted records are hidden,
                'yes' indicates deleted records are visible,
                'only' indicates that *only* deleted records are visible.

           :param overwrite: Set to False to ensure that the greenthread local
                copy of the index is not overwritten.

           :param user_auth_plugin: The auth plugin for the current request's
                authentication data.

           :param kwargs: Extra arguments that might be present, but we ignore
                because they possibly came in from older rpc messages.
        """
        user = kwargs.pop('user', None)
        tenant = kwargs.pop('tenant', None)
        super(RequestContext, self).__init__(
            auth_token=auth_token,
            user=user_id or user,
            tenant=project_id or tenant,
            domain=kwargs.pop('domain', None),
            user_domain=kwargs.pop('user_domain', None),
            project_domain=kwargs.pop('project_domain', None),
            is_admin=is_admin,
            read_only=kwargs.pop('read_only', False),
            show_deleted=kwargs.pop('show_deleted', False),
            request_id=request_id,
            resource_uuid=kwargs.pop('resource_uuid', None),
            overwrite=overwrite)
        # oslo_context's RequestContext.to_dict() generates this field, we can
        # safely ignore this as we don't use it.
        kwargs.pop('user_identity', None)
        if kwargs:
            LOG.warning(_LW('Arguments dropped when creating context: %s') %
                        str(kwargs))

        # FIXME(dims): user_id and project_id duplicate information that is
        # already present in the oslo_context's RequestContext. We need to
        # get rid of them.
        self.user_id = user_id
        self.project_id = project_id
        self.roles = roles or []
        self.read_deleted = read_deleted
        self.remote_address = remote_address
        if not timestamp:
            timestamp = timeutils.utcnow()
        if isinstance(timestamp, six.string_types):
            timestamp = timeutils.parse_strtime(timestamp)
        self.timestamp = timestamp

        if service_catalog:
            # Only include required parts of service_catalog
            self.service_catalog = [s for s in service_catalog
                if s.get('type') in ('volume', 'volumev2', 'key-manager')]
        else:
            # if list is empty or none
            self.service_catalog = []

        self.instance_lock_checked = instance_lock_checked

        # NOTE(markmc): this attribute is currently only used by the
        # rs_limits turnstile pre-processor.
        # See https://lists.launchpad.net/openstack/msg12200.html
        self.quota_class = quota_class
        self.user_name = user_name
        self.project_name = project_name
        self.is_admin = is_admin
        self.user_auth_plugin = user_auth_plugin
        if self.is_admin is None:
            self.is_admin = policy.check_is_admin(self)

    def get_auth_plugin(self):
        if self.user_auth_plugin:
            return self.user_auth_plugin
        else:
            return _ContextAuthPlugin(self.auth_token, self.service_catalog)

    def _get_read_deleted(self):
        return self._read_deleted

    def _set_read_deleted(self, read_deleted):
        if read_deleted not in ('no', 'yes', 'only'):
            raise ValueError(_("read_deleted can only be one of 'no', "
                               "'yes' or 'only', not %r") % read_deleted)
        self._read_deleted = read_deleted

    def _del_read_deleted(self):
        del self._read_deleted

    read_deleted = property(_get_read_deleted, _set_read_deleted,
                            _del_read_deleted)

    def to_dict(self):
        values = super(RequestContext, self).to_dict()
        # FIXME(dims): defensive hasattr() checks need to be
        # removed once we figure out why we are seeing stack
        # traces
        values.update({
            'user_id': getattr(self, 'user_id', None),
            'project_id': getattr(self, 'project_id', None),
            'is_admin': getattr(self, 'is_admin', None),
            'read_deleted': getattr(self, 'read_deleted', 'no'),
            'roles': getattr(self, 'roles', None),
            'remote_address': getattr(self, 'remote_address', None),
            'timestamp': timeutils.strtime(self.timestamp) if hasattr(
                self, 'timestamp') else None,
            'request_id': getattr(self, 'request_id', None),
            'quota_class': getattr(self, 'quota_class', None),
            'user_name': getattr(self, 'user_name', None),
            'service_catalog': getattr(self, 'service_catalog', None),
            'project_name': getattr(self, 'project_name', None),
            'instance_lock_checked': getattr(self, 'instance_lock_checked',
                                             False)
        })
        return values

    @classmethod
    def from_dict(cls, values):
        return cls(**values)

    def elevated(self, read_deleted=None, overwrite=False):
        """Return a version of this context with admin flag set."""
        context = copy.deepcopy(self)
        context.is_admin = True

        if 'admin' not in context.roles:
            context.roles.append('admin')

        if read_deleted is not None:
            context.read_deleted = read_deleted

        return context

    def __str__(self):
        return "<Context %s>" % self.to_dict()

def get_admin_context(read_deleted="no"):
    return RequestContext(user_id=None,
                          project_id=None,
                          is_admin=True,
                          read_deleted=read_deleted,
                          overwrite=False)

def is_user_context(context):
    """Indicates if the request context is a normal user."""
    if not context:
        return False
    if context.is_admin:
        return False
    if not context.user_id or not context.project_id:
        return False
    return True

def require_admin_context(ctxt):
    """Raise exception.AdminRequired() if context is an admin context."""
    if not ctxt.is_admin:
        raise exception.AdminRequired()

def require_context(ctxt):
    """Raise exception.Forbidden() if context is not a user or an
    admin context.
    """
    if not ctxt.is_admin and not is_user_context(ctxt):
        raise exception.Forbidden()

def authorize_project_context(context, project_id):
    """Ensures a request has permission to access the given project."""
    if is_user_context(context):
        if not context.project_id:
            raise exception.Forbidden()
        elif context.project_id != project_id:
            raise exception.Forbidden()

def authorize_user_context(context, user_id):
    """Ensures a request has permission to access the given user."""
    if is_user_context(context):
        if not context.user_id:
            raise exception.Forbidden()
        elif context.user_id != user_id:
            raise exception.Forbidden()

def authorize_quota_class_context(context, class_name):
    """Ensures a request has permission to access the given quota class."""
    if is_user_context(context):
        if not context.quota_class:
            raise exception.Forbidden()
        elif context.quota_class != class_name:
            raise exception.Forbidden()


text:
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import abc
from unittest import mock

import httpretty
import requests
from requests.adapters import BaseAdapter
from requests.models import Response

import opentelemetry.instrumentation.requests
from opentelemetry import context, trace

# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined.
from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
from opentelemetry.propagate import get_global_textmap, set_global_textmap
from opentelemetry.sdk import resources
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.mock_textmap import MockTextMapPropagator
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import StatusCode
from opentelemetry.util.http import get_excluded_urls

class TransportMock:
    def read(self, *args, **kwargs):
        pass

class MyAdapter(BaseAdapter):
    def __init__(self, response):
        super().__init__()
        self._response = response

    def send(self, *args, **kwargs):  # pylint:disable=signature-differs
        return self._response

    def close(self):
        pass

class InvalidResponseObjectException(Exception):
    def __init__(self):
        super().__init__()
        self.response = {}

class RequestsIntegrationTestBase(abc.ABC):
    # pylint: disable=no-member
    # pylint: disable=too-many-public-methods

    URL = "http://httpbin.org/status/200"

    # pylint: disable=invalid-name
    def setUp(self):
        super().setUp()

        self.env_patch = mock.patch.dict(
            "os.environ",
            {
                "OTEL_PYTHON_REQUESTS_EXCLUDED_URLS": "http://localhost/env_excluded_arg/123,env_excluded_noarg"
            },
        )
        self.env_patch.start()

        self.exclude_patch = mock.patch(
            "opentelemetry.instrumentation.requests._excluded_urls_from_env",
            get_excluded_urls("REQUESTS"),
        )
        self.exclude_patch.start()

        RequestsInstrumentor().instrument()
        httpretty.enable()
        httpretty.register_uri(httpretty.GET, self.URL, body="Hello!")

    # pylint: disable=invalid-name
    def tearDown(self):
        super().tearDown()
        self.env_patch.stop()
        RequestsInstrumentor().uninstrument()
        httpretty.disable()

    def assert_span(self, exporter=None, num_spans=1):
        if exporter is None:
            exporter = self.memory_exporter
        span_list = exporter.get_finished_spans()
        self.assertEqual(num_spans, len(span_list))
        if num_spans == 0:
            return None
        if num_spans == 1:
            return span_list[0]
        return span_list

    @staticmethod
    @abc.abstractmethod
    def perform_request(url: str, session: requests.Session = None):
        pass

    def test_basic(self):
        result = self.perform_request(self.URL)
        self.assertEqual(result.text, "Hello!")
        span = self.assert_span()

        self.assertIs(span.kind, trace.SpanKind.CLIENT)
        self.assertEqual(span.name, "HTTP GET")

        self.assertEqual(
            span.attributes,
            {
                SpanAttributes.HTTP_METHOD: "GET",
                SpanAttributes.HTTP_URL: self.URL,
                SpanAttributes.HTTP_STATUS_CODE: 200,
            },
        )

        self.assertIs(span.status.status_code, trace.StatusCode.UNSET)

        self.assertEqualSpanInstrumentationInfo(
            span, opentelemetry.instrumentation.requests
        )

    def test_name_callback(self):
        def name_callback(method, url):
            return "GET" + url

        RequestsInstrumentor().uninstrument()
        RequestsInstrumentor().instrument(name_callback=name_callback)
        result = self.perform_request(self.URL)
        self.assertEqual(result.text, "Hello!")
        span = self.assert_span()

        self.assertEqual(span.name, "GET" + self.URL)

    def test_excluded_urls_explicit(self):
        url_404 = "http://httpbin.org/status/404"
        httpretty.register_uri(
            httpretty.GET,
            url_404,
            status=404,
        )

        RequestsInstrumentor().uninstrument()
        RequestsInstrumentor().instrument(excluded_urls=".*/404")
        self.perform_request(self.URL)
        self.perform_request(url_404)

        self.assert_span(num_spans=1)

    def test_excluded_urls_from_env(self):
        url = "http://localhost/env_excluded_arg/123"
        httpretty.register_uri(
            httpretty.GET,
            url,
            status=200,
        )

        RequestsInstrumentor().uninstrument()
        RequestsInstrumentor().instrument()
        self.perform_request(self.URL)
        self.perform_request(url)

        self.assert_span(num_spans=1)

    def test_name_callback_default(self):
        def name_callback(method, url):
            return 123

        RequestsInstrumentor().uninstrument()
        RequestsInstrumentor().instrument(name_callback=name_callback)
        result = self.perform_request(self.URL)
        self.assertEqual(result.text, "Hello!")
        span = self.assert_span()

        self.assertEqual(span.name, "HTTP GET")

    def test_not_foundbasic(self):
        url_404 = "http://httpbin.org/status/404"
        httpretty.register_uri(
            httpretty.GET,
            url_404,
            status=404,
        )
        result = self.perform_request(url_404)
        self.assertEqual(result.status_code, 404)

        span = self.assert_span()

        self.assertEqual(
            span.attributes.get(SpanAttributes.HTTP_STATUS_CODE), 404
        )

        self.assertIs(
            span.status.status_code,
            trace.StatusCode.ERROR,
        )

    def test_uninstrument(self):
        RequestsInstrumentor().uninstrument()
        result = self.perform_request(self.URL)
        self.assertEqual(result.text, "Hello!")
        self.assert_span(num_spans=0)
        # instrument again to avoid annoying warning message
        RequestsInstrumentor().instrument()

    def test_uninstrument_session(self):
        session1 = requests.Session()
        RequestsInstrumentor().uninstrument_session(session1)

        result = self.perform_request(self.URL, session1)
        self.assertEqual(result.text, "Hello!")
        self.assert_span(num_spans=0)

        # Test that other sessions as well as global requests is still
        # instrumented
        session2 = requests.Session()
        result = self.perform_request(self.URL, session2)
        self.assertEqual(result.text, "Hello!")
        self.assert_span()

        self.memory_exporter.clear()

        result = self.perform_request(self.URL)
        self.assertEqual(result.text, "Hello!")
        self.assert_span()

    def test_suppress_instrumentation(self):
        token = context.attach(
            context.set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)
        )
        try:
            result = self.perform_request(self.URL)
            self.assertEqual(result.text, "Hello!")
        finally:
            context.detach(token)

        self.assert_span(num_spans=0)

    def test_suppress_http_instrumentation(self):
        token = context.attach(
            context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True)
        )
        try:
            result = self.perform_request(self.URL)
            self.assertEqual(result.text, "Hello!")
        finally:
            context.detach(token)

        self.assert_span(num_spans=0)

    def test_not_recording(self):
        with mock.patch("opentelemetry.trace.INVALID_SPAN") as mock_span:
            RequestsInstrumentor().uninstrument()
            RequestsInstrumentor().instrument(
                tracer_provider=trace.NoOpTracerProvider()
            )
            mock_span.is_recording.return_value = False
            result = self.perform_request(self.URL)
            self.assertEqual(result.text, "Hello!")
            self.assert_span(None, 0)
            self.assertFalse(mock_span.is_recording())
            self.assertTrue(mock_span.is_recording.called)
            self.assertFalse(mock_span.set_attribute.called)
            self.assertFalse(mock_span.set_status.called)

    def test_distributed_context(self):
        previous_propagator = get_global_textmap()
        try:
            set_global_textmap(MockTextMapPropagator())
            result = self.perform_request(self.URL)
            self.assertEqual(result.text, "Hello!")

            span = self.assert_span()

            headers = dict(httpretty.last_request().headers)
            self.assertIn(MockTextMapPropagator.TRACE_ID_KEY, headers)
            self.assertEqual(
                str(span.get_span_context().trace_id),
                headers[MockTextMapPropagator.TRACE_ID_KEY],
            )
            self.assertIn(MockTextMapPropagator.SPAN_ID_KEY, headers)
            self.assertEqual(
                str(span.get_span_context().span_id),
                headers[MockTextMapPropagator.SPAN_ID_KEY],
            )

        finally:
            set_global_textmap(previous_propagator)

    def test_span_callback(self):
        RequestsInstrumentor().uninstrument()

        def span_callback(span, result: requests.Response):
            span.set_attribute(
                "http.response.body", result.content.decode("utf-8")
            )

        RequestsInstrumentor().instrument(
            tracer_provider=self.tracer_provider,
            span_callback=span_callback,
        )

        result = self.perform_request(self.URL)
        self.assertEqual(result.text, "Hello!")

        span = self.assert_span()
        self.assertEqual(
            span.attributes,
            {
                SpanAttributes.HTTP_METHOD: "GET",
                SpanAttributes.HTTP_URL: self.URL,
                SpanAttributes.HTTP_STATUS_CODE: 200,
                "http.response.body": "Hello!",
            },
        )

    def test_custom_tracer_provider(self):
        resource = resources.Resource.create({})
        result = self.create_tracer_provider(resource=resource)
        tracer_provider, exporter = result
        RequestsInstrumentor().uninstrument()
        RequestsInstrumentor().instrument(tracer_provider=tracer_provider)

        result = self.perform_request(self.URL)
        self.assertEqual(result.text, "Hello!")

        span = self.assert_span(exporter=exporter)
        self.assertIs(span.resource, resource)

    @mock.patch(
        "requests.adapters.HTTPAdapter.send",
        side_effect=requests.RequestException,
    )
    def test_requests_exception_without_response(self, *_, **__):
        with self.assertRaises(requests.RequestException):
            self.perform_request(self.URL)

        span = self.assert_span()
        self.assertEqual(
            span.attributes,
            {
                SpanAttributes.HTTP_METHOD: "GET",
                SpanAttributes.HTTP_URL: self.URL,
            },
        )
        self.assertEqual(span.status.status_code, StatusCode.ERROR)

    mocked_response = requests.Response()
    mocked_response.status_code = 500
    mocked_response.reason = "Internal Server Error"

    @mock.patch(
        "requests.adapters.HTTPAdapter.send",
        side_effect=InvalidResponseObjectException,
    )
    def test_requests_exception_without_proper_response_type(self, *_, **__):
        with self.assertRaises(InvalidResponseObjectException):
            self.perform_request(self.URL)

        span = self.assert_span()
        self.assertEqual(
            span.attributes,
            {
                SpanAttributes.HTTP_METHOD: "GET",
                SpanAttributes.HTTP_URL: self.URL,
            },
        )
        self.assertEqual(span.status.status_code, StatusCode.ERROR)

    mocked_response = requests.Response()
    mocked_response.status_code = 500
    mocked_response.reason = "Internal Server Error"

    @mock.patch(
        "requests.adapters.HTTPAdapter.send",
        side_effect=requests.RequestException(response=mocked_response),
    )
    def test_requests_exception_with_response(self, *_, **__):
        with self.assertRaises(requests.RequestException):
            self.perform_request(self.URL)

        span = self.assert_span()
        self.assertEqual(
            span.attributes,
            {
                SpanAttributes.HTTP_METHOD: "GET",
                SpanAttributes.HTTP_URL: self.URL,
                SpanAttributes.HTTP_STATUS_CODE: 500,
            },
        )
        self.assertEqual(span.status.status_code, StatusCode.ERROR)

    @mock.patch("requests.adapters.HTTPAdapter.send", side_effect=Exception)
    def test_requests_basic_exception(self, *_, **__):
        with self.assertRaises(Exception):
            self.perform_request(self.URL)

        span = self.assert_span()
        self.assertEqual(span.status.status_code, StatusCode.ERROR)

    @mock.patch(
        "requests.adapters.HTTPAdapter.send", side_effect=requests.Timeout
    )
    def test_requests_timeout_exception(self, *_, **__):
        with self.assertRaises(Exception):
            self.perform_request(self.URL)

        span = self.assert_span()
        self.assertEqual(span.status.status_code, StatusCode.ERROR)

    def test_adapter_with_custom_response(self):
        response = Response()
        response.status_code = 210
        response.reason = "hello adapter"
        response.raw = TransportMock()

        session = requests.Session()
        session.mount(self.URL, MyAdapter(response))

        self.perform_request(self.URL, session)
        span = self.assert_span()
        self.assertEqual(
            span.attributes,
            {
                "http.method": "GET",
                "http.url": self.URL,
                "http.status_code": 210,
            },
        )

class TestRequestsIntegration(RequestsIntegrationTestBase, TestBase):
    @staticmethod
    def perform_request(url: str, session: requests.Session = None):
        if session is None:
            return requests.get(url)
        return session.get(url)

    def test_invalid_url(self):
        url = "http://[::1/nope"

        with self.assertRaises(ValueError):
            requests.post(url)

        span = self.assert_span()

        self.assertEqual(span.name, "HTTP POST")
        self.assertEqual(
            span.attributes,
            {SpanAttributes.HTTP_METHOD: "POST", SpanAttributes.HTTP_URL: url},
        )
        self.assertEqual(span.status.status_code, StatusCode.ERROR)

    def test_credential_removal(self):
        new_url = "http://username:password@httpbin.org/status/200"
        self.perform_request(new_url)
        span = self.assert_span()

        self.assertEqual(span.attributes[SpanAttributes.HTTP_URL], self.URL)

    def test_if_headers_equals_none(self):
        result = requests.get(self.URL, headers=None)
        self.assertEqual(result.text, "Hello!")
        self.assert_span()

class TestRequestsIntegrationPreparedRequest(
    RequestsIntegrationTestBase, TestBase
):
    @staticmethod
    def perform_request(url: str, session: requests.Session = None):
        if session is None:
            session = requests.Session()
        request = requests.Request("GET", url)
        prepared_request = session.prepare_request(request)
        return session.send(prepared_request)

class TestRequestsIntergrationMetric(TestBase):
    URL = "http://examplehost:8000/status/200"

    def setUp(self):
        super().setUp()
        RequestsInstrumentor().instrument(meter_provider=self.meter_provider)

        httpretty.enable()
        httpretty.register_uri(httpretty.GET, self.URL, body="Hello!")

    def tearDown(self):
        super().tearDown()
        RequestsInstrumentor().uninstrument()
        httpretty.disable()

    @staticmethod
    def perform_request(url: str) -> requests.Response:
        return requests.get(url)

    def test_basic_metric_success(self):
        self.perform_request(self.URL)

        expected_attributes = {
            "http.status_code": 200,
            "http.host": "examplehost",
            "net.peer.port": 8000,
            "net.peer.name": "examplehost",
            "http.method": "GET",
            "http.flavor": "1.1",
            "http.scheme": "http",
        }

        for (
            resource_metrics
        ) in self.memory_metrics_reader.get_metrics_data().resource_metrics:
            for scope_metrics in resource_metrics.scope_metrics:
                for metric in scope_metrics.metrics:
                    for data_point in metric.data.data_points:
                        self.assertDictEqual(
                            expected_attributes, dict(data_point.attributes)
                        )
                        self.assertEqual(data_point.count, 1)


text:
# coding=utf-8
# Copyright (c) 2019 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import collections
import logging

import tensorflow as tf
from tensorflow.contrib.rnn import MultiRNNCell, LSTMStateTuple
from tensorflow.python.framework import dtypes, tensor_shape
from tensorflow.python.framework import ops
from tensorflow.python.util import nest

from ludwig.models.modules.fully_connected_modules import fc_layer
from ludwig.models.modules.initializer_modules import get_initializer
from ludwig.models.modules.reduction_modules import reduce_sequence
from ludwig.utils.tf_utils import sequence_length_3D, sequence_length_2D

def get_cell_fun(cell_type):
    if cell_type == 'rnn':
        cell_fn = tf.nn.rnn_cell.BasicRNNCell
    elif cell_type == 'lstm':
        # allows for optional peephole connections and cell clipping
        cell_fn = tf.nn.rnn_cell.LSTMCell
    elif cell_type == 'lstm_block':
        # Faster version of basic LSTM
        cell_fn = tf.contrib.rnn.LSTMBlockCell
    elif cell_type == 'lstm_ln':
        cell_fn = tf.contrib.rnn.LayerNormBasicLSTMCell
    elif cell_type == 'lstm_cudnn':
        cell_fn = tf.contrib.cudnn_rnn.CudnnCompatibleLSTMCell
    elif cell_type == 'gru':
        cell_fn = tf.nn.rnn_cell.GRUCell
    elif cell_type == 'gru_block':
        # Faster version of GRU (25% faster in my tests)
        cell_fn = tf.contrib.rnn.GRUBlockCell
    elif cell_type == 'gru_cudnn':
        # Faster version of GRU (25% faster in my tests)
        cell_fn = tf.contrib.cudnn_rnn.CudnnCompatibleGRUCell
    else:
        cell_fn = tf.nn.rnn_cell.BasicRNNCell
    return cell_fn

class Projection(tf.layers.Layer):
    def __init__(self, projection_weights, projection_biases, name=None,
                 **kwargs):
        super(Projection, self).__init__(name=name, **kwargs)
        self.projection_weights = projection_weights
        self.projection_biases = projection_biases

    def call(self, inputs, **kwargs):
        inputs_shape = inputs.shape.as_list()
        weights_shape = self.projection_weights.shape.as_list()
        assert inputs_shape[-1] == weights_shape[0]
        inputs = tf.reshape(inputs, [-1, inputs_shape[-1]])

        outputs = tf.matmul(inputs, self.projection_weights)
        if self.projection_biases is not None:
            outputs = tf.nn.bias_add(outputs, self.projection_biases)

        outputs_shape = inputs_shape
        outputs_shape[0] = -1  # batch_size
        outputs_shape[-1] = weights_shape[1]
        outputs = tf.reshape(outputs, outputs_shape)
        return outputs

    def compute_output_shape(self, input_shape):
        input_shape = tensor_shape.TensorShape(input_shape).as_list()
        output_shape = input_shape
        output_shape[-1] = self.projection_biases.shape.as_list()[0]
        # output_shape = [input_shape[0], self.projection_biases.shape.as_list()[0]]
        return tensor_shape.TensorShape(output_shape)

class BasicDecoderOutput(
    collections.namedtuple('BasicDecoderOutput',
                           ('rnn_output', 'sample_id', 'projection_input'))):
    pass

class BasicDecoder(tf.contrib.seq2seq.BasicDecoder):
    def _projection_input_size(self):
        return self._cell.output_size

    @property
    def output_size(self):
        return BasicDecoderOutput(
            rnn_output=self._rnn_output_size(),
            sample_id=self._helper.sample_ids_shape,
            projection_input=self._projection_input_size())

    @property
    def output_dtype(self):
        dtype = nest.flatten(self._initial_state)[0].dtype
        return BasicDecoderOutput(
            nest.map_structure(lambda _: dtype, self._rnn_output_size()),
            self._helper.sample_ids_dtype,
            nest.map_structure(lambda _: dtype, self._projection_input_size()))

    def step(self, time, inputs, state, name=None):
        with ops.name_scope(name, 'BasicDecoderStep', (time, inputs, state)):
            cell_outputs, cell_state = self._cell(inputs, state)
            projection_inputs = cell_outputs  # get projection_inputs to compute sampled_softmax_cross_entropy_loss
            if self._output_layer is not None:
                cell_outputs = self._output_layer(cell_outputs)
            sample_ids = self._helper.sample(
                time=time, outputs=cell_outputs, state=cell_state)
            (finished, next_inputs, next_state) = self._helper.next_inputs(
                time=time,
                outputs=cell_outputs,
                state=cell_state,
                sample_ids=sample_ids)
        outputs = BasicDecoderOutput(cell_outputs, sample_ids,
                                     projection_inputs)
        return (outputs, next_state, next_inputs, finished)

class TimeseriesTrainingHelper(tf.contrib.seq2seq.TrainingHelper):
    def sample(self, time, outputs, name=None, **unused_kwargs):
        with ops.name_scope(name, 'TrainingHelperSample', [time, outputs]):
            return tf.zeros(tf.shape(outputs)[:-1], dtype=dtypes.int32)

class RecurrentStack:
    def __init__(
            self,
            state_size=256,
            cell_type='rnn',
            num_layers=1,
            bidirectional=False,
            dropout=False,
            regularize=True,
            reduce_output='last',
            **kwargs
    ):
        self.state_size = state_size
        self.cell_type = cell_type
        self.num_layers = num_layers
        self.bidirectional = bidirectional
        self.dropout = dropout
        self.regularize = regularize
        self.reduce_output = reduce_output

    def __call__(
            self,
            input_sequence,
            regularizer,
            dropout_rate,
            is_training=True
    ):
        if not self.regularize:
            regularizer = None

        # Calculate the length of input_sequence and the batch size
        sequence_length = sequence_length_3D(input_sequence)

        # RNN cell
        cell_fn = get_cell_fun(self.cell_type)

        # initial state
        # init_state = tf.get_variable(
        #   'init_state',
        #   [1, state_size],
        #   initializer=tf.constant_initializer(0.0),
        # )
        # init_state = tf.tile(init_state, [batch_size, 1])

        # main RNN operation
        with tf.variable_scope('rnn_stack', reuse=tf.AUTO_REUSE,
                               regularizer=regularizer) as vs:
            if self.bidirectional:
                # forward direction cell
                fw_cell = lambda state_size: cell_fn(state_size)
                bw_cell = lambda state_size: cell_fn(state_size)
                fw_cells = [fw_cell(self.state_size) for _ in
                            range(self.num_layers)]
                bw_cells = [bw_cell(self.state_size) for _ in
                            range(self.num_layers)]
                rnn_outputs, final_state_fw, final_state_bw = tf.contrib.rnn.stack_bidirectional_dynamic_rnn(
                    cells_fw=fw_cells,
                    cells_bw=bw_cells,
                    dtype=tf.float32,
                    sequence_length=sequence_length,
                    inputs=input_sequence
                )

            else:
                cell = lambda state_size: cell_fn(state_size)
                cells = MultiRNNCell(
                    [cell(self.state_size) for _ in range(self.num_layers)],
                    state_is_tuple=True)
                rnn_outputs, final_state = tf.nn.dynamic_rnn(
                    cells,
                    input_sequence,
                    sequence_length=sequence_length,
                    dtype=tf.float32)
                # initial_state=init_state)

            for v in tf.global_variables():
                if v.name.startswith(vs.name):
                    logging.debug('  {}: {}'.format(v.name, v))
            logging.debug('  rnn_outputs: {0}'.format(rnn_outputs))

            rnn_output = reduce_sequence(rnn_outputs, self.reduce_output)
            logging.debug('  reduced_rnn_output: {0}'.format(rnn_output))

        # dropout
        if self.dropout and dropout_rate is not None:
            rnn_output = tf.layers.dropout(
                rnn_output,
                rate=dropout_rate,
                training=is_training
            )
            logging.debug('  dropout_rnn: {0}'.format(rnn_output))

        return rnn_output, rnn_output.shape.as_list()[-1]

def recurrent_decoder(encoder_outputs, targets, max_sequence_length, vocab_size,
                      cell_type='rnn', state_size=256, embedding_size=50,
                      num_layers=1,
                      attention_mechanism=None, beam_width=1, projection=True,
                      tied_target_embeddings=True, embeddings=None,
                      initializer=None, regularizer=None,
                      is_timeseries=False):
    with tf.variable_scope('rnn_decoder', reuse=tf.AUTO_REUSE,
                           regularizer=regularizer):

        # ================ Setup ================
        if beam_width > 1 and is_timeseries:
            raise ValueError('Invalid beam_width: {}'.format(beam_width))

        GO_SYMBOL = vocab_size
        END_SYMBOL = 0
        batch_size = tf.shape(encoder_outputs)[0]

        # ================ Projection ================
        # Project the encoder outputs to the size of the decoder state
        encoder_outputs_size = encoder_outputs.shape[-1]
        if projection and encoder_outputs_size != state_size:
            with tf.variable_scope('projection'):
                encoder_output_rank = len(encoder_outputs.shape)
                if encoder_output_rank > 2:
                    sequence_length = tf.shape(encoder_outputs)[1]
                    encoder_outputs = tf.reshape(encoder_outputs,
                                                 [-1, encoder_outputs_size])
                    encoder_outputs = fc_layer(encoder_outputs,
                                               encoder_outputs.shape[-1],
                                               state_size,
                                               activation=None,
                                               initializer=initializer)
                    encoder_outputs = tf.reshape(encoder_outputs,
                                                 [-1, sequence_length,
                                                  state_size])
                else:
                    encoder_outputs = fc_layer(encoder_outputs,
                                               encoder_outputs.shape[-1],
                                               state_size,
                                               activation=None,
                                               initializer=initializer)

        # ================ Targets sequence ================
        # Calculate the length of inputs and the batch size
        with tf.variable_scope('sequence'):
            targets_sequence_length = sequence_length_2D(targets)
            start_tokens = tf.tile([GO_SYMBOL], [batch_size])
            end_tokens = tf.tile([END_SYMBOL], [batch_size])
            if is_timeseries:
                start_tokens = tf.cast(start_tokens, tf.float32)
                end_tokens = tf.cast(end_tokens, tf.float32)
            targets_with_go = tf.concat([
                tf.expand_dims(start_tokens, 1),
                targets,
                tf.expand_dims(end_tokens, 1)], 1)
            logging.debug('  targets_with_go: {0}'.format(targets_with_go))
            targets_sequence_length_with_eos = targets_sequence_length + 1  # the EOS symbol is 0 so it's not increasing the real length of the sequence

        # ================ Embeddings ================
        if is_timeseries:
            targets_embedded = tf.expand_dims(targets_with_go, -1)
            targets_embeddings = None
        else:
            with tf.variable_scope('embedding'):
                if embeddings is not None:
                    embedding_size = embeddings.shape.as_list()[-1]
                    if tied_target_embeddings:
                        state_size = embedding_size
                elif tied_target_embeddings:
                    embedding_size = state_size

                if embeddings is not None:
                    embedding_go = tf.get_variable('embedding_GO',
                                                   initializer=tf.random_uniform(
                                                       [1, embedding_size],
                                                       -1.0, 1.0))
                    targets_embeddings = tf.concat([embeddings, embedding_go],
                                                   axis=0)
                else:
                    initializer_obj = get_initializer(initializer)
                    targets_embeddings = tf.get_variable(
                        'embeddings',
                        initializer=initializer_obj(
                            [vocab_size + 1, embedding_size]),
                        regularizer=regularizer
                    )
                logging.debug(
                    '  targets_embeddings: {0}'.format(targets_embeddings))

                targets_embedded = tf.nn.embedding_lookup(targets_embeddings,
                                                          targets_with_go,
                                                          name='decoder_input_embeddings')
        logging.debug('  targets_embedded: {0}'.format(targets_embedded))

        # ================ Class prediction ================
        if tied_target_embeddings:
            class_weights = tf.transpose(targets_embeddings)
        else:
            initializer_obj = get_initializer(initializer)
            class_weights = tf.get_variable(
                'class_weights',
                initializer=initializer_obj([state_size, vocab_size + 1]),
                regularizer=regularizer
            )
        logging.debug('  class_weights: {0}'.format(class_weights))
        class_biases = tf.get_variable('class_biases', [vocab_size + 1])
        logging.debug('  class_biases: {0}'.format(class_biases))
        projection_layer = Projection(class_weights, class_biases)

        # ================ RNN ================
        initial_state = encoder_outputs
        with tf.variable_scope('rnn_cells') as vs:
            # Cell
            cell_fun = get_cell_fun(cell_type)

            if num_layers == 1:
                cell = cell_fun(state_size)
                if cell_type.startswith('lstm'):
                    initial_state = LSTMStateTuple(c=initial_state,
                                                   h=initial_state)
            elif num_layers > 1:
                cell = MultiRNNCell(
                    [cell_fun(state_size) for _ in range(num_layers)],
                    state_is_tuple=True)
                if cell_type.startswith('lstm'):
                    initial_state = LSTMStateTuple(c=initial_state,
                                                   h=initial_state)
                initial_state = tuple([initial_state] * num_layers)
            else:
                raise ValueError('num_layers in recurrent decoser: {}. '
                                 'Number of layers in a recurrenct decoder cannot be <= 0'.format(
                    num_layers))

            # Attention
            if attention_mechanism is not None:
                if attention_mechanism == 'bahdanau':
                    attention_mechanism = tf.contrib.seq2seq.BahdanauAttention(
                        num_units=state_size, memory=encoder_outputs,
                        memory_sequence_length=sequence_length_3D(
                            encoder_outputs))
                elif attention_mechanism == 'luong':
                    attention_mechanism = tf.contrib.seq2seq.LuongAttention(
                        num_units=state_size, memory=encoder_outputs,
                        memory_sequence_length=sequence_length_3D(
                            encoder_outputs))
                else:
                    raise ValueError(
                        'Attention mechanism {} not supported'.format(
                            attention_mechanism))
                cell = tf.contrib.seq2seq.AttentionWrapper(
                    cell, attention_mechanism, attention_layer_size=state_size)
                initial_state = cell.zero_state(dtype=tf.float32,
                                                batch_size=batch_size)

            for v in tf.global_variables():
                if v.name.startswith(vs.name):
                    logging.debug('  {}: {}'.format(v.name, v))

        # ================ Decoding ================
        def decode(initial_state, cell, helper, beam_width=1,
                   projection_layer=None):
            # The decoder itself
            if beam_width > 1:
                # Tile inputs for beam search decoder
                beam_initial_state = tf.contrib.seq2seq.tile_batch(
                    initial_state, beam_width)
                decoder = tf.contrib.seq2seq.BeamSearchDecoder(
                    cell=cell,
                    embedding=targets_embeddings,
                    start_tokens=start_tokens,
                    end_token=END_SYMBOL,
                    initial_state=beam_initial_state,
                    beam_width=beam_width,
                    output_layer=projection_layer)
            else:
                decoder = BasicDecoder(
                    cell=cell, helper=helper,
                    initial_state=initial_state,
                    output_layer=projection_layer)

            # The decoding operation
            outputs = tf.contrib.seq2seq.dynamic_decode(
                decoder=decoder,
                output_time_major=False,
                impute_finished=False if beam_width > 1 else True,
                maximum_iterations=max_sequence_length
            )

            return outputs

        # ================ Decoding helpers ================
        if is_timeseries:
            train_helper = TimeseriesTrainingHelper(
                inputs=targets_embedded,
                sequence_length=targets_sequence_length_with_eos)
            final_outputs_pred, final_state_pred, final_sequence_lengths_pred = decode(
                initial_state,
                cell,
                train_helper,
                projection_layer=projection_layer)
            eval_logits = final_outputs_pred.rnn_output
            train_logits = final_outputs_pred.projection_input
            predictions_sequence = tf.reshape(eval_logits, [batch_size, -1])
            predictions_sequence_length_with_eos = final_sequence_lengths_pred

        else:
            train_helper = tf.contrib.seq2seq.TrainingHelper(
                inputs=targets_embedded,
                sequence_length=targets_sequence_length_with_eos)
            final_outputs_train, final_state_train, final_sequence_lengths_train, = decode(
                initial_state,


text:
from django.contrib import admin

from adminsortable.admin import SortableAdmin, SortableTabularInline

from .models import SearchTermCategory, SearchTermItem

class SearchTermItemInline(SortableTabularInline):
    model = SearchTermItem
    extra = 1

@admin.register(SearchTermCategory)
class SearchTermCategoryAdmin(SortableAdmin):
    list_display = ('name', 'description', 'order_number')
    ordering = ('order_number',)
    inlines = [SearchTermItemInline]

@admin.register(SearchTermItem)
class SearchTermItemAdmin(SortableAdmin):
    list_display = (
        'slug', 'name', 'category', 'call_to_action_type', 'call_to_action_text', 'description', 'order_number'
    )
    list_filter = ('category', 'call_to_action_type')
    search_fields = ('name', 'slug', 'description')
    ordering = ('order_number',)


text:
"""
Showcases *LLAB(l:c)* colour appearance model computations.
"""

import numpy as np

import colour
from colour.appearance.llab import CAM_ReferenceSpecification_LLAB
from colour.utilities import message_box

message_box('"LLAB(l:c)" Colour Appearance Model Computations')

XYZ = np.array([19.01, 20.00, 21.78])
XYZ_0 = np.array([95.05, 100.00, 108.88])
Y_b = 20.0
L = 318.31
surround = colour.VIEWING_CONDITIONS_LLAB["ref_average_4_minus"]
message_box(
    f'Converting to the  "LLAB(l:c)" colour appearance model specification '
    f"using given parameters:\n\n"
    f"\tXYZ: {XYZ}\n"
    f"\tXYZ_0: {XYZ_0}\n"
    f"\tY_b: {Y_b}\n"
    f"\tL: {L}\n"
    f"\tsurround: {surround}"
)
specification = colour.XYZ_to_LLAB(XYZ, XYZ_0, Y_b, L, surround)
print(specification)

print("\n")

message_box(
    'Broadcasting the current output "LLAB(l:c)" colour appearance '
    "model specification to the reference specification.\n"
    "The intent of this reference specification is to provide names "
    'as closest as possible to the "Mark D. Fairchild" reference.\n'
    "The current output specification is meant to be consistent with "
    "the other colour appearance model specification by using same "
    "argument names for consistency wherever possible."
)

print(CAM_ReferenceSpecification_LLAB(*specification.values))


text:
# AMZ-Driverless
#  Copyright (c) 2019 Authors:
#   - Huub Hendrikx <hhendrik@ethz.ch>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import datetime
import os
import time

import yaml

from rbb_tools.common.shell import CommandGroup
from rbb_tools.extraction.extractor import AbstractExtractionPlugin
from rbb_tools.extraction.registry import Product

class RvizRecorderPlugin(AbstractExtractionPlugin):

    def __init__(self, configuration, logger, resource_directory):
        super(RvizRecorderPlugin, self).__init__(configuration, logger, resource_directory)

    def check_topics(self, topics):
        return True

    def get_plugin_meta_data(self):
        return {
            'name': 'RViz Recorder',
            'version': '0.0.1'
        }

    def get_default_configuration(self):
        return {
            'height': 1080,
            'width': 1920,
            'margin_left': -20,
            'margin_right': -20,
            'rewrite_rviz_file': True,
            'headless': 'auto',
            'color_depth': 24,
            'title': 'RViz Recording'
        }

    def _rewrite_rviz_file(self, rviz_file, tmp_dir):
        data = None
        with open(rviz_file, 'r') as stream:
            try:
                data = yaml.safe_load(stream)
            except yaml.YAMLError as exc:
                self._logger.warning("Cannot rewrite malformed rviz file: %s" % str(exc))
                return rviz_file

        data['Window Geometry']['Height'] = self.config('height')
        data['Window Geometry']['Width'] = self.config('width') - \
                                           self.config('margin_left') - self.config('margin_right')
        data['Window Geometry']['Y'] = 0
        data['Window Geometry']['X'] = self.config('margin_left')
        data['Window Geometry']['Hide Left Dock'] = True
        data['Window Geometry']['Hide Right Dock'] = True
        data['Window Geometry']['Selection']['collapsed'] = True
        data['Window Geometry']['Time']['collapsed'] = False
        data['Window Geometry']['Tool Properties']['collapsed'] = True
        data['Window Geometry']['Views']['collapsed'] = True
        data['Window Geometry']['Displays']['collapsed'] = True

        rewritten_rviz_file = tmp_dir + "/rviz.rviz"
        with open(rewritten_rviz_file, 'w') as outfile:
            yaml.safe_dump(data, outfile, default_flow_style=False)

        return rewritten_rviz_file

    def extract(self, bag_file, topics, tmp_dir, output_dir, product_factory):
        video_file = output_dir + "/output.mp4"
        xdisplay_id = 99

        logo_file = self._resource_directory + "/" + self._configuration['logo']
        font_file = self._resource_directory + "/" + self._configuration['font']

        rviz_file = self._resource_directory + "/" + self._configuration['rviz_file']
        if self.config("rewrite_rviz_file"):
            rviz_file = self._rewrite_rviz_file(rviz_file, tmp_dir)

        # TODO: Output a temporary rviz file that rewrites the configuration to collapse all panels and spawn window at 0,0

        if os.path.exists(video_file):
            return False

        # TODO: Include fingerprints
        name = os.path.basename(bag_file)
        rviz_name = os.path.basename(self._configuration['rviz_file'])
        text = name + " | " + rviz_name + " | " + str(datetime.datetime.today())

        self._logger.info("Video id: " + text)

        xephyr_size = "%dx%d" % (self.config('width'), self.config('height'))
        xephyr_cmd = "Xephyr -ac -nocursor -screen %s -br -reset -terminate :%d" % (xephyr_size, xdisplay_id)

        xvfb_size = "%dx%dx%d" % (self.config('width'), self.config('height'), self.config('color_depth'))
        xvfb_cmd = "Xvfb :%d -screen 0 %s" % (xdisplay_id, xvfb_size)

        roscore_cmd = "roscore"
        rosbag_player_cmd = "rosbag play --clock --hz 1000 -q %s" % (bag_file)

        rviz_splash_option = ""
        if self.config('splash_screen'):
            rviz_splash_option = " -s %s" % (self._resource_directory + "/" + self.config('splash_screen'))

        if os.environ.get('DISPLAY') is not None:
            # We assume hardware acceleration is available and run through VGL
            rviz_vgl_cmd = "vglrun -- rviz -d %s%s" % (rviz_file, rviz_splash_option) #DISPAY
        else:
            # We assume hardware acceleration is NOT available and run directly on Xephyr/Xvfb
            # software acceleration (mesa with llvm) should be isntalled
            rviz_vgl_cmd = "rviz -d %s%s" % (rviz_file, rviz_splash_option) #DISPAY

        ffmpeg_grab_size = "%dx%d" % (self.config('width'), self.config('height'))
        ffmpeg_cmd = "ffmpeg -loglevel warning -video_size %s -framerate 25 -f x11grab -i :%d.0+0,0" \
                     " -i %s -filter_complex \"overlay=%d:%d,drawtext=text=\\'%s\\':x=%d:y=%d:fontfile=%s:fontsize=16:fontcolor=white:shadowcolor=black:shadowx=2:shadowy=2\" " \
                     "-movflags +faststart %s" % (ffmpeg_grab_size, xdisplay_id, logo_file, self._configuration['logo_x'],self._configuration['logo_y'], text, self._configuration['text_x'], self._configuration['text_y'], font_file, output_dir + "/output.mp4")

        self._logger.info(ffmpeg_cmd)

        ##################
        # Run the commands
        ##################
        cmd_group = CommandGroup()
        try:
            roscore = cmd_group.Command(roscore_cmd)

            if os.environ.get('RBB_HEADLESS') == "force":
                # Force headless in server environments
                headless = True
            else:
                headless = self.config('headless')

                if headless == 'auto':
                    headless = os.environ.get('RBB_HEADLESS') == 1

            if headless:
                print ("Running in headless mode! (Xvfb)")
                framebuffer = cmd_group.Command(xvfb_cmd)
            else:
                framebuffer = cmd_group.Command(xephyr_cmd)

            rviz = cmd_group.Command(rviz_vgl_cmd, {'DISPLAY': ":%d.0" % (xdisplay_id)})
            ffmpeg = cmd_group.Command(ffmpeg_cmd)
            rosbag_player = cmd_group.Command(rosbag_player_cmd)
            move_mouse = cmd_group.Command("xdotool mousemove %d %d" % (self.config('width'), self.config('height')), {'DISPLAY': ":%d.0" % (xdisplay_id)})
            rosparam_sim_time = cmd_group.Command("rosparam set use_sim_time true")

            roscore.run()
            framebuffer.run()

            # Make sure they don't directly crash
            time.sleep(1)

            if roscore.is_running() and framebuffer.is_running():
                self._logger.info("Roscore&Xephyr up!")
                rviz.run()
                ffmpeg.run()

                time.sleep(0.5)
                if rviz.is_running() and ffmpeg.is_running():
                    move_mouse.run()
                    rosparam_sim_time.run()
                    self._logger.info("Rviz&ffmpeg up!")
                    rosbag_player.run()

                    while rosbag_player.is_running() and rviz.is_running():
                        time.sleep(1)

                    ffmpeg.send_sigint()
                    ffmpeg.join()

            else:
                self._logger.failure("Couldnt start roscore or xephyr")
        finally:
            cmd_group.ensure_terminated()

        # Register the product
        product = product_factory.new() # type: Product
        product.set_type("video")
        product.set_title(self.config('title'))
        product.set_topics(topics)
        product.add_file("video.mp4", "output.mp4", mime="video/mp4")

        return [product]

plugin = RvizRecorderPlugin


text:
from . import (
    yaw,
    layout,
    base_COE,
    optimization,
    layout_height,
    power_density,
    yaw_wind_rose,
    power_density_1D,
    yaw_wind_rose_parallel,
)