def test_escaping(self): """ The built-in entity references are properly encoded. """ s = "&<>'\"" self.assertEqual(domish.escapeToXml(s), "&<>'\"") self.assertEqual(domish.escapeToXml(s, 1), "&<>'"")
def generate(self, feed, entry): news = domish.Element(('mimir:news', 'news')) if 'title' in entry: content = entry.title if 'source' in entry and 'title' in entry.source: content = "%s: %s" % (entry.source.title, content) if entry.title_detail.type == 'text/plain': content = domish.escapeToXml(content) news.addElement('title', content=content) if 'link' in entry: url = entry.get('feedburner_origlink', entry.link) if url: news.addElement('link', content=url) # Find a description. First try full text, then summary. content = None if 'content' in entry: content = entry.content[0] elif 'summary' in entry: content = entry.summary_detail if content: value = content.value if content.type == 'text/plain': value = domish.escapeToXml(value) news.addElement('description', content=value) return news
def _onCdata(self, data): if self.currElem != None: if len(self.currElem.children)==0: self.currRawElem = self.currRawElem + ">" + domish.escapeToXml(data) #self.currRawElem = self.currRawElem + ">" + data else: self.currRawElem = self.currRawElem + domish.escapeToXml(data) #self.currRawElem = self.currRawElem + data self.currElem.addContent(data)
def format(self, status): match, urls = self._matchStatus(status) if not match: return None notification = { 'title': status.user.screen_name, 'icon': status.user.profile_image_url, 'uri': ('https://twitter.com/%s/statuses/%d' % (status.user.screen_name.encode('utf-8'), status.id)), } # Twitter Entities on retweets have incorrect indices. Use the # retweeted status for rendering the plain text and html. if getattr(status, 'retweeted_status', None): notification['subtitle'] = status.retweeted_status.text else: notification['subtitle'] = status.text urls.sort(key=lambda url: url.indices.start, reverse=True) notification['html'] = notification['subtitle'] for url in urls: if getattr(url, 'display_url'): head = notification['subtitle'][0:url.indices.start] tail = notification['subtitle'][url.indices.end:] text = u''.join([head, url.display_url, tail]) notification['subtitle'] = text headHTML = notification['html'][0:url.indices.start] tailHTML = notification['html'][url.indices.end:] linkHRef = escapeToXml(url.url, isattrib=1) linkText = escapeToXml(url.display_url) link = u"<a href='%s'>%s</a>" % (linkHRef, linkText) html = u''.join([headHTML, link, tailHTML]) notification['html'] = html # Prefix the retweeted status explicitly. if getattr(status, 'retweeted_status', None): notification['subtitle'] = 'RT @%s: %s' % ( status.retweeted_status.user.screen_name, notification['subtitle']) notification['html'] = 'RT @%s: %s' % ( status.retweeted_status.user.screen_name, notification['html']) if getattr(status, 'image_url', None): notification['picture'] = status.image_url self._addVia(notification) return notification
def log(self, room, host, nick, elements): """ log room data based on given elements """ if not self._dbpool: self._createLogger() for e in elements: div_class = 'raw' if e.name == 'presence' and e.hasAttribute('type'): msg = str(nick) + ' has left the room' self.partRoom(str(nick),room,msg,host) elif e.name == 'presence' and not e.hasAttribute('type') and not getattr(e, 'status', None) and not getattr(e, 'show', None): self.joinRoom(nick, room, host) elif e.name == 'message' and getattr(e, 'subject', None): self.changeTopic(str(nick),room,str(e.subject),host) elif e.name == 'message' and e.hasAttribute('type') and e['type'] == 'groupchat' and getattr(e, 'body', None): msg = "" try: msg = unicode(nick) + u' : ' + e.body.__str__() except: msg = nick + ':' + '????? Unicode error' self.groupchat(str(nick),room,msg,host) else: msg = domish.escapeToXml(e.toXml()) self.error(nick,room,host,msg)
def log(self, room, host, nick, elements): # create or grab loggers for each to and from logger = self._getOrCreateLog(host+'/'+room) for e in elements: div_class = 'raw' if e.name == 'presence' and e.hasAttribute('type'): div_class = 'leave' msg = str(nick) + ' has left the room' elif e.name == 'presence' and not e.hasAttribute('type') and not getattr(e, 'status', None) and not getattr(e, 'show', None): div_class = 'join' msg = str(nick) + ' has joined the room' elif e.name == 'message' and getattr(e, 'subject', None): div_class = 'new_subject' msg = str(nick) + ' has changed the topic to ' + str(e.subject) elif e.name == 'message' and e.hasAttribute('type') and e['type'] == 'groupchat' and getattr(e, 'body', None): div_class = 'message' try: msg = unicode(nick) + u' : ' + e.body.__str__() except: msg = nick + ':' + '????? Unicode error' else: msg = domish.escapeToXml(e.toXml()) logger.msg("<span class='%s'><a name='%s'> %s </a></span>" % (div_class, str(time.time()), msg.encode('ascii', 'replace'), ))
def _onCdata(self, data): if self.currElem is not None: xml_string = domish.escapeToXml(data) if len(self.currElem.children) == 0: self.currRawElem += ">" + xml_string else: self.currRawElem += xml_string self.currElem.addContent(data)
def sendNotifications(self): """Formats and sends notification to the remote party""" message = "" for event in self.notificationQueue: params = ", ".join("%s=%s" % item for item in event.params.items()) params = escapeToXml(params) message += "[<b>%s</b> occurred (%s)]<br/>" % (event.name, params) self.notificationQueue = [] preStyle = '<font color="gray"><i>' postStyle = "</i></font>" self.say(preStyle + message + postStyle)
def sendNotifications(self): """Formats and sends notification to the remote party""" message = "" for event in self.notificationQueue: params = ', '.join("%s=%s" % item for item in event.params.items()) params = escapeToXml(params) message += "[<b>%s</b> occurred (%s)]<br/>" % (event.name, params) self.notificationQueue = [] preStyle = '<font color="gray"><i>' postStyle = '</i></font>' self.say(preStyle + message + postStyle)
def generate_text(self, name, data): element = domish.Element((None, name)) if data['value']: if data.type == "text/plain": element['type'] = 'text' element.addContent(domish.escapeToXml(data.value)) else: element['type'] = 'html' element.addContent(data.value) if 'language' in data and data['language']: element[(NS_XML, 'lang')] = data['language'] if 'base' in data: element[(NS_XML, 'base')] = data['base'] return element
def log(self, room, host, nick, elements): """ log room data based on given elements """ if not self._dbpool: self._createLogger() for e in elements: div_class = "raw" if e.name == "presence" and e.hasAttribute("type"): msg = str(nick) + " has left the room" self.partRoom(str(nick), room, msg, host) elif ( e.name == "presence" and not e.hasAttribute("type") and not getattr(e, "status", None) and not getattr(e, "show", None) ): self.joinRoom(nick, room, host) elif e.name == "message" and getattr(e, "subject", None): self.changeTopic(str(nick), room, str(e.subject), host) elif ( e.name == "message" and e.hasAttribute("type") and e["type"] == "groupchat" and getattr(e, "body", None) ): msg = "" try: msg = unicode(nick) + u" : " + e.body.__str__() except: msg = nick + ":" + "????? Unicode error" self.groupchat(str(nick), room, msg, host) else: msg = domish.escapeToXml(e.toXml()) self.error(nick, room, host, msg)
def log(self, room, host, nick, elements): # create or grab loggers for each to and from logger = self._getOrCreateLog(host + "/" + room) for e in elements: div_class = "raw" if e.name == "presence" and e.hasAttribute("type"): div_class = "leave" msg = str(nick) + " has left the room" elif ( e.name == "presence" and not e.hasAttribute("type") and not getattr(e, "status", None) and not getattr(e, "show", None) ): div_class = "join" msg = str(nick) + " has joined the room" elif e.name == "message" and getattr(e, "subject", None): div_class = "new_subject" msg = str(nick) + " has changed the topic to " + str(e.subject) elif ( e.name == "message" and e.hasAttribute("type") and e["type"] == "groupchat" and getattr(e, "body", None) ): div_class = "message" try: msg = unicode(nick) + u" : " + e.body.__str__() except: msg = nick + ":" + "????? Unicode error" else: msg = domish.escapeToXml(e.toXml()) logger.msg( "<span class='%s'><a name='%s'> %s </a></span>" % (div_class, str(time.time()), msg.encode("ascii", "replace")) )
def _onStartElement(self, name, attrs): # Generate a qname tuple from the provided name attr_str = '' prefix_str = '' defaultUri = None uri = None qname = name.split(" ") if len(qname) == 1: qname = ('', name) currentUri = None else: currentUri = qname[0] if self.currElem: defaultUri = self.currElem.defaultUri uri = self.currElem.uri if not defaultUri and currentUri in self.defaultNsStack: defaultUri = self.defaultNsStack[1] if defaultUri and currentUri != defaultUri: raw_xml = u"""<%s xmlns='%s'%s""" % (qname[1], qname[0], '%s') else: raw_xml = u"""<%s%s""" % (qname[1], '%s') # Process attributes for k, v in attrs.items(): if k.find(" ") != -1: aqname = k.split(" ") attrs[(aqname[0], aqname[1])] = v attr_prefix = self.getPrefix(aqname[0]) if not self.prefixInScope(attr_prefix): attr_str = attr_str + " xmlns:%s='%s'" % (attr_prefix, aqname[0]) self.prefixStack[-1].append(attr_prefix) attr_str = attr_str + " %s:%s='%s'" % ( attr_prefix, aqname[1], domish.escapeToXml(v, True)) del attrs[k] else: v = domish.escapeToXml(v, True) attr_str = attr_str + " " + k + "='" + v + "'" raw_xml = raw_xml % (attr_str, ) # Construct the new element e = domish.Element(qname, self.defaultNsStack[-1], attrs, self.localPrefixes) self.localPrefixes = {} # Document already started if self.documentStarted == 1: if self.currElem != None: if len(self.currElem.children) == 0 or isinstance( self.currElem.children[-1], domish.Element): if self.currRawElem[-1] != ">": self.currRawElem = self.currRawElem + ">" self.currElem.children.append(e) e.parent = self.currElem self.currRawElem = self.currRawElem + raw_xml self.currElem = e # New document else: self.currRawElem = u'' self.documentStarted = 1 self.DocumentStartEvent(e)
def testEscaping(self): s = "&<>'\"" self.assertEquals(domish.escapeToXml(s), "&<>'\"") self.assertEquals(domish.escapeToXml(s, 1), "&<>'"")
def _onStartElement(self, name, attrs): # Generate a qname tuple from the provided name attr_str = '' defaultUri = None uri = None qname = name.split(" ") if len(qname) == 1: qname = ('', name) currentUri = None else: currentUri = qname[0] if self.currElem: defaultUri = self.currElem.defaultUri uri = self.currElem.uri if not defaultUri and currentUri in self.defaultNsStack: defaultUri = self.defaultNsStack[1] if defaultUri and currentUri != defaultUri: raw_xml = u"""<%s xmlns='%s'%s""" % (qname[1], qname[0], '%s') else: raw_xml = u"""<%s%s""" % (qname[1], '%s') # Process attributes for k, v in attrs.items(): if k.find(" ") != -1: aqname = k.split(" ") attrs[(aqname[0], aqname[1])] = v attr_prefix = self.getPrefix(aqname[0]) if not self.prefixInScope(attr_prefix): attr_str = attr_str + " xmlns:%s='%s'" % (attr_prefix, aqname[0]) self.prefixStack[-1].append(attr_prefix) attr_str = attr_str + " %s:%s='%s'" % (attr_prefix, aqname[1], domish.escapeToXml(v, True)) del attrs[k] else: v = domish.escapeToXml(v, True) attr_str = attr_str + " " + k + "='" + v + "'" raw_xml = raw_xml % (attr_str,) # Construct the new element e = domish.Element(qname, self.defaultNsStack[-1], attrs, self.localPrefixes) self.localPrefixes = {} # Document already started if self.documentStarted == 1: if self.currElem != None: if len(self.currElem.children)==0 or isinstance(self.currElem.children[-1], domish.Element): if self.currRawElem[-1] != ">": self.currRawElem = self.currRawElem +">" self.currElem.children.append(e) e.parent = self.currElem self.currRawElem = self.currRawElem + raw_xml self.currElem = e # New document else: self.currRawElem = u'' self.documentStarted = 1 self.DocumentStartEvent(e)