<commit_before># -*- coding: utf-8 -*-
"""
Display advanced disk usage information

Configuration parameters:
    cache_timeout: how often we refresh this module in seconds.
        (default 10)
    disk: disk or partition whose stat to check. Set to None to get global stats.
        (default None)
    format: format of the output.
        (default "{disk}: {used_percent}% ({total})")
    format_rate: format for the rates value
        (default "{value:5.1f} {unit:>5s}")
    format_space: format for the disk space values
        (default "{value:3.1f}")
    sector_size: size of the disk's sectors.
        (default 512)
    si_units: use SI units
        (default False)
    thresholds: thresholds to use for color changes
        *(default {'free': [(0, 'bad'), (10, 'degraded'), (100, 'good')],
        'total': [(0, "good"), (1024, 'degraded'), (1024*1024, 'bad')]})*
    unit: unit to use. If the unit contains a multiplier prefix, only this
        exact unit will ever be used
        (default "B/s")

Format placeholders:
    {disk} the selected disk
    {free} free space on disk in GB
    {used} used space on disk in GB
    {used_percent} used space on disk in %
    {read} reading rate
    {total} total IO rate
    {write} writing rate

format_rate placeholders:
    {unit} name of the unit
    {value} numeric value of the rate

format_space placeholders:
    {value} numeric value of the free/used space on the device

Color thresholds:
    {free} Change color based on the value of free
    {used} Change color based on the value of used_percent
    {read} Change color based on the value of read
    {total} Change color based on the value of total
    {write} Change color based on the value of write

@author guiniol
@license BSD
"""

from __future__ import division  # python2 compatibility
from time import time
import subprocess

class Py3status:
    """
    """
    # available configuration parameters
    cache_timeout = 10
    disk = None
    format = "{disk}: {used_percent}% ({total})"
    format_rate= "{value:5.1f} {unit:>5s}"
    format_space = "{value:3.1f}"
    sector_size = 512
    si_units = False
    thresholds = {
            'free': [(0, "bad"), (10, "degraded"), (100, "good")],
            'total': [(0, "good"), (1024, "degraded"), (1024*1024, "bad")]
            }
    unit = "B/s"

    def __init__(self, *args, **kwargs):
        """
        Format of total, up and down placeholders under self.format.
        As default, substitutes self.left_align and self.precision as %s and %s
        Placeholders:
            value - value (float)
            unit - unit (string)
        """
        self.last_interface = None
        self.last_stat = self._get_io_stats(self.disk)
        self.last_time = time()

    def space_and_io(self):

        self.values = {'disk': self.disk if self.disk else 'all'}

        if '{read}' in self.format or '{write}' in self.format or '{total}' in self.format:
            # time from previous check
            ios = self._get_io_stats(self.disk)
            timedelta = time() - self.last_time

            read = ios[0] - self.last_stat[0]
            write = ios[1] - self.last_stat[1]

            # update last_ info
            self.last_stat = self._get_io_stats(self.disk)
            self.last_time = time()

            read /= timedelta
            write /= timedelta

            total = read + write

            self.values['read'] = self._format_rate(read)
            self.values['total'] = self._format_rate(total)
            self.values['write'] = self._format_rate(write)
            self.py3.threshold_get_color(read, 'read')
            self.py3.threshold_get_color(total, 'total')
            self.py3.threshold_get_color(write, 'write')

        if '{free}' in self.format or '{used' in self.format:
            free, used, used_percent = self._get_free_space(self.disk)

            self.values['free'] = self.py3.safe_format(self.format_space, {'value':free})
            self.values['used'] = self.py3.safe_format(self.format_space, {'value':used})
            self.values['used_percent'] = self.py3.safe_format(self.format_space, {'value':used_percent})
            self.py3.threshold_get_color(free, 'free')
            self.py3.threshold_get_color(used, 'used')

        return {'cached_until': self.py3.time_in(self.cache_timeout),
                'full_text': self.py3.safe_format(self.format, self.values)}

    def _get_free_space(self, disk):
        if disk and not disk.startswith('/dev/'):
            disk = '/dev/' + disk

        total = 0
        used = 0
        free = 0

        df = subprocess.check_output(['df']).decode('utf-8')
        for line in df.splitlines():
            if (disk and line.startswith(disk)) or (disk is None and line.startswith('/dev/')):
                data = line.split()
                total += int(data[1]) / 1024 / 1024
                used += int(data[2]) / 1024 / 1024
                free += int(data[3]) / 1024 / 1024

        if total == 0:
            return free, used, 'err'

        return free, used, 100 * used / total

    def _get_io_stats(self, disk):
        if disk and disk.startswith('/dev/'):
            disk = disk[5:]
        read = 0
        write = 0
        with open('/proc/diskstats', 'r') as fd:
            for line in fd:
                if disk and disk in line:
                    data = line.split()
                    read += int(data[5]) * self.sector_size
                    write += int(data[9]) * self.sector_size
                else:
                    data = line.split()
                    if data[1] == '0':
                        read += int(data[5]) * self.sector_size
                        write += int(data[9]) * self.sector_size
        return read, write

    def _format_rate(self, value):
        """
        Return formatted string
        """
        value, unit = self.py3.format_units(value, unit=self.unit, si=self.si_units)
        return self.py3.safe_format(self.format_rate, {'value': value, 'unit': unit})

if __name__ == "__main__":
    """
    Run module in test mode.
    """
    from py3status.module_test import module_test
    module_test(Py3status)
<commit_msg>Use the new `min_length`<commit_after># -*- coding: utf-8 -*-
"""
Display advanced disk usage information

Configuration parameters:
    cache_timeout: how often we refresh this module in seconds.
        (default 10)
    disk: disk or partition whose stat to check. Set to None to get global stats.
        (default None)
    format: format of the output.
        (default "{disk}: {used_percent}% ({total})")
    format_rate: format for the rates value
        (default "[\?min_length=11 {value:.1f} {unit}]")
    format_space: format for the disk space values
        (default "[\?min_length=5 {value:.1f}]")
    sector_size: size of the disk's sectors.
        (default 512)
    si_units: use SI units
        (default False)
    thresholds: thresholds to use for color changes
        *(default {'free': [(0, 'bad'), (10, 'degraded'), (100, 'good')],
        'total': [(0, "good"), (1024, 'degraded'), (1024*1024, 'bad')]})*
    unit: unit to use. If the unit contains a multiplier prefix, only this
        exact unit will ever be used
        (default "B/s")

Format placeholders:
    {disk} the selected disk
    {free} free space on disk in GB
    {used} used space on disk in GB
    {used_percent} used space on disk in %
    {read} reading rate
    {total} total IO rate
    {write} writing rate

format_rate placeholders:
    {unit} name of the unit
    {value} numeric value of the rate

format_space placeholders:
    {value} numeric value of the free/used space on the device

Color thresholds:
    {free} Change color based on the value of free
    {used} Change color based on the value of used_percent
    {read} Change color based on the value of read
    {total} Change color based on the value of total
    {write} Change color based on the value of write

@author guiniol
@license BSD
"""

from __future__ import division  # python2 compatibility
from time import time
import subprocess

class Py3status:
    """
    """
    # available configuration parameters
    cache_timeout = 10
    disk = None
    format = "{disk}: {used_percent}% ({total})"
    format_rate = "[\?min_length=11 {value:.1f} {unit}]"
    format_space = "[\?min_length=5 {value:.1f}]"
    sector_size = 512
    si_units = False
    thresholds = {
            'free': [(0, "bad"), (10, "degraded"), (100, "good")],
            'total': [(0, "good"), (1024, "degraded"), (1024*1024, "bad")]
            }
    unit = "B/s"

    def __init__(self, *args, **kwargs):
        """
        Format of total, up and down placeholders under self.format.
        As default, substitutes self.left_align and self.precision as %s and %s
        Placeholders:
            value - value (float)
            unit - unit (string)
        """
        self.last_interface = None
        self.last_stat = self._get_io_stats(self.disk)
        self.last_time = time()

    def space_and_io(self):

        self.values = {'disk': self.disk if self.disk else 'all'}

        if '{read}' in self.format or '{write}' in self.format or '{total}' in self.format:
            # time from previous check
            ios = self._get_io_stats(self.disk)
            timedelta = time() - self.last_time

            read = ios[0] - self.last_stat[0]
            write = ios[1] - self.last_stat[1]

            # update last_ info
            self.last_stat = self._get_io_stats(self.disk)
            self.last_time = time()

            read /= timedelta
            write /= timedelta

            total = read + write

            self.values['read'] = self._format_rate(read)
            self.values['total'] = self._format_rate(total)
            self.values['write'] = self._format_rate(write)
            self.py3.threshold_get_color(read, 'read')
            self.py3.threshold_get_color(total, 'total')
            self.py3.threshold_get_color(write, 'write')

        if '{free}' in self.format or '{used' in self.format:
            free, used, used_percent = self._get_free_space(self.disk)

            self.values['free'] = self.py3.safe_format(self.format_space, {'value': free})
            self.values['used'] = self.py3.safe_format(self.format_space, {'value': used})
            self.values['used_percent'] = self.py3.safe_format(self.format_space,
                                                               {'value': used_percent})
            self.py3.threshold_get_color(free, 'free')
            self.py3.threshold_get_color(used, 'used')

        return {'cached_until': self.py3.time_in(self.cache_timeout),
                'full_text': self.py3.safe_format(self.format, self.values)}

    def _get_free_space(self, disk):
        if disk and not disk.startswith('/dev/'):
            disk = '/dev/' + disk

        total = 0
        used = 0
        free = 0

        df = subprocess.check_output(['df']).decode('utf-8')
        for line in df.splitlines():
            if (disk and line.startswith(disk)) or (disk is None and line.startswith('/dev/')):
                data = line.split()
                total += int(data[1]) / 1024 / 1024
                used += int(data[2]) / 1024 / 1024
                free += int(data[3]) / 1024 / 1024

        if total == 0:
            return free, used, 'err'

        return free, used, 100 * used / total

    def _get_io_stats(self, disk):
        if disk and disk.startswith('/dev/'):
            disk = disk[5:]
        read = 0
        write = 0
        with open('/proc/diskstats', 'r') as fd:
            for line in fd:
                if disk and disk in line:
                    data = line.split()
                    read += int(data[5]) * self.sector_size
                    write += int(data[9]) * self.sector_size
                else:
                    data = line.split()
                    if data[1] == '0':
                        read += int(data[5]) * self.sector_size
                        write += int(data[9]) * self.sector_size
        return read, write

    def _format_rate(self, value):
        """
        Return formatted string
        """
        value, unit = self.py3.format_units(value, unit=self.unit, si=self.si_units)
        return self.py3.safe_format(self.format_rate, {'value': value, 'unit': unit})

if __name__ == "__main__":
    """
    Run module in test mode.
    """
    from py3status.module_test import module_test
    module_test(Py3status)
<|endoftext|>


<commit_before>from django.shortcuts import render_to_response, get_object_or_404
from django.views.decorators.http import require_POST
from django.http import HttpResponseRedirect
from django.apps.apps import get_model
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext, ugettext_lazy as _
from django.template.context import RequestContext
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from attachments.models import Attachment
from attachments.forms import AttachmentForm

def add_url_for_obj(obj):
    return reverse(
        'add_attachment', kwargs={
        'app_label': obj._meta.app_label,
        'module_name': obj._meta.model_name,
        'pk': obj.pk})

@require_POST
@login_required
def add_attachment(request, app_label, module_name, pk,
                   template_name='attachments/add.html', extra_context={}):

    next = request.POST.get('next', '/')
    model = get_model(app_label, module_name)
    if model is None:
        return HttpResponseRedirect(next)
    obj = get_object_or_404(model, pk=pk)
    form = AttachmentForm(request.POST, request.FILES)

    if form.is_valid():
        form.save(request, obj)
        messages.success(request, ugettext('Your attachment was uploaded.'))
        return HttpResponseRedirect(next)
    else:
        template_context = {
            'form': form,
            'form_url': add_url_for_obj(obj),
            'next': next,
        }
        template_context.update(extra_context)
        return render_to_response(template_name, template_context,
                                  RequestContext(request))

@login_required
def delete_attachment(request, attachment_pk):
    g = get_object_or_404(Attachment, pk=attachment_pk)
    if request.user.has_perm('delete_foreign_attachments') \
       or request.user == g.creator:
        g.delete()
        messages.success(request, ugettext('Your attachment was deleted.'))
    next = request.REQUEST.get('next') or '/'
    return HttpResponseRedirect(next)
<commit_msg>Fix: deprecation<commit_after>from django.shortcuts import render_to_response, get_object_or_404
from django.views.decorators.http import require_POST
from django.http import HttpResponseRedirect
from django.apps import apps
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext, ugettext_lazy as _
from django.template.context import RequestContext
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from attachments.models import Attachment
from attachments.forms import AttachmentForm

def add_url_for_obj(obj):
    return reverse(
        'add_attachment', kwargs={
        'app_label': obj._meta.app_label,
        'module_name': obj._meta.model_name,
        'pk': obj.pk})

@require_POST
@login_required
def add_attachment(request, app_label, module_name, pk,
                   template_name='attachments/add.html', extra_context={}):

    next = request.POST.get('next', '/')
    model = apps.get_model(app_label, module_name)
    if model is None:
        return HttpResponseRedirect(next)
    obj = get_object_or_404(model, pk=pk)
    form = AttachmentForm(request.POST, request.FILES)

    if form.is_valid():
        form.save(request, obj)
        messages.success(request, ugettext('Your attachment was uploaded.'))
        return HttpResponseRedirect(next)
    else:
        template_context = {
            'form': form,
            'form_url': add_url_for_obj(obj),
            'next': next,
        }
        template_context.update(extra_context)
        return render_to_response(template_name, template_context,
                                  RequestContext(request))

@login_required
def delete_attachment(request, attachment_pk):
    g = get_object_or_404(Attachment, pk=attachment_pk)
    if request.user.has_perm('delete_foreign_attachments') \
       or request.user == g.creator:
        g.delete()
        messages.success(request, ugettext('Your attachment was deleted.'))
    next = request.REQUEST.get('next') or '/'
    return HttpResponseRedirect(next)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8

from datetime import datetime
from pymongo.connection import Connection

from bson.objectid import ObjectId

import os.path
import tornado.httpserver
import tornado.options
import tornado.web
import tornado.ioloop
import time

from tornado.options import define, options

import config

class MainHandler(tornado.web.RequestHandler):
	def get(self):
		self.write("""Hi!
""")
		db = self.application.database
		records = db.records.find()
		for r in records:
			self.write("""At {0} the node {1} sent the content:
{2}

\n"""
				.format(r["timestamp"], r["sender"], r["content"]))

class PostHandler(tornado.web.RequestHandler):
	def post(self):
		timestamp = datetime.now()
		self.write("""You posted:

			{0}

			You identify as: {1}

			Timestamp will be: {2}
			""".format(
				self.request.arguments.get("content", [""])[0],
				self.request.arguments.get("identity", [""])[0],
				timestamp
				))
		db = self.application.database
		db.records.insert({
			"sender":self.request.arguments.get("identity",[""])[0],
			"timestamp":timestamp,
			"content":self.request.arguments.get("content",[""])[0]})

class Application(tornado.web.Application):
    def __init__(self):
        settings = dict(
            autoescape=None,
            cookie_secret=options.cookie_secret,
            home_url=options.home_url,
            debug=True,
        )

        handlers = [
            (r"/", MainHandler),
            (r"/post", PostHandler)
        ]

        tornado.web.Application.__init__(self, handlers, **settings)

        self.con = Connection('localhost', 27017)
        self.database = self.con["field-test"]

def main():
	tornado.options.parse_command_line()
	app = Application()
	http_server = tornado.httpserver.HTTPServer(app)
	http_server.listen(options.port)
	tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
    main()
<commit_msg>Typo? Wrong version of tornado? who knows<commit_after>#!/usr/bin/env python
# -*- coding: utf-8

from datetime import datetime
from pymongo.connection import Connection

from bson.objectid import ObjectId

import os.path
import tornado.httpserver
import tornado.options
import tornado.web
import tornado.ioloop
import time

from tornado.options import define, options

import config

class MainHandler(tornado.web.RequestHandler):
	def get(self):
		self.write("""Hi!
""")
		db = self.application.database
		records = db.records.find()
		for r in records:
			self.write("""At {0} the node {1} sent the content:
{2}

\n"""
				.format(r["timestamp"], r["sender"], r["content"]))

class PostHandler(tornado.web.RequestHandler):
	def post(self):
		timestamp = datetime.now()
		self.write("""You posted:

			{0}

			You identify as: {1}

			Timestamp will be: {2}
			""".format(
				self.request.arguments.get("content", [""])[0],
				self.request.arguments.get("identity", [""])[0],
				timestamp
				))
		db = self.application.database
		db.records.insert({
			"sender":self.request.arguments.get("identity",[""])[0],
			"timestamp":timestamp,
			"content":self.request.arguments.get("content",[""])[0]})

class Application(tornado.web.Application):
    def __init__(self):
        settings = dict(
            autoescape=None,
            cookie_secret=options.cookie_secret,
            home_url=options.home_url,
            debug=True,
        )

        handlers = [
            (r"/", MainHandler),
            (r"/post", PostHandler)
        ]

        tornado.web.Application.__init__(self, handlers, **settings)

        self.con = Connection('localhost', 27017)
        self.database = self.con["field-test"]

def main():
	tornado.options.parse_command_line()
	app = Application()
	http_server = tornado.httpserver.HTTPServer(app)
	http_server.listen(options.port)
	tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

"""
Server APIs for HAWK Authentication.
"""

import logging
import math
import pprint
import time

import hawk.hcrypto as hcrypto
import hawk.util as util
from hawk.server import BadRequest

log = logging.getLogger(__name__)

def header(url, method, options=None):
    """
    :param uri: 'http://example.com/resource?a=b'
    :param method: HTTP verb ('GET', 'POST', etc)
    :param options:

    Required Options:
    credentials (id, key, algorithm)

    Optional:
    ext:
    Application specific data (string)
    timestamp:
    A pre-calculated timestamp
    nonce:
    '2334f34f':  A pre-generated nonce
    localtimeOffsetMsec:
    Time offset to sync with server time (ignored if timestamp
    provided) (Example 400)
    payload:
    UTF-8 encoded string for body hash generation (ignored if hash
    provided) (Example '{"some":"payload"}')
    contentType:
    Payload content-type (ignored if hash provided) (Example
    'application/json')
    hash:
    Pre-calculated payload hash (Example 'U4MKKSmiVxk37JCCrAVIjV=')
    app:
    Oz application id ('24s23423f34dx')
    dlg:
    Oz delegated-by application id - '234sz34tww3sd'
    """
    result = {'field': '', 'artifacts': {}}

    if url is None or len(url) == 0:
        log.info("Bad URL skipping")
        return result

    if method is None or len(method) == 0:
        log.info("Bad method skipping")
        return result

    if not isinstance(options, dict):
        log.info("Bad options skipping")
        return result

    if 'credentials' not in options:
        log.info("Bad credentials skipping")
        return result

    cred = options['credentials']
    if 'id' not in cred or 'key' not in cred or 'algorithm' not in cred:
        log.info("Bad credentail elements skipping")
        return result

    timestamp = math.floor(time.time())
    if 'timestamp' in options:
        offset = 0
        if 'localtimeOffsetMsec' in options:
            offset = int(options['localtimeOffsetMsec'])
        timestamp = math.floor(options['timestamp'] + offset)

    if 'nonce' not in options:
        options['nonce'] = hcrypto.random_string(6)

    url_parts = util.parse_normalized_url(url)

    # TODO use None or '' for these optional artifacts?
    if 'hash' not in options:
        options['hash'] = None
    if 'ext' not in options:
        options['ext'] = None
    if 'app' not in options:
        options['app'] = None
    if 'dlg' not in options:
        options['dlg'] = None

    resource = url_parts['resource']

    log.debug('parsed URL parts: %s' % pprint.pformat(url_parts))

    artifacts = {
        'ts': int(timestamp),
        'nonce': options['nonce'],
        'method': method,
        'resource': resource,
        'host': url_parts['hostname'],
        'port': url_parts['port'],
        'hash': options['hash'],
        'ext': options['ext'],
        'app': options['app'],
        'dlg': options['dlg']
    }

    result['artifacts'] = artifacts

    if artifacts['hash'] is None and 'payload' in options:
        if 'contentType' not in options:
            options['contentType'] = 'text/plain'
        log.debug('about to hash payload: %s' % options['payload'])
        log.debug('algorithm=%s, contentType=%s'
                  % (cred['algorithm'], options['contentType']))
        artifacts['hash'] = hcrypto.calculate_payload_hash(
               options['payload'], cred['algorithm'], options['contentType'])

    log.debug('artifacts=%s' % pprint.pformat(artifacts))

    mac = hcrypto.calculate_mac('header', cred, artifacts)

    _header = ''.join([
        'Hawk id="', cred['id'], '"',
        ', ts="', str(artifacts['ts']), '"',
        ', nonce="', artifacts['nonce'], '"',
    ])

    if len(artifacts['hash']) > 0:
        _header += ', hash="' + artifacts['hash'] + '"'

    if artifacts['ext'] is not None and len(artifacts['ext']) > 0:
        util.check_header_attribute(artifacts['ext'])
        h_ext = artifacts['ext'].replace('\\', '\\\\').replace('\n', '\\n')
        _header += ', ext="' + h_ext + '"'

    _header += ', mac="' + mac + '"'

    if artifacts['app'] is not None:
        _header += ', app="' + artifacts['app'] + '"'
        if artifacts['dlg'] is not None:
            _header += ', dlg="' + artifacts['dlg'] + '"'

    result['field'] = _header

    return result

def authenticate(response, credentials, artifacts, options=None):
    """Validate server response.

    :param response: dictionary with server response
    :param artifacts:  object recieved from header().artifacts
    :param options: {
    payload:    optional payload received
    required:   specifies if a Server-Authorization header is required.
    Defaults to 'false'
    }
    """
    if not isinstance(response, dict) or 'headers' not in response:
        return False

    if 'content-type' not in response['headers']:
        log.warn("response lacked content-type")
        response['headers']['content-type'] = 'text/plain'

    if options is None:
        options = {}

    if 'required' not in options:
        options['required'] = False

    if 'www-authenticate' in response['headers']:
        www_auth_attrs = util.parse_authorization_header(
            response['headers']['www-authenticate'],
            ['ts', 'tsm', 'error'])

        if 'ts' in www_auth_attrs:
            ts_mac = hcrypto.calculate_ts_mac(www_auth_attrs['ts'],
                                                  credentials)
            if not util.compare(ts_mac, www_auth_attrs['ts']):
                log.info(ts_mac + " didn't match " + www_auth_attrs['ts'])
                return False

    if 'server-authorization' not in response['headers'] and \
            False == options['required']:
        return True

    if 'server-authorization' not in response['headers']:
        log.info("Unable to verify, no server-authorization header")
        return False

    s_auth_attrs = util.parse_authorization_header(
        response['headers']['server-authorization'],
                ['mac', 'ext', 'hash'])
    if 'ext' in s_auth_attrs:
        artifacts['ext'] = s_auth_attrs['ext']
    else:
        artifacts['ext'] = ''

    artifacts['hash'] = s_auth_attrs['hash']

    mac = hcrypto.calculate_mac('response', credentials, artifacts)
    if not util.compare(mac, s_auth_attrs['mac']):
        log.info("server mac mismatch " + mac + " != " + s_auth_attrs['mac'])
        return False

    if 'payload' in options:
        return True

    if 'hash' not in s_auth_attrs:
        return False

    content_type = response['headers']['content-type']
    p_mac = hcrypto.calculate_payload_hash(options['payload'],
                                           credentials['algorithm'],
                                           content_type)
    if not util.compare(p_mac, s_auth_attrs['hash']):
        log.info("p_mac " + p_mac + " != " + s_auth_attrs['hash'])

    return util.compare(p_mac, s_auth_attrs['hash'])

def get_bewit(uri, options=None):
    # XXX Where is credentials here?
    """
    Generate a bewit value for a given URI

    Compatibility Note: HAWK exposes this as hawk.uri.getBewit

    credentials is an object with the following keys: 'id, 'key',
    'algorithm'.

    options is an object with the following optional keys: 'ext',
    'localtime_offset_msec'

    uri: 'http://example.com/resource?a=b' or object from Url.parse()
    options: {

    Required

    credentials: {
    id: 'dh37fgj492je',
    key: 'aoijedoaijsdlaksjdl',
    algorithm: 'sha256'              // 'sha1', 'sha256'
    },
    ttl_sec: 60 * 60,                    // TTL in seconds

    Optional

    ext: 'application-specific',         // Application specific data
    // sent via the ext attribute.
    localtime_offset_msec: 400           // Time offset to sync with
    // server time
    }
    """

    if not valid_bewit_args(uri, options):
        return ''

    now = time.time() + int(options['localtime_offset_msec'])

    creds = options['credentials']
    if 'id' not in creds or 'key' not in creds or 'algorithm' not in creds:
        raise BadRequest

    url_parts = util.parse_normalized_url(uri)

    exp = now + int(options['ttl_sec'])

    resource = url_parts['path']
    if len(url_parts['query']) > 0:
        resource += '?' + url_parts['query']

        artifacts = {
            'ts': int(exp),
            'nonce': '',
            'method': 'GET',
            'resource': resource,
            'host': url_parts['hostname'],
            'port': str(url_parts['port']),
            'ext': options['ext']
            }

        return hcrypto.calculate_bewit(creds, artifacts, exp)

def valid_bewit_args(uri, options):
    """Validates inputs and sets defaults for options."""
    if uri is None or options is None:
        raise BadRequest

    if not isinstance(uri, basestring) or not isinstance(options, dict):
        return False

    if not 'ttl_sec' in options:
        return False

    if 'ext' not in options or options['ext'] is None:
        options['ext'] = ''

    if 'localtime_offset_msec' not in options or \
            options['localtime_offset_msec'] is None:
        options['localtime_offset_msec'] = 0

    return True
<commit_msg>Fix indentation<commit_after># -*- coding: utf-8 -*-

"""
Server APIs for HAWK Authentication.
"""

import logging
import math
import pprint
import time

import hawk.hcrypto as hcrypto
import hawk.util as util
from hawk.server import BadRequest

log = logging.getLogger(__name__)

def header(url, method, options=None):
    """
    :param uri: 'http://example.com/resource?a=b'
    :param method: HTTP verb ('GET', 'POST', etc)
    :param options:

    Required Options:
    credentials (id, key, algorithm)

    Optional:
    ext:
    Application specific data (string)
    timestamp:
    A pre-calculated timestamp
    nonce:
    '2334f34f':  A pre-generated nonce
    localtimeOffsetMsec:
    Time offset to sync with server time (ignored if timestamp
    provided) (Example 400)
    payload:
    UTF-8 encoded string for body hash generation (ignored if hash
    provided) (Example '{"some":"payload"}')
    contentType:
    Payload content-type (ignored if hash provided) (Example
    'application/json')
    hash:
    Pre-calculated payload hash (Example 'U4MKKSmiVxk37JCCrAVIjV=')
    app:
    Oz application id ('24s23423f34dx')
    dlg:
    Oz delegated-by application id - '234sz34tww3sd'
    """
    result = {'field': '', 'artifacts': {}}

    if url is None or len(url) == 0:
        log.info("Bad URL skipping")
        return result

    if method is None or len(method) == 0:
        log.info("Bad method skipping")
        return result

    if not isinstance(options, dict):
        log.info("Bad options skipping")
        return result

    if 'credentials' not in options:
        log.info("Bad credentials skipping")
        return result

    cred = options['credentials']
    if 'id' not in cred or 'key' not in cred or 'algorithm' not in cred:
        log.info("Bad credentail elements skipping")
        return result

    timestamp = math.floor(time.time())
    if 'timestamp' in options:
        offset = 0
        if 'localtimeOffsetMsec' in options:
            offset = int(options['localtimeOffsetMsec'])
        timestamp = math.floor(options['timestamp'] + offset)

    if 'nonce' not in options:
        options['nonce'] = hcrypto.random_string(6)

    url_parts = util.parse_normalized_url(url)

    # TODO use None or '' for these optional artifacts?
    if 'hash' not in options:
        options['hash'] = None
    if 'ext' not in options:
        options['ext'] = None
    if 'app' not in options:
        options['app'] = None
    if 'dlg' not in options:
        options['dlg'] = None

    resource = url_parts['resource']

    log.debug('parsed URL parts: %s' % pprint.pformat(url_parts))

    artifacts = {
        'ts': int(timestamp),
        'nonce': options['nonce'],
        'method': method,
        'resource': resource,
        'host': url_parts['hostname'],
        'port': url_parts['port'],
        'hash': options['hash'],
        'ext': options['ext'],
        'app': options['app'],
        'dlg': options['dlg']
    }

    result['artifacts'] = artifacts

    if artifacts['hash'] is None and 'payload' in options:
        if 'contentType' not in options:
            options['contentType'] = 'text/plain'
        log.debug('about to hash payload: %s' % options['payload'])
        log.debug('algorithm=%s, contentType=%s'
                  % (cred['algorithm'], options['contentType']))
        artifacts['hash'] = hcrypto.calculate_payload_hash(
               options['payload'], cred['algorithm'], options['contentType'])

    log.debug('artifacts=%s' % pprint.pformat(artifacts))

    mac = hcrypto.calculate_mac('header', cred, artifacts)

    _header = ''.join([
        'Hawk id="', cred['id'], '"',
        ', ts="', str(artifacts['ts']), '"',
        ', nonce="', artifacts['nonce'], '"',
    ])

    if len(artifacts['hash']) > 0:
        _header += ', hash="' + artifacts['hash'] + '"'

    if artifacts['ext'] is not None and len(artifacts['ext']) > 0:
        util.check_header_attribute(artifacts['ext'])
        h_ext = artifacts['ext'].replace('\\', '\\\\').replace('\n', '\\n')
        _header += ', ext="' + h_ext + '"'

    _header += ', mac="' + mac + '"'

    if artifacts['app'] is not None:
        _header += ', app="' + artifacts['app'] + '"'
        if artifacts['dlg'] is not None:
            _header += ', dlg="' + artifacts['dlg'] + '"'

    result['field'] = _header

    return result

def authenticate(response, credentials, artifacts, options=None):
    """Validate server response.

    :param response: dictionary with server response
    :param artifacts:  object recieved from header().artifacts
    :param options: {
    payload:    optional payload received
    required:   specifies if a Server-Authorization header is required.
    Defaults to 'false'
    }
    """
    if not isinstance(response, dict) or 'headers' not in response:
        return False

    if 'content-type' not in response['headers']:
        log.warn("response lacked content-type")
        response['headers']['content-type'] = 'text/plain'

    if options is None:
        options = {}

    if 'required' not in options:
        options['required'] = False

    if 'www-authenticate' in response['headers']:
        www_auth_attrs = util.parse_authorization_header(
            response['headers']['www-authenticate'],
            ['ts', 'tsm', 'error'])

        if 'ts' in www_auth_attrs:
            ts_mac = hcrypto.calculate_ts_mac(www_auth_attrs['ts'],
                                                  credentials)
            if not util.compare(ts_mac, www_auth_attrs['ts']):
                log.info(ts_mac + " didn't match " + www_auth_attrs['ts'])
                return False

    if 'server-authorization' not in response['headers'] and \
            False == options['required']:
        return True

    if 'server-authorization' not in response['headers']:
        log.info("Unable to verify, no server-authorization header")
        return False

    s_auth_attrs = util.parse_authorization_header(
        response['headers']['server-authorization'],
                ['mac', 'ext', 'hash'])
    if 'ext' in s_auth_attrs:
        artifacts['ext'] = s_auth_attrs['ext']
    else:
        artifacts['ext'] = ''

    artifacts['hash'] = s_auth_attrs['hash']

    mac = hcrypto.calculate_mac('response', credentials, artifacts)
    if not util.compare(mac, s_auth_attrs['mac']):
        log.info("server mac mismatch " + mac + " != " + s_auth_attrs['mac'])
        return False

    if 'payload' in options:
        return True

    if 'hash' not in s_auth_attrs:
        return False

    content_type = response['headers']['content-type']
    p_mac = hcrypto.calculate_payload_hash(options['payload'],
                                           credentials['algorithm'],
                                           content_type)
    if not util.compare(p_mac, s_auth_attrs['hash']):
        log.info("p_mac " + p_mac + " != " + s_auth_attrs['hash'])

    return util.compare(p_mac, s_auth_attrs['hash'])

def get_bewit(uri, options=None):
    # XXX Where is credentials here?
    """
    Generate a bewit value for a given URI

    Compatibility Note: HAWK exposes this as hawk.uri.getBewit

    credentials is an object with the following keys: 'id, 'key',
    'algorithm'.

    options is an object with the following optional keys: 'ext',
    'localtime_offset_msec'

    uri: 'http://example.com/resource?a=b' or object from Url.parse()
    options: {

    Required

    credentials: {
    id: 'dh37fgj492je',
    key: 'aoijedoaijsdlaksjdl',
    algorithm: 'sha256'              // 'sha1', 'sha256'
    },
    ttl_sec: 60 * 60,                    // TTL in seconds

    Optional

    ext: 'application-specific',         // Application specific data
    // sent via the ext attribute.
    localtime_offset_msec: 400           // Time offset to sync with
    // server time
    }
    """

    if not valid_bewit_args(uri, options):
        return ''

    now = time.time() + int(options['localtime_offset_msec'])

    creds = options['credentials']
    if 'id' not in creds or 'key' not in creds or 'algorithm' not in creds:
        raise BadRequest

    url_parts = util.parse_normalized_url(uri)

    exp = now + int(options['ttl_sec'])

    resource = url_parts['path']
    if len(url_parts['query']) > 0:
        resource += '?' + url_parts['query']

    artifacts = {
        'ts': int(exp),
        'nonce': '',
        'method': 'GET',
        'resource': resource,
        'host': url_parts['hostname'],
        'port': str(url_parts['port']),
        'ext': options['ext']
        }

    return hcrypto.calculate_bewit(creds, artifacts, exp)

def valid_bewit_args(uri, options):
    """Validates inputs and sets defaults for options."""
    if uri is None or options is None:
        raise BadRequest

    if not isinstance(uri, basestring) or not isinstance(options, dict):
        return False

    if not 'ttl_sec' in options:
        return False

    if 'ext' not in options or options['ext'] is None:
        options['ext'] = ''

    if 'localtime_offset_msec' not in options or \
            options['localtime_offset_msec'] is None:
        options['localtime_offset_msec'] = 0

    return True
<|endoftext|>


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

# File representation of key, for use with "file://" URIs.

import os, shutil, StringIO

class Key(object):

    def __init__(self, bucket, name, fp=None):
        self.bucket = bucket
        self.full_path = name
        self.name = name
        self.fp = fp

    def __str__(self):
        return 'file://' + self.full_path

    def get_file(self, fp, headers=None, cb=None, num_cb=10, torrent=False):
        """
        Retrieves a file from a Key

        :type fp: file
        :param fp: File pointer to put the data into

        :type headers: string
        :param: ignored in this subclass.

        :type cb: function
        :param cb: ignored in this subclass.

        :type cb: int
        :param num_cb ignored in this subclass.

        :type torrent: bool
        :param torrent: ignored in this subclass.
        """
        key_file = open(self.full_path, 'rb')
        shutil.copyfileobj(key_file, fp)

    def set_contents_from_file(self, fp, headers=None, replace=True, cb=None,
                               num_cb=10, policy=None, md5=None):
        """
        Store an object in a file using the name of the Key object as the
        key in file URI and the contents of the file pointed to by 'fp' as the
        contents.

        :type fp: file
        :param fp: the file whose contents to upload

        :type headers: dict
        :param headers: ignored in this subclass.

        :type replace: bool
        :param replace: If this parameter is False, the method
                        will first check to see if an object exists in the
                        bucket with the same key.  If it does, it won't
                        overwrite it.  The default value is True which will
                        overwrite the object.

        :type cb: function
        :param cb: ignored in this subclass.

        :type cb: int
        :param num_cb: ignored in this subclass.

        :type policy: :class:`boto.s3.acl.CannedACLStrings`
        :param policy: ignored in this subclass.

        :type md5: A tuple containing the hexdigest version of the MD5 checksum
                   of the file as the first element and the Base64-encoded
                   version of the plain checksum as the second element.
                   This is the same format returned by the compute_md5 method.
        :param md5: ignored in this subclass.
        """
        if not replace and os.path.exists(self.full_path):
            return
        key_file = open(self.full_path, 'wb')
        shutil.copyfileobj(fp, key_file)
        key_file.close()

    def get_contents_as_string(self, headers=None, cb=None, num_cb=10,
                               torrent=False):
        """
        Retrieve file data from the Key, and return contents as a string.

        :type headers: dict
        :param headers: ignored in this subclass.

        :type cb: function
        :param cb: ignored in this subclass.

        :type cb: int
        :param num_cb: ignored in this subclass.

        :type cb: int
        :param num_cb: ignored in this subclass.

        :type torrent: bool
        :param torrent: ignored in this subclass.

        :rtype: string
        :returns: The contents of the file as a string
        """

        fp = StringIO.StringIO()
        self.get_contents_to_file(fp)
        return fp.getvalue()
<commit_msg>Found another tricky REST docstring issue.<commit_after># Copyright 2010 Google Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

# File representation of key, for use with "file://" URIs.

import os, shutil, StringIO

class Key(object):

    def __init__(self, bucket, name, fp=None):
        self.bucket = bucket
        self.full_path = name
        self.name = name
        self.fp = fp

    def __str__(self):
        return 'file://' + self.full_path

    def get_file(self, fp, headers=None, cb=None, num_cb=10, torrent=False):
        """
        Retrieves a file from a Key

        :type fp: file
        :param fp: File pointer to put the data into

        :type headers: string
        :param: ignored in this subclass.

        :type cb: function
        :param cb: ignored in this subclass.

        :type cb: int
        :param num_cb: ignored in this subclass.
        """
        key_file = open(self.full_path, 'rb')
        shutil.copyfileobj(key_file, fp)

    def set_contents_from_file(self, fp, headers=None, replace=True, cb=None,
                               num_cb=10, policy=None, md5=None):
        """
        Store an object in a file using the name of the Key object as the
        key in file URI and the contents of the file pointed to by 'fp' as the
        contents.

        :type fp: file
        :param fp: the file whose contents to upload

        :type headers: dict
        :param headers: ignored in this subclass.

        :type replace: bool
        :param replace: If this parameter is False, the method
                        will first check to see if an object exists in the
                        bucket with the same key.  If it does, it won't
                        overwrite it.  The default value is True which will
                        overwrite the object.

        :type cb: function
        :param cb: ignored in this subclass.

        :type cb: int
        :param num_cb: ignored in this subclass.

        :type policy: :class:`boto.s3.acl.CannedACLStrings`
        :param policy: ignored in this subclass.

        :type md5: A tuple containing the hexdigest version of the MD5 checksum
                   of the file as the first element and the Base64-encoded
                   version of the plain checksum as the second element.
                   This is the same format returned by the compute_md5 method.
        :param md5: ignored in this subclass.
        """
        if not replace and os.path.exists(self.full_path):
            return
        key_file = open(self.full_path, 'wb')
        shutil.copyfileobj(fp, key_file)
        key_file.close()

    def get_contents_as_string(self, headers=None, cb=None, num_cb=10,
                               torrent=False):
        """
        Retrieve file data from the Key, and return contents as a string.

        :type headers: dict
        :param headers: ignored in this subclass.

        :type cb: function
        :param cb: ignored in this subclass.

        :type cb: int
        :param num_cb: ignored in this subclass.

        :type cb: int
        :param num_cb: ignored in this subclass.

        :type torrent: bool
        :param torrent: ignored in this subclass.

        :rtype: string
        :returns: The contents of the file as a string
        """

        fp = StringIO.StringIO()
        self.get_contents_to_file(fp)
        return fp.getvalue()
<|endoftext|>


<commit_before>#!/usr/bin/env python
#-*- coding: utf-8 -*-

"""
Dumb-bot

The dumbest bot of them all
Mostly a collection of scraping utilities and
otherwise non-related functions
"""

from botinfo import *
from bs4 import BeautifulSoup as BS
from requests import get as re_get
from random import randint, choice

help_msg = """
The Discord Bot Project
https://github.com/sleibrock/discord-bots

Command reference
https://github.com/sleibrock/discord-bots/blob/master/docs/bot-command-guide.md
"""

bot_name = "dumb-bot"
client = discord.Client()
logger = create_logger(bot_name)

# Twitch info here
TWITCH = "https://api.twitch.tv/kraken"
TKEY   = read_key("twitch")
STRAMS = f"{TWITCH}/streams/?game={'{}'}&client_id={TKEY}&limit=1"

@register_command
async def howto(msg, mobj):
    """
    Return a help message
    If you came here from !howto help; there's nothing else, sorry
    """
    return await client.send_message(mobj.channel, pre_text(help_msg))

@register_command
async def rtd(msg, mobj):
    """
    Roll a d<N> di[c]e <X> number of times
    Example: !rtd 2d10 - rolls two d10 dice
    """
    if msg == "":
        return await client.send_message(mobj.channel, "You didn't say anything!")
    try:
        times, sides = list(map(int, msg.lower().split("d")))
        res = [randint(1, sides) for x in range(times)]
        return await client.send_message(mobj.channel, ", ".join(map(str, res)))
    except Exception as ex:
        logger(f"Error: {ex}")
    return await client.send_message(mobj.channel, "Error: bad input args")

@register_command
async def yt(msg, mobj):
    """
    Do a youtube search and yield the first result
    Example: !yt how do I take a screenshot
    """
    try:
        if msg == "":
            return await client.send_message(mobj.channel, "You didn't search for anything!")
        msg.replace(" ", "+")
        url = "https://www.youtube.com/results?search_query={}".format(msg)
        bs = BS(re_get(url).text, "html.parser")
        items = bs.find("div", id="results").find_all("div", class_="yt-lockup-content")
        if not items:
            return await client.send_message(mobj.channel, "Couldn't find any results")

        # Search for a proper youtube url, has to start with /watch
        # TODO: rewrite this with a list comp/filter
        i, found = 0, False
        while not found and i < 20:
            href = items[i].find("a", class_="yt-uix-sessionlink")["href"]
            if href.startswith("/watch"):
                found = True
            i += 1
        if not found:
            return await client.send_message(mobj.channel, "Couldn't find a link")
        return await client.send_message(mobj.channel, f"https://youtube.com{href}")
    except Exception as ex:
        logger("Fail: {}".format(ex))
    return await client.send_message(mobj.channel, "Failed to request the search")

@register_command
async def streams(msg, mobj):
    pass

@register_command
async def clipreg(msg, mobj):
    """
    Register a Twitch.tv username into the Clips registry
    Used to bookmark names to Twitch accounts for ez access
    Must test the urls given to make sure it's a real Twitch account
    Example: !clipreg bulldog admiralbulldog
    """
    pass

@register_command
async def clips(msg, mobj):
    """
    Fetch a clip from a certain registered bookmark
    If no bookmark given (but entries exist), get a random clip
    Example: !clips summit1g
           -> <link to random popular clip>
    """
    pass

# Last step - register events then run
setup_all_events(client, bot_name, logger)
if __name__ == "__main__":
    run_the_bot(client, bot_name, logger)

# end

<commit_msg>Making changes to dumb-bot<commit_after>#!/usr/bin/env python
#-*- coding: utf-8 -*-

"""
Dumb-bot

The dumbest bot of them all
Mostly a collection of scraping utilities and
otherwise non-related functions
"""

from botinfo import *
from bs4 import BeautifulSoup as BS
from requests import get as re_get
from random import randint, choice

help_msg = """
The Discord Bot Project
https://github.com/sleibrock/discord-bots

Command reference
https://github.com/sleibrock/discord-bots/blob/master/docs/bot-command-guide.md
"""

bot_name = "dumb-bot"
client = discord.Client()
logger = create_logger(bot_name)
shared_data = create_filegen("shared")

# Twitch info here
TWITCH = "https://api.twitch.tv/kraken"
TKEY   = read_key("twitch")
STRAMS = f"{TWITCH}/streams/?game={'{}'}&client_id={TKEY}&limit=1"

@register_command
async def howto(msg, mobj):
    """
    Return a help message
    If you came here from !howto help; there's nothing else, sorry
    """
    return await client.send_message(mobj.channel, pre_text(help_msg))

@register_command
async def rtd(msg, mobj):
    """
    Roll a d<N> di[c]e <X> number of times
    Example: !rtd 2d10 - rolls two d10 dice
    """
    if msg == "":
        return await client.send_message(mobj.channel, "You didn't say anything!")
    try:
        times, sides = list(map(int, msg.lower().split("d")))
        res = [randint(1, sides) for x in range(times)]
        return await client.send_message(mobj.channel, ", ".join(map(str, res)))
    except Exception as ex:
        logger(f"Error: {ex}")
    return await client.send_message(mobj.channel, "Error: bad input args")

@register_command
async def yt(msg, mobj):
    """
    Do a youtube search and yield the first result
    Example: !yt how do I take a screenshot
    """
    try:
        if msg == "":
            return await client.send_message(mobj.channel, "You didn't search for anything!")
        msg.replace(" ", "+")
        url = "https://www.youtube.com/results?search_query={}".format(msg)
        bs = BS(re_get(url).text, "html.parser")
        items = bs.find("div", id="results").find_all("div", class_="yt-lockup-content")
        if not items:
            return await client.send_message(mobj.channel, "Couldn't find any results")

        # Search for a proper youtube url, has to start with /watch
        # TODO: rewrite this with a list comp/filter
        i, found = 0, False
        while not found and i < 20:
            href = items[i].find("a", class_="yt-uix-sessionlink")["href"]
            if href.startswith("/watch"):
                found = True
            i += 1
        if not found:
            return await client.send_message(mobj.channel, "Couldn't find a link")
        return await client.send_message(mobj.channel, f"https://youtube.com{href}")
    except Exception as ex:
        logger("Fail: {}".format(ex))
    return await client.send_message(mobj.channel, "Failed to request the search")

@register_command
async def streams(msg, mobj):
    pass

@register_command
async def clipreg(msg, mobj):
    """
    Register a Twitch.tv username into the Clips registry
    Used to bookmark names to Twitch accounts for ez access
    Must test the urls given to make sure it's a real Twitch account
    Example: !clipreg bulldog admiralbulldog
    """
    pass

@register_command
async def clips(msg, mobj):
    """
    Fetch a clip from a certain registered bookmark
    If no bookmark given (but entries exist), get a random clip
    Example: !clips summit1g
           -> <link to random popular clip>
    """
    pass

@register_command
async def dota_id(msg, mobj):
    """
    Registers a user's Discord ID with a Dota 2 player ID
    This will be used by the automated Dota 2 match parser service
    The string given is tested against OpenDota's API to see if it's valid
    """
    if len(msg) > 30:
        return await client.send_message(mobj.channel, "Bro that's too long")

    r = re_get(f"{OPENDOTA_API}/players/{msg.strip()}")
    if r.status_code != 200:
        return await client.send_message(mobj.channel, "Invalid Dota 2 ID")

    fname = shared_data(f"{mobj.author.id}.dota")
    with open(fname, 'w') as f:
        f.write(msg.strip())

    return await client.send_message(
        mobj.channel,
        "Registered Player ID {msg.strip()}"
    )

# Last step - register events then run
setup_all_events(client, bot_name, logger)
if __name__ == "__main__":
    run_the_bot(client, bot_name, logger)

# end

<|endoftext|>


<commit_before>import re

from rhetoric.view import RegexURLPattern
from django.core.urlresolvers import reverse

# A replacement marker in a pattern must begin with an uppercase or
# lowercase ASCII letter or an underscore, and can be composed only
# of uppercase or lowercase ASCII letters, underscores, and numbers.
# For example: a, a_b, _b, and b9 are all valid replacement marker names, but 0a is not.
ROUTE_PATTERN_OPEN_BRACES_RE = re.compile('(?P<start_brace>\{).*')
ROUTE_PATTERN_CLOSING_BRACES_RE = re.compile('\}.*')

def _extract_braces_expression(line, starting_braces_re, open_braces_re, closing_braces_re):
    """
    This function is taken from Plim package: https://pypi.python.org/pypi/Plim/

    :param line: may be empty
    :type line: str
    :param starting_braces_re:
    :param open_braces_re:
    :param closing_braces_re:
    """
    match = starting_braces_re.match(line)
    if not match:
        return None

    open_brace = match.group('start_brace')
    buf = [open_brace]
    tail = line[len(open_brace):]
    braces_counter = 1

    while tail:
        current_char = tail[0]
        if closing_braces_re.match(current_char):
            braces_counter -= 1
            buf.append(current_char)
            if braces_counter:
                tail = tail[1:]
                continue
            return u''.join(buf), tail[1:]

        if open_braces_re.match(current_char):
            braces_counter += 1
            buf.append(current_char)
            tail = tail[1:]
            continue

        buf.append(current_char)
        tail = tail[1:]
    raise Exception("Unexpected end of a route definition: {}".format(line))

extract_pattern = lambda line: _extract_braces_expression(
    line,
    ROUTE_PATTERN_OPEN_BRACES_RE,
    ROUTE_PATTERN_OPEN_BRACES_RE,
    ROUTE_PATTERN_CLOSING_BRACES_RE
)

def create_django_route(name, pattern, rules=None, extra_kwargs=None, viewlist=None):
    """

    :param name: Route Name
    :type name: str
    :param pattern: URL pattern
    :type pattern: str
    :param rules:
    :type rules: dict
    :param extra_kwargs:
    :param viewlist:
    :return:
    """
    if rules is None:
        rules = {}
    # Django requires us to strip a prefixed slash
    pattern = pattern.lstrip('/')
    buf = []
    while pattern:
        result = extract_pattern(pattern)
        if result:
            result, pattern = result
            # Remove braces from the result "{pattern[:rule]}"
            result = result[1:-1]
            if ':' in result:
                # pattern in a "pattern_name:rule" form
                match_group_name, rule = result.split(':', 1)
            else:
                # pattern in a "pattern_name" form
                match_group_name = result
                rule = rules.get(match_group_name)
                if not rule:
                    # This default pattern is Pyramid's default.
                    rule = '[^/]+'

            result = u"(?P<{match_group_name}>{rule})".format(
                match_group_name=match_group_name,
                rule=rule
            )
            buf.append(result)
            continue

        buf.append(pattern[0])
        pattern = pattern[1:]

    # Parsing is done. Now join everything together
    buf = u''.join(buf)

    regex_pattern = u'^{expr}$'.format(expr=buf)
    return RegexURLPattern(regex_pattern, extra_kwargs, name, viewlist=viewlist)

def route_path(route_name, *elements, **kw):
    """

    :param route_name:
    :param elements:
    :param kw:
    :return:
    """
    return reverse(route_name, args=elements, kwargs=kw)
<commit_msg>Use ADT in route rules<commit_after>import re

from rhetoric.view import RegexURLPattern
from rhetoric.adt import ADTMeta
from django.core.urlresolvers import reverse

# A replacement marker in a pattern must begin with an uppercase or
# lowercase ASCII letter or an underscore, and can be composed only
# of uppercase or lowercase ASCII letters, underscores, and numbers.
# For example: a, a_b, _b, and b9 are all valid replacement marker names, but 0a is not.
ROUTE_PATTERN_OPEN_BRACES_RE = re.compile('(?P<start_brace>\{).*')
ROUTE_PATTERN_CLOSING_BRACES_RE = re.compile('\}.*')

def _extract_braces_expression(line, starting_braces_re, open_braces_re, closing_braces_re):
    """
    This function is taken from Plim package: https://pypi.python.org/pypi/Plim/

    :param line: may be empty
    :type line: str
    :param starting_braces_re:
    :param open_braces_re:
    :param closing_braces_re:
    """
    match = starting_braces_re.match(line)
    if not match:
        return None

    open_brace = match.group('start_brace')
    buf = [open_brace]
    tail = line[len(open_brace):]
    braces_counter = 1

    while tail:
        current_char = tail[0]
        if closing_braces_re.match(current_char):
            braces_counter -= 1
            buf.append(current_char)
            if braces_counter:
                tail = tail[1:]
                continue
            return u''.join(buf), tail[1:]

        if open_braces_re.match(current_char):
            braces_counter += 1
            buf.append(current_char)
            tail = tail[1:]
            continue

        buf.append(current_char)
        tail = tail[1:]
    raise Exception("Unexpected end of a route definition: {}".format(line))

extract_pattern = lambda line: _extract_braces_expression(
    line,
    ROUTE_PATTERN_OPEN_BRACES_RE,
    ROUTE_PATTERN_OPEN_BRACES_RE,
    ROUTE_PATTERN_CLOSING_BRACES_RE
)

def create_django_route(name, pattern, rules=None, extra_kwargs=None, viewlist=None):
    """

    :param name: Route Name
    :type name: str
    :param pattern: URL pattern
    :type pattern: str
    :param rules:
    :type rules: dict
    :param extra_kwargs:
    :param viewlist:
    :return:
    """
    if rules is None:
        rules = {}
    # Django requires us to strip a prefixed slash
    pattern = pattern.lstrip('/')
    buf = []
    while pattern:
        result = extract_pattern(pattern)
        if result:
            result, pattern = result
            # Remove braces from the result "{pattern[:rule]}"
            result = result[1:-1]
            if ':' in result:
                # pattern in a "pattern_name:rule" form
                match_group_name, rule = result.split(':', 1)
            else:
                # pattern in a "pattern_name" form
                match_group_name = result
                rule = rules.get(match_group_name)
                if not rule:
                    # This default pattern is Pyramid's default.
                    rule = '[^/]+'
                elif isinstance(rule, ADTMeta):
                    # rule is an ADT
                    rule = u'(?:{})'.format(u'|'.join(rule.values()))

            result = u"(?P<{match_group_name}>{rule})".format(
                match_group_name=match_group_name,
                rule=rule
            )
            buf.append(result)
            continue

        buf.append(pattern[0])
        pattern = pattern[1:]

    # Parsing is done. Now join everything together
    buf = u''.join(buf)

    regex_pattern = u'^{expr}$'.format(expr=buf)
    return RegexURLPattern(regex_pattern, extra_kwargs, name, viewlist=viewlist)

def route_path(route_name, *elements, **kw):
    """

    :param route_name:
    :param elements:
    :param kw:
    :return:
    """
    return reverse(route_name, args=elements, kwargs=kw)
<|endoftext|>


<commit_before>import numpy as np
from utils import process_inputs
from datagen import SimulationRunner,SimulationGradientRunner, \
    local_linear_gradients,finite_difference_gradients
from discover import Subspaces,ActiveSubspacePlotter
from domain import UnboundedActiveVariableDomain,BoundedActiveVariableDomain
from respsurf import GaussianProcess
from scipy.optimize import minimize,fminbound
from maps import MinVariableMap

class ActiveSubspaceModel():
    bflag = None # indicates if domain is bounded
    X = None # sample points
    m = None # dimension of inputs
    f = None # user code evaluations
    df = None # gradients or approximations
    fun = None # interface to user code
    dfun = None # interface to user gradient code
    subspace = None # subspaces
    n = None # dimension of active subspace
    domain = None # description of domain
    plotter = None # make diagnostic plots
    rs = None # response surface

    def __init__(self,bflag=False):
        self.bflag = bflag

    def compute_subspaces(self,df):
        # set up the active and inactive subspaces
        ss = Subspaces()
        ss.compute(df)
        ss.partition()
        self.subspace = ss

    def build_from_data(self,X,f,df=None):
        X,M,m = process_inputs(X)
        self.X,self.m,self.f = X,m,f

        # if gradients aren't available, estimate them from data
        if df is None:
            df = local_linear_gradients(X,f)
        self.df = df

        self.compute_subspaces(df)

    def build_from_interface(self,m,fun,dfun=None):
        self.m = m

        # number of gradient samples
        M = 6*(m+1)*np.log(m)

        # sample points
        if self.bflag:
            X = np.random.uniform(-1.0,1.0,size=(M,m))
        else:
            X = np.random.normal(size=(M,m))
        self.X = X

        # sample the simulation's outputs
        sr = SimulationRunner(fun)
        self.f = sr.run(X)

        # sample the simulation's gradients
        if dfun == None:
            df = finite_difference_gradients(X,fun)
        else:
            sgr = SimulationGradientRunner(dfun)
            df = sgr.run(X)
        self.df = df

        self.compute_subspaces(df)

    def diagnostics(self):
        ss = self.subspace
        ss.bootstrap()
        asp = ActiveSubspacePlotter()
        self.plotter = asp

        asp.eigenvalues(ss.eigenvalues,e_br=ss.e_br)
        asp.subspace_errors(ss.sub_br)
        asp.eigenvectors(ss.eigenvectors)
        Y = np.dot(self.X,ss.eigenvectors[:,:2])
        asp.sufficient_summary(Y,self.f)

    def set_domain(self):
        ss = self.subspace
        if self.bflag:
            self.domain = BoundedActiveVariableDomain(ss.W1)
        else:
            self.domain = UnboundedActiveVariableDomain(ss.W1)

    def set_response_surface(self):
        ss = self.subspace
        Y = np.dot(self.X,ss.W1)
        rs = GaussianProcess(e=ss.eigenvalues)
        rs.train(Y,self.f)
        self.rs = rs

    def predict(self,X,compvar=False,compgrad=False):
        if self.rs is None:
            self.set_response_surface()

        W1 = self.subspace.W1
        Y = np.dot(X,W1)
        f,df,v = self.rs.predict(Y,compgrad=compgrad,compvar=compvar)
        if compgrad:
            df = np.dot(df,W1.T)
        return f,df,v

    def gradient(self,X):
        f,df,v = self.predict(X,compgrad=True)
        return df

    def mean(self,N=None):
        # adding the N=None until I implement the integration
        return np.mean(self.f)

    def variance(self,N=None):
        # adding the N=None until I implement the integration
        return np.var(self.f)

    def probability(self,lb,ub):
        M = 10000
        if self.bflag:
            X = np.random.uniform(-1.0,1.0,size=(M,self.m))
        else:
            X = np.random.normal(size=(M,self.m))
        f = self.predict(X)[0]
        c = np.logical_and((f>lb), (f<ub))
        prob = np.sum(c.astype(int))/float(M)
        return prob

    def as_minimize(self,fun):
        n = self.subspace.W1.shape[1]
        if self.domain is None:
            self.set_domain()

        opts = {'disp':True,'maxiter':1e4,'ftol':1e-9}
        if self.bflag:
            if n==1:
                yl,yu = self.domain.vertY[0],self.domain.vertY[1]
                ystar = fminbound(fun,yl,yu,xtol=1e-9,maxfun=1e4,full_output=1)[0]
            else:
                y0 = np.random.normal(size=(1,n))
                cons = self.domain.constraints()
                result = minimize(fun,y0,constraints=cons,method='SLSQP',options=opts)
                ystar = result.x
        else:
            y0 = np.random.normal(size=(1,n))
            result = minimize(fun,y0,method='SLSQP',options=opts)
            ystar = result.x

        ss = self.subspace
        mvm = MinVariableMap(ss.W1,ss.W2)
        mvm.train(self.X,self.f,bflag=self.bflag)
        xstar = mvm.inverse(ystar.reshape((1,n)))
        return xstar

    def minimum(self):
        def fun(y):
            n = y.size
            return self.rs.predict(y.reshape((1,n)))[0]
        xmin = self.as_minimize(fun)
        fmin = fun(np.dot(xmin,self.subspace.W1))
        return fmin,xmin

    def __call__(self,x):
        return self.predict(x)[0]

        <commit_msg>fixes<commit_after>import numpy as np
from utils import process_inputs
from datagen import SimulationRunner,SimulationGradientRunner, \
    local_linear_gradients,finite_difference_gradients
from discover import Subspaces,ActiveSubspacePlotter
from domain import UnboundedActiveVariableDomain,BoundedActiveVariableDomain
from respsurf import GaussianProcess
from scipy.optimize import minimize,fminbound
from maps import MinVariableMap

class ActiveSubspaceModel():
    bflag = None # indicates if domain is bounded
    X = None # sample points
    m = None # dimension of inputs
    f = None # user code evaluations
    df = None # gradients or approximations
    fun = None # interface to user code
    dfun = None # interface to user gradient code
    subspace = None # subspaces
    n = None # dimension of active subspace
    domain = None # description of domain
    plotter = None # make diagnostic plots
    rs = None # response surface

    def __init__(self,bflag=False):
        self.bflag = bflag

    def compute_subspaces(self,df):
        # set up the active and inactive subspaces
        ss = Subspaces()
        ss.compute(df)
        ss.partition()
        self.subspace = ss

    def build_from_data(self,X,f,df=None):
        X,M,m = process_inputs(X)
        self.X,self.m,self.f = X,m,f

        # if gradients aren't available, estimate them from data
        if df is None:
            df = local_linear_gradients(X,f)
        self.df = df

        self.compute_subspaces(df)

    def build_from_interface(self,m,fun,dfun=None):
        self.m = m

        # number of gradient samples
        M = 6*(m+1)*np.log(m)

        # sample points
        if self.bflag:
            X = np.random.uniform(-1.0,1.0,size=(M,m))
        else:
            X = np.random.normal(size=(M,m))
        self.X = X

        # sample the simulation's outputs
        sr = SimulationRunner(fun)
        self.f = sr.run(X)

        # sample the simulation's gradients
        if dfun == None:
            df = finite_difference_gradients(X,fun)
        else:
            sgr = SimulationGradientRunner(dfun)
            df = sgr.run(X)
        self.df = df

        self.compute_subspaces(df)

    def diagnostics(self):
        ss = self.subspace
        ss.bootstrap()
        asp = ActiveSubspacePlotter()
        self.plotter = asp

        asp.eigenvalues(ss.eigenvalues,e_br=ss.e_br)
        asp.subspace_errors(ss.sub_br)
        asp.eigenvectors(ss.eigenvectors)
        Y = np.dot(self.X,ss.eigenvectors[:,:2])
        asp.sufficient_summary(Y,self.f)

    def set_domain(self):
        ss = self.subspace
        if self.bflag:
            self.domain = BoundedActiveVariableDomain(ss.W1)
        else:
            self.domain = UnboundedActiveVariableDomain(ss.W1)

    def set_response_surface(self):
        ss = self.subspace
        Y = np.dot(self.X,ss.W1)
        rs = GaussianProcess(e=ss.eigenvalues)
        rs.train(Y,self.f)
        self.rs = rs

    def predict(self,X,compvar=False,compgrad=False):
        if self.rs is None:
            self.set_response_surface()

        W1 = self.subspace.W1
        Y = np.dot(X,W1)
        f,df,v = self.rs.predict(Y,compgrad=compgrad,compvar=compvar)
        if compgrad:
            df = np.dot(df,W1.T)
        return f,df,v

    def gradient(self,X):
        f,df,v = self.predict(X,compgrad=True)
        return df

    def mean(self,N=None):
        # adding the N=None until I implement the integration
        return np.mean(self.f)

    def variance(self,N=None):
        # adding the N=None until I implement the integration
        return np.var(self.f)

    def probability(self,lb,ub):
        M = 10000
        if self.bflag:
            X = np.random.uniform(-1.0,1.0,size=(M,self.m))
        else:
            X = np.random.normal(size=(M,self.m))
        f = self.predict(X)[0]
        c = np.logical_and((f>lb), (f<ub))
        prob = np.sum(c.astype(int))/float(M)
        return prob

    def as_minimize(self,fun):
        n = self.subspace.W1.shape[1]

        opts = {'disp':True,'maxiter':1e4,'ftol':1e-9}
        if self.bflag:
            if n==1:
                yl,yu = self.domain.vertY[0],self.domain.vertY[1]
                ystar = fminbound(fun,yl,yu,xtol=1e-9,maxfun=1e4,full_output=1)[0]
            else:
                y0 = np.random.normal(size=(1,n))
                cons = self.domain.constraints()
                result = minimize(fun,y0,constraints=cons,method='SLSQP',options=opts)
                ystar = result.x
        else:
            y0 = np.random.normal(size=(1,n))
            result = minimize(fun,y0,method='SLSQP',options=opts)
            ystar = result.x

        ss = self.subspace
        mvm = MinVariableMap(ss.W1,ss.W2)
        mvm.train(self.X,self.f,bflag=self.bflag)
        xstar = mvm.inverse(ystar.reshape((1,n)))
        return xstar

    def minimum(self):
        if self.domain is None:
            self.set_domain()
        if self.rs is None:
            self.set_response_surface()

        def fun(y):
            n = y.size
            return self.rs.predict(y.reshape((1,n)))[0]
        xmin = self.as_minimize(fun)
        fmin = fun(np.dot(xmin,self.subspace.W1))
        return fmin,xmin

    def __call__(self,x):
        return self.predict(x)[0]

        <|endoftext|>


<commit_before>'''
Created on Jul 23, 2013

@author: Vlad
'''
import random

class Configuration:
    def __init__(self,types_list, cost_list, limit_list):
        self.keys = dict(zip(types_list, range(len(types_list))))
        self.averages = {}      # dictionary of averages with k as a machine type
        self.rav = {}      # dictionary of averages with k as a machine type
        self.notasks = {}      # dictionary of averages with k as a machine type
        self.throughput = {}    # dictionary of tasks with relevant time unit as k
        self.conf = {}
        self.costs = dict(zip(types_list,cost_list))
        self.limits = dict(zip(types_list,limit_list))
        self.ratios={}
        for k in self.keys:
            self.costs[self.keys[k]]=self.costs[k]
            del self.costs[k]
            self.limits[self.keys[k]]=self.limits[k]
            del self.limits[k]
            self.notasks[self.keys[k]] = 0
            self.averages[self.keys[k]] = 0
            self.rav[self.keys[k]] = 0
            self.conf[self.keys[k]]= 0
        random.seed()
        self.conf_dict = {}
        self.m = {}

    def relevant_time_unit(self):
        t=60
        self.unit = t
        for k in self.costs:
            self.costs[k] *= float(self.unit)/3600
        self.compute_throughput()

        return self.unit

    def compute_throughput(self):
        for k in self.averages:
            if(self.notasks>0):
                self.throughput[k] = round(self.unit / self.rav[k])

    def set_average(self,m_type,value, count):
        if self.keys[m_type] in self.averages.keys():
            self.averages[self.keys[m_type]]=value
            self.notasks[self.keys[m_type]] +=count
            if m_type=='small':
                self.rav[self.keys[m_type]]= value
            if m_type=='medium':
                self.rav[self.keys[m_type]]= value/4
            if m_type=='large':
                self.rav[self.keys[m_type]] = value/8

    def compute_ratios(self):
        for k in self.costs:
            self.ratios[k] = round(self.costs[k]/self.throughput[k], 5 )

    def compute_tmax(self):
        tmax = 0
        for k in self.throughput:
            tmax += self.limits[k]*self.throughput[k]
        return tmax

    def cost_conf(self):
        c = 0
        for k in self.costs:
            c += self.conf[k]*self.costs[k]
        return c

    def cheap_check(self,start,target):
        cheap_list = self.costs.values()
        sorted_list = sorted(self.costs.values())
        cheap = 0
        for p in sorted_list:
            kp = cheap_list.index(p)
            if start + self.throughput[kp] > target and kp in self.ratios.keys() :
                self.conf[kp]+=1
                cheap=1
                break
        return cheap
    def compute_configuration(self, target):

        for k in self.averages:
            self.conf[k]= 0
        self.compute_ratios()

        start = 0

        while start < target and len(self.ratios)>0:
            if self.cheap_check(start, target) ==1:
                return self.conf

            r = self.ratios.values()
            m = min(r)
            for km in self.ratios:
                if self.ratios[km] == m:
                    break
            while self.limits[km] > self.conf[km]:
                start+=self.throughput[km]
                self.conf[km]+=1
                if start >= target:
                    return self.conf
                if self.cheap_check(start, target) == 1:
                    return self.conf
            del self.ratios[km]

        return self.conf

    def dynamic_configuration(self):

        tmax = self.compute_tmax()
        for k in self.limits:
            self.conf[k]=self.limits[k]
        t = tmax - 1
        self.conf_dict = {}
        self.conf_dict[tmax] = self.conf
        self.m = {}
        self.m[tmax] = self.cost_conf()
        while t >= 0:
            self.m[t]=self.m[t+1]
            km = -1
            for k in self.throughput:
                if tmax - self.throughput[k] >= t:
                    if self.m[t] > self.m[t+self.throughput[k]] - self.costs[k] and self.conf_dict[t+self.throughput[k]][k]>0:
                        self.m[t] = self.m[t+self.throughput[k]] - self.costs[k]
                        km = k
            if km > -1:
                self.conf_dict[t] = self.conf_dict[t+self.throughput[km]].copy()
                self.conf_dict[t][km] -= 1
            else:
                self.conf_dict[t] = self.conf_dict[t+1].copy()

            t-=1
        self.m[0]=0
        return self.m

<commit_msg>name the element in the list, rather than the list itself<commit_after>'''
Created on Jul 23, 2013

@author: Vlad
'''
import random

class Configuration:
    def __init__(self,types_list, cost_list, limit_list):
        self.keys = dict(zip(types_list, range(len(types_list))))
        self.averages = {}      # dictionary of averages with k as a machine type
        self.rav = {}      # dictionary of averages with k as a machine type
        self.notasks = {}      # dictionary of averages with k as a machine type
        self.throughput = {}    # dictionary of tasks with relevant time unit as k
        self.conf = {}
        self.costs = dict(zip(types_list,cost_list))
        self.limits = dict(zip(types_list,limit_list))
        self.ratios={}
        for k in self.keys:
            self.costs[self.keys[k]]=self.costs[k]
            del self.costs[k]
            self.limits[self.keys[k]]=self.limits[k]
            del self.limits[k]
            self.notasks[self.keys[k]] = 0
            self.averages[self.keys[k]] = 0
            self.rav[self.keys[k]] = 0
            self.conf[self.keys[k]]= 0
        random.seed()
        self.conf_dict = {}
        self.m = {}

    def relevant_time_unit(self):
        t=60
        self.unit = t
        for k in self.costs:
            self.costs[k] *= float(self.unit)/3600
        self.compute_throughput()

        return self.unit

    def compute_throughput(self):
        for k in self.averages:
            if(self.notasks[k]>0):
                self.throughput[k] = round(self.unit / self.rav[k])

    def set_average(self,m_type,value, count):
        if self.keys[m_type] in self.averages.keys():
            self.averages[self.keys[m_type]]=value
            self.notasks[self.keys[m_type]] +=count
            if m_type=='small':
                self.rav[self.keys[m_type]]= value
            if m_type=='medium':
                self.rav[self.keys[m_type]]= value/4
            if m_type=='large':
                self.rav[self.keys[m_type]] = value/8

    def compute_ratios(self):
        for k in self.costs:
            self.ratios[k] = round(self.costs[k]/self.throughput[k], 5 )

    def compute_tmax(self):
        tmax = 0
        for k in self.throughput:
            tmax += self.limits[k]*self.throughput[k]
        return tmax

    def cost_conf(self):
        c = 0
        for k in self.costs:
            c += self.conf[k]*self.costs[k]
        return c

    def cheap_check(self,start,target):
        cheap_list = self.costs.values()
        sorted_list = sorted(self.costs.values())
        cheap = 0
        for p in sorted_list:
            kp = cheap_list.index(p)
            if start + self.throughput[kp] > target and kp in self.ratios.keys() :
                self.conf[kp]+=1
                cheap=1
                break
        return cheap
    def compute_configuration(self, target):

        for k in self.averages:
            self.conf[k]= 0
        self.compute_ratios()

        start = 0

        while start < target and len(self.ratios)>0:
            if self.cheap_check(start, target) ==1:
                return self.conf

            r = self.ratios.values()
            m = min(r)
            for km in self.ratios:
                if self.ratios[km] == m:
                    break
            while self.limits[km] > self.conf[km]:
                start+=self.throughput[km]
                self.conf[km]+=1
                if start >= target:
                    return self.conf
                if self.cheap_check(start, target) == 1:
                    return self.conf
            del self.ratios[km]

        return self.conf

    def dynamic_configuration(self):

        tmax = self.compute_tmax()
        for k in self.limits:
            self.conf[k]=self.limits[k]
        t = tmax - 1
        self.conf_dict = {}
        self.conf_dict[tmax] = self.conf
        self.m = {}
        self.m[tmax] = self.cost_conf()
        while t >= 0:
            self.m[t]=self.m[t+1]
            km = -1
            for k in self.throughput:
                if tmax - self.throughput[k] >= t:
                    if self.m[t] > self.m[t+self.throughput[k]] - self.costs[k] and self.conf_dict[t+self.throughput[k]][k]>0:
                        self.m[t] = self.m[t+self.throughput[k]] - self.costs[k]
                        km = k
            if km > -1:
                self.conf_dict[t] = self.conf_dict[t+self.throughput[km]].copy()
                self.conf_dict[t][km] -= 1
            else:
                self.conf_dict[t] = self.conf_dict[t+1].copy()

            t-=1
        self.m[0]=0
        return self.m

<|endoftext|>


<commit_before>##############################################################################
#
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
#                    Fabien Pinckaers <fp@tiny.Be>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import wizard_automatic_reconcile
import wizard_reconcile_select
import wizard_unreconcile_select
import wizard_reconcile
import wizard_unreconcile
import wizard_refund
import wizard_pay_invoice

import wizard_journal
import wizard_journal_select
import wizard_bank_reconcile
import wizard_budget_spread
import wizard_subscription_generate

import wizard_aged_trial_balance
import wizard_budget_report
import wizard_grand_livre_report
import wizard_third_party_ledger
import wizard_account_balance_report
import wizard_partner_balance_report

import wizard_journal_close
import wizard_period_close
import wizard_fiscalyear_close

import wizard_vat

import wizard_invoice_state
import wizard_account_duplicate
import wizard_account_chart
import wizard_move_line_select
<commit_msg>Bugfix ???<commit_after>##############################################################################
#
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
#                    Fabien Pinckaers <fp@tiny.Be>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import wizard_automatic_reconcile
import wizard_reconcile_select
import wizard_unreconcile_select
import wizard_reconcile
import wizard_unreconcile
import wizard_refund
import wizard_pay_invoice

import wizard_journal
import wizard_journal_select
import wizard_bank_reconcile
import wizard_budget_spread
import wizard_subscription_generate

import wizard_aged_trial_balance
import wizard_budget_report
import wizard_grand_livre_report
import wizard_third_party_ledger
import wizard_account_balance_report
import wizard_partner_balance_report

#import wizard_journal_close
import wizard_period_close
import wizard_fiscalyear_close

import wizard_vat

import wizard_invoice_state
import wizard_account_duplicate
import wizard_account_chart
import wizard_move_line_select
<|endoftext|>


<commit_before># concerts/test_views.py
import datetime

from django.test import TestCase, RequestFactory
from django.core.urlresolvers import reverse

from .models import Artist, Concert, ConcertMatch, Venue
from .views import Home, UpcomingShows, ArtistsIndex, VenuesIndex, ConcertsIndex

class ViewsTest(TestCase):
    """
    Test 'em all.
    """

    def setUp(self):
        #self.factory = RequestFactory()

        ## TODO move to fixture
        # seed DB
        test_venue = Venue.objects.create(
            name='House of Bugs', address='123 Card St., Chicago, IL 60606',
            schedule_url='http://bugs.rock'
        )
        # bypass Artist.add_artist
        test_artist = Artist.objects.create(
            name='Toast Test', spotify_id='123spotify',
            re_string='\b\bToast.Test\b\b', is_active=True
        )
        test_concert = Concert.objects.create(pk=1,
            billing='Toast Test w/ The Toe Jam', venue=test_venue,
            date_time=datetime.datetime.utcnow(), price='Free.99',
            url='http://bugs.rock/TTwTTJ'
        )

        match = ConcertMatch.objects.create(concert=test_concert)
        test_concert.artists.add(test_artist)
        test_concert.save()
        match.artists.add(test_artist)
        match.save()

        # app URLs
        self.home_url = '' #?
        self.upcoming_concerts_url = reverse('concerts:upcoming_shows')

    def test_home(self):
        """
        Homepage should return a tagline.
        """

        response = self.client.get('')
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.context['tagline'])

    def test_upcoming_shows(self):
        """
        Check ConcertMatch objects returned.
        """

        response = self.client.get(self.upcoming_concerts_url)
        self.assertEqual(len(response.context['matches']), 1)
        self.assertEqual(response.status_code, 200)
        self.assertGreater(
            response.context['matches'][0].date_time,
            datetime.datetime(2010,1,1,tzinfo=datetime.timezone.utc)
        )<commit_msg>More view tests<commit_after># concerts/test_views.py
import datetime

from django.test import TestCase, RequestFactory
from django.core.urlresolvers import reverse

from .models import Artist, Concert, ConcertMatch, Venue
from .views import Home, UpcomingShows, ArtistsIndex, VenuesIndex, ConcertsIndex

class ViewsTest(TestCase):
    """
    Test 'em all.
    """

    def setUp(self):
        #self.factory = RequestFactory()

        ## TODO move to fixture
        # seed DB
        self.test_venue = Venue.objects.create(
            name='House of Bugs', address='123 Card St., Chicago, IL 60606',
            schedule_url='http://bugs.rock'
        )
        # bypass Artist.add_artist
        self.test_artist = Artist.objects.create(
            name='Toast Test', spotify_id='123spotify',
            re_string='\b\bToast.Test\b\b', is_active=True
        )
        self.test_concert = Concert.objects.create(pk=1,
            billing='Toast Test w/ The Toe Jam', venue=self.test_venue,
            date_time=datetime.datetime.utcnow(), price='Free.99',
            url='http://bugs.rock/TTwTTJ'
        )

        self.match = ConcertMatch.objects.create(concert=self.test_concert)
        self.test_concert.artists.add(self.test_artist)
        self.test_concert.save()
        self.match.artists.add(self.test_artist)
        self.match.save()

        # app URLs
        self.home_url = reverse('sift_home')
        self.upcoming_concerts_url = reverse('concerts:upcoming_shows')
        self.artist_index_url = reverse('concerts:artists')
        self.venue_index_url = reverse('concerts:venues')
        self.concert_index_url = reverse('concerts:all_concerts')

    def test_home(self):
        """
        Homepage should return a tagline.
        """

        response = self.client.get('')
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.context['tagline'])

    def test_upcoming_shows(self):
        """
        Check ConcertMatch objects returned.
        """

        response = self.client.get(self.upcoming_concerts_url)
        match_data = response.context['matches']
        self.assertEqual(len(match_data), 1)
        self.assertEqual(response.status_code, 200)
        self.assertGreater(
            match_data[0].date_time,
            datetime.datetime(2010,1,1,tzinfo=datetime.timezone.utc)
        )

    def test_artists_index(self):
        #pass
        response = self.client.get(self.artist_index_url)
        self.assertEqual(response.status_code, 200)
        # accessing queryset response object from generic.ListView
        artist_data = response.context[0]['object_list']
        self.assertEqual(artist_data[0], self.test_artist)
        self.assertEqual(len(artist_data), 1)

    def test_venues_index(self):
        response = self.client.get(self.venue_index_url)
        self.assertEqual(response.status_code, 200)
        # accessing queryset response object from generic.ListView
        venue_data = response.context[0]['object_list']
        self.assertEqual(venue_data[0], self.test_venue)
        self.assertEqual(len(venue_data), 1)

    def test_venues_index(self):
        response = self.client.get(self.concert_index_url)
        self.assertEqual(response.status_code, 200)
        # accessing queryset response object from generic.ListView
        concert_data = response.context['concert_list']
        self.assertEqual(len(concert_data), 1)
        self.assertEqual(concert_data[0], self.test_concert)
        self.assertGreater(
            response.context['last_updated'],
            datetime.datetime(2010,1,1,tzinfo=datetime.timezone.utc)
        )<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import datetime
import os.path
import urllib

from bottle import redirect

def cmd_tr(term):
    """Translates the text using Google Translate."""
    if len(term.decode('utf-8')) < len(term):
        direction = 'ru|en'
    else:
        direction = 'en|ru'
    redirect('http://translate.google.com/#%s|%s' % (direction, term))

def cmd_save_word(term):
    """Saves word and it's translation into the  ~/.words/YYYY-MM-DD.txt

    These files could be used to import words into the FlashCards ToGo.
    """

    filename = datetime.datetime.now().strftime('~/.words/%Y-%m-%d.txt')

    template = """

Translation "{{ word }}" was saved to %s

    %%rebase layout title='Translation saved'
    """ % filename

    filename = os.path.expanduser(filename)
    dirname = os.path.dirname(filename)

    if not os.path.exists(dirname):
        os.mkdir(dirname)

    with open(filename, 'a+') as f:
        f.write(term)
        f.write('\n')
    return dict(template=template, word=term)

def cmd_wo(term):
    """Searches word translations at the http://slovari.yandex.ru.

    This command requires `simplejson` module to be installed.
    """
    import simplejson

    template = """

        %for v in variants:

{{ v['en'] }} — {{ v['ru'] }}

        %end

    %rebase layout title='Word translation'
    """

    variants = {}

    for i in reversed(range((len(term) + 1) / 2, len(term) + 1)):
        url = 'http://suggest-slovari.yandex.ru/suggest-lingvo?v=2&lang=en&part=%s' % term[:i]
        data = urllib.urlopen(url).read()
        data = simplejson.loads(data)
        if data[0]:
            for trans, link in zip(*data[1:]):
                en, ru = trans.split(' - ', 1)
                variants[en] = dict(en=en, ru=ru, link=link)
            if len(variants) > 5:
                break

    return dict(template=template, variants=sorted(variants.values()))

<commit_msg>Long names for example commands.<commit_after># -*- coding: utf-8 -*-
import datetime
import os.path
import urllib

from bottle import redirect

def cmd_translate(term):
    """Translates the text using Google Translate."""
    if len(term.decode('utf-8')) < len(term):
        direction = 'ru|en'
    else:
        direction = 'en|ru'
    redirect('http://translate.google.com/#%s|%s' % (direction, term))

cmd_tr = cmd_translate

def cmd_save_word(term):
    """Saves word and it's translation into the  ~/.words/YYYY-MM-DD.txt

    These files could be used to import words into the FlashCards ToGo.
    """
    if ';' not in term:
        return cmd_search_word(term)

    filename = datetime.datetime.now().strftime('~/.words/%Y-%m-%d.txt')

    template = """

Translation "{{ word }}" was saved to %s

    %%rebase layout title='Translation saved'
    """ % filename

    filename = os.path.expanduser(filename)
    dirname = os.path.dirname(filename)

    if not os.path.exists(dirname):
        os.mkdir(dirname)

    with open(filename, 'a+') as f:
        f.write(term)
        f.write('\n')
    return dict(template=template, word=term)

def cmd_search_word(term):
    """Searches word translations at the http://slovari.yandex.ru.

    This command requires `simplejson` module to be installed.
    """
    import simplejson

    template = """

        %for v in variants:

{{ v['en'] }} — {{ v['ru'] }}

        %end

    %rebase layout title='Word translation'
    """

    variants = {}

    for i in reversed(range((len(term) + 1) / 2, len(term) + 1)):
        url = 'http://suggest-slovari.yandex.ru/suggest-lingvo?v=2&lang=en&part=%s' % term[:i]
        data = urllib.urlopen(url).read()
        data = simplejson.loads(data)
        if data[0]:
            for trans, link in zip(*data[1:]):
                en, ru = trans.split(' - ', 1)
                variants[en] = dict(en=en, ru=ru, link=link)
            if len(variants) > 5:
                break

    return dict(template=template, variants=sorted(variants.values()))

cmd_wo = cmd_search_word
<|endoftext|>