本文整理汇总了Python中aspen.utils.typecheck函数的典型用法代码示例。如果您正苦于以下问题:Python typecheck函数的具体用法?Python typecheck怎么用?Python typecheck使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了typecheck函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wrap
def wrap(u):
"""Given a unicode, return a unicode.
"""
typecheck(u, unicode)
u = linkify(u) # Do this first, because it calls xthml_escape.
u = u.replace(u"\r\n", u"<br />\r\n").replace(u"\n", u"<br />\n")
return u if u else "..."
示例2: __init__
def __init__(self, headers, fp, server_software):
"""Takes a str, a file-like object, and another str.
If the Mapping API is used (in/one/all/has), then the iterable will be
read and parsed as media of type application/x-www-form-urlencoded or
multipart/form-data, according to content_type.
"""
typecheck(headers, Headers, server_software, str)
raw_len = int(headers.get('Content-length', '') or '0')
self.raw = self._read_raw(server_software, fp, raw_len) # XXX lazy!
parsed = self._parse(headers, self.raw)
if parsed is None:
# There was no content-type. Use self.raw.
pass
else:
for k in parsed.keys():
v = parsed[k]
if isinstance(v, cgi.MiniFieldStorage):
v = v.value.decode("UTF-8") # XXX Really? Always UTF-8?
else:
assert isinstance(v, cgi.FieldStorage), v
if v.filename is None:
v = v.value.decode("UTF-8")
self[k] = v
示例3: transfer
def transfer(self, tipper, tippee, amount, pachinko=False):
"""Given two unicodes, a Decimal, and a boolean, return a boolean.
If the tipper doesn't have enough in their Gittip account then we
return False. Otherwise we decrement tipper's balance and increment
tippee's *pending* balance by amount.
"""
typecheck( tipper, unicode
, tippee, unicode
, amount, Decimal
, pachinko, bool
)
with self.db.get_connection() as conn:
cursor = conn.cursor()
try:
self.debit_participant(cursor, tipper, amount)
except IntegrityError:
return False
self.credit_participant(cursor, tippee, amount)
self.record_transfer(cursor, tipper, tippee, amount)
if pachinko:
self.mark_pachinko(cursor, amount)
else:
self.mark_transfer(cursor, amount)
conn.commit()
return True
示例4: __init__
def __init__(self, d):
"""Takes headers as a dict or str.
"""
typecheck(d, (dict, str))
if isinstance(d, str):
from aspen.exceptions import MalformedHeader
def genheaders():
for line in d.splitlines():
if b':' not in line:
# no colon separator in header
raise MalformedHeader(line)
k, v = line.split(b':', 1)
if k != k.strip():
# disallowed leading or trailing whitspace
# (per http://tools.ietf.org/html/rfc7230#section-3.2.4)
raise MalformedHeader(line)
yield k, v.strip()
else:
genheaders = d.iteritems
CaseInsensitiveMapping.__init__(self, genheaders)
# Cookie
# ======
self.cookie = SimpleCookie()
try:
self.cookie.load(self.get('Cookie', b''))
except CookieError:
pass # XXX really?
示例5: get_balanced_account
def get_balanced_account(participant_id, balanced_account_uri):
"""Find or create a balanced.Account.
"""
typecheck( participant_id, unicode
, balanced_account_uri, (unicode, None)
)
# XXX Balanced requires an email address
# https://github.com/balanced/balanced-api/issues/20
email_address = '{}@gittip.com'.format(participant_id)
if balanced_account_uri is None:
try:
account = \
balanced.Account.query.filter(email_address=email_address).one()
except balanced.exc.NoResultFound:
account = balanced.Account(email_address=email_address).save()
BALANCED_ACCOUNT = """\
UPDATE participants
SET balanced_account_uri=%s
WHERE id=%s
"""
gittip.db.execute(BALANCED_ACCOUNT, (account.uri, participant_id))
account.meta['participant_id'] = participant_id
account.save() # HTTP call under here
else:
account = balanced.Account.find(balanced_account_uri)
return account
示例6: get_user_info
def get_user_info(login):
"""Get the given user's information from the DB or failing that, github.
:param login:
A unicode string representing a username in github.
:returns:
A dictionary containing github specific information for the user.
"""
typecheck(login, unicode)
rec = gittip.db.fetchone( "SELECT user_info FROM elsewhere "
"WHERE platform='github' "
"AND user_info->'login' = %s"
, (login,)
)
if rec is not None:
user_info = rec['user_info']
else:
url = "https://api.github.com/users/%s"
user_info = requests.get(url % login)
status = user_info.status_code
content = user_info.text
if status == 200:
user_info = json.loads(content)
elif status == 404:
raise Response(404,
"GitHub identity '{0}' not found.".format(login))
else:
log("Github api responded with {0}: {1}".format(status, content),
level=logging.WARNING)
raise Response(502, "GitHub lookup failed with %d." % status)
return user_info
示例7: transfer
def transfer(self, tipper, tippee, amount, pachinko=False):
"""Given two unicodes, a Decimal, and a boolean, return a boolean.
If the tipper doesn't have enough in their Gittip account then we
return False. Otherwise we decrement tipper's balance and increment
tippee's *pending* balance by amount.
"""
typecheck( tipper, unicode
, tippee, unicode
, amount, Decimal
, pachinko, bool
)
with self.db.get_cursor() as cursor:
try:
self.debit_participant(cursor, tipper, amount)
except NegativeBalance:
return False
self.credit_participant(cursor, tippee, amount)
context = 'take' if pachinko else 'tip'
self.record_transfer(cursor, tipper, tippee, amount, context)
return True
示例8: clear
def clear(participant_id, stripe_customer_id):
typecheck(participant_id, unicode, stripe_customer_id, unicode)
# "Unlike other objects, deleted customers can still be retrieved through
# the API, in order to be able to track the history of customers while
# still removing their credit card details and preventing any further
# operations to be performed" https://stripe.com/docs/api#delete_customer
#
# Hmm ... should we protect against that in associate (above)?
#
# What this means though is (I think?) that we'll continue to be able to
# search for customers in the Stripe management UI by participant_id (which
# is stored as description in associate) even after the association is lost
# in our own database. This should be helpful for customer support.
customer = stripe.Customer.retrieve(stripe_customer_id)
customer.delete()
CLEAR = """\
UPDATE participants
SET stripe_customer_id=NULL
, last_bill_result=NULL
WHERE id=%s
"""
db.execute(CLEAR, (participant_id,))
示例9: get_user_info
def get_user_info(db, username, osm_api_url):
"""Get the given user's information from the DB or failing that, openstreetmap.
:param username:
A unicode string representing a username in OpenStreetMap.
:param osm_api_url:
URL of OpenStreetMap API.
:returns:
A dictionary containing OpenStreetMap specific information for the user.
"""
typecheck(username, (unicode, PathPart))
rec = db.one("""
SELECT user_info FROM elsewhere
WHERE platform='openstreetmap'
AND user_info->'username' = %s
""", (username,))
if rec is not None:
user_info = rec
else:
osm_user = requests.get("%s/user/%s" % (osm_api_url, username))
if osm_user.status_code == 200:
log("User %s found in OpenStreetMap but not in gittip." % username)
user_info = None
elif osm_user.status_code == 404:
raise Response(404,
"OpenStreetMap identity '{0}' not found.".format(username))
else:
log("OpenStreetMap api responded with {0}: {1}".format(status, content),
level=logging.WARNING)
raise Response(502, "OpenStreetMap lookup failed with %d." % status)
return user_info
示例10: charge
def charge(self, participant, amount):
"""Given dict and Decimal, return None.
This is the only place where we actually charge credit cards. Amount
should be the nominal amount. We'll compute Gittip's fee below this
function and add it to amount to end up with charge_amount.
"""
typecheck(participant, Participant, amount, Decimal)
username = participant.username
balanced_customer_href = participant.balanced_customer_href
typecheck(username, unicode, balanced_customer_href, (unicode, None))
# Perform some last-minute checks.
# ================================
if balanced_customer_href is None:
self.mark_missing_funding()
return # Participant has no funding source.
if not is_whitelisted(participant):
return # Participant not trusted.
# Go to Balanced.
# ===============
things = self.charge_on_balanced(username, balanced_customer_href, amount)
charge_amount, fee, error = things
amount = charge_amount - fee # account for possible rounding under
# charge_on_*
self.record_charge(amount, charge_amount, fee, error, username)
示例11: get_user_info
def get_user_info(login):
"""Get the given user's information from the DB or failing that, github.
:param login:
A unicode string representing a username in github.
:returns:
A dictionary containing github specific information for the user.
"""
typecheck(login, unicode)
rec = gittip.db.fetchone( "SELECT user_info FROM elsewhere "
"WHERE platform='github' "
"AND user_info->'login' = %s"
, (login,)
)
if rec is not None:
user_info = rec['user_info']
else:
url = "https://api.github.com/users/%s"
user_info = requests.get(url % login, params={
'client_id': os.environ.get('GITHUB_CLIENT_ID'),
'client_secret': os.environ.get('GITHUB_CLIENT_SECRET')
})
status = user_info.status_code
content = user_info.text
# Calculate how much of our ratelimit we have consumed
remaining = int(user_info.headers['x-ratelimit-remaining'])
limit = int(user_info.headers['x-ratelimit-limit'])
# thanks to from __future__ import division this is a float
percent_remaining = remaining/limit
log_msg = ''
log_lvl = None
# We want anything 50% or over
if 0.5 <= percent_remaining:
log_msg = ("{0}% of GitHub's ratelimit has been consumed. {1}"
" requests remaining.").format(percent_remaining * 100,
remaining)
if 0.5 <= percent_remaining < 0.8:
log_lvl = logging.WARNING
elif 0.8 <= percent_remaining < 0.95:
log_lvl = logging.ERROR
elif 0.95 <= percent_remaining:
log_lvl = logging.CRITICAL
if log_msg and log_lvl:
log(log_msg, log_lvl)
if status == 200:
user_info = json.loads(content)
elif status == 404:
raise Response(404,
"GitHub identity '{0}' not found.".format(login))
else:
log("Github api responded with {0}: {1}".format(status, content),
level=logging.WARNING)
raise Response(502, "GitHub lookup failed with %d." % status)
return user_info
示例12: authorize
def authorize(participant_id, pmt):
"""Given two unicodes, return a dict.
This function attempts to authorize the credit card details referenced by
pmt. If the attempt succeeds we cancel the transaction. If it fails we log
the failure. Even for failure we keep the payment_method_token, we don't
reset it to None/NULL. It's useful for loading the previous (bad) credit
card info from Samurai in order to prepopulate the form.
"""
typecheck(pmt, unicode, participant_id, unicode)
transaction = Processor.authorize(pmt, '1.00', custom=participant_id)
if transaction.errors:
last_bill_result = json.dumps(transaction.errors)
out = dict(transaction.errors)
else:
transaction.reverse()
last_bill_result = ''
out = {}
STANDING = """\
UPDATE participants
SET payment_method_token=%s
, last_bill_result=%s
WHERE id=%s
"""
db.execute(STANDING, (pmt, last_bill_result, participant_id))
return out
示例13: transfer
def transfer(tipper, tippee, amount):
"""Given two unicodes and a Decimal, return a boolean indicating success.
If the tipper doesn't have enough in their Gittip account then we return
False. Otherwise we decrement tipper's balance and increment tippee's
*pending* balance by amount.
"""
typecheck(tipper, unicode, tippee, unicode, amount, decimal.Decimal)
with db.get_connection() as conn:
cursor = conn.cursor()
try:
debit_participant(cursor, tipper, amount)
except ValueError:
return False
credit_participant(cursor, tippee, amount)
record_transfer(cursor, tipper, tippee, amount)
increment_payday(cursor, amount)
# Success.
# ========
conn.commit()
return True
示例14: capture_card_hold
def capture_card_hold(db, participant, amount, hold):
"""Capture the previously created hold on the participant's credit card.
"""
typecheck( hold, balanced.CardHold
, amount, Decimal
)
username = participant.username
assert participant.id == int(hold.meta['participant_id'])
route = ExchangeRoute.from_address(participant, 'balanced-cc', hold.card_href)
assert isinstance(route, ExchangeRoute)
cents, amount_str, charge_amount, fee = _prep_hit(amount)
amount = charge_amount - fee # account for possible rounding
e_id = record_exchange(db, route, amount, fee, participant, 'pre')
meta = dict(participant_id=participant.id, exchange_id=e_id)
try:
hold.capture(amount=cents, description=username, meta=meta)
record_exchange_result(db, e_id, 'succeeded', None, participant)
except Exception as e:
error = repr_exception(e)
record_exchange_result(db, e_id, 'failed', error, participant)
raise
hold.meta['state'] = 'captured'
hold.save()
log("Captured " + amount_str + " on Balanced for " + username)
示例15: get_img_src
def get_img_src(self, size=128):
"""Return a value for <img src="..." />.
Until we have our own profile pics, delegate. XXX Is this an attack
vector? Can someone inject this value? Don't think so, but if you make
it happen, let me know, eh? Thanks. :)
https://www.gittip.com/security.txt
"""
typecheck(size, int)
src = '/assets/%s/avatar-default.gif' % os.environ['__VERSION__']
github, twitter, bitbucket = self.get_accounts_elsewhere()
if github is not None:
# GitHub -> Gravatar: http://en.gravatar.com/site/implement/images/
if 'gravatar_id' in github.user_info:
gravatar_hash = github.user_info['gravatar_id']
src = "https://www.gravatar.com/avatar/%s.jpg?s=%s"
src %= (gravatar_hash, size)
elif twitter is not None:
# https://dev.twitter.com/docs/api/1/get/users/profile_image/%3Ascreen_name
if 'profile_image_url_https' in twitter.user_info:
src = twitter.user_info['profile_image_url_https']
# For Twitter, we don't have good control over size. We don't
# want the original, cause that can be huge. The next option is
# 73px(?!).
src = src.replace('_normal.', '_bigger.')
return src