本文整理匯總了Python中twisted.python.compat.unicode方法的典型用法代碼示例。如果您正苦於以下問題:Python compat.unicode方法的具體用法?Python compat.unicode怎麽用?Python compat.unicode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類twisted.python.compat
的用法示例。
在下文中一共展示了compat.unicode方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: escapeForContent
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def escapeForContent(data):
"""
Escape some character or UTF-8 byte data for inclusion in an HTML or XML
document, by replacing metacharacters (C{&<>}) with their entity
equivalents (C{&<>}).
This is used as an input to L{_flattenElement}'s C{dataEscaper} parameter.
@type data: C{bytes} or C{unicode}
@param data: The string to escape.
@rtype: C{bytes}
@return: The quoted form of C{data}. If C{data} is unicode, return a utf-8
encoded string.
"""
if isinstance(data, unicode):
data = data.encode('utf-8')
data = data.replace(b'&', b'&'
).replace(b'<', b'<'
).replace(b'>', b'>')
return data
示例2: attributeEscapingDoneOutside
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def attributeEscapingDoneOutside(data):
"""
Escape some character or UTF-8 byte data for inclusion in the top level of
an attribute. L{attributeEscapingDoneOutside} actually passes the data
through unchanged, because L{writeWithAttributeEscaping} handles the
quoting of the text within attributes outside the generator returned by
L{_flattenElement}; this is used as the C{dataEscaper} argument to that
L{_flattenElement} call so that that generator does not redundantly escape
its text output.
@type data: C{bytes} or C{unicode}
@param data: The string to escape.
@return: The string, unchanged, except for encoding.
@rtype: C{bytes}
"""
if isinstance(data, unicode):
return data.encode("utf-8")
return data
示例3: __init__
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def __init__(self, url, fileOrName,
method=b'GET', postdata=None, headers=None,
agent=b"Twisted client", supportPartial=False,
timeout=0, cookies=None, followRedirect=True,
redirectLimit=20, afterFoundGet=False):
self.requestedPartial = 0
if isinstance(fileOrName, (str, unicode)):
self.fileName = fileOrName
self.file = None
if supportPartial and os.path.exists(self.fileName):
fileLength = os.path.getsize(self.fileName)
if fileLength:
self.requestedPartial = fileLength
if headers == None:
headers = {}
headers[b"range"] = b"bytes=" + intToBytes(fileLength) + b"-"
else:
self.file = fileOrName
HTTPClientFactory.__init__(
self, url, method=method, postdata=postdata, headers=headers,
agent=agent, timeout=timeout, cookies=cookies,
followRedirect=followRedirect, redirectLimit=redirectLimit,
afterFoundGet=afterFoundGet)
示例4: test_nameEncoding
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def test_nameEncoding(self):
"""
Passing L{unicode} to any function that takes a header name will encode
said header name as ISO-8859-1.
"""
h = Headers()
# We set it using a Unicode string.
h.setRawHeaders(u"\u00E1", [b"foo"])
# It's encoded to the ISO-8859-1 value, which we can use to access it
self.assertTrue(h.hasHeader(b"\xe1"))
self.assertEqual(h.getRawHeaders(b"\xe1"), [b'foo'])
# We can still access it using the Unicode string..
self.assertTrue(h.hasHeader(u"\u00E1"))
示例5: _encodeValues
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def _encodeValues(self, values):
"""
Encode a L{list} of header values to a L{list} of UTF-8 encoded
bytestrings if required.
@param values: A list of HTTP header values.
@type values: L{list} of L{bytes} or L{unicode} (mixed types allowed)
@return: C{values}, with each item encoded if required
@rtype: L{list} of L{bytes}
"""
newValues = []
for value in values:
newValues.append(self._encodeValue(value))
return newValues
示例6: addRawHeader
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def addRawHeader(self, name, value):
"""
Add a new raw value for the given header.
@type name: L{bytes} or L{unicode}
@param name: The name of the header for which to set the value.
@type value: L{bytes} or L{unicode}
@param value: The value to set for the named header.
"""
values = self.getRawHeaders(name)
if values is not None:
values.append(value)
else:
values = [value]
self.setRawHeaders(name, values)
示例7: getRawHeaders
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def getRawHeaders(self, name, default=None):
"""
Returns a list of headers matching the given name as the raw string
given.
@type name: L{bytes} or L{unicode}
@param name: The name of the HTTP header to get the values of.
@param default: The value to return if no header with the given C{name}
exists.
@rtype: L{list} of strings, same type as C{name} (except when
C{default} is returned).
@return: If the named header is present, a L{list} of its
values. Otherwise, C{default}.
"""
encodedName = self._encodeName(name)
values = self._rawHeaders.get(encodedName, default)
if isinstance(name, unicode) and values is not default:
return self._decodeValues(values)
return values
示例8: _stringTags
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def _stringTags(self, tags):
"""
Converts a tag dictionary to a string.
@param tags: The tag dict passed to sendMsg.
@rtype: L{unicode}
@return: IRCv3-format tag string
"""
self._validateTags(tags)
tagStrings = []
for tag, value in tags.items():
if value:
tagStrings.append("%s=%s" % (tag, self._escapeTagValue(value)))
else:
tagStrings.append(tag)
return ";".join(tagStrings)
示例9: privmsg
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def privmsg(self, sender, recip, message):
"""
Send a message to a channel or user
@type sender: C{str} or C{unicode}
@param sender: Who is sending this message. Should be of the form
username!ident@hostmask (unless you know better!).
@type recip: C{str} or C{unicode}
@param recip: The recipient of this message. If a channel, it must
start with a channel prefix.
@type message: C{str} or C{unicode}
@param message: The message being sent.
"""
self.sendCommand("PRIVMSG", (recip, ":%s" % (lowQuote(message),)), sender)
示例10: notice
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def notice(self, sender, recip, message):
"""
Send a "notice" to a channel or user.
Notices differ from privmsgs in that the RFC claims they are different.
Robots are supposed to send notices and not respond to them. Clients
typically display notices differently from privmsgs.
@type sender: C{str} or C{unicode}
@param sender: Who is sending this message. Should be of the form
username!ident@hostmask (unless you know better!).
@type recip: C{str} or C{unicode}
@param recip: The recipient of this message. If a channel, it must
start with a channel prefix.
@type message: C{str} or C{unicode}
@param message: The message being sent.
"""
self.sendCommand("NOTICE", (recip, ":%s" % (message,)), sender)
示例11: topicAuthor
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def topicAuthor(self, user, channel, author, date):
"""
Send the author of and time at which a topic was set for the given
channel.
This sends a 333 reply message, which is not part of the IRC RFC.
@type user: C{str} or C{unicode}
@param user: The user receiving the topic. Only their nickname, not
the full hostmask.
@type channel: C{str} or C{unicode}
@param channel: The channel for which this information is relevant.
@type author: C{str} or C{unicode}
@param author: The nickname (without hostmask) of the user who last set
the topic.
@type date: C{int}
@param date: A POSIX timestamp (number of seconds since the epoch) at
which the topic was last set.
"""
self.sendLine(':%s %d %s %s %s %d' % (
self.hostname, 333, user, channel, author, date))
示例12: part
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def part(self, who, where, reason=None):
"""
Send a part message.
@type who: C{str} or C{unicode}
@param who: The name of the user joining. Should be of the form
username!ident@hostmask (unless you know better!).
@type where: C{str} or C{unicode}
@param where: The channel the user is joining.
@type reason: C{str} or C{unicode}
@param reason: A string describing the misery which caused this poor
soul to depart.
"""
if reason:
self.sendLine(":%s PART %s :%s" % (who, where, reason))
else:
self.sendLine(":%s PART %s" % (who, where))
示例13: channelMode
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def channelMode(self, user, channel, mode, *args):
"""
Send information about the mode of a channel.
@type user: C{str} or C{unicode}
@param user: The user receiving the name list. Only their nickname,
not the full hostmask.
@type channel: C{str} or C{unicode}
@param channel: The channel for which this is the namelist.
@type mode: C{str}
@param mode: A string describing this channel's modes.
@param args: Any additional arguments required by the modes.
"""
self.sendLine(":%s %s %s %s %s %s" % (
self.hostname, RPL_CHANNELMODEIS, user, channel, mode, ' '.join(args)))
示例14: _cbAuthQuery
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def _cbAuthQuery(self, iq):
jid = self.xmlstream.authenticator.jid
password = _coercedUnicode(self.xmlstream.authenticator.password)
# Construct auth request
reply = xmlstream.IQ(self.xmlstream, "set")
reply.addElement(("jabber:iq:auth", "query"))
reply.query.addElement("username", content = jid.user)
reply.query.addElement("resource", content = jid.resource)
# Prefer digest over plaintext
if DigestAuthQry.matches(iq):
digest = xmlstream.hashPassword(self.xmlstream.sid, password)
reply.query.addElement("digest", content=unicode(digest))
else:
reply.query.addElement("password", content = password)
d = reply.send()
d.addCallbacks(self._cbAuth, self._ebAuth)
return d
示例15: XMPPClientFactory
# 需要導入模塊: from twisted.python import compat [as 別名]
# 或者: from twisted.python.compat import unicode [as 別名]
def XMPPClientFactory(jid, password):
"""
Client factory for XMPP 1.0 (only).
This returns a L{xmlstream.XmlStreamFactory} with an L{XMPPAuthenticator}
object to perform the stream initialization steps (such as authentication).
@see: The notes at L{XMPPAuthenticator} describe how the L{jid} and
L{password} parameters are to be used.
@param jid: Jabber ID to connect with.
@type jid: L{jid.JID}
@param password: password to authenticate with.
@type password: L{unicode}
@return: XML stream factory.
@rtype: L{xmlstream.XmlStreamFactory}
"""
a = XMPPAuthenticator(jid, password)
return xmlstream.XmlStreamFactory(a)