Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_to_text():
assert to_text(6) == six.text_type(6)
assert to_text(b"aa") == "aa"
assert to_text("cc") == "cc"
if six.PY2:
assert to_text(u"喵") == u"喵"
assert to_text("喵") == u"喵"
def test_to_text():
assert to_text(6) == six.text_type(6)
assert to_text(b"aa") == "aa"
assert to_text("cc") == "cc"
if six.PY2:
assert to_text(u"喵") == u"喵"
assert to_text("喵") == u"喵"
class TextReply(WeChatReply):
TEMPLATE = to_text(
"""
{time}
<content></content>
"""
)
class ImageReply(WeChatReply):
TEMPLATE = to_text(
"""
{time}
<img>
"""
)
class VoiceReply(WeChatReply):
TEMPLATE = to_text(
if 'account' in self._args:
return to_text(
"""
{time}
"""
)
else:
return to_text(
"""
class ArticlesReply(WeChatReply):
TEMPLATE = to_text("""
{time}
<content></content>
{count}
{items}
{flag}
""")
ITEM_TEMPLATE = to_text("""
<title><![CDATA[{title}]]></title>
""")
def __init__(self, message=None, star=False, **kwargs):
super(ArticlesReply, self).__init__(message, star, **kwargs)
self._articles = []
def add_article(self, article):
if len(self._articles) >= 10:
raise AttributeError("Can't add more than 10 articles"
" in an ArticlesReply")
kwargs["flag"] = 0
args = dict()
for k, v in kwargs.items():
if is_string(v):
v = to_text(v)
args[k] = v
self._args = args
def render(self):
raise NotImplementedError()
class TextReply(WeChatReply):
TEMPLATE = to_text("""
{time}
<content></content>
{flag}
""")
def render(self):
return TextReply.TEMPLATE.format(**self._args)
class ArticlesReply(WeChatReply):
TEMPLATE = to_text("""
:param app_id: 微信公众平台的 AppID
:return: 解密后的字符串
"""
text = to_binary(text)
decryptor = self.cipher.decryptor()
plain_text = decryptor.update(base64.b64decode(text)
) + decryptor.finalize()
padding = byte2int(plain_text, -1)
content = plain_text[16:-padding]
xml_len = socket.ntohl(struct.unpack("I", content[:4])[0])
xml_content = content[4:xml_len + 4]
from_appid = content[xml_len + 4:]
if to_text(from_appid) != app_id:
raise AppIdValidationError(text, app_id)
return xml_content
def process_args(self, kwargs):
args = defaultdict(str)
for k, v in kwargs.items():
if is_string(v):
v = to_text(v)
args[k] = v
return args
def render(self):
items = []
for article in self._articles:
items.append(ArticlesReply.ITEM_TEMPLATE.format(
title=to_text(article.title),
description=to_text(article.description),
img=to_text(article.img),
url=to_text(article.url)
))
self._args["items"] = ''.join(items)
self._args["count"] = len(items)
if "content" not in self._args:
self._args["content"] = ''
return ArticlesReply.TEMPLATE.format(**self._args)