<commit_before>__all__ = ['Message']

import sys
import traceback
from string import Template

class Message(object):
    """A log message.  All attributes are read-only."""

    __slots__ = ['fields', 'suppress_newlines', 'traceback', 'text']

    #: default option values. Don't change these!
    _default_options = {'suppress_newlines' : True,
                        'trace' : None,
                        'style': 'braces'}

    # XXX I need a __repr__!

    def __init__(self, level, format_spec, fields, options,
                 args, kwargs):
        """
        :arg LogLevel level: the level of the message
        :arg string format_spec: the human-readable message template. Should match the ``style`` in options.
        :arg dict fields: dictionary of fields for :ref:`structured logging <structured-logging>`
        :arg tuple args: substitution arguments for ``format_spec``.
        :arg dict kwargs: substitution keyword arguments for ``format_spec``.
        :arg dict options: a dictionary of :ref:`options <message-options>` to control message creation.
        """

        self.fields = fields
        self.suppress_newlines = options['suppress_newlines']
        self.fields['level'] = level

        ## format traceback
        # XXX this needs some cleanup/branch consolidation
        trace = options['trace']
        if isinstance(trace, tuple) and len(trace) == 3:
            self.traceback = "\n".join(traceback.format_exception(*trace))
        elif trace == "error":
            tb = sys.exc_info()
            if tb[0] is None:
                self.traceback = None
            else:
                self.traceback = traceback.format_exc()
        elif trace == "always":
            raise NotImplementedError
            # XXX build a traceback using getframe
            # XXX maybe an option to just provide current frame info instead of full stack?
        elif trace is not None:
            raise ValueError("bad trace {0!r}".format(trace))
        else:
            self.traceback = None

        style = options['style']
        aliases = {'{}':'braces', '$':'dollar', '%':'percent'}
        try:
            style = aliases[style]
        except KeyError:
            pass
        ## XXX maybe allow '%', '$', and '{}' as aliases?
        if style not in ('braces', 'percent', 'dollar'):
            raise ValueError("Bad format spec style {0!r}".format(style))

        ## Populate `text` by calling callables in `fields`, `args` and `kwargs`,
        ## and substituting into `format_spec`.

        ## call any callables
        for k, v in fields.iteritems():
            if callable(v):
                fields[k] = v()

        for k, v in kwargs.iteritems():
            if callable(v):
                kwargs[k] = v()

        args = tuple(v() if callable(v) else v for v in args)

        ## substitute
        if format_spec == '':
            self.text = ''
            return

        if style == 'braces':
            s = format_spec.format(*args, **kwargs)
        elif style == 'percent':
            # a % style format
            if args and kwargs:
                raise ValueError("can't have both args & kwargs with % style format specs")
            else:
                s = format_spec % (args or kwargs)
        elif style == 'dollar':
            if args:
                raise ValueError("can't use args with $ style format specs")
            s = Template(format_spec).substitute(kwargs)
        else:
            assert False, "impossible style"

        self.text = s

    @property
    def name(self):
        """Shortcut for ``fields['name']``. Empty string if no name."""
        return self.fields.get('name', '')

    @property
    def level(self):
        """Shortcut for ``fields['level']``"""
        return self.fields['level']
<commit_msg>Faster style alias implementation.<commit_after>__all__ = ['Message']

import sys
import traceback
from string import Template

class Message(object):
    """A log message.  All attributes are read-only."""

    __slots__ = ['fields', 'suppress_newlines', 'traceback', 'text']

    #: default option values. Don't change these!
    _default_options = {'suppress_newlines' : True,
                        'trace' : None,
                        'style': 'braces'}

    # XXX I need a __repr__!

    def __init__(self, level, format_spec, fields, options,
                 args, kwargs):
        """
        :arg LogLevel level: the level of the message
        :arg string format_spec: the human-readable message template. Should match the ``style`` in options.
        :arg dict fields: dictionary of fields for :ref:`structured logging <structured-logging>`
        :arg tuple args: substitution arguments for ``format_spec``.
        :arg dict kwargs: substitution keyword arguments for ``format_spec``.
        :arg dict options: a dictionary of :ref:`options <message-options>` to control message creation.
        """

        self.fields = fields
        self.suppress_newlines = options['suppress_newlines']
        self.fields['level'] = level

        ## format traceback
        # XXX this needs some cleanup/branch consolidation
        trace = options['trace']
        if isinstance(trace, tuple) and len(trace) == 3:
            self.traceback = "\n".join(traceback.format_exception(*trace))
        elif trace == "error":
            tb = sys.exc_info()
            if tb[0] is None:
                self.traceback = None
            else:
                self.traceback = traceback.format_exc()
        elif trace == "always":
            raise NotImplementedError
            # XXX build a traceback using getframe
            # XXX maybe an option to just provide current frame info instead of full stack?
        elif trace is not None:
            raise ValueError("bad trace {0!r}".format(trace))
        else:
            self.traceback = None

        style = options['style']

        style_aliases = {'braces':'braces', 'dollar':'dollar',
                'percent':'percent', '{}':'braces', '$':'dollar',
                '%':'percent'}
        try:
            style = style_aliases[style]
        except KeyError:
            raise ValueError("Bad format spec style {0!r}".format(style))

        ## Populate `text` by calling callables in `fields`, `args` and `kwargs`,
        ## and substituting into `format_spec`.

        ## call any callables
        for k, v in fields.iteritems():
            if callable(v):
                fields[k] = v()

        for k, v in kwargs.iteritems():
            if callable(v):
                kwargs[k] = v()

        args = tuple(v() if callable(v) else v for v in args)

        ## substitute
        if format_spec == '':
            self.text = ''
            return

        if style == 'braces':
            s = format_spec.format(*args, **kwargs)
        elif style == 'percent':
            # a % style format
            if args and kwargs:
                raise ValueError("can't have both args & kwargs with % style format specs")
            else:
                s = format_spec % (args or kwargs)
        elif style == 'dollar':
            if args:
                raise ValueError("can't use args with $ style format specs")
            s = Template(format_spec).substitute(kwargs)
        else:
            assert False, "impossible style"

        self.text = s

    @property
    def name(self):
        """Shortcut for ``fields['name']``. Empty string if no name."""
        return self.fields.get('name', '')

    @property
    def level(self):
        """Shortcut for ``fields['level']``"""
        return self.fields['level']
<|endoftext|>


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

import datetime
import time
import hashlib
import json

import requests

from pyelong.api import ApiSpec
from pyelong.response import Response

class Request(object):
    def __init__(self, user, app_key, secret_key,
                 host=ApiSpec.host,
                 version=ApiSpec.version,
                 local=ApiSpec.local,
                 debug=False):
        self.user = user
        self.app_key = app_key
        self.secret_key = secret_key

        self.host = host
        self.version = version
        self.local = local

        self.debug = debug

    def do(self, api, params, https, raw=False):
        self.timestamp = str(int(time.time()))
        self.data = self.build_data(params, raw)
        scheme = 'https' if https else 'http'
        url = "%s://%s" % (scheme, self.host)
        resp = Response(requests.get(url, params=self.build_params(api)))
        self._log('request:', resp.url)
        self._log("response:", resp)
        return resp

    def build_params(self, api):
        return {
            'method': api,
            'user': self.user,
            'timestamp': self.timestamp,
            'data': self.data,
            'signature': self.signature(),
            'format': 'json'  # 只支持 JSON
        }

    def build_data(self, params, raw=False):
        if not raw:
            data = {
                'Version': self.version,
                'Local': self.local,
                'Request': params
            }
        else:
            data = params
        return json.dumps(data, separators=(',', ':'))

    def signature(self):
        s = self._md5(self.data + self.app_key)
        return self._md5("%s%s%s" % (self.timestamp, s, self.secret_key))

    def _md5(self, data):
        return hashlib.md5(data).hexdigest()

    def _log(self, *args):
        if not self.debug:
            return
        prefix = '%s - pyelong -' % datetime.datetime.now()
        msg = '%s ' * len(args) % args
        print prefix, msg
<commit_msg>make thread-safe(?)<commit_after># -*- coding: utf-8 -*-

import datetime
import time
import hashlib
import json

import requests

from pyelong.api import ApiSpec
from pyelong.response import Response

class Request(object):
    def __init__(self, user, app_key, secret_key,
                 host=ApiSpec.host,
                 version=ApiSpec.version,
                 local=ApiSpec.local,
                 debug=False):
        self.user = user
        self.app_key = app_key
        self.secret_key = secret_key

        self.host = host
        self.version = version
        self.local = local

        self.debug = debug

    def do(self, api, params, https, raw=False):
        timestamp = str(int(time.time()))
        data = self.build_data(params, raw)
        scheme = 'https' if https else 'http'
        url = "%s://%s" % (scheme, self.host)
        params = {
            'method': api,
            'user': self.user,
            'timestamp': timestamp,
            'data': data,
            'signature': self.signature(data, timestamp),
            'format': 'json'    # 只支持 json
        }
        resp = Response(requests.get(url, params=params))
        self._log('request:', resp.url)
        self._log("response:", resp)
        return resp

    def build_data(self, params, raw=False):
        if not raw:
            data = {
                'Version': self.version,
                'Local': self.local,
                'Request': params
            }
        else:
            data = params
        return json.dumps(data, separators=(',', ':'))

    def signature(self, data, timestamp):
        s = self._md5(data + self.app_key)
        return self._md5("%s%s%s" % (timestamp, s, self.secret_key))

    def _md5(self, data):
        return hashlib.md5(data.encode('utf-8')).hexdigest()

    def _log(self, *args):
        if not self.debug:
            return
        prefix = '%s - pyelong -' % datetime.datetime.now()
        msg = '%s ' * len(args) % args
        print prefix, msg
<|endoftext|>


<commit_before>from django.db import models

# -- Twitter variable defs --
# user_name      | Do not trust, users can change it
# location  | Users location listed in profile
# user_id        | Trust this, users can never change it
# tweet_id       | ID of a given tweet
# real_name      | Users real name. Don't trust this, users can change it
# description    | description of a user by itself
# text           | text of actual tweet
# date           | date user/tweet was created/posted
# avatar         | href linking directly to users avatar
# language       | 2 char language code for user/tweet
# source         | Application used to create tweet
# link           | Direct href to origional tweet
# irt_status_id  | ID of tweet this is tweet is responding to
# irt_user_id    | ID of the user whos tweet this is tweet is responding to
# irt_user_name  | Name of the user whos tweet this is tweet is responding to
# retweet_count  | Number of times this tweet has been retweeted
# sentiment      | 0/P/N for neutral/positive/negative sentiment
# following      | Number of users a user is following
# followers      | Number of users following this user
# listed         | Number of lists including this user
# total_tweets   | Total number of tweets a user has posted
# last_updated   | last time this particular users info was updated
# geo_enabled    | Indicates user support for GPS tracking on tweets

SENTIMENT_CHOICES = (
        ('0','Neutral'),
        ('P','Positive'),
        ('N','Negative'),
)

class TwitterUser(models.Model):
    user_id = models.BigIntegerField(primary_key=True,unique=True)
    user_name = models.CharField(max_length=100,blank=True)
    real_name = models.CharField(max_length=100,blank=True)
    description = models.TextField(blank=True)
    location = models.CharField(max_length=100,blank=True)
    avatar = models.URLField(blank=True)
    date = models.DateTimeField(blank=True)
    language = models.CharField(max_length=2)
    total_tweets = models.IntegerField(blank=True)
    time_zone = models.CharField(max_length=100,blank=True)
    listed = models.IntegerField(blank=True,null=True)
    following = models.IntegerField(blank=True)
    followers = models.IntegerField(blank=True)
    geo_enabled = models.BooleanField()
    contributors_enabled = models.BooleanField()
    utc_offset = models.CharField(max_length=20,blank=True)
    last_modified = models.DateTimeField(auto_now=True,auto_now_add=True)
    def __unicode__(self):
        return self.user_name

class TwitterTweet(models.Model):
    user_id = models.ForeignKey('TwitterUser')
    contributors = models.TextField(blank=True,null=True)
    tweet_id = models.BigIntegerField(primary_key=True,unique=True)
    geo = models.CharField(max_length=250,blank=True,null=True)
    place = models.CharField(max_length=100,blank=True,null=True)
    text = models.TextField()
    date = models.DateTimeField()
    language = models.CharField(max_length=2,default='en')
    source = models.CharField(max_length=100,blank=True,null=True)
    irt_status_id = models.BigIntegerField(blank=True,null=True)
    irt_user_id = models.BigIntegerField(blank=True,null=True)
    irt_user_name = models.CharField(max_length=100,unique=True,blank=True,null=True)
    retweet_count = models.BigIntegerField(blank=True,null=True)
    sentiment = models.CharField(max_length=1,default='0',choices=SENTIMENT_CHOICES)
    truncated = models.BooleanField(default=0)
    last_modified = models.DateTimeField(auto_now=True,auto_now_add=True)
    class Meta:
        unique_together = (('text', 'user_id'))
    def __unicode__(self):
        return self.text[:20]

# vim: ai ts=4 sts=4 et sw=4
<commit_msg>realized users sometimes tweet the exact same thing more than once<commit_after>from django.db import models

# -- Twitter variable defs --
# user_name      | Do not trust, users can change it
# location  | Users location listed in profile
# user_id        | Trust this, users can never change it
# tweet_id       | ID of a given tweet
# real_name      | Users real name. Don't trust this, users can change it
# description    | description of a user by itself
# text           | text of actual tweet
# date           | date user/tweet was created/posted
# avatar         | href linking directly to users avatar
# language       | 2 char language code for user/tweet
# source         | Application used to create tweet
# link           | Direct href to origional tweet
# irt_status_id  | ID of tweet this is tweet is responding to
# irt_user_id    | ID of the user whos tweet this is tweet is responding to
# irt_user_name  | Name of the user whos tweet this is tweet is responding to
# retweet_count  | Number of times this tweet has been retweeted
# sentiment      | 0/P/N for neutral/positive/negative sentiment
# following      | Number of users a user is following
# followers      | Number of users following this user
# listed         | Number of lists including this user
# total_tweets   | Total number of tweets a user has posted
# last_updated   | last time this particular users info was updated
# geo_enabled    | Indicates user support for GPS tracking on tweets

SENTIMENT_CHOICES = (
        ('0','Neutral'),
        ('P','Positive'),
        ('N','Negative'),
)

class TwitterUser(models.Model):
    user_id = models.BigIntegerField(primary_key=True,unique=True)
    user_name = models.CharField(max_length=100,blank=True)
    real_name = models.CharField(max_length=100,blank=True)
    description = models.TextField(blank=True)
    location = models.CharField(max_length=100,blank=True)
    avatar = models.URLField(blank=True)
    date = models.DateTimeField(blank=True)
    language = models.CharField(max_length=2)
    total_tweets = models.IntegerField(blank=True)
    time_zone = models.CharField(max_length=100,blank=True)
    listed = models.IntegerField(blank=True,null=True)
    following = models.IntegerField(blank=True)
    followers = models.IntegerField(blank=True)
    geo_enabled = models.BooleanField()
    contributors_enabled = models.BooleanField()
    utc_offset = models.CharField(max_length=20,blank=True)
    last_modified = models.DateTimeField(auto_now=True,auto_now_add=True)
    def __unicode__(self):
        return self.user_name

class TwitterTweet(models.Model):
    user_id = models.ForeignKey('TwitterUser')
    contributors = models.TextField(blank=True,null=True)
    tweet_id = models.BigIntegerField(primary_key=True,unique=True)
    geo = models.CharField(max_length=250,blank=True,null=True)
    place = models.CharField(max_length=100,blank=True,null=True)
    text = models.TextField()
    date = models.DateTimeField()
    language = models.CharField(max_length=2,default='en')
    source = models.CharField(max_length=100,blank=True,null=True)
    irt_status_id = models.BigIntegerField(blank=True,null=True)
    irt_user_id = models.BigIntegerField(blank=True,null=True)
    irt_user_name = models.CharField(max_length=100,unique=True,blank=True,null=True)
    retweet_count = models.BigIntegerField(blank=True,null=True)
    sentiment = models.CharField(max_length=1,default='0',choices=SENTIMENT_CHOICES)
    truncated = models.BooleanField(default=0)
    last_modified = models.DateTimeField(auto_now=True,auto_now_add=True)
    def __unicode__(self):
        return self.text[:20]

# vim: ai ts=4 sts=4 et sw=4
<|endoftext|>


<commit_before>import serpent
import processblock as pb
import blocks as b
import transactions as t
import utils as u
import sys

k = u.sha3('cow')
v = u.privtoaddr(k)

k2 = u.sha3('horse')
v2 = u.privtoaddr(k2)

print "Starting boring transfer test"
blk = b.Block.genesis({ v: 10**18 })
# Give tx2 some money
tx = t.Transaction(0,10**16,10**12,1000,v2,'').sign(k)
pb.apply_tx(blk,tx)
print "New balance of v1: ", blk.get_balance(v)
print "New balance of v2: ", blk.get_balance(v2)

print "Starting namecoin tests"
blk = b.Block.genesis({ v: 10**18 })
scode1 = '''
if !contract.storage[msg.data[0]]:
    contract.storage[msg.data[0]] = msg.data[1]
    return(1)
else:
    return(0)
'''
code1 = serpent.compile(scode1)
print "AST", serpent.rewrite(serpent.parse(scode1))
print "Assembly", serpent.compile_to_assembly(scode1)
tx1 = t.Transaction.contract(0,0,10**12,1000,code1).sign(k)
addr = pb.apply_tx(blk,tx1)
snapshot = blk.snapshot()
print "Contract address", addr.encode('hex')
tx2 = t.Transaction(1,0,10**12,1000,addr,serpent.encode_datalist(['george',45]))
tx2.sign(k)
o = pb.apply_tx(blk,tx2)
print "Result of registering george:45: ", serpent.decode_datalist(o)
tx3 = t.Transaction(2,0,10**12,1000,addr,serpent.encode_datalist(['george',20])).sign(k)
o = pb.apply_tx(blk,tx3)
print "Result of registering george:20: ", serpent.decode_datalist(o)
tx4 = t.Transaction(3,0,10**12,1000,addr,serpent.encode_datalist(['harry',60])).sign(k)
o = pb.apply_tx(blk,tx4)
print "Result of registering harry:60: ", serpent.decode_datalist(o)

scode2 = '''
if !contract.storage[1000]:
    contract.storage[1000] = 1
    contract.storage[0x%s] = 1000
elif msg.datasize == 1:
    addr = msg.data[0]
    return(contract.storage[addr])
else:
    from = msg.sender
    fromvalue = contract.storage[from]
    to = msg.data[0]
    value = msg.data[1]
    if fromvalue >= value:
        contract.storage[from] = fromvalue - value
        contract.storage[to] = contract.storage[to] + value
        return(1)
    else:
        return(0)
''' % v
code2 = serpent.compile(scode2)
print "AST", serpent.rewrite(serpent.parse(scode2))
print "Assembly", serpent.compile_to_assembly(scode2)
print "Starting currency contract tests"
blk = b.Block.genesis({ v: 10**18 })
tx4 = t.Transaction.contract(0,0,10**12,1000,code2).sign(k)
addr = pb.apply_tx(blk,tx4)
print "Contract address", addr.encode('hex')
tx5 = t.Transaction(1,0,10**12,1000,addr,'').sign(k)
o = pb.apply_tx(blk,tx5)
print "Initialization finished"
tx6 = t.Transaction(2,0,10**12,1000,addr,serpent.encode_datalist([v2,200])).sign(k)
o = pb.apply_tx(blk,tx6)
print "Result of sending v->v2 200: ", serpent.decode_datalist(o)
tx7 = t.Transaction(3,0,10**12,1000,addr,serpent.encode_datalist([v2,900])).sign(k)
o = pb.apply_tx(blk,tx7)
print "Result of sending v->v2 900: ", serpent.decode_datalist(o)
tx8 = t.Transaction(4,0,10**12,1000,addr,serpent.encode_datalist([v])).sign(k)
o = pb.apply_tx(blk,tx8)
print "Result of querying v: ", serpent.decode_datalist(o)
tx9 = t.Transaction(5,0,10**12,1000,addr,serpent.encode_datalist([v2])).sign(k)
o = pb.apply_tx(blk,tx9)
print "Result of querying v2: ", serpent.decode_datalist(o)

scode3 = '''
if !contract.storage[1000]:
    contract.storage[1000] = 1
    contract.storage[1001] = msg.sender
    return(0)
elif msg.sender == contract.storage[1001] and msg.datasize == 2:
    contract.storage[msg.data[0]] = msg.data[1]
    return(1)
else:
    return(contract.storage[msg.data[0]])
'''
code3 = serpent.compile(scode3)
print "AST", serpent.rewrite(serpent.parse(scode3))
print "Assembly", serpent.compile_to_assembly(scode3)
blk = b.Block.genesis({ v: 10**18, v2: 10**18 })
tx10 = t.Transaction.contract(0,0,10**12,1000,code3).sign(k)
addr = pb.apply_tx(blk,tx10)
print "Address:", addr.encode('hex')
tx11 = t.Transaction(1,0,10**12,1000,addr,'').sign(k)
o = pb.apply_tx(blk,tx11)
print "Initialization complete", serpent.decode_datalist(o)
tx12 = t.Transaction(2,0,10**12,1000,addr,serpent.encode_datalist([500])).sign(k)
o = pb.apply_tx(blk,tx12)
print "Balance", serpent.decode_datalist(o)
tx13 = t.Transaction(3,0,10**12,1000,addr,serpent.encode_datalist([500,726])).sign(k)
o = pb.apply_tx(blk,tx13)
print "Set balance to 500:726", serpent.decode_datalist(o)
tx14 = t.Transaction(4,0,10**12,1000,addr,serpent.encode_datalist([500])).sign(k)
o = pb.apply_tx(blk,tx14)
print "Balance", serpent.decode_datalist(o)

scode4 = '''
if !contract.storage[1000]:
    contract.storage[1000] = msg.sender
    contract.storage[1002] = msg.value
    contract.storage[1003] = msg.data[0]
    return(1)
elif !contract.storage[1001]:
    ethvalue = contract.storage[1002]
    if msg.value >= ethvalue:
        contract.storage[1001] = msg.sender
    othervalue = ethvalue * msg(0x%s,0,tx.gas-100,[contract.storage[1003]],1)
    contract.storage[1004] = othervalue
    contract.storage[1005] = block.timestamp + 86400
    return([2,othervalue],2)
else:
    othervalue = contract.storage[1004]
    ethvalue = othervalue / msg(0x%s,0,tx.gas-100,[contract.storage[1003]],1)
    if ethvalue >= contract.balance:
        send(contract.storage[1000],contract.balance,tx.gas-100)
        return(3)
    elif block.timestamp > contract.storage[1005]:
        send(contract.storage[1001],contract.balance - ethvalue,tx.gas-100)
        send(contract.storage[1000],ethvalue,tx.gas-100)
        return(4)
    else:
        return(5)
''' % (addr.encode('hex'),addr.encode('hex'))
code4 = serpent.compile(scode4)
print "AST", serpent.rewrite(serpent.parse(scode4))
print "Assembly", serpent.compile_to_assembly(scode4)
# important: no new genesis block
tx15 = t.Transaction.contract(5,0,10**12,2000,code4).sign(k)
addr2 = pb.apply_tx(blk,tx15)
print "Address:", addr.encode('hex')
tx16 = t.Transaction(6,10**17,10**12,2000,addr2,serpent.encode_datalist([500])).sign(k)
o = pb.apply_tx(blk,tx16)
print "First participant added", serpent.decode_datalist(o)
tx17 = t.Transaction(0,10**17,10**12,2000,addr2,serpent.encode_datalist([500])).sign(k2)
o = pb.apply_tx(blk,tx17)
print "Second participant added, USDvalue settled", serpent.decode_datalist(o)
snapshot = blk.snapshot()
tx18 = t.Transaction(7,0,10**12,2000,addr2,'').sign(k)
o = pb.apply_tx(blk,tx18)
print "Attempting to cash out immediately", o if o == -1 else serpent.decode_datalist(o)
tx19 = t.Transaction(8,0,10**12,2000,addr,serpent.encode_datalist([500,300])).sign(k)
o = pb.apply_tx(blk,tx19)
print "Changing value to 300", serpent.decode_datalist(o)
print blk.to_dict()
tx20 = t.Transaction(9,0,10**12,2000,addr2,'').sign(k)
print "Old balances", blk.get_balance(v), blk.get_balance(v2)
o = pb.apply_tx(blk,tx20)
print "Attempting to cash out with value drop", o if o == -1 else serpent.decode_datalist(o)
print "New balances", blk.get_balance(v), blk.get_balance(v2)
blk.revert(snapshot)
blk.timestamp += 200000
tx21 = t.Transaction(7,0,10**12,2000,addr2,'').sign(k)
print "Old balances", blk.get_balance(v), blk.get_balance(v2)
o = pb.apply_tx(blk,tx21)
print "Attempting to cash out after expiry", o if o == -1 else serpent.decode_datalist(o)
print "New balances", blk.get_balance(v), blk.get_balance(v2)
<commit_msg>fix: adjust to new transaction signature<commit_after>import serpent
import processblock as pb
import blocks as b
import transactions as t
import utils as u
import sys

k = u.sha3('cow')
v = u.privtoaddr(k)

k2 = u.sha3('horse')
v2 = u.privtoaddr(k2)

print "Starting boring transfer test"
blk = b.genesis({ v: 10**18 })
# Give tx2 some money

gasprice = 10**12
startgas = 1000

# nonce,gasprice,startgas,to,value,data,v,r,s
tx = t.Transaction(0,gasprice,startgas,v2,10**16,'').sign(k)

pb.apply_tx(blk,tx)
print "New balance of v1: ", blk.get_balance(v)
print "New balance of v2: ", blk.get_balance(v2)

print "Starting namecoin tests"
blk = b.genesis({ v: 10**18 })
scode1 = '''
if !contract.storage[msg.data[0]]:
    contract.storage[msg.data[0]] = msg.data[1]
    return(1)
else:
    return(0)
'''
code1 = serpent.compile(scode1)
print "AST", serpent.rewrite(serpent.parse(scode1))
print "Assembly", serpent.compile_to_assembly(scode1)
tx1 = t.contract(0,0,gasprice,startgas,code1).sign(k)
s, addr = pb.apply_tx(blk,tx1)
snapshot = blk.snapshot()
print "Contract address", addr.encode('hex')
tx2 = t.Transaction(1,gasprice,startgas,addr,0,serpent.encode_datalist(['george',45]))
tx2.sign(k)
s,o = pb.apply_tx(blk,tx2)
print "Result of registering george:45: ", serpent.decode_datalist(o)
tx3 = t.Transaction(2,gasprice,startgas,addr,0,serpent.encode_datalist(['george',20])).sign(k)
s,o = pb.apply_tx(blk,tx3)
print "Result of registering george:20: ", serpent.decode_datalist(o)
tx4 = t.Transaction(3,gasprice,startgas,addr,0,serpent.encode_datalist(['harry',60])).sign(k)
s,o = pb.apply_tx(blk,tx4)
print "Result of registering harry:60: ", serpent.decode_datalist(o)

scode2 = '''
if !contract.storage[1000]:
    contract.storage[1000] = 1
    contract.storage[0x%s] = 1000
elif msg.datasize == 1:
    addr = msg.data[0]
    return(contract.storage[addr])
else:
    from = msg.sender
    fromvalue = contract.storage[from]
    to = msg.data[0]
    value = msg.data[1]
    if fromvalue >= value:
        contract.storage[from] = fromvalue - value
        contract.storage[to] = contract.storage[to] + value
        return(1)
    else:
        return(0)
''' % v
code2 = serpent.compile(scode2)
print "AST", serpent.rewrite(serpent.parse(scode2))
print "Assembly", serpent.compile_to_assembly(scode2)
print "Starting currency contract tests"
blk = b.genesis({ v: 10**18 })
tx4 = t.contract(0,0,gasprice,startgas,code2).sign(k)
s, addr = pb.apply_tx(blk,tx4)
print "Contract address", addr.encode('hex')
tx5 = t.Transaction(1,gasprice,startgas,addr,0,'').sign(k)
s,o = pb.apply_tx(blk,tx5)
print "Initialization finished"
tx6 = t.Transaction(2,gasprice,startgas,addr,0,serpent.encode_datalist([v2,200])).sign(k)
s,o = pb.apply_tx(blk,tx6)
print "Result of sending v->v2 200: ", serpent.decode_datalist(o)
tx7 = t.Transaction(3,gasprice,startgas,addr,0,serpent.encode_datalist([v2,900])).sign(k)
s,o = pb.apply_tx(blk,tx7)
print "Result of sending v->v2 900: ", serpent.decode_datalist(o)
tx8 = t.Transaction(4,gasprice,startgas,addr,0,serpent.encode_datalist([v])).sign(k)
s,o = pb.apply_tx(blk,tx8)
print "Result of querying v: ", serpent.decode_datalist(o)
tx9 = t.Transaction(5,gasprice,startgas,addr,0,serpent.encode_datalist([v2])).sign(k)
s,o = pb.apply_tx(blk,tx9)
print "Result of querying v2: ", serpent.decode_datalist(o)

scode3 = '''
if !contract.storage[1000]:
    contract.storage[1000] = 1
    contract.storage[1001] = msg.sender
    return(0)
elif msg.sender == contract.storage[1001] and msg.datasize == 2:
    contract.storage[msg.data[0]] = msg.data[1]
    return(1)
else:
    return(contract.storage[msg.data[0]])
'''
code3 = serpent.compile(scode3)
print "AST", serpent.rewrite(serpent.parse(scode3))
print "Assembly", serpent.compile_to_assembly(scode3)
blk = b.genesis({ v: 10**18, v2: 10**18 })
tx10 = t.contract(0,0,gasprice,startgas,code3).sign(k)
s, addr = pb.apply_tx(blk,tx10)
print "Address:", addr.encode('hex')
tx11 = t.Transaction(1,gasprice,startgas,addr,0,'').sign(k)
s,o = pb.apply_tx(blk,tx11)
print "Initialization complete", serpent.decode_datalist(o)
tx12 = t.Transaction(2,gasprice,startgas,addr,0,serpent.encode_datalist([500])).sign(k)
s,o = pb.apply_tx(blk,tx12)
print "Balance", serpent.decode_datalist(o)
tx13 = t.Transaction(3,gasprice,startgas,addr,0,serpent.encode_datalist([500,726])).sign(k)
s,o = pb.apply_tx(blk,tx13)
print "Set balance to 500:726", serpent.decode_datalist(o)
tx14 = t.Transaction(4,gasprice,startgas,addr,0,serpent.encode_datalist([500])).sign(k)
s,o = pb.apply_tx(blk,tx14)
print "Balance", serpent.decode_datalist(o)

scode4 = '''
if !contract.storage[1000]:
    contract.storage[1000] = msg.sender
    contract.storage[1002] = msg.value
    contract.storage[1003] = msg.data[0]
    return(1)
elif !contract.storage[1001]:
    ethvalue = contract.storage[1002]
    if msg.value >= ethvalue:
        contract.storage[1001] = msg.sender
    othervalue = ethvalue * msg(0x%s,0,tx.gas-100,[contract.storage[1003]],1)
    contract.storage[1004] = othervalue
    contract.storage[1005] = block.timestamp + 86400
    return([2,othervalue],2)
else:
    othervalue = contract.storage[1004]
    ethvalue = othervalue / msg(0x%s,0,tx.gas-100,[contract.storage[1003]],1)
    if ethvalue >= contract.balance:
        send(contract.storage[1000],contract.balance,tx.gas-100)
        return(3)
    elif block.timestamp > contract.storage[1005]:
        send(contract.storage[1001],contract.balance - ethvalue,tx.gas-100)
        send(contract.storage[1000],ethvalue,tx.gas-100)
        return(4)
    else:
        return(5)
''' % (addr.encode('hex'),addr.encode('hex'))
code4 = serpent.compile(scode4)
print "AST", serpent.rewrite(serpent.parse(scode4))
print "Assembly", serpent.compile_to_assembly(scode4)
# important: no new genesis block
tx15 = t.contract(5,0,gasprice,2000,code4).sign(k)
s, addr2 = pb.apply_tx(blk,tx15)
print "Address:", addr.encode('hex')
tx16 = t.Transaction(6,gasprice,2000,addr2,10**17,serpent.encode_datalist([500])).sign(k)
s,o = pb.apply_tx(blk,tx16)
print "First participant added", serpent.decode_datalist(o)
tx17 = t.Transaction(0,gasprice,2000,addr2,10**17,serpent.encode_datalist([500])).sign(k2)
s,o = pb.apply_tx(blk,tx17)
print "Second participant added, USDvalue settled", serpent.decode_datalist(o)
snapshot = blk.snapshot()
tx18 = t.Transaction(7,gasprice,2000,addr2,0,'').sign(k)
s,o = pb.apply_tx(blk,tx18)
print "Attempting to cash out immediately", o if o == -1 else serpent.decode_datalist(o)
tx19 = t.Transaction(8,gasprice,2000,addr,0,serpent.encode_datalist([500,300])).sign(k)
s,o = pb.apply_tx(blk,tx19)
print "Changing value to 300", serpent.decode_datalist(o)
print blk.to_dict()
tx20 = t.Transaction(9,gasprice,2000,addr2,0,'').sign(k)
print "Old balances", blk.get_balance(v), blk.get_balance(v2)
s,o = pb.apply_tx(blk,tx20)
print "Attempting to cash out with value drop", o if o == -1 else serpent.decode_datalist(o)
print "New balances", blk.get_balance(v), blk.get_balance(v2)
blk.revert(snapshot)
blk.timestamp += 200000
tx21 = t.Transaction(7,gasprice,2000,addr2,0,'').sign(k)
print "Old balances", blk.get_balance(v), blk.get_balance(v2)
s,o = pb.apply_tx(blk,tx21)
print "Attempting to cash out after expiry", o if o == -1 else serpent.decode_datalist(o)
print "New balances", blk.get_balance(v), blk.get_balance(v2)
<|endoftext|>


<commit_before>"""
    pyexcel_xls
    ~~~~~~~~~~~~~~~~~~~

    The lower level xls/xlsm file format handler using xlrd/xlwt

    :copyright: (c) 2015-2016 by Onni Software Ltd
    :license: New BSD License
"""
import sys
import math
import datetime
import xlrd
from xlwt import Workbook, XFStyle

from pyexcel_io.book import BookReader, BookWriter
from pyexcel_io.sheet import SheetReader, SheetWriter

PY2 = sys.version_info[0] == 2
if PY2 and sys.version_info[1] < 7:
    from ordereddict import OrderedDict
else:
    from collections import OrderedDict

DEFAULT_DATE_FORMAT = "DD/MM/YY"
DEFAULT_TIME_FORMAT = "HH:MM:SS"
DEFAULT_DATETIME_FORMAT = "%s %s" % (DEFAULT_DATE_FORMAT, DEFAULT_TIME_FORMAT)

def is_integer_ok_for_xl_float(value):
    """check if a float value had zero value in digits"""
    return value == math.floor(value)

def xldate_to_python_date(value):
    """
    convert xl date to python date
    """
    date_tuple = xlrd.xldate_as_tuple(value, 0)
    ret = None
    if date_tuple == (0, 0, 0, 0, 0, 0):
        ret = datetime.datetime(1900, 1, 1, 0, 0, 0)
    elif date_tuple[0:3] == (0, 0, 0):
        ret = datetime.time(date_tuple[3],
                            date_tuple[4],
                            date_tuple[5])
    elif date_tuple[3:6] == (0, 0, 0):
        ret = datetime.date(date_tuple[0],
                            date_tuple[1],
                            date_tuple[2])
    else:
        ret = datetime.datetime(
            date_tuple[0],
            date_tuple[1],
            date_tuple[2],
            date_tuple[3],
            date_tuple[4],
            date_tuple[5]
        )
    return ret

class XLSheet(SheetReader):
    """
    xls sheet

    Currently only support first sheet in the file
    """
    def __init__(self, sheet, auto_detect_int=True, **keywords):
        SheetReader.__init__(self, sheet, **keywords)
        self.auto_detect_int = auto_detect_int

    @property
    def name(self):
        return self.native_sheet.name

    def number_of_rows(self):
        """
        Number of rows in the xls sheet
        """
        return self.native_sheet.nrows

    def number_of_columns(self):
        """
        Number of columns in the xls sheet
        """
        return self.native_sheet.ncols

    def cell_value(self, row, column):
        """
        Random access to the xls cells
        """
        cell_type = self.native_sheet.cell_type(row, column)
        value = self.native_sheet.cell_value(row, column)
        if cell_type == xlrd.XL_CELL_DATE:
            value = xldate_to_python_date(value)
        elif cell_type == xlrd.XL_CELL_NUMBER and self.auto_detect_int:
            if is_integer_ok_for_xl_float(value):
                value = int(value)
        return value

    def to_array(self):
        for row in range(0, self.number_of_rows()):
            if self.skip_row(row, self.start_row, self.row_limit):
                continue

            return_row = []
            tmp_row = []
            for column in range(0, self.number_of_columns()):
                skip_column = self.skip_column(column,
                                               self.start_column,
                                               self.column_limit)
                if skip_column:
                    continue

                cell_value = self.cell_value(row, column)
                tmp_row.append(cell_value)
                if cell_value is not None and cell_value != '':
                    return_row += tmp_row
                    tmp_row = []
            yield return_row

class XLSBook(BookReader):
    """
    XLSBook reader

    It reads xls, xlsm, xlsx work book
    """
    def __init__(self):
        BookReader.__init__(self)
        self.file_content = None

    def open(self, file_name, **keywords):
        BookReader.open(self, file_name, **keywords)
        self._get_params()

    def open_stream(self, file_stream, **keywords):
        BookReader.open_stream(self, file_stream, **keywords)
        self._get_params()

    def open_content(self, file_content, **keywords):
        self.keywords = keywords
        self.file_content = file_content
        self._get_params()

    def close(self):
        if self.native_book:
            self.native_book.release_resources()

    def read_sheet_by_index(self, sheet_index):
        self.native_book = self._get_book(on_demand=True)
        sheet = self.native_book.sheet_by_index(sheet_index)
        return self.read_sheet(sheet)

    def read_sheet_by_name(self, sheet_name):
        self.native_book = self._get_book(on_demand=True)
        try:
            sheet = self.native_book.sheet_by_name(sheet_name)
        except xlrd.XLRDError:
            raise ValueError("%s cannot be found" % sheet_name)
        return self.read_sheet(sheet)

    def read_all(self):
        result = OrderedDict()
        self.native_book = self._get_book()
        for sheet in self.native_book.sheets():
            if self.skip_hidden_sheets and sheet.visibility != 0:
                continue
            data_dict = self.read_sheet(sheet)
            result.update(data_dict)
        return result

    def read_sheet(self, native_sheet):
        sheet = XLSheet(native_sheet, **self.keywords)
        return {sheet.name: sheet.to_array()}

    def _get_book(self, on_demand=False):
        if self.file_name:
            xls_book = xlrd.open_workbook(self.file_name, on_demand=on_demand)
        elif self.file_stream:
            xls_book = xlrd.open_workbook(
                None,
                file_contents=self.file_stream.getvalue(),
                on_demand=on_demand
            )
        elif self.file_content:
            xls_book = xlrd.open_workbook(
                None,
                file_contents=self.file_content,
                on_demand=on_demand
            )
        return xls_book

    def _get_params(self):
        self.skip_hidden_sheets = self.keywords.get('skip_hidden_sheets', True)

class XLSheetWriter(SheetWriter):
    """
    xls, xlsx and xlsm sheet writer
    """
    def set_sheet_name(self, name):
        """Create a sheet
        """
        self.native_sheet = self.native_book.add_sheet(name)
        self.current_row = 0

    def write_row(self, array):
        """
        write a row into the file
        """
        for i in range(0, len(array)):
            value = array[i]
            style = None
            tmp_array = []
            if isinstance(value, datetime.datetime):
                tmp_array = [
                    value.year, value.month, value.day,
                    value.hour, value.minute, value.second
                ]
                value = xlrd.xldate.xldate_from_datetime_tuple(tmp_array, 0)
                style = XFStyle()
                style.num_format_str = DEFAULT_DATETIME_FORMAT
            elif isinstance(value, datetime.date):
                tmp_array = [value.year, value.month, value.day]
                value = xlrd.xldate.xldate_from_date_tuple(tmp_array, 0)
                style = XFStyle()
                style.num_format_str = DEFAULT_DATE_FORMAT
            elif isinstance(value, datetime.time):
                tmp_array = [value.hour, value.minute, value.second]
                value = xlrd.xldate.xldate_from_time_tuple(tmp_array)
                style = XFStyle()
                style.num_format_str = DEFAULT_TIME_FORMAT
            if style:
                self.native_sheet.write(self.current_row, i, value, style)
            else:
                self.native_sheet.write(self.current_row, i, value)
        self.current_row += 1

class XLSWriter(BookWriter):
    """
    xls, xlsx and xlsm writer
    """
    def __init__(self):
        BookWriter.__init__(self)
        self.work_book = None

    def open(self, file_name,
             encoding='ascii', style_compression=2, **keywords):
        BookWriter.open(self, file_name, **keywords)
        self.work_book = Workbook(style_compression=style_compression,
                                  encoding=encoding)

    def create_sheet(self, name):
        return XLSheetWriter(self.work_book, None, name)

    def close(self):
        """
        This call actually save the file
        """
        self.work_book.save(self.file_alike_object)

_xls_reader_registry = {
    "file_type": "xls",
    "reader": XLSBook,
    "writer": XLSWriter,
    "stream_type": "binary",
    "mime_type": "application/vnd.ms-excel",
    "library": "pyexcel-xls"
}

_XLSM_MIME = (
    "application/" +
    "vnd.openxmlformats-officedocument.spreadsheetml.sheet")

_xlsm_registry = {
    "file_type": "xlsm",
    "reader": XLSBook,
    "stream_type": "binary",
    "mime_type": _XLSM_MIME,
    "library": "pyexcel-xls"
}

_xlsx_registry = {
    "file_type": "xlsx",
    "reader": XLSBook,
    "stream_type": "binary",
    "mime_type": "application/vnd.ms-excel.sheet.macroenabled.12",
    "library": "pyexcel-xls"
}

exports = (_xls_reader_registry,
           _xlsm_registry,
           _xlsx_registry)
<commit_msg>code optimization<commit_after>"""
    pyexcel_xls
    ~~~~~~~~~~~~~~~~~~~

    The lower level xls/xlsm file format handler using xlrd/xlwt

    :copyright: (c) 2015-2016 by Onni Software Ltd
    :license: New BSD License
"""
import sys
import math
import datetime
import xlrd
from xlwt import Workbook, XFStyle

from pyexcel_io.book import BookReader, BookWriter
from pyexcel_io.sheet import SheetReader, SheetWriter

PY2 = sys.version_info[0] == 2
if PY2 and sys.version_info[1] < 7:
    from ordereddict import OrderedDict
else:
    from collections import OrderedDict

DEFAULT_DATE_FORMAT = "DD/MM/YY"
DEFAULT_TIME_FORMAT = "HH:MM:SS"
DEFAULT_DATETIME_FORMAT = "%s %s" % (DEFAULT_DATE_FORMAT, DEFAULT_TIME_FORMAT)

def is_integer_ok_for_xl_float(value):
    """check if a float value had zero value in digits"""
    return value == math.floor(value)

def xldate_to_python_date(value):
    """
    convert xl date to python date
    """
    date_tuple = xlrd.xldate_as_tuple(value, 0)
    ret = None
    if date_tuple == (0, 0, 0, 0, 0, 0):
        ret = datetime.datetime(1900, 1, 1, 0, 0, 0)
    elif date_tuple[0:3] == (0, 0, 0):
        ret = datetime.time(date_tuple[3],
                            date_tuple[4],
                            date_tuple[5])
    elif date_tuple[3:6] == (0, 0, 0):
        ret = datetime.date(date_tuple[0],
                            date_tuple[1],
                            date_tuple[2])
    else:
        ret = datetime.datetime(
            date_tuple[0],
            date_tuple[1],
            date_tuple[2],
            date_tuple[3],
            date_tuple[4],
            date_tuple[5]
        )
    return ret

class XLSheet(SheetReader):
    """
    xls sheet

    Currently only support first sheet in the file
    """
    def __init__(self, sheet, auto_detect_int=True, **keywords):
        SheetReader.__init__(self, sheet, **keywords)
        self.auto_detect_int = auto_detect_int

    @property
    def name(self):
        return self.native_sheet.name

    def number_of_rows(self):
        """
        Number of rows in the xls sheet
        """
        return self.native_sheet.nrows

    def number_of_columns(self):
        """
        Number of columns in the xls sheet
        """
        return self.native_sheet.ncols

    def _cell_value(self, row, column):
        """
        Random access to the xls cells
        """
        cell_type = self.native_sheet.cell_type(row, column)
        value = self.native_sheet.cell_value(row, column)
        if cell_type == xlrd.XL_CELL_DATE:
            value = xldate_to_python_date(value)
        elif cell_type == xlrd.XL_CELL_NUMBER and self.auto_detect_int:
            if is_integer_ok_for_xl_float(value):
                value = int(value)
        return value

class XLSBook(BookReader):
    """
    XLSBook reader

    It reads xls, xlsm, xlsx work book
    """
    def __init__(self):
        BookReader.__init__(self)
        self.file_content = None

    def open(self, file_name, **keywords):
        BookReader.open(self, file_name, **keywords)
        self._get_params()

    def open_stream(self, file_stream, **keywords):
        BookReader.open_stream(self, file_stream, **keywords)
        self._get_params()

    def open_content(self, file_content, **keywords):
        self.keywords = keywords
        self.file_content = file_content
        self._get_params()

    def close(self):
        if self.native_book:
            self.native_book.release_resources()

    def read_sheet_by_index(self, sheet_index):
        self.native_book = self._get_book(on_demand=True)
        sheet = self.native_book.sheet_by_index(sheet_index)
        return self.read_sheet(sheet)

    def read_sheet_by_name(self, sheet_name):
        self.native_book = self._get_book(on_demand=True)
        try:
            sheet = self.native_book.sheet_by_name(sheet_name)
        except xlrd.XLRDError:
            raise ValueError("%s cannot be found" % sheet_name)
        return self.read_sheet(sheet)

    def read_all(self):
        result = OrderedDict()
        self.native_book = self._get_book()
        for sheet in self.native_book.sheets():
            if self.skip_hidden_sheets and sheet.visibility != 0:
                continue
            data_dict = self.read_sheet(sheet)
            result.update(data_dict)
        return result

    def read_sheet(self, native_sheet):
        sheet = XLSheet(native_sheet, **self.keywords)
        return {sheet.name: sheet.to_array()}

    def _get_book(self, on_demand=False):
        if self.file_name:
            xls_book = xlrd.open_workbook(self.file_name, on_demand=on_demand)
        elif self.file_stream:
            xls_book = xlrd.open_workbook(
                None,
                file_contents=self.file_stream.getvalue(),
                on_demand=on_demand
            )
        elif self.file_content:
            xls_book = xlrd.open_workbook(
                None,
                file_contents=self.file_content,
                on_demand=on_demand
            )
        return xls_book

    def _get_params(self):
        self.skip_hidden_sheets = self.keywords.get('skip_hidden_sheets', True)

class XLSheetWriter(SheetWriter):
    """
    xls, xlsx and xlsm sheet writer
    """
    def set_sheet_name(self, name):
        """Create a sheet
        """
        self.native_sheet = self.native_book.add_sheet(name)
        self.current_row = 0

    def write_row(self, array):
        """
        write a row into the file
        """
        for i in range(0, len(array)):
            value = array[i]
            style = None
            tmp_array = []
            if isinstance(value, datetime.datetime):
                tmp_array = [
                    value.year, value.month, value.day,
                    value.hour, value.minute, value.second
                ]
                value = xlrd.xldate.xldate_from_datetime_tuple(tmp_array, 0)
                style = XFStyle()
                style.num_format_str = DEFAULT_DATETIME_FORMAT
            elif isinstance(value, datetime.date):
                tmp_array = [value.year, value.month, value.day]
                value = xlrd.xldate.xldate_from_date_tuple(tmp_array, 0)
                style = XFStyle()
                style.num_format_str = DEFAULT_DATE_FORMAT
            elif isinstance(value, datetime.time):
                tmp_array = [value.hour, value.minute, value.second]
                value = xlrd.xldate.xldate_from_time_tuple(tmp_array)
                style = XFStyle()
                style.num_format_str = DEFAULT_TIME_FORMAT
            if style:
                self.native_sheet.write(self.current_row, i, value, style)
            else:
                self.native_sheet.write(self.current_row, i, value)
        self.current_row += 1

class XLSWriter(BookWriter):
    """
    xls, xlsx and xlsm writer
    """
    def __init__(self):
        BookWriter.__init__(self)
        self.work_book = None

    def open(self, file_name,
             encoding='ascii', style_compression=2, **keywords):
        BookWriter.open(self, file_name, **keywords)
        self.work_book = Workbook(style_compression=style_compression,
                                  encoding=encoding)

    def create_sheet(self, name):
        return XLSheetWriter(self.work_book, None, name)

    def close(self):
        """
        This call actually save the file
        """
        self.work_book.save(self.file_alike_object)

_xls_reader_registry = {
    "file_type": "xls",
    "reader": XLSBook,
    "writer": XLSWriter,
    "stream_type": "binary",
    "mime_type": "application/vnd.ms-excel",
    "library": "pyexcel-xls"
}

_XLSM_MIME = (
    "application/" +
    "vnd.openxmlformats-officedocument.spreadsheetml.sheet")

_xlsm_registry = {
    "file_type": "xlsm",
    "reader": XLSBook,
    "stream_type": "binary",
    "mime_type": _XLSM_MIME,
    "library": "pyexcel-xls"
}

_xlsx_registry = {
    "file_type": "xlsx",
    "reader": XLSBook,
    "stream_type": "binary",
    "mime_type": "application/vnd.ms-excel.sheet.macroenabled.12",
    "library": "pyexcel-xls"
}

exports = (_xls_reader_registry,
           _xlsm_registry,
           _xlsx_registry)
<|endoftext|>


<commit_before>"""
Some helpful tools.
"""

import re

# Regex from https://gist.github.com/andialbrecht/917126
P_PYLINT_ERROR = re.compile(r"^(?P<file>.+?):(?P<line>[0-9]+):\ \[(?P<type>"
    "[a-z])(?P<errno>\d+) (,\ (?P<hint>.+))?\]\ (?P<msg>.*)",
    re.IGNORECASE|re.VERBOSE)

def parsePyLintWarnings(warnings):
    """
    Turns pyLint/TwistedChecker output into something more useful.

    @param warnings: The warnings to process. Needs to be in pyLint's
        'parseable' format.

    @return: L{dict} of L{list}s
    """

    warningsDict = {}

    for line in warnings:

        match = re.search(P_PYLINT_ERROR, line)

        if match:
            matchDict = match.groupdict()

            if not warningsDict.get(matchDict["file"]):
                warningsDict[matchDict["file"]] = []

            warningEntry = {
                "line": matchDict.get("file"),
                "warning_id": matchDict.get("type") + matchDict.get("errno"),
            }

            if matchDict.get("msg"):
                warningEntry["warning_message"] = matchDict.get("msg")

            warningsDict[matchDict["file"]].append(warningEntry)

    return warningsDict

def cleanupMessage(warning, winfo):

    cleanupItems = {
        "C0102": "Blacklisted name",
        "C0103": "Invalid function/variable name",
        "C0301": "Line too long",
        "C0302": "Too many lines in module",
        "W0311": "Bad indentation"
        "W9012": "Expected 2 blank lines",
        "W9013": "Expected 3 blank lines",
        "W9015": "Too many blank lines",
        "W9202": "Missing epytext @param",
    }

    if warning in cleanupItems:
        return cleanupItems[warning]
    else:
        return winfo["message"]
<commit_msg>more messages - now with less parse errors pt 2<commit_after>"""
Some helpful tools.
"""

import re

# Regex from https://gist.github.com/andialbrecht/917126
P_PYLINT_ERROR = re.compile(r"^(?P<file>.+?):(?P<line>[0-9]+):\ \[(?P<type>"
    "[a-z])(?P<errno>\d+) (,\ (?P<hint>.+))?\]\ (?P<msg>.*)",
    re.IGNORECASE|re.VERBOSE)

def parsePyLintWarnings(warnings):
    """
    Turns pyLint/TwistedChecker output into something more useful.

    @param warnings: The warnings to process. Needs to be in pyLint's
        'parseable' format.

    @return: L{dict} of L{list}s
    """

    warningsDict = {}

    for line in warnings:

        match = re.search(P_PYLINT_ERROR, line)

        if match:
            matchDict = match.groupdict()

            if not warningsDict.get(matchDict["file"]):
                warningsDict[matchDict["file"]] = []

            warningEntry = {
                "line": matchDict.get("file"),
                "warning_id": matchDict.get("type") + matchDict.get("errno"),
            }

            if matchDict.get("msg"):
                warningEntry["warning_message"] = matchDict.get("msg")

            warningsDict[matchDict["file"]].append(warningEntry)

    return warningsDict

def cleanupMessage(warning, winfo):

    cleanupItems = {
        "C0102": "Blacklisted name",
        "C0103": "Invalid function/variable name",
        "C0301": "Line too long",
        "C0302": "Too many lines in module",
        "W0311": "Bad indentation",
        "W9012": "Expected 2 blank lines",
        "W9013": "Expected 3 blank lines",
        "W9015": "Too many blank lines",
        "W9202": "Missing epytext @param",
    }

    if warning in cleanupItems:
        return cleanupItems[warning]
    else:
        return winfo["message"]
<|endoftext|>


<commit_before># Provides methods for manipulating text styling in specific terminals.
# Uses a basic chaining method where text properties are added by calling
# methods with related names.
#
# For example, to print "Hello, world!" in red:
#   print pyfancy().red("Hello, world!").get()
#
# Styles can be changed for different text components. Example:
#   print pyfancy().red("Hello").raw(", ").blue("world!").get()
#
# No output text is necessary when calling a styling method. This allows
# styles to be stacked:
#   print pyfancy().red().bold("Hello, world!").get()
#
# There are two provided ways to access the modified text. The first is
# direct access to the string object called "out". However, accessing this
# object will not reset the style, so any text outputted after will have
# the same style as whatever the text was at the end of the chain.
#
# The get() method is better for accessing text because it resets the text
# style so no new text will have unwanted styling.

class pyfancy:
    def __str__(self): return self.get()
    def __init__(self, obj=""):
        # Stores output text, for reset use get()
        self.out = str(obj)

    codes = { # The different escape codes
        'raw':              0,
        'bold':             1,
        'dim':              2,
        'underlined':       4,
        'blinking':         5,
        'inverted':         7,
        'hidden':           8,
        'black':           30,
        'red':             31,
        'green':           32,
        'yellow':          33,
        'blue':            34,
        'magenta':         35,
        'cyan':            36,
        'light_gray':      37,
        'black_bg':        40,
        'red_bg':          41,
        'green_bg':        42,
        'yellow_bg':       43,
        'blue_bg':         44,
        'purple_bg':       45,
        'cyan_bg':         46,
        'gray_bg':         47,
        'dark_gray':       90,
        'light_red':       91,
        'light_green':     92,
        'light_yellow':    93,
        'light_blue':      94,
        'light_magenta':   95,
        'light_cyan':      96,
        'white':           97,
        'dark_gray_bg':    100,
        'light_red_bg':    101,
        'light_green_bg':  102,
        'light_yellow_bg': 103,
        'light_blue_bg':   104,
        'light_purple_bg': 105,
        'light_cyan_bg':   106,
        'white_bg':        107
    }

    # Stores output text, for reset use get()
    out = ""

    # Returns output text and resets properties
    def get(self):
        return self.out + "\033[0m"

    # Outputs text using print (should work in Python 2 and 3)
    def output(self):
        print(self.get())

    # Adds new text without changing the styling
    def add(self,addition):
        self.out += addition;
        return self;

    #Alternate between all the colours of the rainbow
    #No orange, replaced with lightRed
    #No purple/violet so I ignored it
    def rainbow(self,addition=""):
        x = 0
        for i in range(len(addition)):
            if (addition[i] in [" ", "\t", "\n", "\r"]): x+=1
            [self.red, self.light_red, self.yellow, self.green, self.light_blue, self.blue][(i-x) % 6](addition[i])
        return self

    def strip(self):
        text = ""
        i = 0
        while i < len(self.out):
            if self.out[i] == '\033':
                if i + 1 >= len(self.out):
                    return text + '\033'
                if self.out[i + 1] == '[':
                    i += 1
                    if 'm' in self.out[i:]:
                        while self.out[i] != 'm':
                            i += 1
                        i += 1
                    else:
                        text += '\033'
            text += self.out[i]
            i += 1
        return text

    # Simply apply the attribute with the given name
    def attr(self,name):
        if name in self.codes:
            self.out += "\033[%dm" % self.codes[name]

    # Parses text and automatically assigns attributes
    # Attributes are specified through brackets
    # For example, .parse("{red Hello}") is the same as .red("Hello")
    # Multiple attributes can be specified by commas, eg {red,bold Hello}
    # Brackets can be nested, eg {red Hello, {bold world}!}
    # Brackets can be escaped with backslashes
    def parse(self,text):
        i = 0 # Current index
        props = [] # Property stack; required for nested brackets
        while i < len(text):
            c = text[i]
            if c == '\\': # Escape character
                i += 1
                if i < len(text):
                    self.out += text[i]
            elif c == '{': # New property list
                prop = '' # Current property
                i += 1
                curprops = [] # Properties that are part of this bracket
                while text[i] != ' ':
                    if i + 1 == len(text):
                        return self
                    if text[i] == ',':
                        # Properties separated by commas
                        self.attr(prop);
                        curprops.append(prop)
                        prop = ''
                        i += 1
                    prop += text[i]
                    i += 1
                self.attr(prop)
                curprops.append(prop)
                # Add properties to property stack
                props.append(curprops)
            elif c == '}':
                # Reset styling
                self.raw()
                # Remove last entry from property stack
                if len(props) >= 1:
                    props.pop()
                # Apply all properties from any surrounding brackets
                for plist in props:
                    for p in plist:
                        self.attr(p)
            else:
                self.out += c
            i += 1
        return self

    # Multicolored text
    def multi(self,string):
        i = 31 # ID of escape code; starts at 31 (red) and goes to 36 (cyan)
        for c in string: # Iterate through string
            self.out += "\033[" + str(i) + "m" + c
            i += 1 # Why u no have ++i? >:(
            if(i > 36): i = 31
        return self

# Adds a formatting function to pyfancy with the specified name and formatting code
# This shouldn't be exported
def _add(name,number):
    def inner(self, addition = ""):
        self.out += "\033[%dm%s" % (number, addition)
        return self
    setattr(pyfancy,name,inner)

# Generate all default color / format codes
for item in pyfancy.codes.items():
    if len(item) > 1: # Just in case
        _add(item[0],item[1])
<commit_msg>Fixes #20<commit_after># Provides methods for manipulating text styling in specific terminals.
# Uses a basic chaining method where text properties are added by calling
# methods with related names.
#
# For example, to print "Hello, world!" in red:
#   print pyfancy().red("Hello, world!").get()
#
# Styles can be changed for different text components. Example:
#   print pyfancy().red("Hello").raw(", ").blue("world!").get()
#
# No output text is necessary when calling a styling method. This allows
# styles to be stacked:
#   print pyfancy().red().bold("Hello, world!").get()
#
# There are two provided ways to access the modified text. The first is
# direct access to the string object called "out". However, accessing this
# object will not reset the style, so any text outputted after will have
# the same style as whatever the text was at the end of the chain.
#
# The get() method is better for accessing text because it resets the text
# style so no new text will have unwanted styling.

class pyfancy:
    def __str__(self): return self.get()
    def __init__(self, parseText="" obj=""):
        # Stores output text, for reset use get()
        self.out = str(obj)
        self.parseText = str(parseText)
        if (self.parseText != ""):
            self.parse(self.parseText)

    codes = { # The different escape codes
        'raw':              0,
        'bold':             1,
        'dim':              2,
        'underlined':       4,
        'blinking':         5,
        'inverted':         7,
        'hidden':           8,
        'black':           30,
        'red':             31,
        'green':           32,
        'yellow':          33,
        'blue':            34,
        'magenta':         35,
        'cyan':            36,
        'light_gray':      37,
        'black_bg':        40,
        'red_bg':          41,
        'green_bg':        42,
        'yellow_bg':       43,
        'blue_bg':         44,
        'purple_bg':       45,
        'cyan_bg':         46,
        'gray_bg':         47,
        'dark_gray':       90,
        'light_red':       91,
        'light_green':     92,
        'light_yellow':    93,
        'light_blue':      94,
        'light_magenta':   95,
        'light_cyan':      96,
        'white':           97,
        'dark_gray_bg':    100,
        'light_red_bg':    101,
        'light_green_bg':  102,
        'light_yellow_bg': 103,
        'light_blue_bg':   104,
        'light_purple_bg': 105,
        'light_cyan_bg':   106,
        'white_bg':        107
    }

    # Stores output text, for reset use get()
    out = ""

    # Returns output text and resets properties
    def get(self):
        return self.out + "\033[0m"

    # Outputs text using print (should work in Python 2 and 3)
    def output(self):
        print(self.get())

    # Adds new text without changing the styling
    def add(self,addition):
        self.out += addition;
        return self;

    #Alternate between all the colours of the rainbow
    #No orange, replaced with lightRed
    #No purple/violet so I ignored it
    def rainbow(self,addition=""):
        x = 0
        for i in range(len(addition)):
            if (addition[i] in [" ", "\t", "\n", "\r"]): x+=1
            [self.red, self.light_red, self.yellow, self.green, self.light_blue, self.blue][(i-x) % 6](addition[i])
        return self

    def strip(self):
        text = ""
        i = 0
        while i < len(self.out):
            if self.out[i] == '\033':
                if i + 1 >= len(self.out):
                    return text + '\033'
                if self.out[i + 1] == '[':
                    i += 1
                    if 'm' in self.out[i:]:
                        while self.out[i] != 'm':
                            i += 1
                        i += 1
                    else:
                        text += '\033'
            text += self.out[i]
            i += 1
        return text

    # Simply apply the attribute with the given name
    def attr(self,name):
        if name in self.codes:
            self.out += "\033[%dm" % self.codes[name]

    # Parses text and automatically assigns attributes
    # Attributes are specified through brackets
    # For example, .parse("{red Hello}") is the same as .red("Hello")
    # Multiple attributes can be specified by commas, eg {red,bold Hello}
    # Brackets can be nested, eg {red Hello, {bold world}!}
    # Brackets can be escaped with backslashes
    def parse(self,text):
        i = 0 # Current index
        props = [] # Property stack; required for nested brackets
        while i < len(text):
            c = text[i]
            if c == '\\': # Escape character
                i += 1
                if i < len(text):
                    self.out += text[i]
            elif c == '{': # New property list
                prop = '' # Current property
                i += 1
                curprops = [] # Properties that are part of this bracket
                while text[i] != ' ':
                    if i + 1 == len(text):
                        return self
                    if text[i] == ',':
                        # Properties separated by commas
                        self.attr(prop);
                        curprops.append(prop)
                        prop = ''
                        i += 1
                    prop += text[i]
                    i += 1
                self.attr(prop)
                curprops.append(prop)
                # Add properties to property stack
                props.append(curprops)
            elif c == '}':
                # Reset styling
                self.raw()
                # Remove last entry from property stack
                if len(props) >= 1:
                    props.pop()
                # Apply all properties from any surrounding brackets
                for plist in props:
                    for p in plist:
                        self.attr(p)
            else:
                self.out += c
            i += 1
        return self

    # Multicolored text
    def multi(self,string):
        i = 31 # ID of escape code; starts at 31 (red) and goes to 36 (cyan)
        for c in string: # Iterate through string
            self.out += "\033[" + str(i) + "m" + c
            i += 1 # Why u no have ++i? >:(
            if(i > 36): i = 31
        return self

# Adds a formatting function to pyfancy with the specified name and formatting code
# This shouldn't be exported
def _add(name,number):
    def inner(self, addition = ""):
        self.out += "\033[%dm%s" % (number, addition)
        return self
    setattr(pyfancy,name,inner)

# Generate all default color / format codes
for item in pyfancy.codes.items():
    if len(item) > 1: # Just in case
        _add(item[0],item[1])
<|endoftext|>


<commit_before># The purpose of this file is to provide base classes with the needed functions
# already defined; this allows us to guarantee that any exceptions raised
# during function calls are a problem with the module and not just that the
# particular function isn't defined.

class Module(object):
	def hook(self, base):
		self.ircd = base
		return self

class Mode(object):
	def hook(self, base):
		self.ircd = base
		return self
	def checkSet(self, user, target, param):
		return True
	def checkUnset(self, user, target, param):
		return True
	def showParam(self, user, param):
		return param
	def onJoin(self, channel, user, params):
		return "pass"
	def checkPermission(self, user, cmd, data):
		return "pass"
	def onMessage(self, sender, target, message):
		return ["pass"]
	def onPart(self, channel, user, reason):
		pass
	def onTopicChange(self, channel, user, topic):
		pass
	def commandData(self, command, *args):
		pass

def Command(object):
	def hook(self, base):
		self.ircd = base
		return self
	def onUse(self, user, data):
		pass
	def processParams(self, user, params):
		return {
			"user": user,
			"params": params
		}
	def updateActivity(self, user):
		user.lastactivity = now()<commit_msg>Correct an inconsistency<commit_after># The purpose of this file is to provide base classes with the needed functions
# already defined; this allows us to guarantee that any exceptions raised
# during function calls are a problem with the module and not just that the
# particular function isn't defined.

class Module(object):
	def hook(self, base):
		self.ircd = base
		return self

class Mode(object):
	def hook(self, base):
		self.ircd = base
		return self
	def checkSet(self, user, target, param):
		return True
	def checkUnset(self, user, target, param):
		return True
	def showParam(self, user, param):
		return param
	def onJoin(self, channel, user, params):
		return "pass"
	def checkPermission(self, user, cmd, data):
		return data
	def onMessage(self, sender, target, message):
		return ["pass"]
	def onPart(self, channel, user, reason):
		pass
	def onTopicChange(self, channel, user, topic):
		pass
	def commandData(self, command, *args):
		pass

def Command(object):
	def hook(self, base):
		self.ircd = base
		return self
	def onUse(self, user, data):
		pass
	def processParams(self, user, params):
		return {
			"user": user,
			"params": params
		}
	def updateActivity(self, user):
		user.lastactivity = now()<|endoftext|>


<commit_before>#!/usr/bin/env python
# coding:utf-8
import time
import json
from hashlib import md5
from txweb import utils
from txweb import logger
from txweb.storage import Storage
from collections import namedtuple

ApiStatus = namedtuple('ApiStatus', 'code desc msg')
apistatus = Storage(
    success = ApiStatus(code=0,desc='success',msg=u"处理成功"),
    sign_err = ApiStatus(code=90001,desc='message sign error',msg=u"消息签名错误"),
    parse_err = ApiStatus(code=90002,desc='param parse error',msg=u"参数解析失败"),
    verify_err = ApiStatus(code=90003,desc='message verify error',msg=u"消息校验错误"),
    timeout = ApiStatus(code=90004,desc='request timeout',msg=u"请求超时"),
    limit_err = ApiStatus(code=90005,desc='api limit',msg=u"频率限制"),
    server_err = ApiStatus(code=90006,desc='server process failure',msg=u"服务器处理失败"),
    unknow = ApiStatus(code=99999,desc='unknow error',msg=u"未知错误")
)

class SignError(Exception):
    pass

class ParseError(Exception):
    pass

def make_sign(api_secret, params=[]):
    """
        >>> make_sign("123456",[1,'2',u'中文'])
        '33C9065427EECA3490C5642C99165145'
    """
    _params = [utils.safeunicode(p) for p in params if p is not None]
    _params.sort()
    print 'sorted params:',_params
    _params.insert(0, api_secret)
    strs = ''.join(_params)
    print 'sign params:',strs
    mds = md5(strs.encode('utf-8')).hexdigest()
    return mds.upper()

def check_sign(api_secret, msg):
    """
        >>> check_sign("123456",dict(code=1,s='2',msg=u'中文',sign='33C9065427EECA3490C5642C99165145'))
        True

    """
    if "sign" not in msg:
        return False
    sign = msg['sign']
    params = [utils.safestr(msg[k]) for k in msg if k != 'sign']
    local_sign = make_sign(api_secret, params)
    result = (sign == local_sign)
    if not result:
        logger.error("check_sign failure, sign:%s != local_sign:%s" %(sign,local_sign))
    return result

def make_message(api_secret, enc_func=False, **params):
    """
        >>> json.loads(make_message("123456",**dict(code=1,msg=u"中文",nonce=1451122677)))['sign']
        u'58BAF40309BC1DC51D2E2DC43ECCC1A1'
    """
    if 'nonce' not in params:
        params['nonce' ] = str(int(time.time()))
    params['sign'] = make_sign(api_secret, params.values())
    msg = json.dumps(params, ensure_ascii=False)
    if callable(enc_func):
        return enc_func(msg)
    else:
        return msg

def make_error(api_secret, msg=None, enc_func=False):
    return make_message(api_secret,code=1,msg=msg, enc_func=enc_func)

def parse_request(api_secret, reqbody, dec_func=False):
    """
        >>> parse_request("123456",'{"nonce": 1451122677, "msg": "helllo", "code": 0, "sign": "DB30F4D1112C20DFA736F65458F89C64"}')
        <Storage {u'nonce': 1451122677, u'msg': u'helllo', u'code': 0, u'sign': u'DB30F4D1112C20DFA736F65458F89C64'}>
    """
    try:
        if type(reqbody) == type(dict):
            return self.parse_form_request(reqbody)

        if callable(dec_func):
            req_msg = json.loads(dec_func(reqbody))
        else:
            req_msg = json.loads(reqbody)
    except Exception as err:
        raise ParseError(u"parse params error")

    if not check_sign(api_secret, req_msg):
        raise SignError(u"message sign error")

    return Storage(req_msg)

def parse_form_request(api_secret, request):
    """
        >>> parse_form_request("123456",{"nonce": 1451122677, "msg": "helllo", "code": 0, "sign": "DB30F4D1112C20DFA736F65458F89C64"})
        <Storage {'nonce': 1451122677, 'msg': 'helllo', 'code': 0, 'sign': 'DB30F4D1112C20DFA736F65458F89C64'}>
    """
    if not check_sign(api_secret, request):
        raise SignError(u"message sign error")

    return Storage(request)

if __name__ == "__main__":
    # print apistatus
    # import doctest
    # doctest.testmod()
    #isp_name=123&isp_email=222@222.com&isp_idcard=&isp_desc=&isp_phone=22222222222&sign=9E5139D66E8E5C10634A3E96631BCF
    params = ['123','222@222.com','22222222222']
    print make_sign('LpWE9AtfDPQ3ufXBS6gJ37WW8TnSF920',params)

    <commit_msg>0.0.1: no commit message<commit_after>#!/usr/bin/env python
# coding:utf-8
import time
import json
from hashlib import md5
from txweb import utils
from txweb import logger
from txweb.storage import Storage
from collections import namedtuple

ApiStatus = namedtuple('ApiStatus', 'code desc msg')
apistatus = Storage(
    success = ApiStatus(code=0,desc='success',msg=u"处理成功"),
    sign_err = ApiStatus(code=90001,desc='message sign error',msg=u"消息签名错误"),
    parse_err = ApiStatus(code=90002,desc='param parse error',msg=u"参数解析失败"),
    verify_err = ApiStatus(code=90003,desc='message verify error',msg=u"消息校验错误"),
    timeout = ApiStatus(code=90004,desc='request timeout',msg=u"请求超时"),
    limit_err = ApiStatus(code=90005,desc='api limit',msg=u"频率限制"),
    server_err = ApiStatus(code=90006,desc='server process failure',msg=u"服务器处理失败"),
    unknow = ApiStatus(code=99999,desc='unknow error',msg=u"未知错误")
)

class SignError(Exception):
    pass

class ParseError(Exception):
    pass

def make_sign(api_secret, params=[]):
    """
        >>> make_sign("123456",[1,'2',u'中文'])
        '33C9065427EECA3490C5642C99165145'
    """
    _params = [utils.safeunicode(p) for p in params if p is not None]
    _params.sort()
    # print 'sorted params:',_params
    _params.insert(0, api_secret)
    strs = ''.join(_params)
    # print 'sign params:',strs
    mds = md5(strs.encode('utf-8')).hexdigest()
    return mds.upper()

def check_sign(api_secret, msg):
    """
        >>> check_sign("123456",dict(code=1,s='2',msg=u'中文',sign='33C9065427EECA3490C5642C99165145'))
        True

    """
    if "sign" not in msg:
        return False
    sign = msg['sign']
    params = [utils.safestr(msg[k]) for k in msg if k != 'sign']
    local_sign = make_sign(api_secret, params)
    result = (sign == local_sign)
    if not result:
        logger.error("check_sign failure, sign:%s != local_sign:%s" %(sign,local_sign))
    return result

def make_message(api_secret, enc_func=False, **params):
    """
        >>> json.loads(make_message("123456",**dict(code=1,msg=u"中文",nonce=1451122677)))['sign']
        u'58BAF40309BC1DC51D2E2DC43ECCC1A1'
    """
    if 'nonce' not in params:
        params['nonce' ] = str(int(time.time()))
    params['sign'] = make_sign(api_secret, params.values())
    msg = json.dumps(params, ensure_ascii=False)
    if callable(enc_func):
        return enc_func(msg)
    else:
        return msg

def make_error(api_secret, msg=None, enc_func=False):
    return make_message(api_secret,code=1,msg=msg, enc_func=enc_func)

def parse_request(api_secret, reqbody, dec_func=False):
    """
        >>> parse_request("123456",'{"nonce": 1451122677, "msg": "helllo", "code": 0, "sign": "DB30F4D1112C20DFA736F65458F89C64"}')
        <Storage {u'nonce': 1451122677, u'msg': u'helllo', u'code': 0, u'sign': u'DB30F4D1112C20DFA736F65458F89C64'}>
    """
    try:
        if type(reqbody) == type(dict):
            return self.parse_form_request(reqbody)

        if callable(dec_func):
            req_msg = json.loads(dec_func(reqbody))
        else:
            req_msg = json.loads(reqbody)
    except Exception as err:
        raise ParseError(u"parse params error")

    if not check_sign(api_secret, req_msg):
        raise SignError(u"message sign error")

    return Storage(req_msg)

def parse_form_request(api_secret, request):
    """
        >>> parse_form_request("123456",{"nonce": 1451122677, "msg": "helllo", "code": 0, "sign": "DB30F4D1112C20DFA736F65458F89C64"})
        <Storage {'nonce': 1451122677, 'msg': 'helllo', 'code': 0, 'sign': 'DB30F4D1112C20DFA736F65458F89C64'}>
    """
    if not check_sign(api_secret, request):
        raise SignError(u"message sign error")

    return Storage(request)

if __name__ == "__main__":
    # print apistatus
    # import doctest
    # doctest.testmod()
    #isp_name=123&isp_email=222@222.com&isp_idcard=&isp_desc=&isp_phone=22222222222&sign=9E5139D66E8E5C10634A3E96631BCF
    params = ['123','222@222.com','22222222222']
    print make_sign('LpWE9AtfDPQ3ufXBS6gJ37WW8TnSF920',params)

    <|endoftext|>


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

from __future__ import print_function
import os
import re
import subprocess
import threading
from agithub.GitHub import GitHub

from pygcgen_exceptions import GithubApiError

GH_CFG_VARS = ["github.pygcgen.token", "github.token"]
PER_PAGE_NUMBER = 100
MAX_SIMULTANEOUS_REQUESTS = 25
CHANGELOG_GITHUB_TOKEN = "CHANGELOG_GITHUB_TOKEN"
GH_RATE_LIMIT_EXCEEDED_MSG = \
    "GitHub API rate limit exceeded, change log may be missing some issues. " \
    "Please provide a token with -t option or in git config."
NO_TOKEN_PROVIDED = \
    "Warning: No token provided. Neither -t option, git config or variable " \
    "$CHANGELOG_GITHUB_TOKEN was not found. This script can make only " \
    "50 requests to GitHub API per hour without token!"

class Fetcher:
    '''
    A Fetcher is responsible for all requests to GitHub and all basic
    manipulation with related data (such as filtering, validating, e.t.c).
    '''

    def __init__(self, options):
        self.options = options
        self.fetch_github_token()
        if options.token:
            self.github = GitHub(
                token=options.token,
                api_url=options.github_endpoint
            )
        else:
            self.github = GitHub(api_url=options.github_endpoint)

    def fetch_github_token(self):
        '''
        Returns GitHub token. First try to use variable provided
        by --token option, otherwise try to fetch it from git config
        and last CHANGELOG_GITHUB_TOKEN env variable.

        @return [String]
        '''

        if not self.options.token:
            try:
                for v in GH_CFG_VARS:
                    cmd = ['git', 'config', '--get', '{0}'.format(v)]
                    self.options.token = subprocess.Popen(
                        cmd, stdout=subprocess.PIPE).communicate()[0].strip()
                    if self.options.token:
                        break
            except subprocess.CalledProcessError:
                pass
        if not self.options.token:
            self.options.token = os.environ.get(CHANGELOG_GITHUB_TOKEN)
        if not self.options.token:
            print(NO_TOKEN_PROVIDED)

    def get_all_tags(self):
        '''
        Fill input array with tags.

        @return [Array] array of tags in repo
        '''

        verbose = self.options.verbose
        gh = self.github
        user = self.options.user
        repo = self.options.project
        if verbose:
            print("Fetching tags..")

        tags = []
        page = 1
        while page > 0:
            if verbose:
                print(".", end="")
            rc, data = gh.repos[user][repo].tags.get(
                page=page, per_page=PER_PAGE_NUMBER)
            if rc == 200:
                tags.extend(data)
            else:
                self.check_returncode(rc, data, gh.getheaders())
            page = NextPage(gh)
        if self.options.verbose:
            print(".")

        if len(tags) == 0:
            print("Warning: Can't find any tags in repo. Make sure, "
                  "that you push tags to remote repo via 'git push --tags'")
        elif verbose:
            print("Found {0} tag(s)".format(len(tags)))
        return tags

    def fetch_closed_issues_and_pr(self):
        '''
        This method fetch all closed issues and separate them to
        pull requests and pure issues (pull request is kind of issue
        in term of GitHub).

        @return [Tuple] with (issues, pull-requests)
        '''

        verbose = self.options.verbose
        gh = self.github
        user = self.options.user
        repo = self.options.project
        if verbose:
            print("Fetching closed issues and pull requests..")

        issues = []
        page = 1
        while page > 0:
            if verbose:
                print(".", end="")
            rc, data = gh.repos[user][repo].issues.get(
                page=page, per_page=PER_PAGE_NUMBER,
                state='closed', filter='all'
            )
            if rc == 200:
                issues.extend(data)
            else:
                self.check_returncode(rc, data, gh.getheaders())
            if len(issues) >= self.options.max_issues:
                break
            page = NextPage(gh)
        self.first_issue = data[-1] if len(data) > 0 else []
        if verbose:
            print(".")
            print("Received: {0}".format(len(issues)))

        # separate arrays of issues and pull requests:
        prs = []
        iss = []
        for i in issues:
            if "pull_request" in i:
                prs.append(i)
            else:
                iss.append(i)
        return iss, prs

    def fetch_closed_pull_requests(self):
        '''
        Fetch all pull requests. We need them to detect "merged_at" parameter

        @return [Array] all pull requests
        '''

        pull_requests = []
        verbose = self.options.verbose
        gh = self.github
        user = self.options.user
        repo = self.options.project
        if verbose:
            print("Fetching closed pull requests..")
        page = 1
        while page > 0:
            if verbose:
                print(".", end="")

            if self.options.release_branch:
                rc, data = gh.repos[user][repo].pulls.get(
                    page=page, per_page=PER_PAGE_NUMBER, state='closed',
                    base=self.options.release_branch
                )
            else:
                rc, data = gh.repos[user][repo].pulls.get(
                    page=page, per_page=PER_PAGE_NUMBER, state='closed',
                )

            if rc == 200:
                pull_requests.extend(data)
            else:
                self.check_returncode(rc, data, gh.getheaders())
            page = NextPage(gh)
        if verbose:
            print(".")
            print("Fetched closed pull requests: {0}".format(len(pull_requests)))
        return pull_requests

    def get_first_event_date(self):
        gh = self.github
        user = self.options.user
        repo = self.options.project
        rc, data = gh.repos[user][repo].get()
        if rc == 200:
            tag_name = "repo_created_at"
            tag_date = data["created_at"]
            return tag_name, tag_date
        else:
            self.check_returncode(rc, data, gh.getheaders())
        return None, None

    def fetch_events_async(self, issues, label):
        '''
        Fetch events for all issues and add them to self.events

        @param [Array] issues
        '''
        if not issues:
            return issues
        verbose = self.options.verbose
        gh = self.github
        user = self.options.user
        repo = self.options.project
        self.events_cnt = 0
        if verbose:
            print("events for {0} (async)... ".format(label))

        def worker(issue):
            page = 1
            issue['events'] = []
            while page > 0:
                rc, data = gh.repos[user][repo].issues[
                    issue['number']].events.get(
                    page=page, per_page=PER_PAGE_NUMBER)
                if rc == 200:
                    issue['events'].extend(data)
                    self.events_cnt += len(data)
                else:
                    self.check_returncode(rc, data, gh.getheaders())
                page = NextPage(gh)

        threads = []
        cnt = len(issues)
        for i in range(0, (cnt / MAX_SIMULTANEOUS_REQUESTS) + 1):
            for j in range(MAX_SIMULTANEOUS_REQUESTS):
                idx = i * MAX_SIMULTANEOUS_REQUESTS + j
                print("i:",i, "  j:", j, "  cnt:", cnt, "  idx:", idx)
                if idx == cnt:
                    break
                t = threading.Thread(target=worker, args=(issues[idx],))
                threads.append(t)
                t.start()
                if verbose:
                    print(".", end="")
                    if not idx % PER_PAGE_NUMBER:
                        print("")
            for t in threads:
                t.join()
        if verbose:
            print(".")

    def fetch_date_of_tag(self, tag):
        '''Fetch tag time from repo

        @param [Hash] tag
        @return [Time] time of specified tag
        '''

        if self.options.verbose:
            print("Fetching date for tag {0}".format(tag["name"]))
        gh = self.github
        user = self.options.user
        repo = self.options.project

        rc, data = gh.repos[user][repo].git.commits[
            tag["commit"]["sha"]].get()
        if rc == 200:
            time_string = data["committer"]["date"]
            return time_string
        else:
            self.check_returncode(rc, data, gh.getheaders())
        return None

    def fetch_commit(self, event):
        '''
        Fetch commit for specified event
        @return [Hash]
        '''
        gh = self.github
        user = self.options.user
        repo = self.options.project

        rc, data = gh.repos[user][repo].git.commits[
            event["commit_id"]].get()
        if rc == 200:
            return data
        else:
            self.check_returncode(rc, data, gh.getheaders())
        return None

    def check_returncode(self, rc, data, header):
        hdr =dict(header)
        if rc == 403 and hdr.get("x-ratelimit-remaining") == '0':
            raise GithubApiError(GH_RATE_LIMIT_EXCEEDED_MSG)
        raise GithubApiError("({0}) {1}".format(rc, data["message"]))

def NextPage(gh):
    header = dict(gh.getheaders())
    if 'link' in header:
        parts = header['link'].split(',')
        for part in parts:
            subparts = part.split(';')
            sub = subparts[1].split('=')
            if sub[0].strip() == 'rel':
                if sub[1] == '"next"':
                    page = int(re.match(ur'.*page=(\d+).*',
                               subparts[0],
                               re.IGNORECASE | re.DOTALL | re.UNICODE).
                               groups()[0])
                    return page
    return 0
<commit_msg>remove debug output<commit_after># -*- coding: utf-8 -*-

from __future__ import print_function
import os
import re
import subprocess
import threading
from agithub.GitHub import GitHub

from pygcgen_exceptions import GithubApiError

GH_CFG_VARS = ["github.pygcgen.token", "github.token"]
PER_PAGE_NUMBER = 100
MAX_SIMULTANEOUS_REQUESTS = 25
CHANGELOG_GITHUB_TOKEN = "CHANGELOG_GITHUB_TOKEN"
GH_RATE_LIMIT_EXCEEDED_MSG = \
    "GitHub API rate limit exceeded, change log may be missing some issues. " \
    "Please provide a token with -t option or in git config."
NO_TOKEN_PROVIDED = \
    "Warning: No token provided. Neither -t option, git config or variable " \
    "$CHANGELOG_GITHUB_TOKEN was not found. This script can make only " \
    "50 requests to GitHub API per hour without token!"

class Fetcher:
    '''
    A Fetcher is responsible for all requests to GitHub and all basic
    manipulation with related data (such as filtering, validating, e.t.c).
    '''

    def __init__(self, options):
        self.options = options
        self.fetch_github_token()
        if options.token:
            self.github = GitHub(
                token=options.token,
                api_url=options.github_endpoint
            )
        else:
            self.github = GitHub(api_url=options.github_endpoint)

    def fetch_github_token(self):
        '''
        Returns GitHub token. First try to use variable provided
        by --token option, otherwise try to fetch it from git config
        and last CHANGELOG_GITHUB_TOKEN env variable.

        @return [String]
        '''

        if not self.options.token:
            try:
                for v in GH_CFG_VARS:
                    cmd = ['git', 'config', '--get', '{0}'.format(v)]
                    self.options.token = subprocess.Popen(
                        cmd, stdout=subprocess.PIPE).communicate()[0].strip()
                    if self.options.token:
                        break
            except subprocess.CalledProcessError:
                pass
        if not self.options.token:
            self.options.token = os.environ.get(CHANGELOG_GITHUB_TOKEN)
        if not self.options.token:
            print(NO_TOKEN_PROVIDED)

    def get_all_tags(self):
        '''
        Fill input array with tags.

        @return [Array] array of tags in repo
        '''

        verbose = self.options.verbose
        gh = self.github
        user = self.options.user
        repo = self.options.project
        if verbose:
            print("Fetching tags..")

        tags = []
        page = 1
        while page > 0:
            if verbose:
                print(".", end="")
            rc, data = gh.repos[user][repo].tags.get(
                page=page, per_page=PER_PAGE_NUMBER)
            if rc == 200:
                tags.extend(data)
            else:
                self.check_returncode(rc, data, gh.getheaders())
            page = NextPage(gh)
        if self.options.verbose:
            print(".")

        if len(tags) == 0:
            print("Warning: Can't find any tags in repo. Make sure, "
                  "that you push tags to remote repo via 'git push --tags'")
        elif verbose:
            print("Found {0} tag(s)".format(len(tags)))
        return tags

    def fetch_closed_issues_and_pr(self):
        '''
        This method fetch all closed issues and separate them to
        pull requests and pure issues (pull request is kind of issue
        in term of GitHub).

        @return [Tuple] with (issues, pull-requests)
        '''

        verbose = self.options.verbose
        gh = self.github
        user = self.options.user
        repo = self.options.project
        if verbose:
            print("Fetching closed issues and pull requests..")

        issues = []
        page = 1
        while page > 0:
            if verbose:
                print(".", end="")
            rc, data = gh.repos[user][repo].issues.get(
                page=page, per_page=PER_PAGE_NUMBER,
                state='closed', filter='all'
            )
            if rc == 200:
                issues.extend(data)
            else:
                self.check_returncode(rc, data, gh.getheaders())
            if len(issues) >= self.options.max_issues:
                break
            page = NextPage(gh)
        self.first_issue = data[-1] if len(data) > 0 else []
        if verbose:
            print(".")
            print("Received: {0}".format(len(issues)))

        # separate arrays of issues and pull requests:
        prs = []
        iss = []
        for i in issues:
            if "pull_request" in i:
                prs.append(i)
            else:
                iss.append(i)
        return iss, prs

    def fetch_closed_pull_requests(self):
        '''
        Fetch all pull requests. We need them to detect "merged_at" parameter

        @return [Array] all pull requests
        '''

        pull_requests = []
        verbose = self.options.verbose
        gh = self.github
        user = self.options.user
        repo = self.options.project
        if verbose:
            print("Fetching closed pull requests..")
        page = 1
        while page > 0:
            if verbose:
                print(".", end="")

            if self.options.release_branch:
                rc, data = gh.repos[user][repo].pulls.get(
                    page=page, per_page=PER_PAGE_NUMBER, state='closed',
                    base=self.options.release_branch
                )
            else:
                rc, data = gh.repos[user][repo].pulls.get(
                    page=page, per_page=PER_PAGE_NUMBER, state='closed',
                )

            if rc == 200:
                pull_requests.extend(data)
            else:
                self.check_returncode(rc, data, gh.getheaders())
            page = NextPage(gh)
        if verbose:
            print(".")
            print("Fetched closed pull requests: {0}".format(len(pull_requests)))
        return pull_requests

    def get_first_event_date(self):
        gh = self.github
        user = self.options.user
        repo = self.options.project
        rc, data = gh.repos[user][repo].get()
        if rc == 200:
            tag_name = "repo_created_at"
            tag_date = data["created_at"]
            return tag_name, tag_date
        else:
            self.check_returncode(rc, data, gh.getheaders())
        return None, None

    def fetch_events_async(self, issues, label):
        '''
        Fetch events for all issues and add them to self.events

        @param [Array] issues
        '''
        if not issues:
            return issues
        verbose = self.options.verbose
        gh = self.github
        user = self.options.user
        repo = self.options.project
        self.events_cnt = 0
        if verbose:
            print("events for {0} (async)... ".format(label))

        def worker(issue):
            page = 1
            issue['events'] = []
            while page > 0:
                rc, data = gh.repos[user][repo].issues[
                    issue['number']].events.get(
                    page=page, per_page=PER_PAGE_NUMBER)
                if rc == 200:
                    issue['events'].extend(data)
                    self.events_cnt += len(data)
                else:
                    self.check_returncode(rc, data, gh.getheaders())
                page = NextPage(gh)

        threads = []
        cnt = len(issues)
        for i in range(0, (cnt / MAX_SIMULTANEOUS_REQUESTS) + 1):
            for j in range(MAX_SIMULTANEOUS_REQUESTS):
                idx = i * MAX_SIMULTANEOUS_REQUESTS + j
                if idx == cnt:
                    break
                t = threading.Thread(target=worker, args=(issues[idx],))
                threads.append(t)
                t.start()
                if verbose:
                    print(".", end="")
                    if not idx % PER_PAGE_NUMBER:
                        print("")
            for t in threads:
                t.join()
        if verbose:
            print(".")

    def fetch_date_of_tag(self, tag):
        '''Fetch tag time from repo

        @param [Hash] tag
        @return [Time] time of specified tag
        '''

        if self.options.verbose:
            print("Fetching date for tag {0}".format(tag["name"]))
        gh = self.github
        user = self.options.user
        repo = self.options.project

        rc, data = gh.repos[user][repo].git.commits[
            tag["commit"]["sha"]].get()
        if rc == 200:
            time_string = data["committer"]["date"]
            return time_string
        else:
            self.check_returncode(rc, data, gh.getheaders())
        return None

    def fetch_commit(self, event):
        '''
        Fetch commit for specified event
        @return [Hash]
        '''
        gh = self.github
        user = self.options.user
        repo = self.options.project

        rc, data = gh.repos[user][repo].git.commits[
            event["commit_id"]].get()
        if rc == 200:
            return data
        else:
            self.check_returncode(rc, data, gh.getheaders())
        return None

    def check_returncode(self, rc, data, header):
        hdr =dict(header)
        if rc == 403 and hdr.get("x-ratelimit-remaining") == '0':
            raise GithubApiError(GH_RATE_LIMIT_EXCEEDED_MSG)
        raise GithubApiError("({0}) {1}".format(rc, data["message"]))

def NextPage(gh):
    header = dict(gh.getheaders())
    if 'link' in header:
        parts = header['link'].split(',')
        for part in parts:
            subparts = part.split(';')
            sub = subparts[1].split('=')
            if sub[0].strip() == 'rel':
                if sub[1] == '"next"':
                    page = int(re.match(ur'.*page=(\d+).*',
                               subparts[0],
                               re.IGNORECASE | re.DOTALL | re.UNICODE).
                               groups()[0])
                    return page
    return 0
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
__version__ = '1.0.1'
__author__ = 'Nico Schlömer'
__author_email__ = 'nico.schloemer@gmail.com'
__website__ = 'https://github.com/nschloe/pygmsh'

from pygmsh.geometry import Geometry
from pygmsh.helper import *

__all__ = [
        'geometry',
        'helper'
        ]
<commit_msg>bump version to 2.0.0<commit_after># -*- coding: utf-8 -*-
#
__version__ = '2.0.0'
__author__ = 'Nico Schlömer'
__author_email__ = 'nico.schloemer@gmail.com'
__website__ = 'https://github.com/nschloe/pygmsh'

from pygmsh.geometry import Geometry
from pygmsh.helper import *

__all__ = [
        'geometry',
        'helper'
        ]
<|endoftext|>