本文整理汇总了Python中r2.lib.filters._force_utf8函数的典型用法代码示例。如果您正苦于以下问题:Python _force_utf8函数的具体用法?Python _force_utf8怎么用?Python _force_utf8使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_force_utf8函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: change_to_str
def change_to_str(change_tuple):
attr, newval, oldval = change_tuple
if attr in ('StartDate', 'EndDate'):
newval = date_from_adzerk(newval)
oldval = date_from_adzerk(oldval)
return '%s: %s -> %s' % (attr, _force_utf8(oldval), _force_utf8(newval))
示例2: _key_from_url
def _key_from_url(cls, url):
if not utils.domain(url) in g.case_sensitive_domains:
keyurl = _force_utf8(UrlParser.base_url(url.lower()))
else:
# Convert only hostname to lowercase
up = UrlParser(url)
up.hostname = up.hostname.lower()
keyurl = _force_utf8(UrlParser.base_url(up.unparse()))
return keyurl
示例3: send_html_email
def send_html_email(to_addr, from_addr, subject, html, subtype="html", from_full='', session=None):
from r2.lib.filters import _force_utf8
# Open a session if we don't already have one.
if not session:
session = open_smtp_session()
close_session = True
else:
close_session = False
if from_full == '':
from_full = from_addr
# Compose the message headers.
msg = MIMEText(_force_utf8(html), subtype)
msg["Subject"] = subject
msg["From"] = from_full
msg["To"] = to_addr
# Send the mail.
session.sendmail(from_addr, to_addr, msg.as_string())
# Close down the session if we opened it.
if close_session:
session.quit()
示例4: _conv
def _conv(s):
if isinstance(s, str):
return s
elif isinstance(s, unicode):
return _force_utf8(s)
else:
return str(s)
示例5: add_request_info
def add_request_info(select):
from pylons import request
from r2.lib import filters
def sanitize(txt):
return (
_spaces.sub(" ", txt)
.replace("/", "|")
.replace("-", "_")
.replace(";", "")
.replace("*", "")
.replace(r"/", "")
)
s = StringIO.StringIO()
traceback.print_stack(file=s)
tb = s.getvalue()
if tb:
tb = tb.split("\n")[0::2]
tb = [x.split("/")[-1] for x in tb if "/r2/" in x]
tb = "\n".join(tb[-15:-2])
try:
if hasattr(request, "path") and hasattr(request, "ip") and hasattr(request, "user_agent"):
comment = "/*\n%s\n%s\n%s\n*/" % (
tb or "",
filters._force_utf8(sanitize(request.fullpath)),
sanitize(request.ip),
)
return select.prefix_with(comment)
except UnicodeDecodeError:
pass
return select
示例6: log_text
def log_text(classification, text=None, level="info"):
"""Send some log text to log_q for appearance in the streamlog.
This is deprecated. All logging should be done through python's stdlib
logging library.
"""
from r2.lib import amqp
from r2.lib.filters import _force_utf8
if text is None:
text = classification
if level not in ("debug", "info", "warning", "error"):
print "What kind of loglevel is %s supposed to be?" % level
level = "error"
d = _default_dict()
d["type"] = "text"
d["level"] = level
d["text"] = _force_utf8(text)
d["classification"] = classification
amqp.add_item(QUEUE_NAME, cPickle.dumps(d))
示例7: valid_password
def valid_password(a, password):
# bail out early if the account or password's invalid
if not hasattr(a, 'name') or not hasattr(a, 'password') or not password:
return False
# standardize on utf-8 encoding
password = filters._force_utf8(password)
# this is really easy if it's a sexy bcrypt password
if a.password.startswith('$2a$'):
expected_hash = bcrypt.hashpw(password, a.password)
if constant_time_compare(a.password, expected_hash):
return a
return False
# alright, so it's not bcrypt. how old is it?
# if the length of the stored hash is 43 bytes, the sha-1 hash has a salt
# otherwise it's sha-1 with no salt.
salt = ''
if len(a.password) == 43:
salt = a.password[:3]
expected_hash = passhash(a.name, password, salt)
if not constant_time_compare(a.password, expected_hash):
return False
# since we got this far, it's a valid password but in an old format
# let's upgrade it
a.password = bcrypt_password(password)
a._commit()
return a
示例8: process_response
def process_response(self):
data = request.POST
transaction_id = 'RG%s' % data['transaction_id']
pennies = int(data['pennies'])
months = int(data['months'])
status = 'succeeded'
buyer_name = data['buyer']
goldtype = data['goldtype']
buyer = Account._by_name(buyer_name)
blob = {
'goldtype': goldtype,
'account_id': buyer._id,
'account_name': buyer.name,
'status': 'initialized',
}
if goldtype == 'gift':
blob['recipient'] = data['recipient']
giftmessage = data.get('giftmessage', None)
blob['giftmessage'] = _force_utf8(giftmessage)
signed = data.get('signed')
blob['signed'] = True if signed == 'True' else False
passthrough = generate_blob(blob)
return status, passthrough, transaction_id, pennies, months
示例9: add_request_info
def add_request_info(select):
from pylons import request
from r2.lib import filters
def sanitize(txt):
return _spaces.sub(' ', txt).replace("/", "|").replace("-", "_").replace(';', "").replace("*", "").replace(r"/", "")
s = StringIO.StringIO()
traceback.print_stack( file = s)
tb = s.getvalue()
if tb:
tb = tb.split('\n')[0::2]
tb = [x.split('/')[-1] for x in tb if "/r2/" in x]
tb = '\n'.join(tb[-15:-2])
try:
if (hasattr(request, 'path') and
hasattr(request, 'ip') and
hasattr(request, 'user_agent')):
comment = '/*\n%s\n%s\n%s\n*/' % (
tb or "",
filters._force_utf8(sanitize(request.fullpath)),
sanitize(request.ip))
return select.prefix_with(comment)
except UnicodeDecodeError:
pass
return select
示例10: send_html_email
def send_html_email(to_addr, from_addr, subject, html,
subtype="html", attachments=None):
from r2.lib.filters import _force_utf8
if not attachments:
attachments = []
msg = MIMEMultipart()
msg.attach(MIMEText(_force_utf8(html), subtype))
msg["Subject"] = subject
msg["From"] = from_addr
msg["To"] = to_addr
for attachment in attachments:
part = MIMEBase('application', "octet-stream")
part.set_payload(attachment['contents'])
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment',
filename=attachment['name'])
msg.attach(part)
session = smtplib.SMTP(g.smtp_server)
if g.smtp_username and g.smtp_password:
try:
session.login(g.smtp_username, g.smtp_password)
except (smtplib.SMTPHeloError, smtplib.SMTPAuthenticationError,
smtplib.SMTPException):
print "Failed to login smtp server"
traceback.print_exc(file = sys.stdout)
email.set_sent(rejected = True)
session.sendmail(from_addr, to_addr, msg.as_string())
session.quit()
示例11: process_response
def process_response(self):
data = request.POST
transaction_id = 'RG%s' % data['transaction_id']
pennies = int(data['pennies'])
months = int(data['months'])
status = 'succeeded'
goldtype = data['goldtype']
buyer = Account._by_name(data['buyer'])
if goldtype == 'gift':
gift_kw = {
'recipient': Account._by_name(data['recipient']),
'giftmessage': _force_utf8(data.get('giftmessage', None)),
'signed': data.get('signed') == 'True',
}
else:
gift_kw = {}
webhook = Webhook(
transaction_id=transaction_id,
pennies=pennies,
months=months,
goldtype=goldtype,
buyer=buyer,
**gift_kw)
return status, webhook
示例12: process_response
def process_response(self):
data = request.POST
transaction_id = "RG%s" % data["transaction_id"]
pennies = int(data["pennies"])
months = int(data["months"])
status = "succeeded"
buyer_name = data["buyer"]
goldtype = data["goldtype"]
buyer = Account._by_name(buyer_name)
blob = {"goldtype": goldtype, "account_id": buyer._id, "account_name": buyer.name, "status": "initialized"}
if goldtype == "gift":
blob["recipient"] = data["recipient"]
giftmessage = data.get("giftmessage", None)
blob["giftmessage"] = _force_utf8(giftmessage)
signed = data.get("signed")
blob["signed"] = True if signed == "True" else False
passthrough = generate_blob(blob)
return status, passthrough, transaction_id, pennies, months
示例13: by_url_key
def by_url_key(cls, url):
maxlen = 250
template = "byurl(%s,%s)"
keyurl = _force_utf8(base_url(url.lower()))
hexdigest = md5(keyurl).hexdigest()
usable_len = maxlen - len(template) - len(hexdigest)
return template % (hexdigest, keyurl[:usable_len])
示例14: get_domain_links
def get_domain_links(domain, sort, time):
from r2.lib.db import operators
q = Link._query(operators.domain(Link.c.url) == filters._force_utf8(domain), sort=db_sort(sort), data=True)
if time != "all":
q._filter(db_times[time])
return make_results(q)
示例15: run
def run(self, username):
if username:
try:
name = _force_utf8(username)
return Account._by_name(name)
except (TypeError, UnicodeEncodeError, NotFound):
return self.error(errors.USER_DOESNT_EXIST)
self.error()