def fromXml(self, element): """ Get a Tune instance from an XML representation. This class method parses the given XML document into a L{Tune} instances. @param element: The XML tune document. @type element: object providing L{IElement<twisted.words.xish.domish.IElement>} @return: A L{Tune} instance or C{None} if C{element} was not a tune document. """ if element.uri != NS_TUNE or element.name != 'tune': return None tune = Tune() for child in element.elements(): if child.uri != NS_TUNE: continue if child.name in ('artist', 'source', 'title', 'track', 'uri'): setattr(tune, child.name, unicode(child)) elif child.name == 'length': try: tune.length = int(unicode(child)) except ValueError: pass return tune
def test_onVersion(self): """ Test response to incoming version request. """ self.stub = XmlStreamStub() self.protocol.xmlstream = self.stub.xmlstream self.protocol.send = self.stub.xmlstream.send self.protocol.connectionInitialized() iq = domish.Element((None, 'iq')) iq['from'] = '[email protected]/Home' iq['to'] = 'example.org' iq['type'] = 'get' iq.addElement((NS_VERSION, 'query')) self.stub.send(iq) response = self.stub.output[-1] self.assertEquals('[email protected]/Home', response['to']) self.assertEquals('example.org', response['from']) self.assertEquals('result', response['type']) self.assertEquals(NS_VERSION, response.query.uri) elements = list(domish.generateElementsQNamed(response.query.children, 'name', NS_VERSION)) self.assertEquals(1, len(elements)) self.assertEquals('Test', unicode(elements[0])) elements = list(domish.generateElementsQNamed(response.query.children, 'version', NS_VERSION)) self.assertEquals(1, len(elements)) self.assertEquals('0.1.0', unicode(elements[0]))
def test_availableLanguages(self): """ It should be possible to pass a sender address. """ self.protocol.available(JID('*****@*****.**'), show=u'chat', statuses={None: u'Talk to me!', 'nl': u'Praat met me!'}, priority=50) element = self.output[-1] self.assertEquals("*****@*****.**", element.getAttribute('to')) self.assertIdentical(None, element.getAttribute('type')) self.assertEquals(u'chat', unicode(element.show)) statuses = {} for status in element.elements(): if status.name == 'status': lang = status.getAttribute((NS_XML, 'lang')) statuses[lang] = unicode(status) self.assertIn(None, statuses) self.assertEquals(u'Talk to me!', statuses[None]) self.assertIn('nl', statuses) self.assertEquals(u'Praat met me!', statuses['nl']) self.assertEquals(u'50', unicode(element.priority))
def authenticate(self, database_name, username, password, mechanism): database_name = str(database_name) username = unicode(username) password = unicode(password) yield self.__auth_lock.acquire() try: if mechanism == "MONGODB-CR": auth_func = self.authenticate_mongo_cr elif mechanism == "SCRAM-SHA-1": auth_func = self.authenticate_scram_sha1 elif mechanism == "DEFAULT": if self.max_wire_version >= 3: auth_func = self.authenticate_scram_sha1 else: auth_func = self.authenticate_mongo_cr else: raise MongoAuthenticationError( "Unknown authentication mechanism: {0}".format(mechanism)) result = yield auth_func(database_name, username, password) defer.returnValue(result) finally: self.__auth_lock.release()
def test_observeWrites(self): """ L{FileLogObserver} writes to the given file when it observes events. """ with StringIO() as fileHandle: observer = FileLogObserver(fileHandle, lambda e: unicode(e)) event = dict(x=1) observer(event) self.assertEqual(fileHandle.getvalue(), unicode(event))
def onAuthSet(iq): """ Called when the initializer sent the authentication request. The server checks the credentials and responds with an empty result signalling success. """ self.assertEqual("user", unicode(iq.query.username)) self.assertEqual(sha1(b"12345secret").hexdigest(), unicode(iq.query.digest)) self.assertEqual("resource", unicode(iq.query.resource)) # Send server response response = xmlstream.toResponse(iq, "result") self.pipe.source.send(response)
def test_available(self): """ It should be possible to pass a sender address. """ self.protocol.available(JID('*****@*****.**'), show=u'chat', status=u'Talk to me!', priority=50) element = self.output[-1] self.assertEquals("*****@*****.**", element.getAttribute('to')) self.assertIdentical(None, element.getAttribute('type')) self.assertEquals(u'chat', unicode(element.show)) self.assertEquals(u'Talk to me!', unicode(element.status)) self.assertEquals(u'50', unicode(element.priority))
def _onPresenceAvailable(self, presence): entity = JID(presence["from"]) show = unicode(presence.show or '') if show not in ['away', 'xa', 'chat', 'dnd']: show = None statuses = self._getStatuses(presence) try: priority = int(unicode(presence.priority or '')) or 0 except ValueError: priority = 0 self.availableReceived(entity, show, statuses, priority)
def test_addElementContent(self): """ Content passed to addElement becomes character data on the new child. """ element = domish.Element((u"testns", u"foo")) child = element.addElement("bar", content=u"abc") self.assertEqual(u"abc", unicode(child))
def onResult(self, result): def reply(validity): reply = domish.Element((NS_DIALBACK, 'result')) reply['from'] = result['to'] reply['to'] = result['from'] reply['type'] = validity self.xmlstream.send(reply) def valid(xs): reply('valid') if not self.xmlstream.thisEntity: self.xmlstream.thisEntity = jid.internJID(receivingServer) self.xmlstream.otherEntity = jid.internJID(originatingServer) self.xmlstream.dispatch(self.xmlstream, xmlstream.STREAM_AUTHD_EVENT) def invalid(failure): log.err(failure) reply('invalid') receivingServer = result['to'] originatingServer = result['from'] key = unicode(result) d = self.service.validateConnection(receivingServer, originatingServer, self.xmlstream.sid, key) d.addCallbacks(valid, invalid) return d
def fromXml(self, element): """ Get a Mood instance from an XML representation. This class method parses the given XML document into a L{Mood} instances. @param element: The XML mood document. @type element: object providing L{IElement<twisted.words.xish.domish.IElement>} @return: A L{Mood} instance or C{None} if C{element} was not a mood document or if there was no mood value element. """ if element.uri != NS_MOOD or element.name != 'mood': return None value = None text = None for child in element.elements(): if child.uri != NS_MOOD: continue if child.name == 'text': text = unicode(child) else: value = child.name if value: return Mood(value, text) else: return None
def test_addContentNativeStringASCII(self): """ ASCII native strings passed to C{addContent} become the character data. """ element = domish.Element((u"testns", u"foo")) element.addContent("native") self.assertEqual(u"native", unicode(element))
def test_addContent(self): """ Unicode strings passed to C{addContent} become the character data. """ element = domish.Element((u"testns", u"foo")) element.addContent(u"unicode") self.assertEqual(u"unicode", unicode(element))
def _childParser_mucUser(self, element): """ Parse the MUC user extension element. """ for child in element.elements(): if child.uri != NS_MUC_USER: continue elif child.name == 'status': try: value = int(child.getAttribute('code')) statusCode = STATUS_CODE.lookupByValue(value) except (TypeError, ValueError): continue self.mucStatuses.add(statusCode) elif child.name == 'item': if child.hasAttribute('jid'): self.entity = jid.JID(child['jid']) self.nick = child.getAttribute('nick') self.affiliation = child.getAttribute('affiliation') self.role = child.getAttribute('role') for reason in child.elements(NS_MUC_ADMIN, 'reason'): self.reason = unicode(reason)
def _parseError(error, errorNamespace): """ Parses an error element. @param error: The error element to be parsed @type error: L{domish.Element} @param errorNamespace: The namespace of the elements that hold the error condition and text. @type errorNamespace: C{str} @return: Dictionary with extracted error information. If present, keys C{condition}, C{text}, C{textLang} have a string value, and C{appCondition} has an L{domish.Element} value. @rtype: C{dict} """ condition = None text = None textLang = None appCondition = None for element in error.elements(): if element.uri == errorNamespace: if element.name == 'text': text = unicode(element) textLang = element.getAttribute((NS_XML, 'lang')) else: condition = element.name else: appCondition = element return { 'condition': condition, 'text': text, 'textLang': textLang, 'appCondition': appCondition, }
def onVerify(self, verify): try: receivingServer = jid.JID(verify['from']).host originatingServer = jid.JID(verify['to']).host except (KeyError, jid.InvalidFormat): raise error.StreamError('improper-addressing') if originatingServer not in self.service.domains: raise error.StreamError('host-unknown') if (self.xmlstream.otherEntity and receivingServer != self.xmlstream.otherEntity.host): raise error.StreamError('invalid-from') streamID = verify.getAttribute('id', '') key = unicode(verify) calculatedKey = generateKey(self.service.secret, receivingServer, originatingServer, streamID) validity = (key == calculatedKey) and 'valid' or 'invalid' reply = domish.Element((NS_DIALBACK, 'verify')) reply['from'] = originatingServer reply['to'] = receivingServer reply['id'] = streamID reply['type'] = validity self.xmlstream.send(reply)
def fromElement(element): valueElements = list(domish.generateElementsQNamed(element.children, "value", NS_X_DATA)) if not valueElements: raise Error("Option has no value") label = element.getAttribute("label") return Option(unicode(valueElements[0]), label)
def test_toElementBooleanTextSingle(self): """ A boolean value should render to text if the field type is text-single. """ field = data_form.Field(var='test', value=True) element = field.toElement() self.assertEqual(u'true', unicode(element.value))
def formatWithCall(formatString, mapping): """ Format a string like L{unicode.format}, but: - taking only a name mapping; no positional arguments - with the additional syntax that an empty set of parentheses correspond to a formatting item that should be called, and its result C{str}'d, rather than calling C{str} on the element directly as normal. For example:: >>> formatWithCall("{string}, {function()}.", ... dict(string="just a string", ... function=lambda: "a function")) 'just a string, a function.' @param formatString: A PEP-3101 format string. @type formatString: L{unicode} @param mapping: A L{dict}-like object to format. @return: The string with formatted values interpolated. @rtype: L{unicode} """ return unicode( aFormatter.vformat(formatString, (), CallMapping(mapping)) )
def formatTime(when, timeFormat=timeFormatRFC3339, default=u"-"): """ Format a timestamp as text. Example:: >>> from time import time >>> from twisted.logger import formatTime >>> >>> t = time() >>> formatTime(t) u'2013-10-22T14:19:11-0700' >>> formatTime(t, timeFormat="%Y/%W") # Year and week number u'2013/42' >>> @param when: A timestamp. @type then: L{float} @param timeFormat: A time format. @type timeFormat: L{unicode} or L{None} @param default: Text to return if C{when} or C{timeFormat} is L{None}. @type default: L{unicode} @return: A formatted time. @rtype: L{unicode} """ if (timeFormat is None or when is None): return default else: tz = FixedOffsetTimeZone.fromLocalTimeStamp(when) datetime = DateTime.fromtimestamp(when, tz) return unicode(datetime.strftime(timeFormat))
def _getStatuses(self, presence): statuses = {} for element in presence.elements(): if element.name == 'status': lang = element.getAttribute((NS_XML, 'lang')) text = unicode(element) statuses[lang] = text return statuses
def test_observeFailureThatRaisesInGetTraceback(self): """ If the C{"log_failure"} key exists in an event, and contains an object that raises when you call it's C{getTraceback()}, then the observer appends a message noting the problem, instead of raising. """ with StringIO() as fileHandle: observer = FileLogObserver(fileHandle, lambda e: unicode(e)) event = dict(log_failure=object()) # object has no getTraceback() observer(event) output = fileHandle.getvalue() expected = ( "{0}\n(UNABLE TO OBTAIN TRACEBACK FROM EVENT)\n" .format(unicode(event)) ) self.assertEqual(output, expected)
def test_toElementJIDTextSingle(self): """ A JID value should render to text if field type is text-single. """ field = data_form.Field(fieldType='text-single', var='test', value=jid.JID(u'*****@*****.**')) element = field.toElement() self.assertEqual(u'*****@*****.**', unicode(element.value))
def _decode(self, data): if 'UTF-16' in self.encodings or 'UCS-2' in self.encodings: assert not len(data) & 1, 'UTF-16 must come in pairs for now' if self._prepend: data = self._prepend + data for encoding in self.encodings: data = unicode(data, encoding) return data
def test_sendAuthEmptyInitialResponse(self): """ Test starting authentication where the initial response is empty. """ self.init.initialResponse = b"" self.init.start() auth = self.output[0] self.assertEqual('=', unicode(auth))
def test_toElementBoolean(self): """ A boolean value should render to text. """ field = data_form.Field(fieldType='boolean', var='test', value=True) element = field.toElement() self.assertEqual(u'true', unicode(element.value))
def test_header(self): headers = shim.Headers([('Urgency', 'high')]) elements = list(headers.elements()) self.assertEquals(1, len(elements)) header = elements[0] self.assertEquals(NS_SHIM, header.uri) self.assertEquals('header', header.name) self.assertEquals('Urgency', header['name']) self.assertEquals('high', unicode(header))
def test_characterDataMixed(self): """ Mixing addChild with cdata and element, the first cdata is returned. """ element = domish.Element((u"testns", u"foo")) element.addChild(u"abc") element.addElement("bar") element.addChild(u"def") self.assertEqual(u"abc", unicode(element))
def drop_collection(self, name_or_collection, **kwargs): if isinstance(name_or_collection, Collection): name = name_or_collection._collection_name elif isinstance(name_or_collection, (bytes, unicode)): name = name_or_collection else: raise TypeError("TxMongo: name must be an instance of basestring or txmongo.Collection") yield self.command("drop", unicode(name), allowable_errors=["ns not found"], **kwargs)
def test_toElementInstructionsMultiple(self): """ Instructions render as one element per instruction, in order. """ form = data_form.Form('form', instructions=['Fill out this form!', 'no really']) element = form.toElement() elements = list(element.elements()) self.assertEqual(2, len(elements)) instructions1 = elements[0] instructions2 = elements[1] self.assertEqual('instructions', instructions1.name) self.assertEqual(NS_X_DATA, instructions1.uri) self.assertEqual('Fill out this form!', unicode(instructions1)) self.assertEqual('instructions', instructions2.name) self.assertEqual(NS_X_DATA, instructions2.uri) self.assertEqual('no really', unicode(instructions2))
def formatEventAsClassicLogText(event, formatTime=formatTime): """ Format an event as a line of human-readable text for, e.g. traditional log file output. The output format is C{u"{timeStamp} [{system}] {event}\\n"}, where: - C{timeStamp} is computed by calling the given C{formatTime} callable on the event's C{"log_time"} value - C{system} is the event's C{"log_system"} value, if set, otherwise, the C{"log_namespace"} and C{"log_level"}, joined by a C{u"#"}. Each defaults to C{u"-"} is not set. - C{event} is the event, as formatted by L{formatEvent}. Example:: >>> from __future__ import print_function >>> from time import time >>> from twisted.logger import formatEventAsClassicLogText >>> from twisted.logger import LogLevel >>> >>> formatEventAsClassicLogText(dict()) # No format, returns None >>> formatEventAsClassicLogText(dict(log_format=u"Hello!")) u'- [-#-] Hello!\\n' >>> formatEventAsClassicLogText(dict( ... log_format=u"Hello!", ... log_time=time(), ... log_namespace="my_namespace", ... log_level=LogLevel.info, ... )) u'2013-10-22T17:30:02-0700 [my_namespace#info] Hello!\\n' >>> formatEventAsClassicLogText(dict( ... log_format=u"Hello!", ... log_time=time(), ... log_system="my_system", ... )) u'2013-11-11T17:22:06-0800 [my_system] Hello!\\n' >>> @param event: an event. @type event: L{dict} @param formatTime: A time formatter @type formatTime: L{callable} that takes an C{event} argument and returns a L{unicode} @return: A formatted event, or C{None} if no output is appropriate. @rtype: L{unicode} or C{None} """ eventText = formatEvent(event) if not eventText: return None eventText = eventText.replace(u"\n", u"\n\t") timeStamp = formatTime(event.get("log_time", None)) system = event.get("log_system", None) if system is None: level = event.get("log_level", None) if level is None: levelName = u"-" else: levelName = level.name system = u"{namespace}#{level}".format( namespace=event.get("log_namespace", u"-"), level=levelName, ) else: try: system = unicode(system) except Exception: system = u"UNFORMATTABLE" return u"{timeStamp} [{system}] {event}\n".format( timeStamp=timeStamp, system=system, event=eventText, )
def _childParser_body(self, element): self.body = unicode(element)
def _childParser_subject(self, element): self.subject = unicode(element)
def _childParser_show(self, element): show = unicode(element) if show in ('chat', 'away', 'xa', 'dnd'): self.show = show
def __init__(self, factory, database_name, write_concern=None, codec_options=None): self.__factory = factory self.__write_concern = write_concern self.__codec_options = codec_options self._database_name = unicode(database_name)
def _flatten(request, write, root, slotData, renderFactory, inAttribute, inXML): """ Make C{root} slightly more flat by yielding all or part of it as strings or generators. @param request: A request object which will be passed to L{IRenderable.render}. @param root: An object to be made flatter. This may be of type C{unicode}, C{str}, L{raw}, L{Proto}, L{xml}, L{slot}, L{_PrecompiledSlot}, L{Tag}, L{URL}, L{tuple}, L{list}, L{GeneratorType}, L{Entity}, L{Deferred}, or it may be an object which is adaptable to L{IRenderable}. Deprecated backwards-compatibility support is also present for objects adaptable to L{IRenderer} or for which a flattener has been registered via L{registerFlattener}. @param slotData: A C{list} of C{dict} mapping C{str} slot names to data with which those slots will be replaced. @param inAttribute: A flag which, if set, indicates that C{str} and C{unicode} instances encountered must be quoted as for XML tag attribute values. @param inXML: A flag which, if set, indicates that C{str} and C{unicode} instances encountered must be quoted as for XML text node data. @return: An iterator which yields C{str}, L{Deferred}, and more iterators of the same type. """ if isinstance(root, WovenContext): # WovenContext is supported via the getFlattener case, but that is a # very slow case. Checking here is an optimization. It also lets us # avoid the deprecation warning which would be emitted whenever a # precompiled document was flattened, since those contain WovenContexts # for tags with render directives. -exarkun inAttribute = root.isAttrib inXML = True root = root.tag if isinstance(root, raw): root = compat.unicode(root) if inAttribute: root = root.replace('"', '"') write(root) elif isinstance(root, Proto): root = compat.unicode(root) if root: if root in allowSingleton: write(u'<' + root + u' />') else: write(u'<' + root + '></' + root + '>') elif isinstance(root, (compat.unicode, bytes)): write(escapedData(root, inAttribute, inXML)) elif isinstance(root, slot): slotValue = _getSlotValue(root.name, slotData) yield _flatten(request, write, slotValue, slotData, renderFactory, inAttribute, inXML) elif isinstance(root, _PrecompiledSlot): slotValue = _getSlotValue(root.name, slotData) yield _flatten(request, write, slotValue, slotData, renderFactory, root.isAttrib, inXML) elif isinstance(root, Tag): if root.pattern is Unset or root.pattern is None: slotData.append(root.slotData) if root.render is Unset: if not root.tagName: yield _flatten(request, write, root.children, slotData, renderFactory, False, True) else: write(u'<') if isinstance(root.tagName, bytes): tagName = root.tagName.decode('utf-8') else: tagName = compat.unicode(root.tagName) write(tagName) for k, v in sorted(root.attributes.items()): if isinstance(k, bytes): k = k.decode('utf-8') write(u" " + k + "=\"") yield _flatten(request, write, v, slotData, renderFactory, True, True) write(u"\"") if root.children or tagName not in allowSingleton: write(u'>') yield _flatten(request, write, root.children, slotData, renderFactory, False, True) write(u'</' + tagName + '>') else: write(u' />') else: if isinstance(root.render, directive): rendererName = root.render.name else: rendererName = root.render root = root.clone(False) del root._specials['render'] result = renderFactory.renderer(rendererName)(request, root) yield _flatten(request, write, result, slotData, renderFactory, None, inXML) slotData.pop() elif isinstance(root, URL): write(escapedData(compat.unicode(root), inAttribute, inXML)) elif isinstance(root, (tuple, list, GeneratorType)): for element in root: yield _flatten(request, write, element, slotData, renderFactory, inAttribute, inXML) elif isinstance(root, Entity): write(u'&#') write(root.num) write(u';') elif isinstance(root, xml): if isinstance(root.content, bytes): write(root.content.decode('utf-8')) else: write(root.content) elif isinstance(root, Deferred): yield root.addCallback( lambda result: (result, _flatten(request, write, result, slotData, renderFactory, inAttribute, inXML))) else: renderable = IRenderable(root, None) if renderable is not None: # [] for the slotData parameter of this call to _flatten means # slots returned by this renderable's render method won't be filled # with data which has so far accumulated in the slotData stack. # This seems like a reasonable thing to me, since a renderable is a # piece of Python code. It should be isolated from this other # stuff, which is primarily data. -exarkun yield _flatten(request, write, renderable.render(request), [], renderable, inAttribute, inXML) else: renderer = IRenderer(root, None) if renderer is not None: ctx = _ctxForRequest(request, slotData, None, inAttribute) results = [] synchronous = [] flattened = flattenFactory(renderer, ctx, results.append, lambda ign: None) def cbFlattened(result): synchronous.append(None) return (result, (compat.unicode(s) for s in results)) flattened.addCallback(cbFlattened) if synchronous: write(u''.join(map(str, results))) else: yield flattened else: flattener = getFlattener(root) if flattener is not None: ctx = _ctxForRequest(request, slotData, renderFactory, inAttribute) yield _flatten(request, write, flattener(root, ctx), slotData, renderFactory, False, False) else: raise UnsupportedType(root)
def __bytes__(self): """ Retrieve the first character data node as UTF-8 bytes. """ return unicode(self).encode('utf-8')
def __init__(self, name, num, description): self.name = compat.unicode(name) self.num = compat.unicode(num) self.description = description
def _childParser_priority(self, element): try: self.priority = int(unicode(element)) except ValueError: pass
def cbFlattened(result): synchronous.append(None) return (result, (compat.unicode(s) for s in results))
def _childParser_status(self, element): lang = element.getAttribute((NS_XML, 'lang'), None) text = unicode(element) self.statuses[lang] = text
def test_unicode(self): x = unicode('blah') y = jelly.unjelly(jelly.jelly(x)) self.assertEqual(x, y) self.assertEqual(type(x), type(y))
def __bytes__(self): return unicode(self).encode("utf-8")