<commit_before>import datetime
from django import forms
from django.contrib.admin import widgets
from django.core import validators
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from main.models import SiteConfig, UserProfile, Module, StatusSiteDomain, PORT_CHECK_OPTIONS, ScheduledMaintenance

class ProfileForm(forms.ModelForm):
    class Meta:
        model=UserProfile
        exclude=('user', 'username', 'email')

    first_name = forms.CharField(max_length=30, required=False)
    last_name = forms.CharField(max_length=30, required=False)
    password1 = forms.CharField(widget=forms.PasswordInput(render_value=False),
        label=_("Password"),
        validators=[validators.MinLengthValidator(4)],
        error_messages={'min_length': _('Password must be at least 4 characters')},
        required = False)
    password2 = forms.CharField(widget=forms.PasswordInput(render_value=False),
        label=_("Password (again)"), required=False)

    def __init__(self, *args, **kw):
        super(forms.ModelForm, self).__init__(*args, **kw)
        self.fields.keyOrder = ['first_name', 'last_name','birth_date', 'address', 'city', 'country',
                           'state', 'phone_number','password1', 'password2']

    def clean(self):
        cleaned_data = super(ProfileForm, self).clean()
        if 'password1' in cleaned_data and 'password2' in cleaned_data:
            if cleaned_data['password1'] != cleaned_data['password2']:
                raise forms.ValidationError(_("The two password fields didn't match."))
        return self.cleaned_data

class SiteConfigForm(forms.ModelForm):
    class Meta:
        model = SiteConfig
        exclude = ('user','twitter_account','api_key','api_secret', 'notification_sender', 'notification_to',
                   'custom_css','logo','preview_custom_css','preview_logo', 'public_internal_url', 'user_theme_selection')

class ScheduledMaintenanceForm(forms.ModelForm):
    def __init__(self, user, *args, **kw):
        super(forms.ModelForm, self).__init__(*args, **kw)
        self.fields.keyOrder = ['scheduled_to','time_estimate','message', 'total_downtime', 'created_at', 'updated_at']
    scheduled_to = forms.DateTimeField(required=True,
                                        help_text="Date format is: YYYY-mm-dd HH:ii:ss")
    created_at = forms.DateTimeField(required=False)
    updated_at = forms.DateTimeField(required=False)

    time_estimate = forms.IntegerField(help_text="Used units: minutes")
    total_downtime = forms.FloatField(widget=forms.TextInput(attrs=dict({'class':'disabled', 'readonly':'readonly', 'disabled':'disabled' })), required=False)

    class Meta:
        model = ScheduledMaintenance
        exclude = ('module', 'site_config', 'status')

class ScheduledMaintenanceTemplateForm(forms.ModelForm):
    def __init__(self, user, *args, **kw):
        super(forms.ModelForm, self).__init__(*args, **kw)
        self.fields.keyOrder = ['scheduled_to','time_estimate','message']
    scheduled_to = forms.DateTimeField(required=True,
        help_text="Date format is: YYYY-mm-dd HH:ii:ss")

    class Meta:
        model = ScheduledMaintenance
        exclude = ('module', 'site_config', 'status', 'total_downtime', 'created_at', 'updated_at')

class ModuleForm(forms.ModelForm):
    def __init__(self, user, *args, **kw):
        existing = False
        if kw.has_key('instance'):
            existing = True
        super(forms.ModelForm, self).__init__(*args, **kw)
        self.fields.keyOrder = ['module_type','name','description', 'host', 'url', 'site_config', 'tags', 'expected_status', 'search_keyword', 'check_port', 'public', 'fail_on_error', 'follow_location']

        self.fields['site_config'].choices = [ (o.pk, str(o) ) for o in SiteConfig.objects.filter(user=user)]

    check_port = forms.ChoiceField(choices=PORT_CHECK_OPTIONS, required=False, widget=forms.Select(attrs=dict({'class':'check_port'})))
    expected_status = forms.IntegerField(initial=200, required=False, widget=forms.TextInput(attrs=dict({'class':'expected_status'})),
                                        help_text="HTTP response status code.")
    search_keyword = forms.CharField(required=False, widget=forms.TextInput(attrs=dict({'class':'search_keyword'})),
                                        help_text="Keyword or regex to search for in the result page.")
    fail_on_error = forms.BooleanField(default=True,
                                        widget=forms.CheckboxInput(attrs=dict({'class':'fail_on_error'})),
                                        help_text="Returns with failure if the status code is greater than 300.")
    follow_location = forms.BooleanField(default=True,
                                        widget=forms.CheckboxInput(attrs=dict({'class':'follow_location'})),
                                        help_text="Follow redirects.")
    status = forms.CharField(widget=forms.TextInput(attrs=dict({'class':'disabled', 'readonly':'readonly', 'disabled':'disabled' })), required=False)

    class Meta:
        model = Module

class StatusSiteCustomizationForm(forms.Form):
    logo = forms.ImageField(required=False)
    custom_css = forms.CharField(widget=forms.Textarea, required=False)
    site_config_id = forms.CharField(widget=forms.HiddenInput)

class StatusSiteDomainForm(forms.ModelForm):
    def __init__(self, user, *args, **kw):
        super(forms.ModelForm, self).__init__(*args, **kw)
        self.fields['site_config'].choices = [ (o.pk, str(o) ) for o in SiteConfig.objects.filter(user=user)]

    class Meta:
        model = StatusSiteDomain
<commit_msg>* fixed just added form inputs<commit_after>import datetime
from django import forms
from django.contrib.admin import widgets
from django.core import validators
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from main.models import SiteConfig, UserProfile, Module, StatusSiteDomain, PORT_CHECK_OPTIONS, ScheduledMaintenance

class ProfileForm(forms.ModelForm):
    class Meta:
        model=UserProfile
        exclude=('user', 'username', 'email')

    first_name = forms.CharField(max_length=30, required=False)
    last_name = forms.CharField(max_length=30, required=False)
    password1 = forms.CharField(widget=forms.PasswordInput(render_value=False),
        label=_("Password"),
        validators=[validators.MinLengthValidator(4)],
        error_messages={'min_length': _('Password must be at least 4 characters')},
        required = False)
    password2 = forms.CharField(widget=forms.PasswordInput(render_value=False),
        label=_("Password (again)"), required=False)

    def __init__(self, *args, **kw):
        super(forms.ModelForm, self).__init__(*args, **kw)
        self.fields.keyOrder = ['first_name', 'last_name','birth_date', 'address', 'city', 'country',
                           'state', 'phone_number','password1', 'password2']

    def clean(self):
        cleaned_data = super(ProfileForm, self).clean()
        if 'password1' in cleaned_data and 'password2' in cleaned_data:
            if cleaned_data['password1'] != cleaned_data['password2']:
                raise forms.ValidationError(_("The two password fields didn't match."))
        return self.cleaned_data

class SiteConfigForm(forms.ModelForm):
    class Meta:
        model = SiteConfig
        exclude = ('user','twitter_account','api_key','api_secret', 'notification_sender', 'notification_to',
                   'custom_css','logo','preview_custom_css','preview_logo', 'public_internal_url', 'user_theme_selection')

class ScheduledMaintenanceForm(forms.ModelForm):
    def __init__(self, user, *args, **kw):
        super(forms.ModelForm, self).__init__(*args, **kw)
        self.fields.keyOrder = ['scheduled_to','time_estimate','message', 'total_downtime', 'created_at', 'updated_at']
    scheduled_to = forms.DateTimeField(required=True,
                                        help_text="Date format is: YYYY-mm-dd HH:ii:ss")
    created_at = forms.DateTimeField(required=False)
    updated_at = forms.DateTimeField(required=False)

    time_estimate = forms.IntegerField(help_text="Used units: minutes")
    total_downtime = forms.FloatField(widget=forms.TextInput(attrs=dict({'class':'disabled', 'readonly':'readonly', 'disabled':'disabled' })), required=False)

    class Meta:
        model = ScheduledMaintenance
        exclude = ('module', 'site_config', 'status')

class ScheduledMaintenanceTemplateForm(forms.ModelForm):
    def __init__(self, user, *args, **kw):
        super(forms.ModelForm, self).__init__(*args, **kw)
        self.fields.keyOrder = ['scheduled_to','time_estimate','message']
    scheduled_to = forms.DateTimeField(required=True,
        help_text="Date format is: YYYY-mm-dd HH:ii:ss")

    class Meta:
        model = ScheduledMaintenance
        exclude = ('module', 'site_config', 'status', 'total_downtime', 'created_at', 'updated_at')

class ModuleForm(forms.ModelForm):
    def __init__(self, user, *args, **kw):
        existing = False
        if kw.has_key('instance'):
            existing = True
        super(forms.ModelForm, self).__init__(*args, **kw)
        self.fields.keyOrder = ['module_type','name','description', 'host', 'url', 'site_config', 'tags', 'expected_status', 'search_keyword', 'check_port', 'public', 'fail_on_error', 'follow_location']

        self.fields['site_config'].choices = [ (o.pk, str(o) ) for o in SiteConfig.objects.filter(user=user)]

    check_port = forms.ChoiceField(choices=PORT_CHECK_OPTIONS, required=False, widget=forms.Select(attrs=dict({'class':'check_port'})))
    expected_status = forms.IntegerField(initial=200, required=False, widget=forms.TextInput(attrs=dict({'class':'expected_status'})),
                                        help_text="HTTP response status code.")
    search_keyword = forms.CharField(required=False, widget=forms.TextInput(attrs=dict({'class':'search_keyword'})),
                                        help_text="Keyword or regex to search for in the result page.")
    fail_on_error = forms.BooleanField(widget=forms.CheckboxInput(attrs=dict({'class':'fail_on_error'})),
                                        help_text="Returns with failure if the status code is greater than 300.", required=False)
    follow_location = forms.BooleanField(widget=forms.CheckboxInput(attrs=dict({'class':'follow_location'})),
                                        help_text="Follow redirects.", required=False)
    status = forms.CharField(widget=forms.TextInput(attrs=dict({'class':'disabled', 'readonly':'readonly', 'disabled':'disabled' })), required=False)

    class Meta:
        model = Module

class StatusSiteCustomizationForm(forms.Form):
    logo = forms.ImageField(required=False)
    custom_css = forms.CharField(widget=forms.Textarea, required=False)
    site_config_id = forms.CharField(widget=forms.HiddenInput)

class StatusSiteDomainForm(forms.ModelForm):
    def __init__(self, user, *args, **kw):
        super(forms.ModelForm, self).__init__(*args, **kw)
        self.fields['site_config'].choices = [ (o.pk, str(o) ) for o in SiteConfig.objects.filter(user=user)]

    class Meta:
        model = StatusSiteDomain
<|endoftext|>


<commit_before>import json
import urllib
import urllib2
import gdata.gauth
import gdata.client
import gdata.docs.data
import gdata.docs.client

from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required

from gdata.spreadsheets import client

from main.models import  TokenStorageModel
from main.views import home

token = gdata.gauth.OAuth2Token(client_id=settings.GOOGLE_CLIENT_ID,
    client_secret=settings.GOOGLE_CLIENT_SECRET,
    scope=', '.join(['https://docs.google.com/feeds/',
        'https://spreadsheets.google.com/feeds/']),
    user_agent='formhub')

redirect_uri = token.generate_authorize_url(
    redirect_uri=settings.GOOGLE_STEP2_URI,
    access_type='offline', approval_prompt='force')

@login_required
def google_oauth2_request(request):
    try:
        ts = TokenStorageModel.objects.get(id=request.user)
    except TokenStorageModel.DoesNotExist:
        pass
    else:
        stored_token = gdata.gauth.token_from_blob(ts.token)
        print stored_token.refresh_token, stored_token.access_token
        if stored_token.refresh_token is not None and\
           stored_token.access_token is not None:
            token.refresh_token = stored_token.refresh_token
            working_token = refresh_access_token(token, request.user)
            docs_client = client.SpreadsheetsClient(source=token.user_agent)
            docs_client = working_token.authorize(docs_client)
            docs_feed = docs_client.GetSpreadsheets()
            _l = '
'
            for entry in docs_feed.entry:
                _l += '
%s
' % entry.title.text
                print entry.title.text
            _l += '
'
            return HttpResponse(_l)
    print redirect_uri
    return HttpResponseRedirect(redirect_uri)

@login_required
def google_auth_return(request):
    if 'code' not in request.REQUEST:
        return HttpResponse(u"Invalid Request")
    try:
        ts = TokenStorageModel.objects.get(id=request.user)
    except TokenStorageModel.DoesNotExist:
        ts = TokenStorageModel(id=request.user)
    access_token = token.get_access_token(request.REQUEST)
    ts.token = gdata.gauth.token_to_blob(token=access_token)
    ts.save()
    return HttpResponseRedirect(reverse(home))

def refresh_access_token(token, user):
    try:
        ts = TokenStorageModel.objects.get(id=user)
    except TokenStorageModel.DoesNotExist:
        ts = TokenStorageModel(id=user)
    data = urllib.urlencode({
        'client_id': settings.GOOGLE_CLIENT_ID,
        'client_secret': settings.GOOGLE_CLIENT_SECRET,
        'refresh_token': token.refresh_token,
        'grant_type': 'refresh_token'})
    request = urllib2.Request(
        url='https://accounts.google.com/o/oauth2/token',
        data=data)
    request_open = urllib2.urlopen(request)
    response = request_open.read()
    request_open.close()
    tokens = json.loads(response)
    token.access_token = tokens['access_token']
    ts.token = gdata.gauth.token_to_blob(token)
    ts.save()
    return token
<commit_msg>corrected scope format, separate b space not comma<commit_after>import json
import urllib
import urllib2
import gdata.gauth
import gdata.client
import gdata.docs.data
import gdata.docs.client

from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required

from gdata.spreadsheets import client

from main.models import  TokenStorageModel
from main.views import home

token = gdata.gauth.OAuth2Token(client_id=settings.GOOGLE_CLIENT_ID,
    client_secret=settings.GOOGLE_CLIENT_SECRET,
    scope=' '.join(['https://docs.google.com/feeds/',
        'https://spreadsheets.google.com/feeds/']),
    user_agent='formhub')

redirect_uri = token.generate_authorize_url(
    redirect_uri=settings.GOOGLE_STEP2_URI,
    access_type='offline', approval_prompt='force')

@login_required
def google_oauth2_request(request):
    try:
        ts = TokenStorageModel.objects.get(id=request.user)
    except TokenStorageModel.DoesNotExist:
        pass
    else:
        stored_token = gdata.gauth.token_from_blob(ts.token)
        print stored_token.refresh_token, stored_token.access_token
        if stored_token.refresh_token is not None and\
           stored_token.access_token is not None:
            token.refresh_token = stored_token.refresh_token
            working_token = refresh_access_token(token, request.user)
            docs_client = client.SpreadsheetsClient(source=token.user_agent)
            docs_client = working_token.authorize(docs_client)
            docs_feed = docs_client.GetSpreadsheets()
            _l = '
'
            for entry in docs_feed.entry:
                _l += '
%s
' % entry.title.text
                print entry.title.text
            _l += '
'
            return HttpResponse(_l)
    print redirect_uri
    return HttpResponseRedirect(redirect_uri)

@login_required
def google_auth_return(request):
    if 'code' not in request.REQUEST:
        return HttpResponse(u"Invalid Request")
    try:
        ts = TokenStorageModel.objects.get(id=request.user)
    except TokenStorageModel.DoesNotExist:
        ts = TokenStorageModel(id=request.user)
    access_token = token.get_access_token(request.REQUEST)
    ts.token = gdata.gauth.token_to_blob(token=access_token)
    ts.save()
    return HttpResponseRedirect(reverse(home))

def refresh_access_token(token, user):
    try:
        ts = TokenStorageModel.objects.get(id=user)
    except TokenStorageModel.DoesNotExist:
        ts = TokenStorageModel(id=user)
    data = urllib.urlencode({
        'client_id': settings.GOOGLE_CLIENT_ID,
        'client_secret': settings.GOOGLE_CLIENT_SECRET,
        'refresh_token': token.refresh_token,
        'grant_type': 'refresh_token'})
    request = urllib2.Request(
        url='https://accounts.google.com/o/oauth2/token',
        data=data)
    request_open = urllib2.urlopen(request)
    response = request_open.read()
    request_open.close()
    tokens = json.loads(response)
    token.access_token = tokens['access_token']
    ts.token = gdata.gauth.token_to_blob(token)
    ts.save()
    return token
<|endoftext|>


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

from datapreparations import *
from datatester import *

class YeahMobiTask(object):

    def __init__(self, taskid, taskname, caseidlist, queryNow = True) :
        self.dber = Dber()
        self.dter = DataTester()
        self.dper = SyncData()
        self.logger = getLogger()
        self.queryNow = queryNow
        if self.queryNow:
            start, end = get_unixtime_range()
            self.dper.sync(start, end)
            self.updateCase()
        self.taskid = taskid
        self.taskname = taskname
        self.caseidlist = caseidlist
        self.passcount = 0
        self.failcount = 0
        self.getCaseList()

    def updateCase(self):
        updateCaseSql = 'update ym_case(start_time_of_case, end_time_of_case) values({0}, {2})'.format(
            get_unixtime_range()
        )
        self.dber.executSql(updateCaseSql)
        self.dber.setCommit()
        self.logger.info('update case success')

    def getTaskId(self):
        return self.taskid

    def getTaskName(self):
        return self.taskname

    def getCaseIdList(self):
        return self.caseidlist

    def getCaseList(self):
        self.caseList = list()
        getCaseSql = 'select casename, start_time_of_case, end_time_of_case, casecontent from ym_case where caseid = {0}'
        for caseid in self.caseidlist.split(','):
            caseMap = dict()
            caseinfo = self.dber.getRecord(getCaseSql.format(caseid))
            caseMap['caseid'] = caseid
            caseMap['casename'] = caseinfo[0]
            caseMap['start_time_of_case'] = caseinfo[1]
            caseMap['end_time_of_case'] = caseinfo[2]
            caseMap['casecontent'] = caseinfo[3]
            self.caseList.append(caseMap)

    def runTask(self):
        self.logger.info('get task success. taskid: {0} task name: {1} caseid of task: {2}'.format(self.taskid, self.taskname, self.caseidlist))
        for case in self.caseList:
            caseid = case.get('caseid')
            casename = case.get('casename')
            case = case.get('casecontent') % (int(case.get('start_time_of_case')), int(case.get('end_time_of_case')))
            self.logger.info('get case: {0} query: {1}'.format(casename, case))
            resultInfo = self.dter.runCase(case)
            isPass = resultInfo.get('isPass')
            druid_result = resultInfo['druid_result']
            druid_query = resultInfo['druid_query']
            mysql_query = resultInfo['mysql_query']
            mysql_result = resultInfo['mysql_result']
            if isPass:
                self.passcount += 1
            else:
                self.failcount += 1
            syncDetailResultSql = "insert into ym_detail_result(taskid, caseid, druid_result, druid_query, mysql_query, mysql_result, run_time) values ({0}, {1}, '{2}', '{3}', '{4}', '{5}', '{6}')".format(
                self.taskid, caseid, druid_result, druid_query, mysql_query, mysql_result, get_now()
            )
            self.dber.executSql(syncDetailResultSql)
        if self.failcount == 0 and self.passcount != 0:
            taskResult = 'success'
        else:
            taskResult = 'failed'
        syncTaskResultSql = 'insert into ym_task_result(taskid, passcount, failcount, taskresult, runtime) values({0}, {1}, {2}, "{3}", "{4}")'.format(
            self.taskid, self.passcount, self.failcount, taskResult, get_now()
        )
        self.dber.executSql(syncTaskResultSql)
        self.dber.setCommit()
        self.dber.setColse()

if __name__ == '__main__':
    ymt = YeahMobiTask('1', 'task001', '0,1')
    ymt.runTask()

<commit_msg>add update case when need query now<commit_after>#! /usr/bin/env python
# --*-- coding:utf-8 --*--

from datapreparations import *
from datatester import *

class YeahMobiTask(object):

    def __init__(self, taskid, taskname, caseidlist, queryNow = True) :
        self.dber = Dber()
        self.dter = DataTester()
        self.dper = SyncData()
        self.logger = getLogger()
        self.queryNow = queryNow
        if self.queryNow:
            start, end = get_unixtime_range()
            self.dper.sync(start, end)
            self.updateCase()
        self.taskid = taskid
        self.taskname = taskname
        self.caseidlist = caseidlist
        self.passcount = 0
        self.failcount = 0
        self.getCaseList()

    def updateCase(self):
        unix_range = get_unixtime_range()
        start, end = unix_range[0], unix_range[1]
        updateCaseSql = 'update ym_case(start_time_of_case, end_time_of_case) values({0}, {2})'.format(
            start, end
        )
        self.dber.executSql(updateCaseSql)
        self.dber.setCommit()
        self.logger.info('update case success')

    def getTaskId(self):
        return self.taskid

    def getTaskName(self):
        return self.taskname

    def getCaseIdList(self):
        return self.caseidlist

    def getCaseList(self):
        self.caseList = list()
        getCaseSql = 'select casename, start_time_of_case, end_time_of_case, casecontent from ym_case where caseid = {0}'
        for caseid in self.caseidlist.split(','):
            caseMap = dict()
            caseinfo = self.dber.getRecord(getCaseSql.format(caseid))
            caseMap['caseid'] = caseid
            caseMap['casename'] = caseinfo[0]
            caseMap['start_time_of_case'] = caseinfo[1]
            caseMap['end_time_of_case'] = caseinfo[2]
            caseMap['casecontent'] = caseinfo[3]
            self.caseList.append(caseMap)

    def runTask(self):
        self.logger.info('get task success. taskid: {0} task name: {1} caseid of task: {2}'.format(self.taskid, self.taskname, self.caseidlist))
        for case in self.caseList:
            caseid = case.get('caseid')
            casename = case.get('casename')
            case = case.get('casecontent') % (int(case.get('start_time_of_case')), int(case.get('end_time_of_case')))
            self.logger.info('get case: {0} query: {1}'.format(casename, case))
            resultInfo = self.dter.runCase(case)
            isPass = resultInfo.get('isPass')
            druid_result = resultInfo['druid_result']
            druid_query = resultInfo['druid_query']
            mysql_query = resultInfo['mysql_query']
            mysql_result = resultInfo['mysql_result']
            if isPass:
                self.passcount += 1
            else:
                self.failcount += 1
            syncDetailResultSql = "insert into ym_detail_result(taskid, caseid, druid_result, druid_query, mysql_query, mysql_result, run_time) values ({0}, {1}, '{2}', '{3}', '{4}', '{5}', '{6}')".format(
                self.taskid, caseid, druid_result, druid_query, mysql_query, mysql_result, get_now()
            )
            self.dber.executSql(syncDetailResultSql)
        if self.failcount == 0 and self.passcount != 0:
            taskResult = 'success'
        else:
            taskResult = 'failed'
        syncTaskResultSql = 'insert into ym_task_result(taskid, passcount, failcount, taskresult, runtime) values({0}, {1}, {2}, "{3}", "{4}")'.format(
            self.taskid, self.passcount, self.failcount, taskResult, get_now()
        )
        self.dber.executSql(syncTaskResultSql)
        self.dber.setCommit()
        self.dber.setColse()

if __name__ == '__main__':
    ymt = YeahMobiTask('1', 'task001', '0,1')
    ymt.runTask()

<|endoftext|>


<commit_before>import numpy as np

from malcolm.compat import str_
from .serializable import Serializable, deserialize_object

def sort_names(d):
    return list(sorted(d, key=d.__getitem__))

class AlarmSeverity(object):
    NO_ALARM, MINOR_ALARM, MAJOR_ALARM, INVALID_ALARM, UNDEFINED_ALARM = \
        range(5)
    names = sort_names(locals())

class AlarmStatus(object):
    NO_STATUS, DEVICE_STATUS, DRIVER_STATUS, RECORD_STATUS, DB_STATUS, \
        CONF_STATUS, UNDEFINED_STATUS, CLIENT_STATUS = range(8)
    names = sort_names(locals())

@Serializable.register_subclass("alarm_t")
class Alarm(Serializable):

    endpoints = ["severity", "status", "message"]
    __slots__ = endpoints

    def __init__(self, severity=AlarmSeverity.NO_ALARM,
                 status=AlarmStatus.NO_STATUS, message=""):
        # Set initial values
        assert int(severity) in range(len(AlarmSeverity.names)), \
            "Expected AlarmSeverity.*_ALARM, got %r" % severity
        self.severity = np.int32(severity)
        assert int(status) in range(len(AlarmStatus.names)), \
            "Expected AlarmStatus.*_STATUS, got %r" % status
        self.status = np.int32(status)
        self.message = deserialize_object(message, str_)

    @classmethod
    def major(cls, message):
        return cls(
            AlarmSeverity.MAJOR_ALARM, AlarmStatus.DEVICE_STATUS, message)

    @classmethod
    def invalid(cls, message):
        return cls(
            AlarmSeverity.INVALID_ALARM, AlarmStatus.DEVICE_STATUS, message)

Alarm.ok = Alarm()<commit_msg>Fix name sorting for Python3<commit_after>import numpy as np

from malcolm.compat import str_
from .serializable import Serializable, deserialize_object

def sort_names(d):
    name_d = dict((k, v) for k, v in d.items() if isinstance(v, int))
    return list(sorted(name_d, key=name_d.__getitem__))

class AlarmSeverity(object):
    NO_ALARM, MINOR_ALARM, MAJOR_ALARM, INVALID_ALARM, UNDEFINED_ALARM = \
        range(5)
    names = sort_names(locals())

class AlarmStatus(object):
    NO_STATUS, DEVICE_STATUS, DRIVER_STATUS, RECORD_STATUS, DB_STATUS, \
        CONF_STATUS, UNDEFINED_STATUS, CLIENT_STATUS = range(8)
    names = sort_names(locals())

@Serializable.register_subclass("alarm_t")
class Alarm(Serializable):

    endpoints = ["severity", "status", "message"]
    __slots__ = endpoints

    def __init__(self, severity=AlarmSeverity.NO_ALARM,
                 status=AlarmStatus.NO_STATUS, message=""):
        # Set initial values
        assert int(severity) in range(len(AlarmSeverity.names)), \
            "Expected AlarmSeverity.*_ALARM, got %r" % severity
        self.severity = np.int32(severity)
        assert int(status) in range(len(AlarmStatus.names)), \
            "Expected AlarmStatus.*_STATUS, got %r" % status
        self.status = np.int32(status)
        self.message = deserialize_object(message, str_)

    @classmethod
    def major(cls, message):
        return cls(
            AlarmSeverity.MAJOR_ALARM, AlarmStatus.DEVICE_STATUS, message)

    @classmethod
    def invalid(cls, message):
        return cls(
            AlarmSeverity.INVALID_ALARM, AlarmStatus.DEVICE_STATUS, message)

Alarm.ok = Alarm()<|endoftext|>


<commit_before>from __future__ import (
    unicode_literals,
    absolute_import,
    division,
    print_function,
    )

# Make Py2's str type like Py3's
str = type('')

from itertools import chain
import random
import re

import nltk
from nltk.tokenize import wordpunct_tokenize
from nltk.corpus import cmudict

from .dialects import manc

phoneme_reprs = {
    "AA":   "o",    # 'o' as in 'odd'
    "AE":   "a",    # 'a' as in 'at'
    "AH":   "uh",   # 'u' as in 'hut'
    "AO":   "oar",  # 'augh' as in 'caught'
    "AW":   "ow",   # 'ow' as in 'cow'
    "AY":   'iy',   # 'i' as in 'hide'
    "B":    'b',    # 'b' as in 'bee'
    "CH":   'ch',   # 'ch' as in 'cheese'
    "D":    'd',    # 'd' as in 'dog'
    "DH":   'th',   # 'th' as in 'thee'
    "EH":   'e',    # 'e' as in 'Ed'
    "ER":   'ur',   # 'ur' as in 'hurt'
    "EY":   'ey',   # 'a' as in 'ate'
    "F":    'f',    # 'f' as in 'fee'
    "G":    'g',    # 'g' as in 'green'
    "HH":   'h',    # 'h' as in 'house'
    "IH":   'i',    # 'i' as in 'it'
    "IY":   'ee',   # 'ea' as in 'eat'
    "JH":   'j',    # 'g' as in 'gee'
    "K":    'k',    # 'k' as in 'key'
    "L":    'l',    # 'l' as in 'lee'
    "M":    'm',    # 'm' as in 'me'
    "N":    'n',    # 'kn' as in 'knee'
    "NG":   'ng',   # 'ng' as in 'ping'
    "OW":   'oh',   # 'oa' as in 'oat'
    "OY":   'oy',   # 'oy' as in 'toy'
    "P":    'p',    # 'p' as in 'pee'
    "R":    'r',    # 'r' as in 'read'
    "S":    's',    # 's' as in 'sea'
    "SH":   'sh',   # 'sh' as in 'she'
    "T":    't',    # 't' as in 'tea'
    "TH":   'th',   # 'th' as in 'theta'
    "UH":   'u',    # 'oo' as in 'hood'
    "UW":   'oo',   # 'wo' as in 'two'
    "V":    'v',    # 'v' as in 'vee'
    "W":    'w',    # 'w' as in 'we'
    "Y":    'y',    # 'y' as in 'yeild'
    "Z":    'z',    # 'z' as in 'zee'
    "ZH":   'z',    # 'z' as in 'seizure'
    "'":    "'",    # glottal stop
    }

phoneme_dict = cmudict.dict()

def translate(text, dialect=manc):
    """Translate from plain English to given dialect"""
    tokens = tokenize(text)
    restructured = restructure(tokens,dialect)
    translated = substitute(restructured, dialect)
    return untokenize(translated)

def restructure(tokens, dialect):
    """Rearranges the structure of the input based on the
        given dialect"""
    tagged = [("START","START")] + nltk.pos_tag(tokens) + [("END","END")]
    for patterns,replacements,chance in dialect.structure_rules:
        for pattern in patterns:
            for i in range(len(tagged)):
                if not pos_tag_match(tagged,i,pattern):
                    continue
                if random.random() >= chance:
                    continue
                replacement = random.choice(replacements)
                new = tagged[:i]
                for r in replacement:
                    if type(r)==int:
                        new += [tagged[i+r]]
                    else:
                        new += [(r,"?")]
                new += tagged[i+len(pattern):]
                tagged = new
                break
    return iter([word for word,tag in tagged[1:-1]])

def pos_tag_match(tagged,i,pattern):
    for j,ptag in enumerate(pattern):
        if i+j >= len(tagged):
            return False
        if not bool(re.match("^"+ptag.replace("*",".*")+"$",tagged[i+j][1])):
            return False
    return True

def phoneme_match(phons,i,pattern):
    for j,pfon in enumerate(pattern):
        if i+j >= len(phons):
            return False
        if pfon == "VOWEL":
            if phons[i+j] not in (
                    "AA","AE","AH","AO","AW","AY","EH","ER","EY","IH","IY","OW","OY","UH","UW"):
                return False
            else: continue
        if pfon == "CONS":
            if phons[i+j] not in (
                    "B","CH","D","DH","F","G","HH","JH","K","L","M","N","NG","P","R","S","SH","T","TH","V","W","Y","Z","ZH","'"):
                return False
            else: continue
        if pfon != phons[i+j]:
            return False
    return True

def substitute(tokens, dialect):
    """Generator producing translated words for given tokens.

    Algorithm:
        1. Try direct word substitution
        2. If (1) fails, try phonetic substitution.
        3. If (2) fails, it's a symbol! Return as is...

    """
    for token in tokens:
        token_lower = token.lower()
        if token_lower in dialect.ignores:
            yield token
            continue
        substitution = replace_random(token_lower, dialect)
        if substitution == token_lower:
            substitution = alter_phonemes(token_lower, dialect)

        yield match_case(substitution, token)

def alter_phonemes(word,dialect):
    """Write out word phonetically, applying phoneme rules in the
        process"""
    try:
        phons = phoneme_dict[word][0]
    except KeyError:
        return word

    phons = [re.sub("[0-9]","",p) for p in phons]
    phons = ["START"] + phons + ["END"]

    for patterns, replacement in dialect.phoneme_rules:
        for pattern in patterns:
            for i in range(len(phons)):
                if phoneme_match(phons,i,pattern):
                    r = [phons[i+rfon] if type(rfon)==int else rfon for rfon in replacement]
                    phons = phons[:i] + r + phons[i+len(pattern):]
                    break

    return "".join([phoneme_reprs[p] for p in phons[1:-1]])

def replace_random(word,dialect):
    """Replace given word with a random alternative from given dialect.

    If a replacement word does not exist, return the original word.

    """
    for patterns, replacements in dialect.word_rules:
        if word in patterns:
            return random.choice(replacements)

    # No replacement found
    return word

def tokenize(text):
    """Turn a block of text and tokenize into a list of words and symbols.

    Features:
      - Retain newlines.
      - Retain special characters.

    """
    lines = (line for line in text.split('\n'))
    tokens = (wordpunct_tokenize(line) + ['\n'] for line in lines)
    return list(chain.from_iterable(tokens))[:-1]

def untokenize(tokens):
    """Join a sequence of tokens into a single block of text.

    An attempt is made to respect the location of whitespace in the
    original text.

    """

    def spacer():
        token = tokens.next()
        yield token

        last = token
        for token in tokens:
            if any(c.isalnum() for c in token) and not last.isspace():
                yield ' '
            yield token
            last = token

    return ''.join(list(spacer()))

def match_case(text, match_text):
    """Return text capitalised to match match_text."""
    word_count = len(match_text.split())
    if match_text.istitle() and word_count == 1:
        return text.capitalize()
    elif match_text.istitle():
        return text.title()
    elif match_text[0].isupper():
        return text.capitalize()
    else:
        return text

def main():
    """Entry point to run the standalone translator on a file or on stdin."""
    import sys
    f = sys.stdin if len(sys.argv) == 1 else open(sys.argv[1])
    print(translate(f.read()))

if __name__ == '__main__':
    main()
<commit_msg>added seed parameter for translator<commit_after>from __future__ import (
    unicode_literals,
    absolute_import,
    division,
    print_function,
    )

# Make Py2's str type like Py3's
str = type('')

from itertools import chain
import random
import re

import nltk
from nltk.tokenize import wordpunct_tokenize
from nltk.corpus import cmudict

from .dialects import manc

phoneme_reprs = {
    "AA":   "o",    # 'o' as in 'odd'
    "AE":   "a",    # 'a' as in 'at'
    "AH":   "uh",   # 'u' as in 'hut'
    "AO":   "oar",  # 'augh' as in 'caught'
    "AW":   "ow",   # 'ow' as in 'cow'
    "AY":   'iy',   # 'i' as in 'hide'
    "B":    'b',    # 'b' as in 'bee'
    "CH":   'ch',   # 'ch' as in 'cheese'
    "D":    'd',    # 'd' as in 'dog'
    "DH":   'th',   # 'th' as in 'thee'
    "EH":   'e',    # 'e' as in 'Ed'
    "ER":   'ur',   # 'ur' as in 'hurt'
    "EY":   'ey',   # 'a' as in 'ate'
    "F":    'f',    # 'f' as in 'fee'
    "G":    'g',    # 'g' as in 'green'
    "HH":   'h',    # 'h' as in 'house'
    "IH":   'i',    # 'i' as in 'it'
    "IY":   'ee',   # 'ea' as in 'eat'
    "JH":   'j',    # 'g' as in 'gee'
    "K":    'k',    # 'k' as in 'key'
    "L":    'l',    # 'l' as in 'lee'
    "M":    'm',    # 'm' as in 'me'
    "N":    'n',    # 'kn' as in 'knee'
    "NG":   'ng',   # 'ng' as in 'ping'
    "OW":   'oh',   # 'oa' as in 'oat'
    "OY":   'oy',   # 'oy' as in 'toy'
    "P":    'p',    # 'p' as in 'pee'
    "R":    'r',    # 'r' as in 'read'
    "S":    's',    # 's' as in 'sea'
    "SH":   'sh',   # 'sh' as in 'she'
    "T":    't',    # 't' as in 'tea'
    "TH":   'th',   # 'th' as in 'theta'
    "UH":   'u',    # 'oo' as in 'hood'
    "UW":   'oo',   # 'wo' as in 'two'
    "V":    'v',    # 'v' as in 'vee'
    "W":    'w',    # 'w' as in 'we'
    "Y":    'y',    # 'y' as in 'yeild'
    "Z":    'z',    # 'z' as in 'zee'
    "ZH":   'z',    # 'z' as in 'seizure'
    "'":    "'",    # glottal stop
    }

phoneme_dict = cmudict.dict()

def translate(text, dialect=manc, seed=None):
    """Translate from plain English to given dialect"""
    random.seed(seed)
    tokens = tokenize(text)
    restructured = restructure(tokens,dialect)
    translated = substitute(restructured, dialect)
    return untokenize(translated)

def restructure(tokens, dialect):
    """Rearranges the structure of the input based on the
        given dialect"""
    tagged = [("START","START")] + nltk.pos_tag(tokens) + [("END","END")]
    for patterns,replacements,chance in dialect.structure_rules:
        for pattern in patterns:
            for i in range(len(tagged)):
                if not pos_tag_match(tagged,i,pattern):
                    continue
                if random.random() >= chance:
                    continue
                replacement = random.choice(replacements)
                new = tagged[:i]
                for r in replacement:
                    if type(r)==int:
                        new += [tagged[i+r]]
                    else:
                        new += [(r,"?")]
                new += tagged[i+len(pattern):]
                tagged = new
                break
    return iter([word for word,tag in tagged[1:-1]])

def pos_tag_match(tagged,i,pattern):
    for j,ptag in enumerate(pattern):
        if i+j >= len(tagged):
            return False
        if not bool(re.match("^"+ptag.replace("*",".*")+"$",tagged[i+j][1])):
            return False
    return True

def phoneme_match(phons,i,pattern):
    for j,pfon in enumerate(pattern):
        if i+j >= len(phons):
            return False
        if pfon == "VOWEL":
            if phons[i+j] not in (
                    "AA","AE","AH","AO","AW","AY","EH","ER","EY","IH","IY","OW","OY","UH","UW"):
                return False
            else: continue
        if pfon == "CONS":
            if phons[i+j] not in (
                    "B","CH","D","DH","F","G","HH","JH","K","L","M","N","NG","P","R","S","SH","T","TH","V","W","Y","Z","ZH","'"):
                return False
            else: continue
        if pfon != phons[i+j]:
            return False
    return True

def substitute(tokens, dialect):
    """Generator producing translated words for given tokens.

    Algorithm:
        1. Try direct word substitution
        2. If (1) fails, try phonetic substitution.
        3. If (2) fails, it's a symbol! Return as is...

    """
    for token in tokens:
        token_lower = token.lower()
        if token_lower in dialect.ignores:
            yield token
            continue
        substitution = replace_random(token_lower, dialect)
        if substitution == token_lower:
            substitution = alter_phonemes(token_lower, dialect)

        yield match_case(substitution, token)

def alter_phonemes(word,dialect):
    """Write out word phonetically, applying phoneme rules in the
        process"""
    try:
        phons = phoneme_dict[word][0]
    except KeyError:
        return word

    phons = [re.sub("[0-9]","",p) for p in phons]
    phons = ["START"] + phons + ["END"]

    for patterns, replacement in dialect.phoneme_rules:
        for pattern in patterns:
            for i in range(len(phons)):
                if phoneme_match(phons,i,pattern):
                    r = [phons[i+rfon] if type(rfon)==int else rfon for rfon in replacement]
                    phons = phons[:i] + r + phons[i+len(pattern):]
                    break

    return "".join([phoneme_reprs[p] for p in phons[1:-1]])

def replace_random(word,dialect):
    """Replace given word with a random alternative from given dialect.

    If a replacement word does not exist, return the original word.

    """
    for patterns, replacements in dialect.word_rules:
        if word in patterns:
            return random.choice(replacements)

    # No replacement found
    return word

def tokenize(text):
    """Turn a block of text and tokenize into a list of words and symbols.

    Features:
      - Retain newlines.
      - Retain special characters.

    """
    lines = (line for line in text.split('\n'))
    tokens = (wordpunct_tokenize(line) + ['\n'] for line in lines)
    return list(chain.from_iterable(tokens))[:-1]

def untokenize(tokens):
    """Join a sequence of tokens into a single block of text.

    An attempt is made to respect the location of whitespace in the
    original text.

    """

    def spacer():
        token = tokens.next()
        yield token

        last = token
        for token in tokens:
            if any(c.isalnum() for c in token) and not last.isspace():
                yield ' '
            yield token
            last = token

    return ''.join(list(spacer()))

def match_case(text, match_text):
    """Return text capitalised to match match_text."""
    word_count = len(match_text.split())
    if match_text.istitle() and word_count == 1:
        return text.capitalize()
    elif match_text.istitle():
        return text.title()
    elif match_text[0].isupper():
        return text.capitalize()
    else:
        return text

def main():
    """Entry point to run the standalone translator on a file or on stdin."""
    import sys
    f = sys.stdin if len(sys.argv) == 1 else open(sys.argv[1])
    print(translate(f.read()))

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>import datetime, decimal
from functools import wraps
from uuid import uuid1
from jsonrpc._json import loads, dumps
from jsonrpc.exceptions import *
from jsonrpc.types import *
from django.core import signals
empty_dec = lambda f: f
try:
  from django.views.decorators.csrf import csrf_exempt
except (NameError, ImportError):
  csrf_exempt = empty_dec

from django.core.serializers.json import DjangoJSONEncoder

NoneType = type(None)
encode_kw = lambda p: dict([(str(k), v) for k, v in p.iteritems()])

def encode_kw11(p):
  if not type(p) is dict:
    return {}
  ret = p.copy()
  removes = []
  for k, v in ret.iteritems():
    try:
      int(k)
    except ValueError:
      pass
    else:
      removes.append(k)
  for k in removes:
    ret.pop(k)
  return ret

def encode_arg11(p):
  if type(p) is list:
    return p
  elif not type(p) is dict:
    return []
  else:
    pos = []
    d = encode_kw(p)
    for k, v in d.iteritems():
      try:
        pos.append(int(k))
      except ValueError:
        pass
    pos = list(set(pos))
    pos.sort()
    return [d[str(i)] for i in pos]

def validate_params(method, D):
  if type(D['params']) == Object:
    keys = method.json_arg_types.keys()
    if len(keys) != len(D['params']):
      raise InvalidParamsError('Not eough params provided for %s' % method.json_sig)
    for k in keys:
      if not k in D['params']:
        raise InvalidParamsError('%s is not a valid parameter for %s'
                                 % (k, method.json_sig))
      if not Any.kind(D['params'][k]) == method.json_arg_types[k]:
        raise InvalidParamsError('%s is not the correct type %s for %s'
          % (type(D['params'][k]), method.json_arg_types[k], method.json_sig))
  elif type(D['params']) == Array:
    arg_types = method.json_arg_types.values()
    try:
      for i, arg in enumerate(D['params']):
        if not Any.kind(arg) == arg_types[i]:
          raise InvalidParamsError('%s is not the correct type %s for %s'
            % (type(arg), arg_types[i], method.json_sig))
    except IndexError:
      raise InvalidParamsError('Too many params provided for %s' % method.json_sig)
    else:
      if len(D['params']) != len(arg_types):
        raise InvalidParamsError('Not enouh params provided for %s' % method.json_sig)

class JSONRPCSite(object):
  "A JSON-RPC Site"
  def __init__(self, json_encoder=DjangoJSONEncoder):
    self.urls = {}
    self.uuid = str(uuid1())
    self.version = '1.0'
    self.name = 'django-json-rpc'
    self.register('system.describe', self.describe)
    self.set_json_encoder(json_encoder)

  def set_json_encoder(self, json_encoder=DjangoJSONEncoder):
    self.json_encoder = json_encoder

  def register(self, name, method):
    self.urls[unicode(name)] = method

  def empty_response(self, version='1.0'):
    resp = {'id': None}
    if version == '1.1':
      resp['version'] = version
      return resp
    if version == '2.0':
      resp['jsonrpc'] = version
    resp.update({'error': None, 'result': None})
    return resp

  def validate_get(self, request, method):
    encode_get_params = lambda r: dict([(k, v[0] if len(v) == 1 else v)
                                         for k, v in r])
    if request.method == 'GET':
      method = unicode(method)
      if method in self.urls and getattr(self.urls[method], 'json_safe', False):
        D = {
          'params': encode_get_params(request.GET.lists()),
          'method': method,
          'id': 'jsonrpc',
          'version': '1.1'
        }
        return True, D
    return False, {}

  def response_dict(self, request, D, is_batch=False, version_hint='1.0', json_encoder=None):
    json_encoder = json_encoder or self.json_encoder
    version = version_hint
    response = self.empty_response(version=version)
    apply_version = {'2.0': lambda f, r, p: f(r, **encode_kw(p)) if type(p) is dict else f(r, *p),
                     '1.1': lambda f, r, p: f(r, *encode_arg11(p), **encode_kw(encode_kw11(p))),
                     '1.0': lambda f, r, p: f(r, *p)}

    try:
      # params: An Array or Object, that holds the actual parameter values
      # for the invocation of the procedure. Can be omitted if empty.
      if 'params' not in D:
         D['params'] = []
      if 'method' not in D or 'params' not in D:
        raise InvalidParamsError('Request requires str:"method" and list:"params"')
      if D['method'] not in self.urls:
        raise MethodNotFoundError('Method not found. Available methods: %s' % (
                        '\n'.join(self.urls.keys())))

      if 'jsonrpc' in D:
        if str(D['jsonrpc']) not in apply_version:
          raise InvalidRequestError('JSON-RPC version %s not supported.' % D['jsonrpc'])
        version = request.jsonrpc_version = response['jsonrpc'] = str(D['jsonrpc'])
      elif 'version' in D:
        if str(D['version']) not in apply_version:
          raise InvalidRequestError('JSON-RPC version %s not supported.' % D['version'])
        version = request.jsonrpc_version = response['version'] = str(D['version'])
      else:
        request.jsonrpc_version = '1.0'

      method = self.urls[str(D['method'])]
      if getattr(method, 'json_validate', False):
        validate_params(method, D)
      R = apply_version[version](method, request, D['params'])

      encoder = json_encoder()
      if not sum(map(lambda e: isinstance(R, e), # type of `R` should be one of these or...
         (dict, str, unicode, int, long, list, set, NoneType, bool))):
        try:
          rs = encoder.default(R) # ...or something this thing supports
        except TypeError, exc:
          raise TypeError("Return type not supported, for %r" % R)

      if 'id' in D and D['id'] is not None: # regular request
        response['result'] = R
        response['id'] = D['id']
        if version in ('1.1', '2.0') and 'error' in response:
          response.pop('error')
      elif is_batch: # notification, not ok in a batch format, but happened anyway
        raise InvalidRequestError
      else: # notification
        return None, 204

      status = 200

    except Error, e:
      signals.got_request_exception.send(sender=self.__class__, request=request)
      response['error'] = e.json_rpc_format
      if version in ('1.1', '2.0') and 'result' in response:
        response.pop('result')
      status = e.status
    except Exception, e:
      # exception missed by others
      signals.got_request_exception.send(sender=self.__class__, request=request)
      other_error = OtherError(e)
      response['error'] = other_error.json_rpc_format
      status = other_error.status
      if version in ('1.1', '2.0') and 'result' in response:
        response.pop('result')

    # Exactly one of result or error MUST be specified. It's not
    # allowed to specify both or none.
    if version in ('1.1', '2.0') and 'error' in response and not response['error']:
      response.pop('error')

    return response, status

  @csrf_exempt
  def dispatch(self, request, method='', json_encoder=None):
    from django.http import HttpResponse
    json_encoder = json_encoder or self.json_encoder

    try:
      # in case we do something json doesn't like, we always get back valid json-rpc response
      response = self.empty_response()
      if request.method.lower() == 'get':
        valid, D = self.validate_get(request, method)
        if not valid:
          raise InvalidRequestError('The method you are trying to access is '
                                    'not availble by GET requests')
      elif not request.method.lower() == 'post':
        raise RequestPostError
      else:
        try:
          D = loads(request.raw_post_data)
        except:
          raise InvalidRequestError

      if type(D) is list:
        response = [self.response_dict(request, d, is_batch=True, json_encoder=json_encoder)[0] for d in D]
        status = 200
      else:
        response, status = self.response_dict(request, D, json_encoder=json_encoder)
        if response is None and (not u'id' in D or D[u'id'] is None): # a notification
          return HttpResponse('', status=status)

      json_rpc = dumps(response, cls=json_encoder)
    except Error, e:
      signals.got_request_exception.send(sender=self.__class__, request=request)
      response['error'] = e.json_rpc_format
      status = e.status
      json_rpc = dumps(response, cls=json_encoder)
    except Exception, e:
      # exception missed by others
      signals.got_request_exception.send(sender=self.__class__, request=request)
      other_error = OtherError(e)
      response['result'] = None
      response['error'] = other_error.json_rpc_format
      status = other_error.status

      json_rpc = dumps(response,cls=json_encoder)

    return HttpResponse(json_rpc, status=status, content_type='application/json-rpc')

  def procedure_desc(self, key):
    M = self.urls[key]
    return {
      'name': M.json_method,
      'summary': M.__doc__,
      'idempotent': M.json_safe,
      'params': [{'type': str(Any.kind(t)), 'name': k}
        for k, t in M.json_arg_types.iteritems()],
      'return': {'type': M.json_return_type}}

  def service_desc(self):
    return {
      'sdversion': '1.0',
      'name': self.name,
      'id': 'urn:uuid:%s' % str(self.uuid),
      'summary': self.__doc__,
      'version': self.version,
      'procs': [self.procedure_desc(k)
        for k in self.urls.iterkeys()
          if self.urls[k] != self.describe]}

  def describe(self, request):
    return self.service_desc()

jsonrpc_site = JSONRPCSite()
<commit_msg>Make sure id gets assigned before raising an exception in apply_version if the response is an Error<commit_after>import datetime, decimal
from functools import wraps
from uuid import uuid1
from jsonrpc._json import loads, dumps
from jsonrpc.exceptions import *
from jsonrpc.types import *
from django.core import signals
empty_dec = lambda f: f
try:
  from django.views.decorators.csrf import csrf_exempt
except (NameError, ImportError):
  csrf_exempt = empty_dec

from django.core.serializers.json import DjangoJSONEncoder

NoneType = type(None)
encode_kw = lambda p: dict([(str(k), v) for k, v in p.iteritems()])

def encode_kw11(p):
  if not type(p) is dict:
    return {}
  ret = p.copy()
  removes = []
  for k, v in ret.iteritems():
    try:
      int(k)
    except ValueError:
      pass
    else:
      removes.append(k)
  for k in removes:
    ret.pop(k)
  return ret

def encode_arg11(p):
  if type(p) is list:
    return p
  elif not type(p) is dict:
    return []
  else:
    pos = []
    d = encode_kw(p)
    for k, v in d.iteritems():
      try:
        pos.append(int(k))
      except ValueError:
        pass
    pos = list(set(pos))
    pos.sort()
    return [d[str(i)] for i in pos]

def validate_params(method, D):
  if type(D['params']) == Object:
    keys = method.json_arg_types.keys()
    if len(keys) != len(D['params']):
      raise InvalidParamsError('Not eough params provided for %s' % method.json_sig)
    for k in keys:
      if not k in D['params']:
        raise InvalidParamsError('%s is not a valid parameter for %s'
                                 % (k, method.json_sig))
      if not Any.kind(D['params'][k]) == method.json_arg_types[k]:
        raise InvalidParamsError('%s is not the correct type %s for %s'
          % (type(D['params'][k]), method.json_arg_types[k], method.json_sig))
  elif type(D['params']) == Array:
    arg_types = method.json_arg_types.values()
    try:
      for i, arg in enumerate(D['params']):
        if not Any.kind(arg) == arg_types[i]:
          raise InvalidParamsError('%s is not the correct type %s for %s'
            % (type(arg), arg_types[i], method.json_sig))
    except IndexError:
      raise InvalidParamsError('Too many params provided for %s' % method.json_sig)
    else:
      if len(D['params']) != len(arg_types):
        raise InvalidParamsError('Not enouh params provided for %s' % method.json_sig)

class JSONRPCSite(object):
  "A JSON-RPC Site"
  def __init__(self, json_encoder=DjangoJSONEncoder):
    self.urls = {}
    self.uuid = str(uuid1())
    self.version = '1.0'
    self.name = 'django-json-rpc'
    self.register('system.describe', self.describe)
    self.set_json_encoder(json_encoder)

  def set_json_encoder(self, json_encoder=DjangoJSONEncoder):
    self.json_encoder = json_encoder

  def register(self, name, method):
    self.urls[unicode(name)] = method

  def empty_response(self, version='1.0'):
    resp = {'id': None}
    if version == '1.1':
      resp['version'] = version
      return resp
    if version == '2.0':
      resp['jsonrpc'] = version
    resp.update({'error': None, 'result': None})
    return resp

  def validate_get(self, request, method):
    encode_get_params = lambda r: dict([(k, v[0] if len(v) == 1 else v)
                                         for k, v in r])
    if request.method == 'GET':
      method = unicode(method)
      if method in self.urls and getattr(self.urls[method], 'json_safe', False):
        D = {
          'params': encode_get_params(request.GET.lists()),
          'method': method,
          'id': 'jsonrpc',
          'version': '1.1'
        }
        return True, D
    return False, {}

  def response_dict(self, request, D, is_batch=False, version_hint='1.0', json_encoder=None):
    json_encoder = json_encoder or self.json_encoder
    version = version_hint
    response = self.empty_response(version=version)
    apply_version = {'2.0': lambda f, r, p: f(r, **encode_kw(p)) if type(p) is dict else f(r, *p),
                     '1.1': lambda f, r, p: f(r, *encode_arg11(p), **encode_kw(encode_kw11(p))),
                     '1.0': lambda f, r, p: f(r, *p)}

    try:
      # params: An Array or Object, that holds the actual parameter values
      # for the invocation of the procedure. Can be omitted if empty.
      if 'params' not in D:
         D['params'] = []
      if 'method' not in D or 'params' not in D:
        raise InvalidParamsError('Request requires str:"method" and list:"params"')
      if D['method'] not in self.urls:
        raise MethodNotFoundError('Method not found. Available methods: %s' % (
                        '\n'.join(self.urls.keys())))

      if 'jsonrpc' in D:
        if str(D['jsonrpc']) not in apply_version:
          raise InvalidRequestError('JSON-RPC version %s not supported.' % D['jsonrpc'])
        version = request.jsonrpc_version = response['jsonrpc'] = str(D['jsonrpc'])
      elif 'version' in D:
        if str(D['version']) not in apply_version:
          raise InvalidRequestError('JSON-RPC version %s not supported.' % D['version'])
        version = request.jsonrpc_version = response['version'] = str(D['version'])
      else:
        request.jsonrpc_version = '1.0'

      method = self.urls[str(D['method'])]
      if getattr(method, 'json_validate', False):
        validate_params(method, D)

      if 'id' in D and D['id'] is not None: # regular request
        response['id'] = D['id']
        if version in ('1.1', '2.0') and 'error' in response:
          response.pop('error')
      elif is_batch: # notification, not ok in a batch format, but happened anyway
        raise InvalidRequestError
      else: # notification
        return None, 204

      R = apply_version[version](method, request, D['params'])

      encoder = json_encoder()
      if not sum(map(lambda e: isinstance(R, e), # type of `R` should be one of these or...
         (dict, str, unicode, int, long, list, set, NoneType, bool))):
        try:
          rs = encoder.default(R) # ...or something this thing supports
        except TypeError, exc:
          raise TypeError("Return type not supported, for %r" % R)

      response['result'] = R

      status = 200

    except Error, e:
      signals.got_request_exception.send(sender=self.__class__, request=request)
      response['error'] = e.json_rpc_format
      if version in ('1.1', '2.0') and 'result' in response:
        response.pop('result')
      status = e.status
    except Exception, e:
      # exception missed by others
      signals.got_request_exception.send(sender=self.__class__, request=request)
      other_error = OtherError(e)
      response['error'] = other_error.json_rpc_format
      status = other_error.status
      if version in ('1.1', '2.0') and 'result' in response:
        response.pop('result')

    # Exactly one of result or error MUST be specified. It's not
    # allowed to specify both or none.
    if version in ('1.1', '2.0') and 'error' in response and not response['error']:
      response.pop('error')

    return response, status

  @csrf_exempt
  def dispatch(self, request, method='', json_encoder=None):
    from django.http import HttpResponse
    json_encoder = json_encoder or self.json_encoder

    try:
      # in case we do something json doesn't like, we always get back valid json-rpc response
      response = self.empty_response()
      if request.method.lower() == 'get':
        valid, D = self.validate_get(request, method)
        if not valid:
          raise InvalidRequestError('The method you are trying to access is '
                                    'not availble by GET requests')
      elif not request.method.lower() == 'post':
        raise RequestPostError
      else:
        try:
          D = loads(request.raw_post_data)
        except:
          raise InvalidRequestError

      if type(D) is list:
        response = [self.response_dict(request, d, is_batch=True, json_encoder=json_encoder)[0] for d in D]
        status = 200
      else:
        response, status = self.response_dict(request, D, json_encoder=json_encoder)
        if response is None and (not u'id' in D or D[u'id'] is None): # a notification
          return HttpResponse('', status=status)

      json_rpc = dumps(response, cls=json_encoder)
    except Error, e:
      signals.got_request_exception.send(sender=self.__class__, request=request)
      response['error'] = e.json_rpc_format
      status = e.status
      json_rpc = dumps(response, cls=json_encoder)
    except Exception, e:
      # exception missed by others
      signals.got_request_exception.send(sender=self.__class__, request=request)
      other_error = OtherError(e)
      response['result'] = None
      response['error'] = other_error.json_rpc_format
      status = other_error.status

      json_rpc = dumps(response,cls=json_encoder)

    return HttpResponse(json_rpc, status=status, content_type='application/json-rpc')

  def procedure_desc(self, key):
    M = self.urls[key]
    return {
      'name': M.json_method,
      'summary': M.__doc__,
      'idempotent': M.json_safe,
      'params': [{'type': str(Any.kind(t)), 'name': k}
        for k, t in M.json_arg_types.iteritems()],
      'return': {'type': M.json_return_type}}

  def service_desc(self):
    return {
      'sdversion': '1.0',
      'name': self.name,
      'id': 'urn:uuid:%s' % str(self.uuid),
      'summary': self.__doc__,
      'version': self.version,
      'procs': [self.procedure_desc(k)
        for k in self.urls.iterkeys()
          if self.urls[k] != self.describe]}

  def describe(self, request):
    return self.service_desc()

jsonrpc_site = JSONRPCSite()
<|endoftext|>


<commit_before>import pytest
import requests

@pytest.fixture()
def jenkins_url():
    return "http://127.0.0.1:80"

def is_url_up(u):
    return requests.get(u).status_code == requests.codes.ok

def test_is_online(jenkins_url):
    assert is_url_up(jenkins_url)
<commit_msg>Add timeout<commit_after>import pytest
import requests

@pytest.fixture()
def jenkins_url():
    return "http://127.0.0.1/api/json"

def test_is_online(jenkins_url):
    requests.get(jenkins_url, timeout=5).raise_for_status()
<|endoftext|>


<commit_before>""" JSON Web Token implementation

Minimum implementation based on this spec:
http://self-issued.info/docs/draft-jones-json-web-token-01.html
"""
from __future__ import unicode_literals
import base64
import binascii
import hashlib
import hmac
import sys

from datetime import datetime
from calendar import timegm
from collections import Mapping

try:
    import json
except ImportError:
    import simplejson as json

if sys.version_info >= (3, 0, 0):
    unicode = str
    basestring = str

__version__ = '0.2.3'
__all__ = ['encode', 'decode', 'DecodeError']

class DecodeError(Exception):
    pass

class ExpiredSignature(Exception):
    pass

class InvalidAudience(Exception):
    pass

class InvalidIssuer(Exception):
    pass

signing_methods = {
    'none': lambda msg, key: b'',
    'HS256': lambda msg, key: hmac.new(key, msg, hashlib.sha256).digest(),
    'HS384': lambda msg, key: hmac.new(key, msg, hashlib.sha384).digest(),
    'HS512': lambda msg, key: hmac.new(key, msg, hashlib.sha512).digest()
}

verify_methods = {
    'HS256': lambda msg, key: hmac.new(key, msg, hashlib.sha256).digest(),
    'HS384': lambda msg, key: hmac.new(key, msg, hashlib.sha384).digest(),
    'HS512': lambda msg, key: hmac.new(key, msg, hashlib.sha512).digest()
}

def prepare_HS_key(key):
    if not isinstance(key, basestring) and not isinstance(key, bytes):
        raise TypeError('Expecting a string- or bytes-formatted key.')

    if isinstance(key, unicode):
        key = key.encode('utf-8')

    return key

prepare_key_methods = {
    'none': lambda key: None,
    'HS256': prepare_HS_key,
    'HS384': prepare_HS_key,
    'HS512': prepare_HS_key
}

try:
    from Crypto.Signature import PKCS1_v1_5
    from Crypto.Hash import SHA256
    from Crypto.Hash import SHA384
    from Crypto.Hash import SHA512
    from Crypto.PublicKey import RSA

    signing_methods.update({
        'RS256': lambda msg, key: PKCS1_v1_5.new(key).sign(SHA256.new(msg)),
        'RS384': lambda msg, key: PKCS1_v1_5.new(key).sign(SHA384.new(msg)),
        'RS512': lambda msg, key: PKCS1_v1_5.new(key).sign(SHA512.new(msg))
    })

    verify_methods.update({
        'RS256': lambda msg, key, sig: PKCS1_v1_5.new(key).verify(SHA256.new(msg), sig),
        'RS384': lambda msg, key, sig: PKCS1_v1_5.new(key).verify(SHA384.new(msg), sig),
        'RS512': lambda msg, key, sig: PKCS1_v1_5.new(key).verify(SHA512.new(msg), sig)
    })

    def prepare_RS_key(key):
        if isinstance(key, RSA._RSAobj):
            return key

        if isinstance(key, basestring):
            if isinstance(key, unicode):
                key = key.encode('utf-8')

            key = RSA.importKey(key)
        else:
            raise TypeError('Expecting a PEM- or RSA-formatted key.')

        return key

    prepare_key_methods.update({
        'RS256': prepare_RS_key,
        'RS384': prepare_RS_key,
        'RS512': prepare_RS_key
    })

except ImportError:
    pass

try:
    import ecdsa
    from Crypto.Hash import SHA256
    from Crypto.Hash import SHA384
    from Crypto.Hash import SHA512

    signing_methods.update({
        'ES256': lambda msg, key: key.sign(msg, hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_der),
        'ES384': lambda msg, key: key.sign(msg, hashfunc=hashlib.sha384, sigencode=ecdsa.util.sigencode_der),
        'ES512': lambda msg, key: key.sign(msg, hashfunc=hashlib.sha512, sigencode=ecdsa.util.sigencode_der),
    })

    verify_methods.update({
        'ES256': lambda msg, key, sig: key.verify(sig, msg, hashfunc=hashlib.sha256, sigdecode=ecdsa.util.sigdecode_der),
        'ES384': lambda msg, key, sig: key.verify(sig, msg, hashfunc=hashlib.sha384, sigdecode=ecdsa.util.sigdecode_der),
        'ES512': lambda msg, key, sig: key.verify(sig, msg, hashfunc=hashlib.sha512, sigdecode=ecdsa.util.sigdecode_der),
    })

    def prepare_ES_key(key):
        if isinstance(key, ecdsa.SigningKey) or \
           isinstance(key, ecdsa.VerifyingKey):
            return key

        if isinstance(key, basestring):
            if isinstance(key, unicode):
                key = key.encode('utf-8')

            # Attempt to load key. We don't know if it's
            # a Signing Key or a Verifying Key, so we try
            # the Verifying Key first.
            try:
                key = ecdsa.VerifyingKey.from_pem(key)
            except ecdsa.der.UnexpectedDER:
                try:
                    key = ecdsa.SigningKey.from_pem(key)
                except:
                    raise
        else:
            raise TypeError('Expecting a PEM-formatted key.')

        return key

    prepare_key_methods.update({
        'ES256': prepare_ES_key,
        'ES384': prepare_ES_key,
        'ES512': prepare_ES_key
    })

except ImportError:
    pass

def constant_time_compare(val1, val2):
    """
    Returns True if the two strings are equal, False otherwise.

    The time taken is independent of the number of characters that match.
    """
    if len(val1) != len(val2):
        return False

    result = 0

    if sys.version_info >= (3, 0, 0):
        # Bytes are numbers
        for x, y in zip(val1, val2):
            result |= x ^ y
    else:
        for x, y in zip(val1, val2):
            result |= ord(x) ^ ord(y)

    return result == 0

def base64url_decode(input):
    rem = len(input) % 4

    if rem > 0:
        input += b'=' * (4 - rem)

    return base64.urlsafe_b64decode(input)

def base64url_encode(input):
    return base64.urlsafe_b64encode(input).replace(b'=', b'')

def header(jwt):
    header_segment = jwt.split(b'.', 1)[0]
    try:
        header_data = base64url_decode(header_segment).decode('utf-8')
        return json.loads(header_data)
    except (ValueError, TypeError):
        raise DecodeError('Invalid header encoding')

def encode(payload, key, algorithm='HS256', headers=None):
    segments = []

    if algorithm is None:
        algorithm = 'none'

    # Check that we get a mapping
    if not isinstance(payload, Mapping):
        raise TypeError('Expecting a mapping object, as json web token only'
                        'support json objects.')

    # Header
    header = {'typ': 'JWT', 'alg': algorithm}
    if headers:
        header.update(headers)

    json_header = json.dumps(header, separators=(',', ':')).encode('utf-8')
    segments.append(base64url_encode(json_header))

    # Payload
    for time_claim in ['exp', 'iat', 'nbf']:
        # Convert datetime to a intDate value in known time-format claims
        if isinstance(payload.get(time_claim), datetime):
            payload[time_claim] = timegm(payload[time_claim].utctimetuple())

    json_payload = json.dumps(payload, separators=(',', ':')).encode('utf-8')
    segments.append(base64url_encode(json_payload))

    # Segments
    signing_input = b'.'.join(segments)
    try:
        key = prepare_key_methods[algorithm](key)
        signature = signing_methods[algorithm](signing_input, key)
    except KeyError:
        raise NotImplementedError('Algorithm not supported')

    segments.append(base64url_encode(signature))

    return b'.'.join(segments)

def decode(jwt, key='', verify=True, **kwargs):
    payload, signing_input, header, signature = load(jwt)

    if verify:
        verify_expiration = kwargs.pop('verify_expiration', True)
        leeway = kwargs.pop('leeway', 0)

        verify_signature(payload, signing_input, header, signature, key,
                         verify_expiration, leeway, **kwargs)

    return payload

def load(jwt):
    if isinstance(jwt, unicode):
        jwt = jwt.encode('utf-8')
    try:
        signing_input, crypto_segment = jwt.rsplit(b'.', 1)
        header_segment, payload_segment = signing_input.split(b'.', 1)
    except ValueError:
        raise DecodeError('Not enough segments')

    try:
        header_data = base64url_decode(header_segment)
    except (TypeError, binascii.Error):
        raise DecodeError('Invalid header padding')
    try:
        header = json.loads(header_data.decode('utf-8'))
    except ValueError as e:
        raise DecodeError('Invalid header string: %s' % e)

    try:
        payload_data = base64url_decode(payload_segment)
    except (TypeError, binascii.Error):
        raise DecodeError('Invalid payload padding')
    try:
        payload = json.loads(payload_data.decode('utf-8'))
    except ValueError as e:
        raise DecodeError('Invalid payload string: %s' % e)

    try:
        signature = base64url_decode(crypto_segment)
    except (TypeError, binascii.Error):
        raise DecodeError('Invalid crypto padding')

    return (payload, signing_input, header, signature)

def verify_signature(payload, signing_input, header, signature, key='',
                     verify_expiration=True, leeway=0, **kwargs):
    try:
        algorithm = header['alg'].upper()
        key = prepare_key_methods[algorithm](key)

        if algorithm.startswith('HS'):
            expected = verify_methods[algorithm](signing_input, key)

            if not constant_time_compare(signature, expected):
                raise DecodeError('Signature verification failed')
        else:
            if not verify_methods[algorithm](signing_input, key, signature):
                raise DecodeError('Signature verification failed')
    except KeyError:
        raise DecodeError('Algorithm not supported')

    if 'nbf' in payload and verify_expiration:
        utc_timestamp = timegm(datetime.utcnow().utctimetuple())

        if payload['nbf'] > (utc_timestamp + leeway):
            raise ExpiredSignature('Signature not yet valid')

    if 'exp' in payload and verify_expiration:
        utc_timestamp = timegm(datetime.utcnow().utctimetuple())

        if payload['exp'] < (utc_timestamp - leeway):
            raise ExpiredSignature('Signature has expired')

    audience = kwargs.get('audience')

    if audience:
        if isinstance(audience, list):
            audiences = audience
        else:
            audiences = [audience]

        if payload.get('aud') not in audiences:
            raise InvalidAudience('Invalid audience')

    issuer = kwargs.get('issuer')

    if issuer:
        if payload.get('iss') != issuer:
            raise InvalidIssuer('Invalid issuer')
<commit_msg>Bump up version for release<commit_after>""" JSON Web Token implementation

Minimum implementation based on this spec:
http://self-issued.info/docs/draft-jones-json-web-token-01.html
"""
from __future__ import unicode_literals
import base64
import binascii
import hashlib
import hmac
import sys

from datetime import datetime
from calendar import timegm
from collections import Mapping

try:
    import json
except ImportError:
    import simplejson as json

if sys.version_info >= (3, 0, 0):
    unicode = str
    basestring = str

__version__ = '0.3.0'
__all__ = ['encode', 'decode', 'DecodeError']

class DecodeError(Exception):
    pass

class ExpiredSignature(Exception):
    pass

class InvalidAudience(Exception):
    pass

class InvalidIssuer(Exception):
    pass

signing_methods = {
    'none': lambda msg, key: b'',
    'HS256': lambda msg, key: hmac.new(key, msg, hashlib.sha256).digest(),
    'HS384': lambda msg, key: hmac.new(key, msg, hashlib.sha384).digest(),
    'HS512': lambda msg, key: hmac.new(key, msg, hashlib.sha512).digest()
}

verify_methods = {
    'HS256': lambda msg, key: hmac.new(key, msg, hashlib.sha256).digest(),
    'HS384': lambda msg, key: hmac.new(key, msg, hashlib.sha384).digest(),
    'HS512': lambda msg, key: hmac.new(key, msg, hashlib.sha512).digest()
}

def prepare_HS_key(key):
    if not isinstance(key, basestring) and not isinstance(key, bytes):
        raise TypeError('Expecting a string- or bytes-formatted key.')

    if isinstance(key, unicode):
        key = key.encode('utf-8')

    return key

prepare_key_methods = {
    'none': lambda key: None,
    'HS256': prepare_HS_key,
    'HS384': prepare_HS_key,
    'HS512': prepare_HS_key
}

try:
    from Crypto.Signature import PKCS1_v1_5
    from Crypto.Hash import SHA256
    from Crypto.Hash import SHA384
    from Crypto.Hash import SHA512
    from Crypto.PublicKey import RSA

    signing_methods.update({
        'RS256': lambda msg, key: PKCS1_v1_5.new(key).sign(SHA256.new(msg)),
        'RS384': lambda msg, key: PKCS1_v1_5.new(key).sign(SHA384.new(msg)),
        'RS512': lambda msg, key: PKCS1_v1_5.new(key).sign(SHA512.new(msg))
    })

    verify_methods.update({
        'RS256': lambda msg, key, sig: PKCS1_v1_5.new(key).verify(SHA256.new(msg), sig),
        'RS384': lambda msg, key, sig: PKCS1_v1_5.new(key).verify(SHA384.new(msg), sig),
        'RS512': lambda msg, key, sig: PKCS1_v1_5.new(key).verify(SHA512.new(msg), sig)
    })

    def prepare_RS_key(key):
        if isinstance(key, RSA._RSAobj):
            return key

        if isinstance(key, basestring):
            if isinstance(key, unicode):
                key = key.encode('utf-8')

            key = RSA.importKey(key)
        else:
            raise TypeError('Expecting a PEM- or RSA-formatted key.')

        return key

    prepare_key_methods.update({
        'RS256': prepare_RS_key,
        'RS384': prepare_RS_key,
        'RS512': prepare_RS_key
    })

except ImportError:
    pass

try:
    import ecdsa
    from Crypto.Hash import SHA256
    from Crypto.Hash import SHA384
    from Crypto.Hash import SHA512

    signing_methods.update({
        'ES256': lambda msg, key: key.sign(msg, hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_der),
        'ES384': lambda msg, key: key.sign(msg, hashfunc=hashlib.sha384, sigencode=ecdsa.util.sigencode_der),
        'ES512': lambda msg, key: key.sign(msg, hashfunc=hashlib.sha512, sigencode=ecdsa.util.sigencode_der),
    })

    verify_methods.update({
        'ES256': lambda msg, key, sig: key.verify(sig, msg, hashfunc=hashlib.sha256, sigdecode=ecdsa.util.sigdecode_der),
        'ES384': lambda msg, key, sig: key.verify(sig, msg, hashfunc=hashlib.sha384, sigdecode=ecdsa.util.sigdecode_der),
        'ES512': lambda msg, key, sig: key.verify(sig, msg, hashfunc=hashlib.sha512, sigdecode=ecdsa.util.sigdecode_der),
    })

    def prepare_ES_key(key):
        if isinstance(key, ecdsa.SigningKey) or \
           isinstance(key, ecdsa.VerifyingKey):
            return key

        if isinstance(key, basestring):
            if isinstance(key, unicode):
                key = key.encode('utf-8')

            # Attempt to load key. We don't know if it's
            # a Signing Key or a Verifying Key, so we try
            # the Verifying Key first.
            try:
                key = ecdsa.VerifyingKey.from_pem(key)
            except ecdsa.der.UnexpectedDER:
                try:
                    key = ecdsa.SigningKey.from_pem(key)
                except:
                    raise
        else:
            raise TypeError('Expecting a PEM-formatted key.')

        return key

    prepare_key_methods.update({
        'ES256': prepare_ES_key,
        'ES384': prepare_ES_key,
        'ES512': prepare_ES_key
    })

except ImportError:
    pass

def constant_time_compare(val1, val2):
    """
    Returns True if the two strings are equal, False otherwise.

    The time taken is independent of the number of characters that match.
    """
    if len(val1) != len(val2):
        return False

    result = 0

    if sys.version_info >= (3, 0, 0):
        # Bytes are numbers
        for x, y in zip(val1, val2):
            result |= x ^ y
    else:
        for x, y in zip(val1, val2):
            result |= ord(x) ^ ord(y)

    return result == 0

def base64url_decode(input):
    rem = len(input) % 4

    if rem > 0:
        input += b'=' * (4 - rem)

    return base64.urlsafe_b64decode(input)

def base64url_encode(input):
    return base64.urlsafe_b64encode(input).replace(b'=', b'')

def header(jwt):
    header_segment = jwt.split(b'.', 1)[0]
    try:
        header_data = base64url_decode(header_segment).decode('utf-8')
        return json.loads(header_data)
    except (ValueError, TypeError):
        raise DecodeError('Invalid header encoding')

def encode(payload, key, algorithm='HS256', headers=None):
    segments = []

    if algorithm is None:
        algorithm = 'none'

    # Check that we get a mapping
    if not isinstance(payload, Mapping):
        raise TypeError('Expecting a mapping object, as json web token only'
                        'support json objects.')

    # Header
    header = {'typ': 'JWT', 'alg': algorithm}
    if headers:
        header.update(headers)

    json_header = json.dumps(header, separators=(',', ':')).encode('utf-8')
    segments.append(base64url_encode(json_header))

    # Payload
    for time_claim in ['exp', 'iat', 'nbf']:
        # Convert datetime to a intDate value in known time-format claims
        if isinstance(payload.get(time_claim), datetime):
            payload[time_claim] = timegm(payload[time_claim].utctimetuple())

    json_payload = json.dumps(payload, separators=(',', ':')).encode('utf-8')
    segments.append(base64url_encode(json_payload))

    # Segments
    signing_input = b'.'.join(segments)
    try:
        key = prepare_key_methods[algorithm](key)
        signature = signing_methods[algorithm](signing_input, key)
    except KeyError:
        raise NotImplementedError('Algorithm not supported')

    segments.append(base64url_encode(signature))

    return b'.'.join(segments)

def decode(jwt, key='', verify=True, **kwargs):
    payload, signing_input, header, signature = load(jwt)

    if verify:
        verify_expiration = kwargs.pop('verify_expiration', True)
        leeway = kwargs.pop('leeway', 0)

        verify_signature(payload, signing_input, header, signature, key,
                         verify_expiration, leeway, **kwargs)

    return payload

def load(jwt):
    if isinstance(jwt, unicode):
        jwt = jwt.encode('utf-8')
    try:
        signing_input, crypto_segment = jwt.rsplit(b'.', 1)
        header_segment, payload_segment = signing_input.split(b'.', 1)
    except ValueError:
        raise DecodeError('Not enough segments')

    try:
        header_data = base64url_decode(header_segment)
    except (TypeError, binascii.Error):
        raise DecodeError('Invalid header padding')
    try:
        header = json.loads(header_data.decode('utf-8'))
    except ValueError as e:
        raise DecodeError('Invalid header string: %s' % e)

    try:
        payload_data = base64url_decode(payload_segment)
    except (TypeError, binascii.Error):
        raise DecodeError('Invalid payload padding')
    try:
        payload = json.loads(payload_data.decode('utf-8'))
    except ValueError as e:
        raise DecodeError('Invalid payload string: %s' % e)

    try:
        signature = base64url_decode(crypto_segment)
    except (TypeError, binascii.Error):
        raise DecodeError('Invalid crypto padding')

    return (payload, signing_input, header, signature)

def verify_signature(payload, signing_input, header, signature, key='',
                     verify_expiration=True, leeway=0, **kwargs):
    try:
        algorithm = header['alg'].upper()
        key = prepare_key_methods[algorithm](key)

        if algorithm.startswith('HS'):
            expected = verify_methods[algorithm](signing_input, key)

            if not constant_time_compare(signature, expected):
                raise DecodeError('Signature verification failed')
        else:
            if not verify_methods[algorithm](signing_input, key, signature):
                raise DecodeError('Signature verification failed')
    except KeyError:
        raise DecodeError('Algorithm not supported')

    if 'nbf' in payload and verify_expiration:
        utc_timestamp = timegm(datetime.utcnow().utctimetuple())

        if payload['nbf'] > (utc_timestamp + leeway):
            raise ExpiredSignature('Signature not yet valid')

    if 'exp' in payload and verify_expiration:
        utc_timestamp = timegm(datetime.utcnow().utctimetuple())

        if payload['exp'] < (utc_timestamp - leeway):
            raise ExpiredSignature('Signature has expired')

    audience = kwargs.get('audience')

    if audience:
        if isinstance(audience, list):
            audiences = audience
        else:
            audiences = [audience]

        if payload.get('aud') not in audiences:
            raise InvalidAudience('Invalid audience')

    issuer = kwargs.get('issuer')

    if issuer:
        if payload.get('iss') != issuer:
            raise InvalidIssuer('Invalid issuer')
<|endoftext|>


<commit_before>"""Useful functions."""

import numpy as _np

def polyfit(x, y, monomials):
    """Implement Custom polyfit."""
    coef = _np.polynomial.polynomial.polyfit(x, y, deg=monomials)

    # finds maximum diff and its base value
    y_fitted = _np.polynomial.polynomial.polyval(x, coef)
    y_diff = abs(y_fitted - y)
    idx = _np.argmax(y_diff)

    coeffs = coef[monomials]
    return (coeffs, (y_diff[idx], y[idx]))
<commit_msg>FUN.ENH: add method generate_random_numbers.<commit_after>"""Useful functions."""
from functools import partial as _partial
import numpy as _np

def polyfit(x, y, monomials):
    """Implement Custom polyfit."""
    coef = _np.polynomial.polynomial.polyfit(x, y, deg=monomials)

    # finds maximum diff and its base value
    y_fitted = _np.polynomial.polynomial.polyval(x, coef)
    y_diff = abs(y_fitted - y)
    idx = _np.argmax(y_diff)

    coeffs = coef[monomials]
    return (coeffs, (y_diff[idx], y[idx]))

def generate_random_numbers(n_part, dist_type='exp', cutoff=3):
    """Generate random numbers with a cutted off dist_type distribution.

    Inputs:
        n_part = size of the array with random numbers
        dist_type = assume values 'exponential', 'normal' or 'uniform'.
        cutoff = where to cut the distribution tail.
    """
    dist_type = dist_type.lower()
    if dist_type in 'exponential':
        func = _partial(_np.random.exponential, 1)
    elif dist_type in 'normal':
        func = _np.random.randn
    elif dist_type in 'uniform':
        func = _np.random.rand
    else:
        raise NotImplementedError('Distribution type not implemented yet.')

    numbers = func(n_part)
    above, *_ = _np.asarray(_np.abs(numbers) > cutoff).nonzero()
    while above.size:
        parts = func(above.size)
        indcs = _np.abs(parts) > cutoff
        numbers[above[~indcs]] = parts[~indcs]
        above = above[indcs]
    return numbers
<|endoftext|>


<commit_before>import os
import pickle
import sys

from discord.ext import commands
from lib import entity, item

PLAYERDATA = "important/playerdata.pickle"
class GameCog():
    def __init__(self, bot):
        self.bot = bot
        type(self).__name__ = "RPG"

    @commands.command(name='join', aliases=['enter', 'play'],
                      help="Join the RPG!",
                      brief="Creates a player for you so you could participate in the RPG")
    async def join(self, ctx):
        try:
            with open(PLAYERDATA, "rb") as opened_file:
                players = pickle.load(opened_file)
        except FileNotFoundError:
            players = {}
            await ctx.send(f"created new datafile {PLAYERDATA}")
            with open(PLAYERDATA, 'w'): pass
        except pickle.UnpicklingError:
            await ctx.send(f"file {PLAYERDATA} is corrupt, cannot fetch data.")
            return
        except Exception as e:
            await ctx.send(str(e))
        if players.get(ctx.author.id, False):
            await ctx.send(f"{ctx.author.name}, you have already joined!")
            return
        author_data = {
            "name": ctx.author.name,
            "id": ctx.author.id,
            "invsize": 10,
        }
        new_player = entity.Entity(author_data)
        players[ctx.author.id] = new_player
        with open(PLAYERDATA, 'wb') as opened_file:
            pickle.dump(players, opened_file)
        welcome_message = ("```diff\n"
                           f"+ Welcome to the game, {ctx.author.name}!\n"
                           f"For now, your stats are 0. I advise you upgrade them right away.\n"
                           f"Use {ctx.prefix}upgrade <stat> <amount> for that. "
                           f"I have given you 20 stat points\n"
                           f"The stats are: 'Health', 'Attack', 'Defense', 'Speed' and 'Luck'\n"
                           f"! Learn more using {ctx.prefix}about.\n"
                           f"- Good luck!\n"
                           "```")
        await ctx.send(welcome_message)

    @commands.command(name='debugadd', aliases=[],
                      help="Join the RPG!",
                      brief="Creates a player for you so you could participate in the RPG")
    async def debugadd(self, ctx, to_add):
        try:
            with open(PLAYERDATA, "rb") as opened_file:
                players = pickle.load(opened_file)
        except (pickle.UnpicklingError, FileNotFoundError):
            await ctx.send(f"file {PLAYERDATA} is corrupt, cannot fetch data.")
            return
        target_player = players.get(ctx.author.id)
        if target_player is None:
            await ctx.send(f"No such player.")
            return
        try:
            target_player.inv.add(to_add)
        except IndexError as e:
            await ctx.send(f"IndexError {e.args[0]}")
        except entity.ActionSuccesful as e:
            await ctx.send(f"entity.ActionSuccesful {e.args[0]}")

def setup(bot):
    bot.add_cog(GameCog(bot))<commit_msg>forgot to write new data to datafile<commit_after>import os
import pickle
import sys

from discord.ext import commands
from lib import entity, item

PLAYERDATA = "important/playerdata.pickle"
class GameCog():
    def __init__(self, bot):
        self.bot = bot
        type(self).__name__ = "RPG"

    @commands.command(name='join', aliases=['enter', 'play'],
                      help="Join the RPG!",
                      brief="Creates a player for you so you could participate in the RPG")
    async def join(self, ctx):
        try:
            with open(PLAYERDATA, "rb") as opened_file:
                players = pickle.load(opened_file)
        except FileNotFoundError:
            players = {}
            await ctx.send(f"created new datafile {PLAYERDATA}")
            with open(PLAYERDATA, 'w'): pass
        except pickle.UnpicklingError:
            await ctx.send(f"file {PLAYERDATA} is corrupt, cannot fetch data.")
            return
        except Exception as e:
            await ctx.send(str(e))
        if players.get(ctx.author.id, False):
            await ctx.send(f"{ctx.author.name}, you have already joined!")
            return
        author_data = {
            "name": ctx.author.name,
            "id": ctx.author.id,
            "invsize": 10,
        }
        new_player = entity.Entity(author_data)
        players[ctx.author.id] = new_player
        with open(PLAYERDATA, 'wb') as opened_file:
            pickle.dump(players, opened_file)
        welcome_message = ("```diff\n"
                           f"+ Welcome to the game, {ctx.author.name}!\n"
                           f"For now, your stats are 0. I advise you upgrade them right away.\n"
                           f"Use {ctx.prefix}upgrade <stat> <amount> for that. "
                           f"I have given you 20 stat points\n"
                           f"The stats are: 'Health', 'Attack', 'Defense', 'Speed' and 'Luck'\n"
                           f"! Learn more using {ctx.prefix}about.\n"
                           f"- Good luck!\n"
                           "```")
        await ctx.send(welcome_message)

    @commands.command(name='debugadd', aliases=[],
                      help="Join the RPG!",
                      brief="Creates a player for you so you could participate in the RPG")
    async def debugadd(self, ctx, to_add):
        try:
            with open(PLAYERDATA, "rb") as opened_file:
                players = pickle.load(opened_file)
        except (pickle.UnpicklingError, FileNotFoundError):
            await ctx.send(f"file {PLAYERDATA} is corrupt, cannot fetch data.")
            return
        target_player = players.get(ctx.author.id)
        if target_player is None:
            await ctx.send(f"No such player.")
            return
        try:
            target_player.inv.add(to_add)
        except IndexError as e:
            await ctx.send(f"IndexError {e.args[0]}")
        except entity.ActionSuccesful as e:
            await ctx.send(f"entity.ActionSuccesful {e.args[0]}")
        with open(PLAYERDATA, 'wb') as opened_file:
            pickle.dump(players, opened_file)

def setup(bot):
    bot.add_cog(GameCog(bot))<|endoftext|>


<commit_before>"""
https://en.wikipedia.org/wiki/Combination
"""
from math import factorial

def combinations(n: int, k: int) -> int:
    """
    Returns the number of different combinations of k length which can
    be made from n values, where n >= k.

    Examples:
    >>> combinations(10,5)
    252

    >>> combinations(6,3)
    20

    >>> combinations(20,5)
    15504

    >>> combinations(52, 5)
    2598960

    >>> combinations(0, 0)
    1

    >>> combinations(-4, -5)
    ...
    Traceback (most recent call last):
    ValueError: Please enter positive integers for n and k where n >= k
    """

    # If either of the conditions are true, the function is being asked
    # to calculate a factorial of a negative number, which is not possible
    if n < k or k < 0:
        raise ValueError("Please enter positive integers for n and k where n >= k")
    return int(factorial(n) / ((factorial(k)) * (factorial(n - k))))

if __name__ == "__main__":

    print(
        "\nThe number of five-card hands possible from a standard",
        f"fifty-two card deck is: {combinations(52, 5)}",
    )

    print(
        "\nIf a class of 40 students must be arranged into groups of",
        f"4 for group projects, there are {combinations(40, 4)} ways",
        "to arrange them.\n",
    )

    print(
        "If 10 teams are competing in a Formula One race, there",
        f"are {combinations(10, 3)} ways that first, second and",
        "third place can be awarded.\n",
    )
<commit_msg>Remove type cast in combinations algorithm (#7607)<commit_after>"""
https://en.wikipedia.org/wiki/Combination
"""
from math import factorial

def combinations(n: int, k: int) -> int:
    """
    Returns the number of different combinations of k length which can
    be made from n values, where n >= k.

    Examples:
    >>> combinations(10,5)
    252

    >>> combinations(6,3)
    20

    >>> combinations(20,5)
    15504

    >>> combinations(52, 5)
    2598960

    >>> combinations(0, 0)
    1

    >>> combinations(-4, -5)
    ...
    Traceback (most recent call last):
    ValueError: Please enter positive integers for n and k where n >= k
    """

    # If either of the conditions are true, the function is being asked
    # to calculate a factorial of a negative number, which is not possible
    if n < k or k < 0:
        raise ValueError("Please enter positive integers for n and k where n >= k")
    return factorial(n) // (factorial(k) * factorial(n - k))

if __name__ == "__main__":

    print(
        "The number of five-card hands possible from a standard",
        f"fifty-two card deck is: {combinations(52, 5)}\n",
    )

    print(
        "If a class of 40 students must be arranged into groups of",
        f"4 for group projects, there are {combinations(40, 4)} ways",
        "to arrange them.\n",
    )

    print(
        "If 10 teams are competing in a Formula One race, there",
        f"are {combinations(10, 3)} ways that first, second and",
        "third place can be awarded.",
    )
<|endoftext|>