<commit_before># Django settings for expression_data project.

try:
    from localsettings import *
except ImportError:
    pass

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'expression_data.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'expression_data.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}
<commit_msg>Enabled admin and admin-docs.<commit_after># Django settings for expression_data project.

try:
    from localsettings import *
except ImportError:
    pass

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'expression_data.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'expression_data.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    'django.contrib.admindocs',
)

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}
<|endoftext|>


<commit_before>from __future__ import print_function
from __future__ import unicode_literals
from builtins import str
from builtins import object
from flask import Flask, request, abort, make_response
import gevent.pywsgi
import threading
import time
import json
import argparse
import os
import sys
import jwt
import logging

DEBUG=False

def myWebError(msg, code=400):
    resp = make_response(msg, code)
    abort(resp)

class IgorServlet(threading.Thread):

    @staticmethod
    def argumentParser(parser=None, description=None, port=None, name=None):
        if parser is None:
            if description is None:
                description = "Mini-server for use with Igor"
            parser = argparse.ArgumentParser(description=description)

        DEFAULTDIR=os.path.join(os.path.expanduser('~'), '.igor')
        if 'IGORSERVER_DIR' in os.environ:
            DEFAULTDIR = os.environ['IGORSERVER_DIR']
        if port is None:
            port = 8080
        if name is None:
            name = os.path.basename(os.path.splitext(sys.argv[0])[0])

        parser.add_argument("--database", metavar="DIR", help="Config and logfiles in DIR (default: %s, environment IGORSERVER_DIR)" % DEFAULTDIR, default=DEFAULTDIR)
        parser.add_argument("--name", metavar="NAME", help="Program name for use in log and config filenames in database dir(default: %s)" % name, default=name)
        parser.add_argument("--sslname", metavar="NAME", help="Program name to look up SSL certificate and key in database dir (default: igor)", default="igor")
        parser.add_argument("--port", metavar="PORT", type=int, help="Port to serve on (default: %d)" % port, default=port)
        parser.add_argument("--nossl", action="store_true", help="Do no use https (ssl) on the service, even if certificates are available")
        parser.add_argument("--nolog", action="store_true", help="Disable http server logging to stdout")
        parser.add_argument("--noCapabilities", action="store_true", help="Disable access control via capabilities (allowing all access)")
        parser.add_argument("--capabilities", action="store_true", help="Enable access control via capabilities")
        parser.add_argument("--audience", metavar="URL", help="Audience string for this servlet (for checking capabilities and their signature)")
        parser.add_argument("--issuer", metavar="URL", help="Issuer string for this servlet (for checking capabilities and their signature)")
        parser.add_argument("--sharedKey", metavar="B64STRING", help="Secret key shared with issuer (for checking capabilities and their signature)")
        return parser

    def __init__(self, port=8080, name='igorServlet', nossl=False, capabilities=None, noCapabilities=None, database=".", sslname='igor', nolog=False, audience=None, issuer=None, issuerSharedKey=None, **kwargs):
        threading.Thread.__init__(self)
        self.endpoints = {}
        self.useCapabilities = False # Default-default
        self.issuer = None
        self.issuerSharedKey = None
        self.audience = None
        self.server = None
        self.port = port
        self.name = name
#        self.middleware = ()
        self.nolog = nolog
        self.sslname = sslname
        if not self.sslname:
            self.sslname = self.name
        self.datadir = database

        self.ssl = not nossl
        keyFile = os.path.join(self.datadir, self.sslname + '.key')
        if self.ssl and not os.path.exists(keyFile):
            print('Warning: Using http in stead of https: no private key file', keyFile, file=sys.stderr)
            self.ssl = False
        if self.ssl:
            self.privateKeyFile = keyFile
            self.certificateFile = os.path.join(self.datadir, self.sslname + '.crt')
        else:
            self.privateKeyFile = None
            self.certificateFile = None

        if capabilities != None:
            self.useCapabilities = capabilities
        elif noCapabilities != None:
            self.useCapabilities = not noCapabilities
        self.audience = audience
        self.issuer = issuer
        self.issuerSharedKey = issuerSharedKey

        if DEBUG: print('igorServlet: IgorServlet.__init__ called for', self)
        self.app = Flask(__name__)

    def setIssuer(self, issuer, issuerSharedKey):
        self.issuer = issuer
        self.issuerSharedKey = issuerSharedKey

    def hasIssuer(self):
        return not not self.issuer and not not self.issuerSharedKey

    def run(self):
        if self.ssl:
            kwargs = dict(keyfile=self.privateKeyFile, certfile=self.certificateFile)
        else:
            kwargs = {}
        if self.nolog:
            kwargs['log'] = None
        self.server = gevent.pywsgi.WSGIServer(("0.0.0.0", self.port), self.app, **kwargs)
        self.server.serve_forever()

    def stop(self):
        if self.server:
            self.server.stop()
            self.server = None
        return self.join()

    def addEndpoint(self, path, mimetype='application/json', get=None, put=None, post=None, delete=None):
        self.endpoints[path] = dict(mimetype=mimetype, get=get, put=put, post=post, delete=delete)
        methods = []
        if get:
            methods.append("GET")
        if put:
            methods.append("PUT")
        if post:
            methods.append("POST")
        if delete:
            methods.append("DELETE")
        self.app.add_url_rule(path, path, self._forward, methods=methods)

    def _forward(self):
        method = request.method.lower()
        path = request.path
        endpoint = self.endpoints.get(path)
        if not path:
            abort(404)
        entry = endpoint[method]
        if not entry:
            abort(404)
        if self.useCapabilities:
            # We are using capability-based access control Check that the caller has the correct
            # rights.
            if not self._checkRights(method, path):
                abort(401)
        methodArgs = {}
        methodArgs = request.values.to_dict()
        if not methodArgs:
            if request.is_json:
                methodArgs = request.json
            elif request.data:
                data = request.data
                if not isinstance(data, str):
                    assert isinstance(data, bytes)
                    data = data.decode('utf8')
                methodArgs = dict(data=data)
            else:
                methodArgs = {}
        try:
            rv = entry(**methodArgs)
        except TypeError as arg:
            return myWebError("400 Error in parameters: %s" % arg)
        if endpoint['mimetype'] == 'text/plain':
            if type(rv) != type(str):
                rv = "%s" % (rv,)
        elif endpoint['mimetype'] == 'application/json':
            if isinstance(rv, bytes):
                rv = rv.decode('utf8')
            rv = json.dumps(rv)
        else:
            assert 0, 'Unsupported mimetype %s' % endpoint['mimetype']
        # Finally ensure we send utf8 bytes back
        rv = rv.encode('utf-8')
        return rv

    def _checkRights(self, method, path):
        if DEBUG:  print('IgorServlet: check access for method %s on %s' % (method, path))
        if not self.issuer:
            if DEBUG: print('IgorServlet: issuer not set, cannot check access')
            return False
        if not self.issuerSharedKey:
            if DEBUG: print('IgorServlet: issuerSharedKey not set, cannot check access')
            return False
        # Get capability from headers
        headers = request.headers
        authHeader = headers.get('Authorization')
        if not authHeader:
            if DEBUG: print('IgorServlet: no Authorization: header')
            return False
        authFields = authHeader.split()
        if authFields[0].lower() != 'bearer':
            if DEBUG: print('IgorServlet: no Authorization: bearer header')
            return False
        encoded = authFields[1] # base64.b64decode(authFields[1])
        if DEBUG: print('IgorServlet: got bearer token data %s' % encoded)
        decoded = self._decodeBearerToken(encoded)
        if not decoded:
            # _decodeBearerToken will return None if the key is not from the right issuer or the signature doesn't match.
            return False
        capPath = decoded.get('obj')
        capModifier = decoded.get(method)
        if not capPath:
            if DEBUG: print('IgorServlet: capability does not contain obj field')
            return False
        if not capModifier:
            if DEBUG: print('IgorServlet: capability does not have %s right' % method)
            return False
        pathHead = path[:len(capPath)]
        pathTail = path[len(capPath):]
        if pathHead != capPath or (pathTail and pathTail[0] != '/'):
            if DEBUG: print('IgorServlet: capability path %s does not match %s' % (capPath, path))
            return False
        if capModifier == 'self':
            if capPath != path:
                if DEBUG: print('IgorServlet: capability path %s does not match self for %s' % (capPath, path))
                return False
        elif capModifier == 'child':
            if pathTail.count('/') != 1:
                if DEBUG: print('IgorServlet: capability path %s does not match direct child for %s' % (capPath, path))
                return False
        elif capModifier == 'descendant':
            if not pathTail.count:
                if DEBUG: print('IgorServlet: capability path %s does not match descendant for %s' % (capPath, path))
                return False
        elif capModifier == 'descendant-or-self':
            pass
        else:
            if DEBUG: print('IgorServlet: capability has unkown modifier %s for right %s' % (capModifier, method))
            return False
        if DEBUG:
            print('IgorServlet: Capability matches')
        return True

    def _decodeBearerToken(self, data):
        try:
            content = jwt.decode(data, self.issuerSharedKey, issuer=self.issuer, audience=self.audience, algorithms=['RS256', 'HS256'])
        except jwt.DecodeError:
            if DEBUG:
                print('IgorServlet: incorrect signature on bearer token %s' % data)
                print('IgorServlet: content: %s' % jwt.decode(data, verify=False))
            return myWebError('401 Unauthorized, Incorrect signature on key', 401)
        except jwt.InvalidIssuerError:
            if DEBUG:
                print('IgorServlet: incorrect issuer on bearer token %s' % data)
                print('IgorServlet: content: %s' % jwt.decode(data, verify=False))
            return myWebError('401 Unauthorized, incorrect issuer on key', 401)
        except jwt.InvalidAudienceError:
            if DEBUG:
                print('IgorServlet: incorrect audience on bearer token %s' % data)
                print('IgorServlet: content: %s' % jwt.decode(data, verify=False))
            return myWebError('401 Unauthorized, incorrect audience on key', 401)
        return content

def main():
    global DEBUG
    DEBUG = True
    if DEBUG: print('igorServlet: main called')

    class HelloClass(object):
        """Example class that returns static data (which may be recomputed every call)"""

        def __init__(self):
            if DEBUG: print('HelloClass.__init__ called for', self)

        def get_hello(self):
            return 'Hello World from test'

    class CounterClass(threading.Thread):
        """Example active class that returns a counter for the number of seconds it has been running"""

        def __init__(self):
            threading.Thread.__init__(self)
            if DEBUG: print('CounterClass.__init__ called for', self)
            self.counter = 0
            self.stopped = False

        def run(self):
            while not self.stopped:
                time.sleep(1)
                self.counter += 1

        def stop(self):
            self.stopped = 1
            self.join()

        def get_count(self):
            return {'counter':self.counter}

    #
    # Parse command line arguments and instantiate the web server
    #
    parser = IgorServlet.argumentParser()
    args = parser.parse_args()
    s = IgorServlet(**vars(args))
    #
    # Instantiate and start the worker classes for this example program
    #
    helloObject = HelloClass()
    counterObject = CounterClass()
    #
    # Add the endpoints to the web server
    #
    s.addEndpoint('/hello', get=helloObject.get_hello, mimetype='text/plain')
    s.addEndpoint('/helloJSON', get=helloObject.get_hello, mimetype='application/json')
    s.addEndpoint('/count', get=counterObject.get_count, mimetype='text/plain')
    s.addEndpoint('/countJSON', get=counterObject.get_count, mimetype='application/json')
    #
    # Start the web server and possibly the other servers
    #
    s.start()
    counterObject.start()
    try:
        while True:
            time.sleep(10)
            if DEBUG: print('IgorServlet: time passing and still serving...')
    except KeyboardInterrupt:
        pass
    #
    # Stop everything when termination has been requested.
    #
    if DEBUG: print('IgorServlet: stopping server')
    counterObject.stop()
    s.stop()
    s = None

if __name__ == '__main__':
    main()

<commit_msg>Trying to get servlet to terminate cleanly in travis builds<commit_after>from __future__ import print_function
from __future__ import unicode_literals
from builtins import str
from builtins import object
from flask import Flask, request, abort, make_response
import gevent.pywsgi
import threading
import time
import json
import argparse
import os
import sys
import jwt
import logging

DEBUG=False

def myWebError(msg, code=400):
    resp = make_response(msg, code)
    abort(resp)

class IgorServlet(threading.Thread):

    @staticmethod
    def argumentParser(parser=None, description=None, port=None, name=None):
        if parser is None:
            if description is None:
                description = "Mini-server for use with Igor"
            parser = argparse.ArgumentParser(description=description)

        DEFAULTDIR=os.path.join(os.path.expanduser('~'), '.igor')
        if 'IGORSERVER_DIR' in os.environ:
            DEFAULTDIR = os.environ['IGORSERVER_DIR']
        if port is None:
            port = 8080
        if name is None:
            name = os.path.basename(os.path.splitext(sys.argv[0])[0])

        parser.add_argument("--database", metavar="DIR", help="Config and logfiles in DIR (default: %s, environment IGORSERVER_DIR)" % DEFAULTDIR, default=DEFAULTDIR)
        parser.add_argument("--name", metavar="NAME", help="Program name for use in log and config filenames in database dir(default: %s)" % name, default=name)
        parser.add_argument("--sslname", metavar="NAME", help="Program name to look up SSL certificate and key in database dir (default: igor)", default="igor")
        parser.add_argument("--port", metavar="PORT", type=int, help="Port to serve on (default: %d)" % port, default=port)
        parser.add_argument("--nossl", action="store_true", help="Do no use https (ssl) on the service, even if certificates are available")
        parser.add_argument("--nolog", action="store_true", help="Disable http server logging to stdout")
        parser.add_argument("--noCapabilities", action="store_true", help="Disable access control via capabilities (allowing all access)")
        parser.add_argument("--capabilities", action="store_true", help="Enable access control via capabilities")
        parser.add_argument("--audience", metavar="URL", help="Audience string for this servlet (for checking capabilities and their signature)")
        parser.add_argument("--issuer", metavar="URL", help="Issuer string for this servlet (for checking capabilities and their signature)")
        parser.add_argument("--sharedKey", metavar="B64STRING", help="Secret key shared with issuer (for checking capabilities and their signature)")
        return parser

    def __init__(self, port=8080, name='igorServlet', nossl=False, capabilities=None, noCapabilities=None, database=".", sslname='igor', nolog=False, audience=None, issuer=None, issuerSharedKey=None, **kwargs):
        threading.Thread.__init__(self)
        self.endpoints = {}
        self.useCapabilities = False # Default-default
        self.issuer = None
        self.issuerSharedKey = None
        self.audience = None
        self.server = None
        self.port = port
        self.name = name
#        self.middleware = ()
        self.nolog = nolog
        self.sslname = sslname
        if not self.sslname:
            self.sslname = self.name
        self.datadir = database

        self.ssl = not nossl
        keyFile = os.path.join(self.datadir, self.sslname + '.key')
        if self.ssl and not os.path.exists(keyFile):
            print('Warning: Using http in stead of https: no private key file', keyFile, file=sys.stderr)
            self.ssl = False
        if self.ssl:
            self.privateKeyFile = keyFile
            self.certificateFile = os.path.join(self.datadir, self.sslname + '.crt')
        else:
            self.privateKeyFile = None
            self.certificateFile = None

        if capabilities != None:
            self.useCapabilities = capabilities
        elif noCapabilities != None:
            self.useCapabilities = not noCapabilities
        self.audience = audience
        self.issuer = issuer
        self.issuerSharedKey = issuerSharedKey

        if DEBUG: print('igorServlet: IgorServlet.__init__ called for', self)
        self.app = Flask(__name__)

    def setIssuer(self, issuer, issuerSharedKey):
        self.issuer = issuer
        self.issuerSharedKey = issuerSharedKey

    def hasIssuer(self):
        return not not self.issuer and not not self.issuerSharedKey

    def run(self):
        if self.ssl:
            kwargs = dict(keyfile=self.privateKeyFile, certfile=self.certificateFile)
        else:
            kwargs = {}
        if self.nolog:
            kwargs['log'] = None
        self.server = gevent.pywsgi.WSGIServer(("0.0.0.0", self.port), self.app, **kwargs)
        self.server.serve_forever(stop_timeout=10)

    def stop(self):
        if self.server:
            self.server.stop(timeout=10)
            self.server = None
        return self.join()

    def addEndpoint(self, path, mimetype='application/json', get=None, put=None, post=None, delete=None):
        self.endpoints[path] = dict(mimetype=mimetype, get=get, put=put, post=post, delete=delete)
        methods = []
        if get:
            methods.append("GET")
        if put:
            methods.append("PUT")
        if post:
            methods.append("POST")
        if delete:
            methods.append("DELETE")
        self.app.add_url_rule(path, path, self._forward, methods=methods)

    def _forward(self):
        method = request.method.lower()
        path = request.path
        endpoint = self.endpoints.get(path)
        if not path:
            abort(404)
        entry = endpoint[method]
        if not entry:
            abort(404)
        if self.useCapabilities:
            # We are using capability-based access control Check that the caller has the correct
            # rights.
            if not self._checkRights(method, path):
                abort(401)
        methodArgs = {}
        methodArgs = request.values.to_dict()
        if not methodArgs:
            if request.is_json:
                methodArgs = request.json
            elif request.data:
                data = request.data
                if not isinstance(data, str):
                    assert isinstance(data, bytes)
                    data = data.decode('utf8')
                methodArgs = dict(data=data)
            else:
                methodArgs = {}
        try:
            rv = entry(**methodArgs)
        except TypeError as arg:
            return myWebError("400 Error in parameters: %s" % arg)
        if endpoint['mimetype'] == 'text/plain':
            if type(rv) != type(str):
                rv = "%s" % (rv,)
        elif endpoint['mimetype'] == 'application/json':
            if isinstance(rv, bytes):
                rv = rv.decode('utf8')
            rv = json.dumps(rv)
        else:
            assert 0, 'Unsupported mimetype %s' % endpoint['mimetype']
        # Finally ensure we send utf8 bytes back
        rv = rv.encode('utf-8')
        return rv

    def _checkRights(self, method, path):
        if DEBUG:  print('IgorServlet: check access for method %s on %s' % (method, path))
        if not self.issuer:
            if DEBUG: print('IgorServlet: issuer not set, cannot check access')
            return False
        if not self.issuerSharedKey:
            if DEBUG: print('IgorServlet: issuerSharedKey not set, cannot check access')
            return False
        # Get capability from headers
        headers = request.headers
        authHeader = headers.get('Authorization')
        if not authHeader:
            if DEBUG: print('IgorServlet: no Authorization: header')
            return False
        authFields = authHeader.split()
        if authFields[0].lower() != 'bearer':
            if DEBUG: print('IgorServlet: no Authorization: bearer header')
            return False
        encoded = authFields[1] # base64.b64decode(authFields[1])
        if DEBUG: print('IgorServlet: got bearer token data %s' % encoded)
        decoded = self._decodeBearerToken(encoded)
        if not decoded:
            # _decodeBearerToken will return None if the key is not from the right issuer or the signature doesn't match.
            return False
        capPath = decoded.get('obj')
        capModifier = decoded.get(method)
        if not capPath:
            if DEBUG: print('IgorServlet: capability does not contain obj field')
            return False
        if not capModifier:
            if DEBUG: print('IgorServlet: capability does not have %s right' % method)
            return False
        pathHead = path[:len(capPath)]
        pathTail = path[len(capPath):]
        if pathHead != capPath or (pathTail and pathTail[0] != '/'):
            if DEBUG: print('IgorServlet: capability path %s does not match %s' % (capPath, path))
            return False
        if capModifier == 'self':
            if capPath != path:
                if DEBUG: print('IgorServlet: capability path %s does not match self for %s' % (capPath, path))
                return False
        elif capModifier == 'child':
            if pathTail.count('/') != 1:
                if DEBUG: print('IgorServlet: capability path %s does not match direct child for %s' % (capPath, path))
                return False
        elif capModifier == 'descendant':
            if not pathTail.count:
                if DEBUG: print('IgorServlet: capability path %s does not match descendant for %s' % (capPath, path))
                return False
        elif capModifier == 'descendant-or-self':
            pass
        else:
            if DEBUG: print('IgorServlet: capability has unkown modifier %s for right %s' % (capModifier, method))
            return False
        if DEBUG:
            print('IgorServlet: Capability matches')
        return True

    def _decodeBearerToken(self, data):
        try:
            content = jwt.decode(data, self.issuerSharedKey, issuer=self.issuer, audience=self.audience, algorithms=['RS256', 'HS256'])
        except jwt.DecodeError:
            if DEBUG:
                print('IgorServlet: incorrect signature on bearer token %s' % data)
                print('IgorServlet: content: %s' % jwt.decode(data, verify=False))
            return myWebError('401 Unauthorized, Incorrect signature on key', 401)
        except jwt.InvalidIssuerError:
            if DEBUG:
                print('IgorServlet: incorrect issuer on bearer token %s' % data)
                print('IgorServlet: content: %s' % jwt.decode(data, verify=False))
            return myWebError('401 Unauthorized, incorrect issuer on key', 401)
        except jwt.InvalidAudienceError:
            if DEBUG:
                print('IgorServlet: incorrect audience on bearer token %s' % data)
                print('IgorServlet: content: %s' % jwt.decode(data, verify=False))
            return myWebError('401 Unauthorized, incorrect audience on key', 401)
        return content

def main():
    global DEBUG
    DEBUG = True
    if DEBUG: print('igorServlet: main called')

    class HelloClass(object):
        """Example class that returns static data (which may be recomputed every call)"""

        def __init__(self):
            if DEBUG: print('HelloClass.__init__ called for', self)

        def get_hello(self):
            return 'Hello World from test'

    class CounterClass(threading.Thread):
        """Example active class that returns a counter for the number of seconds it has been running"""

        def __init__(self):
            threading.Thread.__init__(self)
            if DEBUG: print('CounterClass.__init__ called for', self)
            self.counter = 0
            self.stopped = False

        def run(self):
            while not self.stopped:
                time.sleep(1)
                self.counter += 1

        def stop(self):
            self.stopped = 1
            self.join()

        def get_count(self):
            return {'counter':self.counter}

    #
    # Parse command line arguments and instantiate the web server
    #
    parser = IgorServlet.argumentParser()
    args = parser.parse_args()
    s = IgorServlet(**vars(args))
    #
    # Instantiate and start the worker classes for this example program
    #
    helloObject = HelloClass()
    counterObject = CounterClass()
    #
    # Add the endpoints to the web server
    #
    s.addEndpoint('/hello', get=helloObject.get_hello, mimetype='text/plain')
    s.addEndpoint('/helloJSON', get=helloObject.get_hello, mimetype='application/json')
    s.addEndpoint('/count', get=counterObject.get_count, mimetype='text/plain')
    s.addEndpoint('/countJSON', get=counterObject.get_count, mimetype='application/json')
    #
    # Start the web server and possibly the other servers
    #
    s.start()
    counterObject.start()
    try:
        while True:
            time.sleep(10)
            if DEBUG: print('IgorServlet: time passing and still serving...')
    except KeyboardInterrupt:
        pass
    #
    # Stop everything when termination has been requested.
    #
    if DEBUG: print('IgorServlet: stopping server')
    counterObject.stop()
    s.stop()
    s = None

if __name__ == '__main__':
    main()

<|endoftext|>


<commit_before>
import datetime
from tornado import web, gen

import base
import models

class ProfileHandler(base.BaseHandler):
    @base.authenticated_async
    @gen.coroutine
    @base.authorized
    def get(self, url):
        self.tf.send({'profile.dashboard.views': 1}, lambda x: x)
        error = self.get_error()
        user = yield self.get_current_user_async()
        profile = user

        # if we're not looking at our own
        # we show another profile if it's public
        if not user or user.url != url:
            profile = yield models.get_user_by_url(self.redis, url)

        recent_runs = yield self.execute_thread(
            models.Run.get_recent_runs, profile, 10)
        week = yield self.execute_thread(
            models.Week.this_week, profile)
        miles_this_week = yield self.execute_thread(
            models.Run.this_week_mileage, profile)
        year = datetime.date.today().year

        self.render('dashboard.html', page_title='Dashboard', user=user, recent_runs=recent_runs, today=datetime.date.today().strftime("%x"), error=error, miles_this_week=miles_this_week, week=week, this_year=year, profile=profile)
<commit_msg>remove unused query<commit_after>
import datetime
from tornado import web, gen

import base
import models

class ProfileHandler(base.BaseHandler):
    @base.authenticated_async
    @gen.coroutine
    @base.authorized
    def get(self, url):
        self.tf.send({'profile.dashboard.views': 1}, lambda x: x)
        error = self.get_error()
        user = yield self.get_current_user_async()
        profile = user

        # if we're not looking at our own
        # we show another profile if it's public
        if not user or user.url != url:
            profile = yield models.get_user_by_url(self.redis, url)

        recent_runs = yield self.execute_thread(
            models.Run.get_recent_runs, profile, 10)
        week = yield self.execute_thread(
            models.Week.this_week, profile)
        year = datetime.date.today().year

        self.render('dashboard.html', page_title='Dashboard', user=user, recent_runs=recent_runs, today=datetime.date.today().strftime("%x"), error=error, week=week, this_year=year, profile=profile)
<|endoftext|>


<commit_before>"""
    ulmo.usgs.core
    ~~~~~~~~~~~~~~

    This module provides direct access to `USGS National Water Information
    System`_ web services.

    .. _USGS National Water Information System: http://waterdata.usgs.gov/nwis

"""
import cStringIO as StringIO
import datetime
import logging

import isodate
import requests

from ulmo import util
import ulmo.waterml.v1_1 as wml

INSTANTANEOUS_URL = "http://waterservices.usgs.gov/nwis/iv/"
DAILY_URL = "http://waterservices.usgs.gov/nwis/dv/"

# configure logging
LOG_FORMAT = '%(message)s'
logging.basicConfig(format=LOG_FORMAT)
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)

def get_sites(sites=None, state_code=None, site_type=None, service=None):
    """Fetches site information from USGS services. See the USGS waterservices
    documentation for options.

    Parameters
    ----------
    sites : str, iterable of strings or ``None``
        The site to use or list of sites to use; lists will be joined by a ','.
    state_code : str or ``None``
        Two-letter state code used in stateCd parameter.
    site_type : str or ``None``
        Type of site used in siteType parameter.
    service : {``None``, 'instantaneous', 'iv', 'daily', 'dv'}
        The service to use, either "instantaneous", "daily", or ``None``
        (default).  If set to ``None``, then both services are used.  The
        abbreviations "iv" and "dv" can be used for "instantaneous" and "daily",
        respectively.

    Returns
    -------
    sites_dict : dict
        a python dict with site codes mapped to site information
    """
    url_params = {'format': 'waterml'}

    if state_code:
        url_params['stateCd'] = state_code

    if sites:
        if isinstance(sites, basestring):
            url_params['sites'] = sites
        else:
            url_params['sites'] = ','.join(sites)

    if site_type:
        url_params['siteType'] = site_type

    if service == 'dv':
        service = 'daily'
    if service == 'iv':
        service == 'instantaneous'

    if not service:
        return_sites = get_sites(sites=sites, state_code=state_code, site_type=site_type, service="daily")
        return_sites.update(get_sites(sites=sites, state_code=state_code, site_type=site_type, service="instantaneous"))

    else:
        url = _get_service_url(service)
        log.info('making request for sites: %s' % url)
        req = requests.get(url, params=url_params)
        log.info("processing data from request: %s" % req.request.url)
        content_io = StringIO.StringIO(str(req.content))

        return_sites = wml.parse_site_infos(content_io)
    return return_sites

def get_site_data(site_code, service=None, parameter_code=None,
                  start=None, end=None, period=None, modified_since=None):
    """Fetches site data.

    Parameters
    ----------
    site_code : str
        The site code of the site you want to query data for.
    service : {``None``, 'instantaneous', 'iv', 'daily', 'dv'}
        The service to use, either "instantaneous", "daily", or ``None``
        (default).  If set to ``None``, then both services are used.  The
        abbreviations "iv" and "dv" can be used for "instantaneous" and "daily",
        respectively.
    parameter_code : str
        Parameter code(s) that will be passed as the parameterCd parameter.
    start : ``None`` or datetime (see :ref:`dates-and-times`)
        Start of a date range for a query. This parameter is mutually exclusive
        with period (you cannot use both).
    end : ``None`` or datetime (see :ref:`dates-and-times`)
        End of a date range for a query. This parameter is mutually exclusive
        with period (you cannot use both).
    period : {``None``, str, datetime.timedelta}
        Period of time to use for requesting data. This will be passed along as
        the period parameter. This can either be 'all' to signal that you'd like
        the entire period of record, or string in ISO 8601 period format (e.g.
        'P1Y2M21D' for a period of one year, two months and 21 days) or it can
        be a datetime.timedelta object representing the period of time. This
        parameter is mutually exclusive with start/end dates.
    modified_since : ``None`` or datetime.timedelta
        Passed along as the modifiedSince parameter.

    Returns
    -------
    data_dict : dict
        a python dict with parameter codes mapped to value dicts
    """
    url_params = {'format': 'waterml',
                  'site': site_code}
    if parameter_code:
        url_params['parameterCd'] = parameter_code
    if modified_since:
        url_params['modifiedSince'] = isodate.duration_isoformat(modified_since)

    if not (start is None or end is None) and period is not None:
        raise ValueError("using date range with start/end AND period is allowed"
                " because it's ambiguous, use one or the other")
    if period is not None:
        if isinstance(period, basestring):
            if period == 'all':
                if service in ('iv', 'instantaneous'):
                    start = datetime.datetime(2007, 10, 1)
                elif service in ('dv', 'daily'):
                    start = datetime.datetime(1851, 1, 1)
            else:
                url_params['period'] = period
        elif isinstance(period, datetime.timedelta):
            url_params['period'] = isodate.duration_isoformat(period)
    if start is not None:
        start_datetime = util.convert_datetime(start)
        url_params['startDT'] = isodate.datetime_isoformat(start_datetime)
    if end is not None:
        end_datetime = util.convert_datetime(end)
        url_params['endDT'] = isodate.datetime_isoformat(end_datetime)

    if service == 'dv':
        service = 'daily'
    if service == 'iv':
        service == 'instantaneous'

    if service in ('daily', 'instantaneous'):
        values = _get_site_values(service, url_params)
    elif service is None:
        kwargs = dict(parameter_code=parameter_code, start=start, end=end,
                period=period, modified_since=modified_since)
        values = get_site_data(site_code, service='daily', **kwargs)
        values.update(
            get_site_data(site_code, service='instantaneous', **kwargs))
    else:
        raise ValueError("service must either be 'daily', 'instantaneous' or none")

    return values

def _get_service_url(service):
    if service in ('daily', 'dv'):
        return DAILY_URL
    elif service in ('instantaneous', 'iv'):
        return INSTANTANEOUS_URL
    else:
        raise "service must be either 'daily' ('dv') or 'instantaneous' ('iv')"

def _get_site_values(service, url_params):
    """downloads and parses values for a site

    returns a values dict containing variable and data values
    """
    service_url = _get_service_url(service)

    query_isodate = isodate.datetime_isoformat(datetime.datetime.now())
    try:
        req = requests.get(service_url, params=url_params)
    except requests.exceptions.ConnectionError:
        log.info("There was a connection error with query:\n\t%s\n\t%s" % (service_url, url_params))
        return {}
    log.info("processing data from request: %s" % req.request.url)

    if req.status_code != 200:
        return {}
    content_io = StringIO.StringIO(str(req.content))

    data_dict = wml.parse_site_values(content_io, query_isodate)

    return data_dict
<commit_msg>rename usgs site properties so they are at the top of the site dict itself<commit_after>"""
    ulmo.usgs.core
    ~~~~~~~~~~~~~~

    This module provides direct access to `USGS National Water Information
    System`_ web services.

    .. _USGS National Water Information System: http://waterdata.usgs.gov/nwis

"""
import cStringIO as StringIO
import datetime
import logging

import isodate
import requests

from ulmo import util
import ulmo.waterml.v1_1 as wml

INSTANTANEOUS_URL = "http://waterservices.usgs.gov/nwis/iv/"
DAILY_URL = "http://waterservices.usgs.gov/nwis/dv/"

# configure logging
LOG_FORMAT = '%(message)s'
logging.basicConfig(format=LOG_FORMAT)
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)

def get_sites(sites=None, state_code=None, site_type=None, service=None):
    """Fetches site information from USGS services. See the USGS waterservices
    documentation for options.

    Parameters
    ----------
    sites : str, iterable of strings or ``None``
        The site to use or list of sites to use; lists will be joined by a ','.
    state_code : str or ``None``
        Two-letter state code used in stateCd parameter.
    site_type : str or ``None``
        Type of site used in siteType parameter.
    service : {``None``, 'instantaneous', 'iv', 'daily', 'dv'}
        The service to use, either "instantaneous", "daily", or ``None``
        (default).  If set to ``None``, then both services are used.  The
        abbreviations "iv" and "dv" can be used for "instantaneous" and "daily",
        respectively.

    Returns
    -------
    sites_dict : dict
        a python dict with site codes mapped to site information
    """
    url_params = {'format': 'waterml'}

    if state_code:
        url_params['stateCd'] = state_code

    if sites:
        if isinstance(sites, basestring):
            url_params['sites'] = sites
        else:
            url_params['sites'] = ','.join(sites)

    if site_type:
        url_params['siteType'] = site_type

    if service == 'dv':
        service = 'daily'
    if service == 'iv':
        service == 'instantaneous'

    if not service:
        return_sites = get_sites(sites=sites, state_code=state_code, site_type=site_type, service="daily")
        return_sites.update(get_sites(sites=sites, state_code=state_code, site_type=site_type, service="instantaneous"))

    else:
        url = _get_service_url(service)
        log.info('making request for sites: %s' % url)
        req = requests.get(url, params=url_params)
        log.info("processing data from request: %s" % req.request.url)
        content_io = StringIO.StringIO(str(req.content))

        return_sites = wml.parse_site_infos(content_io)
        return_sites = dict([
            (code, _extract_site_properties(site))
            for code, site in return_sites.iteritems()
        ])

        if not leave_open:
            content_io.close()
    return return_sites

def get_site_data(site_code, service=None, parameter_code=None,
                  start=None, end=None, period=None, modified_since=None):
    """Fetches site data.

    Parameters
    ----------
    site_code : str
        The site code of the site you want to query data for.
    service : {``None``, 'instantaneous', 'iv', 'daily', 'dv'}
        The service to use, either "instantaneous", "daily", or ``None``
        (default).  If set to ``None``, then both services are used.  The
        abbreviations "iv" and "dv" can be used for "instantaneous" and "daily",
        respectively.
    parameter_code : str
        Parameter code(s) that will be passed as the parameterCd parameter.
    start : ``None`` or datetime (see :ref:`dates-and-times`)
        Start of a date range for a query. This parameter is mutually exclusive
        with period (you cannot use both).
    end : ``None`` or datetime (see :ref:`dates-and-times`)
        End of a date range for a query. This parameter is mutually exclusive
        with period (you cannot use both).
    period : {``None``, str, datetime.timedelta}
        Period of time to use for requesting data. This will be passed along as
        the period parameter. This can either be 'all' to signal that you'd like
        the entire period of record, or string in ISO 8601 period format (e.g.
        'P1Y2M21D' for a period of one year, two months and 21 days) or it can
        be a datetime.timedelta object representing the period of time. This
        parameter is mutually exclusive with start/end dates.
    modified_since : ``None`` or datetime.timedelta
        Passed along as the modifiedSince parameter.

    Returns
    -------
    data_dict : dict
        a python dict with parameter codes mapped to value dicts
    """
    url_params = {'format': 'waterml',
                  'site': site_code}
    if parameter_code:
        url_params['parameterCd'] = parameter_code
    if modified_since:
        url_params['modifiedSince'] = isodate.duration_isoformat(modified_since)

    if not (start is None or end is None) and period is not None:
        raise ValueError("using date range with start/end AND period is allowed"
                " because it's ambiguous, use one or the other")
    if period is not None:
        if isinstance(period, basestring):
            if period == 'all':
                if service in ('iv', 'instantaneous'):
                    start = datetime.datetime(2007, 10, 1)
                elif service in ('dv', 'daily'):
                    start = datetime.datetime(1851, 1, 1)
            else:
                url_params['period'] = period
        elif isinstance(period, datetime.timedelta):
            url_params['period'] = isodate.duration_isoformat(period)
    if start is not None:
        start_datetime = util.convert_datetime(start)
        url_params['startDT'] = isodate.datetime_isoformat(start_datetime)
    if end is not None:
        end_datetime = util.convert_datetime(end)
        url_params['endDT'] = isodate.datetime_isoformat(end_datetime)

    if service == 'dv':
        service = 'daily'
    if service == 'iv':
        service == 'instantaneous'

    if service in ('daily', 'instantaneous'):
        values = _get_site_values(service, url_params)
    elif service is None:
        kwargs = dict(parameter_code=parameter_code, start=start, end=end,
                period=period, modified_since=modified_since)
        values = get_site_data(site_code, service='daily', **kwargs)
        values.update(
            get_site_data(site_code, service='instantaneous', **kwargs))
    else:
        raise ValueError("service must either be 'daily', 'instantaneous' or none")

    return values

def _extract_site_properties(site):
    rename_properties = [
        ('county_cd', 'county'),
        ('huc_cd', 'huc'),
        ('site_type_cd', 'site_type'),
        ('state_cd', 'state_code'),
    ]
    site_properties = site['site_property']
    for old, new in rename_properties:
        if old in site_properties:
            site[new] = site_properties[old]
            del site_properties[old]

    if len(site_properties) == 0:
        del site['site_property']
    else:
        site['site_property'] = site_properties

    return site

def _get_service_url(service):
    if service in ('daily', 'dv'):
        return DAILY_URL
    elif service in ('instantaneous', 'iv'):
        return INSTANTANEOUS_URL
    else:
        raise "service must be either 'daily' ('dv') or 'instantaneous' ('iv')"

def _get_site_values(service, url_params):
    """downloads and parses values for a site

    returns a values dict containing variable and data values
    """
    service_url = _get_service_url(service)

    query_isodate = isodate.datetime_isoformat(datetime.datetime.now())
    try:
        req = requests.get(service_url, params=url_params)
    except requests.exceptions.ConnectionError:
        log.info("There was a connection error with query:\n\t%s\n\t%s" % (service_url, url_params))
        return {}
    log.info("processing data from request: %s" % req.request.url)

    if req.status_code != 200:
        return {}
    content_io = StringIO.StringIO(str(req.content))

    data_dict = wml.parse_site_values(content_io, query_isodate)

    return data_dict
<|endoftext|>


<commit_before># Copyright 2018 The TensorFlow Probability 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.
# ============================================================================
"""Transition Kernel base class and utilities."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import abc
import six

__all__ = [
    "TransitionKernel",
]

# TODO(b/74235190): Add additional documentation/examples to the
# `TransitionKernel` docstring.

@six.add_metaclass(abc.ABCMeta)
class TransitionKernel(object):
  """Base class for all MCMC `TransitionKernel`s.

  This class defines the minimal requirements to efficiently implement a Markov
  chain Monte Carlo (MCMC) transition kernel. A transition kernel returns a new
  state given some old state. It also takes (and returns) "side information"
  which may be used for debugging or optimization purposes (i.e, to "recycle"
  previously computed results).
  """

  @abc.abstractmethod
  def one_step(self, current_state, previous_kernel_results, seed=None):
    """Takes one step of the TransitionKernel.

    Must be overridden by subclasses.

    Args:
      current_state: `Tensor` or Python `list` of `Tensor`s representing the
        current state(s) of the Markov chain(s).
      previous_kernel_results: A (possibly nested) `tuple`, `namedtuple` or
        `list` of `Tensor`s representing internal calculations made within the
        previous call to this function (or as returned by `bootstrap_results`).
      seed: Optional, a seed for reproducible sampling.

    Returns:
      next_state: `Tensor` or Python `list` of `Tensor`s representing the
        next state(s) of the Markov chain(s).
      kernel_results: A (possibly nested) `tuple`, `namedtuple` or `list` of
        `Tensor`s representing internal calculations made within this function.
    """
    raise NotImplementedError()

  @abc.abstractproperty
  def is_calibrated(self):
    """Returns `True` if Markov chain converges to specified distribution.

    `TransitionKernel`s which are "uncalibrated" are often calibrated by
    composing them with the `tfp.mcmc.MetropolisHastings` `TransitionKernel`.
    """
    raise NotImplementedError()

  def bootstrap_results(self, init_state):  # pylint: disable=unused-argument
    """Returns an object with the same type as returned by `one_step(...)[1]`.

    Args:
      init_state: `Tensor` or Python `list` of `Tensor`s representing the
        initial state(s) of the Markov chain(s).

    Returns:
      kernel_results: A (possibly nested) `tuple`, `namedtuple` or `list` of
        `Tensor`s representing internal calculations made within this function.
    """
    return []

  def copy(self, **override_parameter_kwargs):
    """Non-destructively creates a deep copy of the kernel.

    Args:
      **override_parameter_kwargs: Python String/value `dictionary` of
        initialization arguments to override with new values.

    Returns:
      new_kernel: `TransitionKernel` object of same type as `self`,
        initialized with the union of self.parameters and
        override_parameter_kwargs, with any shared keys overridden by the
        value of override_parameter_kwargs, i.e.,
        `dict(self.parameters, **override_parameters_kwargs)`.
    """
    parameters = dict(self.parameters, **override_parameter_kwargs)
    new_kernel = type(self)(**parameters)
    return new_kernel
<commit_msg>Add an example to the `TransitionKernel` docstring.<commit_after># Copyright 2018 The TensorFlow Probability 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.
# ============================================================================
"""Transition Kernel base class and utilities."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import abc
import six

__all__ = [
    "TransitionKernel",
]

@six.add_metaclass(abc.ABCMeta)
class TransitionKernel(object):
  """Base class for all MCMC `TransitionKernel`s.

  This class defines the minimal requirements to efficiently implement a Markov
  chain Monte Carlo (MCMC) transition kernel. A transition kernel returns a new
  state given some old state. It also takes (and returns) "side information"
  which may be used for debugging or optimization purposes (i.e, to "recycle"
  previously computed results).

  #### Example (random walk transition kernel):

  In this example we make isotropic Gaussian proposals of a given step size.

  ```python
  from tensorflow_probability.python.mcmc import kernel as kernel_base
  import tensorflow.compat.v2 as tf
  tf.enable_v2_behavior()
  import tensorflow_probability as tfp

  tfd = tfp.distributions

  RWResult = collections.namedtuple("RWResult", 'target_log_prob')

  class RandomWalkProposalKernel(kernel_base.TransitionKernel):
    def __init__(self, target_log_prob_fn, step_size):
      self._parameters = dict(
        target_log_prob_fn = target_log_prob_fn,
        step_size = step_size)

    @property
    def target_log_prob_fn(self):
      return self._parameters['target_log_prob_fn']

    @property
    def step_size(self):
      return self._parameters['step_size']

    @property
    def is_calibrated(self):
      return False

    def one_step(self, current_state, previous_kernel_results, seed=None):
      scale = tf.broadcast_to(self.step_size, tf.shape(current_state))
      isotropic_normal = tfd.Normal(loc=current_state, scale=scale)

      next_state = isotropic_normal.sample(seed=seed)
      next_target_log_prob = self.target_log_prob_fn(next_state)
      new_kernel_results = previous_kernel_results._replace(
        target_log_prob = next_target_log_prob)

      return next_state, new_kernel_results

    def bootstrap_results(self, init_state):
      kernel_results = RWResult(
        target_log_prob = self.target_log_prob_fn(init_state))
      return kernel_results
  ```

  """

  @abc.abstractmethod
  def one_step(self, current_state, previous_kernel_results, seed=None):
    """Takes one step of the TransitionKernel.

    Must be overridden by subclasses.

    Args:
      current_state: `Tensor` or Python `list` of `Tensor`s representing the
        current state(s) of the Markov chain(s).
      previous_kernel_results: A (possibly nested) `tuple`, `namedtuple` or
        `list` of `Tensor`s representing internal calculations made within the
        previous call to this function (or as returned by `bootstrap_results`).
      seed: Optional, a seed for reproducible sampling.

    Returns:
      next_state: `Tensor` or Python `list` of `Tensor`s representing the
        next state(s) of the Markov chain(s).
      kernel_results: A (possibly nested) `tuple`, `namedtuple` or `list` of
        `Tensor`s representing internal calculations made within this function.
    """
    raise NotImplementedError()

  @abc.abstractproperty
  def is_calibrated(self):
    """Returns `True` if Markov chain converges to specified distribution.

    `TransitionKernel`s which are "uncalibrated" are often calibrated by
    composing them with the `tfp.mcmc.MetropolisHastings` `TransitionKernel`.
    """
    raise NotImplementedError()

  def bootstrap_results(self, init_state):  # pylint: disable=unused-argument
    """Returns an object with the same type as returned by `one_step(...)[1]`.

    Args:
      init_state: `Tensor` or Python `list` of `Tensor`s representing the
        initial state(s) of the Markov chain(s).

    Returns:
      kernel_results: A (possibly nested) `tuple`, `namedtuple` or `list` of
        `Tensor`s representing internal calculations made within this function.
    """
    return []

  def copy(self, **override_parameter_kwargs):
    """Non-destructively creates a deep copy of the kernel.

    Args:
      **override_parameter_kwargs: Python String/value `dictionary` of
        initialization arguments to override with new values.

    Returns:
      new_kernel: `TransitionKernel` object of same type as `self`,
        initialized with the union of self.parameters and
        override_parameter_kwargs, with any shared keys overridden by the
        value of override_parameter_kwargs, i.e.,
        `dict(self.parameters, **override_parameters_kwargs)`.
    """
    parameters = dict(self.parameters, **override_parameter_kwargs)
    new_kernel = type(self)(**parameters)
    return new_kernel
<|endoftext|>


<commit_before># End-screen python file
import pygame
import button
import translate

def update(game):
    pass

def init(game):
    pass

def draw(game):
    # Termination screen colour
    game.screen.fill((199, 251, 126))
    font = pygame.font.Font(None, 72)

    # Termination screen text + colour
    label_1 = font.render(translate.translate("PLAYER_WON"), 1, (255,8,148))
    size = font.size(translate.translate("PLAYER_WON"))

    # Placement text in screen
    game.screen.blit(label_1,(int(game.width/2 - (size[0]/2)), game.height/2 - (size[1]/2)))

    # button variables
    btn_width = game.width / 5;
    btn_height = game.height / 10;
    btn_x_off = (game.width / 2) - (btn_width / 2)
    btn_y_off = lambda idx: (game.height / 10) * (idx + 1) + (idx * 10)

    button.draw(game, btn_x_off, btn_y_off(4), int(btn_width), int(btn_height), translate.translate("BACK"), 20, (25,25,25), (255,255,255), lambda x: game.set_state(0))<commit_msg>end screen change<commit_after># End-screen python file
import pygame
import button
import translate

def update(game):
    pass

def init(game):
    pass

def draw(game):
    # Termination screen colour
    game.screen.fill((199, 251, 126))
    font = pygame.font.Font(None, 72)

    # Termination screen text + colour
    label_1 = font.render(translate.translate("PLAYER_WON"), 1, (255,8,148))
    size = font.size(translate.translate("PLAYER_WON"))

    # Placement text in screen
    game.screen.blit(label_1,(int(game.width/2 - (size[0]/2)), game.height*0.2 - (size[1]/2)))

    # button variables
    btn_width = game.width / 5;
    btn_height = game.height / 10;
    btn_x_off = (game.width / 2) - (btn_width / 2)
    btn_y_off = lambda idx: (game.height / 10) * (idx + 1) + (idx * 10)

    button.draw(game, btn_x_off, btn_y_off(6), int(btn_width), int(btn_height), translate.translate("BACK"), 20, (25,25,25), (255,255,255), lambda x: game.set_state(0))<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
'''
    Mango HTTP base handlers.
'''

# This file is part of mango.

# Distributed under the terms of the last AGPL License.
# The full license is in the file LICENCE, distributed as part of this software.

__author__ = 'Team Machine'

import uuid
import logging
import ujson as json
from tornado import gen
from tornado import web
from mango.system import basic_authentication
from mango.messages import accounts as models
from mango.tools import clean_structure, validate_uuid4
from mango import errors
from tornado import httpclient as _http_client

_http_client.AsyncHTTPClient.configure('tornado.curl_httpclient.CurlAsyncHTTPClient')
http_client = _http_client.AsyncHTTPClient()

class BaseHandler(web.RequestHandler):
    '''
        gente d'armi e ganti
    '''
    @property
    def kvalue(self):
        '''
            Key-value database
        '''
        return self.settings['kvalue']

    def initialize(self, **kwargs):
        '''
            Initialize the Base Handler
        '''
        super(BaseHandler, self).initialize(**kwargs)
        # System database
        self.db = self.settings.get('db')
        # System cache
        self.cache = self.settings.get('cache')
        # Page settings
        self.page_size = self.settings.get('page_size')
        # solr riak
        self.solr = self.settings.get('solr')

    def set_default_headers(self):
        '''
            mango default headers
        '''
        self.set_header("Access-Control-Allow-Origin", self.settings.get('domain', '*'))
        #self.set_header("Access-Control-Allow-Origin", self.settings['domain'])

    def get_username_cookie(self):
        '''
            Return the username from a secure cookie (require cookie_secret)
        '''
        return self.get_secure_cookie('username')

    @gen.coroutine
    def check_account_type(self, account):
        '''
            check account type
        '''
        search_index = 'mango_account_index'
        query = 'account_register:{0}'.format(account)
        filter_query = 'account_register:{0}'.format(account)
        #parse and build url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        got_response = []
        # response message
        message = {'message': 'not found'}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                yield gen.sleep(0.0020) # don't be careless with the time.
            stuff = got_response[0]
            if stuff['response']['numFound']:
                response_doc = stuff['response']['docs'][0]
                IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
                message = dict(
                    (key.split('_register')[0], value)
                    for (key, value) in response_doc.items()
                    if key not in IGNORE_ME
                )
        except Exception, e:
            logging.exception(e)
            raise gen.Return(e)
        raise gen.Return(message.get('account_type', 'not found'))

    @gen.coroutine
    def get_permissions(self, account):
        '''
            Get permissions
        '''
        search_index = 'mango_account_index'
        query = 'account_register:{0}'.format(account)
        filter_query = 'account_register:{0}'.format(account)
        #parse and build url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        got_response = []
        # response message
        message = {'message': 'not found'}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                yield gen.sleep(0.0020) # don't be careless with the time.
            stuff = got_response[0]
            if stuff['response']['numFound']:
                response_doc = stuff['response']['docs'][0]
                IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
                message = dict(
                    (key.split('_register')[0], value)
                    for (key, value) in response_doc.items()
                    if key not in IGNORE_ME
                )

        except Exception, e:
            logging.exception(e)
            raise gen.Return(e)
        raise gen.Return(message.get('permissions', []))

    @gen.coroutine
    def get_account_uuid(self, account):
        '''
            Get valid account uuid
        '''
        logging.info(account)

        search_index = 'mango_account_index'
        query = 'account_register:{0}'.format(account)
        filter_query = 'account_register:{0}'.format(account)
        # parse and build url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        got_response = []
        # clean response message
        message = {}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                yield gen.sleep(0.0020) # don't be careless with the time.
            stuff = got_response[0]
            if stuff['response']['numFound']:
                response_doc = stuff['response']['docs'][0]
                IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
                message = dict(
                    (key.split('_register')[0], value)
                    for (key, value) in response_doc.items()
                    if key not in IGNORE_ME
                )
        except Exception, e:
            logging.exception(e)
            raise gen.Return(e)
        raise gen.Return(message.get('uuid', 'not found'))

    @gen.coroutine
    def get_auth_uuid(self, account, password):
        '''
            Get valid account uuid
        '''
        search_index = 'mango_account_index'
        query = 'password_register:{0}'.format(password)
        filter_query = 'account_register:{0}'.format(account)
        #parse and build url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        got_response = []
        # clean response message
        message = {}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                yield gen.sleep(0.0020) # don't be careless with the time.
            stuff = got_response[0]
            if stuff['response']['numFound']:
                response_doc = stuff['response']['docs'][0]
                IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
                message = dict(
                    (key.split('_register')[0], value)
                    for (key, value) in response_doc.items()
                    if key not in IGNORE_ME
                )
        except Exception, e:
            logging.exception(e)
            raise gen.Return(e)
        raise gen.Return(message.get('uuid', 'not found'))

    @gen.coroutine
    def get_account_labels(self, account):
        '''
            Get account labels
        '''
        search_index = 'mango_account_index'
        query = 'account_register:{0}'.format(account)
        filter_query = 'account_register:{0}'.format(account)
        #parse and build url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        got_response = []
        #response message
        message = {}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                yield gen.sleep(0.0020) # don't be careless with the time.
            stuff = got_response[0]
            if stuff['response']['numFound']:
                response_doc = stuff['response']['docs'][0]
                IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
                message = dict(
                    (key.split('_register')[0], value)
                    for (key, value) in response_doc.items()
                    if key not in IGNORE_ME
                )
        except Exception, e:
            logging.exception(e)
            raise gen.Return(e)
        raise gen.Return([str(x) for x in message.get('labels', [])])

@basic_authentication
class LoginHandler(BaseHandler):
    '''
        BasicAuth login
    '''
    @gen.coroutine
    def get(self):
        # clean message
        message = {}
        message['uuid'] = yield self.get_auth_uuid(self.username, self.password)
        message['account_type'] = yield self.check_account_type(self.username)
        message['permissions'] = yield self.get_permissions(self.username)
        message['labels'] = yield self.get_account_labels(self.username)
        if validate_uuid4(message.get('uuid')):
            self.set_header('Access-Control-Allow-Origin','*')
            self.set_header('Access-Control-Allow-Methods','GET, OPTIONS')
            self.set_header('Access-Control-Allow-Headers','Content-Type, Authorization')
            #self.set_cookie('username', self.username)
            self.set_secure_cookie("username", self.get_argument("username"))
            #account_type
            #self.set_secure_cookie('account_type', str(message['account_type']))
            # permissions
            #self.set_secure_cookie('permissions', str(message['permissions']))
            # yo what is this?
            self.username, self.password = (None, None)
            self.set_status(200)
            self.finish(message)
        else:
            self.set_status(403)
            # I don't know why not 401?
            self.set_header('WWW-Authenticate', 'Basic realm=mango')
            self.finish()

    @gen.coroutine
    def options(self):
        self.set_header('Access-Control-Allow-Origin','*')
        self.set_header('Access-Control-Allow-Methods','GET, OPTIONS')
        self.set_header('Access-Control-Allow-Headers','Content-Type, Authorization')
        self.set_status(200)
        self.finish()

class LogoutHandler(BaseHandler):
    '''
        BasicAuth logout
    '''

    @gen.coroutine
    def get(self):
        '''
            Clear secure cookie
        '''
        self.clear_cookie('username')
        self.clear_cookie('labels')
        self.set_status(200)
        self.finish()
<commit_msg>cookie<commit_after># -*- coding: utf-8 -*-
'''
    Mango HTTP base handlers.
'''

# This file is part of mango.

# Distributed under the terms of the last AGPL License.
# The full license is in the file LICENCE, distributed as part of this software.

__author__ = 'Team Machine'

import uuid
import logging
import ujson as json
from tornado import gen
from tornado import web
from mango.system import basic_authentication
from mango.messages import accounts as models
from mango.tools import clean_structure, validate_uuid4
from mango import errors
from tornado import httpclient as _http_client

_http_client.AsyncHTTPClient.configure('tornado.curl_httpclient.CurlAsyncHTTPClient')
http_client = _http_client.AsyncHTTPClient()

class BaseHandler(web.RequestHandler):
    '''
        gente d'armi e ganti
    '''
    @property
    def kvalue(self):
        '''
            Key-value database
        '''
        return self.settings['kvalue']

    def initialize(self, **kwargs):
        '''
            Initialize the Base Handler
        '''
        super(BaseHandler, self).initialize(**kwargs)
        # System database
        self.db = self.settings.get('db')
        # System cache
        self.cache = self.settings.get('cache')
        # Page settings
        self.page_size = self.settings.get('page_size')
        # solr riak
        self.solr = self.settings.get('solr')

    def set_default_headers(self):
        '''
            mango default headers
        '''
        self.set_header("Access-Control-Allow-Origin", self.settings.get('domain', '*'))
        #self.set_header("Access-Control-Allow-Origin", self.settings['domain'])

    def get_username_cookie(self):
        '''
            Return the username from a secure cookie (require cookie_secret)
        '''
        return self.get_secure_cookie("username")

    @gen.coroutine
    def check_account_type(self, account):
        '''
            check account type
        '''
        search_index = 'mango_account_index'
        query = 'account_register:{0}'.format(account)
        filter_query = 'account_register:{0}'.format(account)
        #parse and build url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        got_response = []
        # response message
        message = {'message': 'not found'}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                yield gen.sleep(0.0020) # don't be careless with the time.
            stuff = got_response[0]
            if stuff['response']['numFound']:
                response_doc = stuff['response']['docs'][0]
                IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
                message = dict(
                    (key.split('_register')[0], value)
                    for (key, value) in response_doc.items()
                    if key not in IGNORE_ME
                )
        except Exception, e:
            logging.exception(e)
            raise gen.Return(e)
        raise gen.Return(message.get('account_type', 'not found'))

    @gen.coroutine
    def get_permissions(self, account):
        '''
            Get permissions
        '''
        search_index = 'mango_account_index'
        query = 'account_register:{0}'.format(account)
        filter_query = 'account_register:{0}'.format(account)
        #parse and build url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        got_response = []
        # response message
        message = {'message': 'not found'}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                yield gen.sleep(0.0020) # don't be careless with the time.
            stuff = got_response[0]
            if stuff['response']['numFound']:
                response_doc = stuff['response']['docs'][0]
                IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
                message = dict(
                    (key.split('_register')[0], value)
                    for (key, value) in response_doc.items()
                    if key not in IGNORE_ME
                )

        except Exception, e:
            logging.exception(e)
            raise gen.Return(e)
        raise gen.Return(message.get('permissions', []))

    @gen.coroutine
    def get_account_uuid(self, account):
        '''
            Get valid account uuid
        '''
        logging.info(account)

        search_index = 'mango_account_index'
        query = 'account_register:{0}'.format(account)
        filter_query = 'account_register:{0}'.format(account)
        # parse and build url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        got_response = []
        # clean response message
        message = {}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                yield gen.sleep(0.0020) # don't be careless with the time.
            stuff = got_response[0]
            if stuff['response']['numFound']:
                response_doc = stuff['response']['docs'][0]
                IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
                message = dict(
                    (key.split('_register')[0], value)
                    for (key, value) in response_doc.items()
                    if key not in IGNORE_ME
                )
        except Exception, e:
            logging.exception(e)
            raise gen.Return(e)
        raise gen.Return(message.get('uuid', 'not found'))

    @gen.coroutine
    def get_auth_uuid(self, account, password):
        '''
            Get valid account uuid
        '''
        search_index = 'mango_account_index'
        query = 'password_register:{0}'.format(password)
        filter_query = 'account_register:{0}'.format(account)
        #parse and build url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        got_response = []
        # clean response message
        message = {}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                yield gen.sleep(0.0020) # don't be careless with the time.
            stuff = got_response[0]
            if stuff['response']['numFound']:
                response_doc = stuff['response']['docs'][0]
                IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
                message = dict(
                    (key.split('_register')[0], value)
                    for (key, value) in response_doc.items()
                    if key not in IGNORE_ME
                )
        except Exception, e:
            logging.exception(e)
            raise gen.Return(e)
        raise gen.Return(message.get('uuid', 'not found'))

    @gen.coroutine
    def get_account_labels(self, account):
        '''
            Get account labels
        '''
        search_index = 'mango_account_index'
        query = 'account_register:{0}'.format(account)
        filter_query = 'account_register:{0}'.format(account)
        #parse and build url
        url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format(
            self.solr, search_index, query, filter_query
        )
        got_response = []
        #response message
        message = {}
        def handle_request(response):
            '''
                Request Async Handler
            '''
            if response.error:
                logging.error(response.error)
                got_response.append({'error':True, 'message': response.error})
            else:
                got_response.append(json.loads(response.body))
        try:
            http_client.fetch(
                url,
                callback=handle_request
            )
            while len(got_response) == 0:
                yield gen.sleep(0.0020) # don't be careless with the time.
            stuff = got_response[0]
            if stuff['response']['numFound']:
                response_doc = stuff['response']['docs'][0]
                IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb"]
                message = dict(
                    (key.split('_register')[0], value)
                    for (key, value) in response_doc.items()
                    if key not in IGNORE_ME
                )
        except Exception, e:
            logging.exception(e)
            raise gen.Return(e)
        raise gen.Return([str(x) for x in message.get('labels', [])])

@basic_authentication
class LoginHandler(BaseHandler):
    '''
        BasicAuth login
    '''
    @gen.coroutine
    def get(self):
        # clean message
        message = {}
        message['uuid'] = yield self.get_auth_uuid(self.username, self.password)
        message['account_type'] = yield self.check_account_type(self.username)
        message['permissions'] = yield self.get_permissions(self.username)
        message['labels'] = yield self.get_account_labels(self.username)
        if validate_uuid4(message.get('uuid')):
            self.set_header('Access-Control-Allow-Origin','*')
            self.set_header('Access-Control-Allow-Methods','GET, OPTIONS')
            self.set_header('Access-Control-Allow-Headers','Content-Type, Authorization')
            #self.set_cookie('username', self.username)
            self.set_secure_cookie("username", self.get_argument("username"))
            #account_type
            #self.set_secure_cookie('account_type', str(message['account_type']))
            # permissions
            #self.set_secure_cookie('permissions', str(message['permissions']))
            # yo what is this?
            self.username, self.password = (None, None)
            self.set_status(200)
            self.finish(message)
        else:
            self.set_status(403)
            # I don't know why not 401?
            self.set_header('WWW-Authenticate', 'Basic realm=mango')
            self.finish()

    @gen.coroutine
    def options(self):
        self.set_header('Access-Control-Allow-Origin','*')
        self.set_header('Access-Control-Allow-Methods','GET, OPTIONS')
        self.set_header('Access-Control-Allow-Headers','Content-Type, Authorization')
        self.set_status(200)
        self.finish()

class LogoutHandler(BaseHandler):
    '''
        BasicAuth logout
    '''

    @gen.coroutine
    def get(self):
        '''
            Clear secure cookie
        '''
        self.clear_cookie('username')
        self.clear_cookie('labels')
        self.set_status(200)
        self.finish()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# coding: utf8

import threading
import time
from datetime import timedelta

from fonts import font_clean

import screenmanager

class Status(object):
    def start(self, disp):
        print "Statusseite gestartet"
        thread = threading.Thread(target=run(disp), args=())
        thread.daemon = True
        thread.start()

def run(disp):
    while True:
        disp.clear((0, 0, 0))
        draw = disp.draw()
        uptime = str(timedelta(seconds = float(open('/proc/uptime', 'r').readline().split()[0]))).rsplit('.', 1)[0]
        screenmanager.draw_text(disp.buffer, "Uptime: " + uptime, (0, 220), 90, font_clean, fill=(255,255,255))
        disp.display()
        time.sleep(1)
<commit_msg>Added CPU Usage<commit_after>#!/usr/bin/env python
# coding: utf8

import threading
import time
from datetime import timedelta

from fonts import font_clean

import screenmanager

class Status(object):
    def start(self, disp):
        print "Statusseite gestartet"
        thread = threading.Thread(target=run(disp), args=())
        thread.daemon = True
        thread.start()

def run(disp):
    while True:
        disp.clear((0, 0, 0))
        draw = disp.draw()
        uptime = str(timedelta(seconds = float(open('/proc/uptime', 'r').readline().split()[0]))).rsplit('.', 1)[0]
        cpu_usage = psutil.cpu_percent()
        screenmanager.draw_text(disp.buffer, "Uptime: " + uptime, (0, 220), 90, font_clean, fill=(255,255,255))
        screenmanager.draw_text(disp.buffer, "CPU Load: " + cpu_usage, (20, 220), 90, font_clean, fill=(255,255,255))
        disp.display()
        time.sleep(1)
<|endoftext|>


<commit_before>import pandas as pd
import matplotlib.pyplot as plt
ANNOTATE = False

df = pd.read_csv('tables/pareto_frontiers.csv')
df['norm_time'] = df.groupby('query')['time'].apply(lambda x: x / x.max())
df['id'] = range(0, df.shape[0])
interesting_queries = [1, 6] # 2 is also nice, but want one from each set of experiments
# plans that are different mainly due to floating point precision, collapse
collapse = [0, 1, 2, 4, 7] + [31, 33]
fig, axes = plt.subplots(nrows=1, ncols=2, sharey=True, sharex=True, figsize=(8,4))
plt.subplots_adjust(hspace=0.2)
for i, q in enumerate(interesting_queries):
  data  = df[(df['query'] == q) & ~df['id'].isin(collapse)]
  ax = axes[i]
  data.plot(kind='scatter', x='norm_time', y = 'penalty', ax=ax, s=20)
  if ANNOTATE:
    for row in data.itertuples():
      ax.annotate('%d' % row.id, (row.norm_time, row.penalty), (row.norm_time * 1.01, row.penalty * 1.01))
  ax.set_title('Query %d' % q)
  # damn you matplotlib
  ax.set_xlabel('')
  ax.set_ylabel('')

plt.subplots_adjust(bottom=0.2)
fig.suptitle('Pareto Frontiers', size=16)
fig.text(0.5, 0.04, 'Time estimate/Max(Time estimate)', ha='center')
fig.text(0.04, 0.5, 'Penalty Estimate', va='center', rotation='vertical')
fig.savefig('figures/pareto_frontiers_plot.png')

<commit_msg>plot for planning<commit_after>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import sys
ANNOTATE = False

def plot_frontier():
  df = pd.read_csv('tables/pareto_frontiers.csv')
  df['norm_time'] = df.groupby('query')['time'].apply(lambda x: x / x.max())
  df['id'] = range(0, df.shape[0])
  interesting_queries = [1, 6] # 2 is also nice, but want one from each set of experiments
  # plans that are different mainly due to floating point precision, collapse
  collapse = [0, 1, 2, 4, 7] + [31, 33]
  fig, axes = plt.subplots(nrows=1, ncols=2, sharey=True, sharex=True, figsize=(8,4))
  plt.subplots_adjust(hspace=0.2)
  for i, q in enumerate(interesting_queries):
    data  = df[(df['query'] == q) & ~df['id'].isin(collapse)]
    ax = axes[i]
    data.plot(kind='scatter', x='norm_time', y = 'penalty', ax=ax, s=20)
    if ANNOTATE:
      for row in data.itertuples():
        ax.annotate('%d' % row.id, (row.norm_time, row.penalty), (row.norm_time * 1.01, row.penalty * 1.01))
    ax.set_title('Query %d' % q)
    # damn you matplotlib
    ax.set_xlabel('')
    ax.set_ylabel('')

    plt.subplots_adjust(bottom=0.2)
    fig.suptitle('Pareto Frontiers', size=16)
    fig.text(0.5, 0.04, 'Time estimate/Max(Time estimate)', ha='center')
    fig.text(0.04, 0.5, 'Penalty Estimate', va='center', rotation='vertical')
    fig.savefig('figures/pareto_frontiers_plot.png')

def plot_planning():
  df_approx = pd.read_csv('tables/approximate_pareto.csv')
  df_approx['plan_type'] = 'approximate'
  df_exact = pd.read_csv('tables/exact_pareto.csv')
  df_exact['plan_type'] = 'exact'
  df = pd.concat([df_approx, df_exact], axis=0)
  df['njoins'] = df['njoins'].astype(int)

  # drop warmups and calculate summary stats
  drop_warmup = lambda df: df.groupby(['plan_type', 'query', 'njoins', 'alpha']).tail(10)
  df = drop_warmup(df)

  means = df.groupby(['njoins', 'plan_type'])['plan_time'].mean().unstack()
  errors =  df.groupby(['njoins', 'plan_type'])['plan_time'].std().unstack()

  # approximate planning doesn't kick in until 6 joins (7 tables)
  # so values for less than that are really just jvm noise
  means.loc[means.index < 6, 'approximate'] = 0.0
  errors.loc[errors.index < 6, 'approximate'] = 0.0

  fig, bar_plot = plt.subplots(1, figsize=(6, 6))
  means.plot(kind='bar', yerr=errors, logy=True, rot=0, ax=bar_plot)
  bar_plot.set_xlabel("Number of joins")
  bar_plot.set_ylabel("Average planning time (ms)")
  bar_plot.legend(labels=['Approximate', 'Exact'], loc='best', title='Frontier type')
  bar_plot.get_figure().savefig('figures/planning_times_plot.png')

def help_msg():
  print "Usage: python pareto_plot.py <frontier|planning>"

if __name__ == '__main__':
  if len(sys.argv) != 2:
    help_msg()
    sys.exit(1)
  task = sys.argv[1]
  if task == "frontier":
    plot_frontier()
  elif task == "planning":
    plot_planning()
  else:
    raise ValueError('Undefined task: ' + task)
<|endoftext|>