def update_category_list(entry): catlist = E.load(file(os.path.join('static', 'catlist.atom'), 'r')).xml_root categories = entry.xml_root.get_children('category', ATOM10_NS) modified = False for category in categories: token = category.get_attribute_value('term', '') + category.get_attribute_value('scheme', '') if token not in _seen_categories: modified = True _seen_categories.append(token) attrs = {} term = category.get_attribute_value('term', None) if term: attrs[u'term'] = term scheme = category.get_attribute_value('scheme', None) if scheme: attrs[u'scheme'] = scheme label = category.get_attribute_value('label', None) if label: attrs[u'label'] = label E(u'category', attributes=attrs, namespace=ATOM10_NS, prefix=ATOM10_PREFIX, parent=catlist) if modified: updated = catlist.get_child('updated', ATOM10_NS) updated.xml_text = get_isodate() file(os.path.join('static', 'catlist.atom'), 'w').write(catlist.xml())
def setup_apps(): indexer = setup_indexer() service, conf = setup_store() initialize_queues(conf) servdoc = service.service update_home_page(servdoc) generate_default_resource_not_found(servdoc) generate_event_form_page(servdoc) update_category_list(E.load(file(os.path.join(base_dir, 'static', 'catlist.atom'), 'r').read()).xml_root) workspace = service.get_workspace('blog') root = Blog(service, indexer) root.pub = Service(service) col = workspace.get_collection('music') col.add_indexer(indexer) col.reload_members() update_collection_page(servdoc, col.feed_handler.public_feed, col.name_or_id) root.music = Section(col) root.pub.music = Store(col, strict=True) col = workspace.get_collection('event') col.add_indexer(indexer) col.reload_members() update_collection_page(servdoc, col.feed_handler.public_feed, col.name_or_id) root.event = Event(col) root.pub.event = Store(col, strict=True) return root, conf
def create_entry(self, text): uuid = generate_uuid_uri() entry = E.load('./entry.atom').xml_root entry.get_child('id', ns=entry.xml_ns).xml_text = uuid dt = get_isodate() entry.get_child('published', ns=entry.xml_ns).xml_text = dt entry.get_child('updated', ns=entry.xml_ns).xml_text = dt entry.get_child('content', ns=entry.xml_ns).xml_text = unicode(text) return uuid, entry
def load_profile(base_dir, atompub, profile_name): store = atompub.service.store info = store.get_content_info(profile_name, 'profile.xml') try: profile = store.fetch_content(info) except UnknownResource: print "Unknown profile: %s" % profile_name else: p = Profile.from_profile_element(E.load(profile).xml_root) return UserProfile(profile_name, p)
def _profile_process_request_body(): if cherrypy.request.method in ['POST', 'PUT']: cherrypy.request.process_request_body = False length = cherrypy.request.headers.get('content-length', 0) if not length: cherrypy.request.params['profile'] = None return cherrypy.request.params['profile'] = None content = cherrypy.request.rfile.read(int(length)) profile = Profile.from_profile_element(E.load(content).xml_root) field = profile.x.field_by_var('nickname') if field and field.values: profile_name = field.values[0] cherrypy.request.params['profile'] = UserProfile(profile_name, profile)
def make_entry(self, msg, node): uuid = generate_uuid_uri() entry = E.load('./entry.atom').xml_root entry.get_child('id', ns=entry.xml_ns).xml_text = uuid dt = get_isodate() entry.get_child('author', ns=entry.xml_ns).get_child('name', ns=entry.xml_ns).xml_text = unicode(self.profile.username) entry.get_child('published', ns=entry.xml_ns).xml_text = dt entry.get_child('updated', ns=entry.xml_ns).xml_text = dt entry.get_child('content', ns=entry.xml_ns).xml_text = unicode(msg) if node != self.pubsub_top_level_node: tag = extract_url_trail(node) E(u'category', namespace=entry.xml_ns, prefix=entry.xml_prefix, attributes={u'term': unicode(tag)}, parent=entry) return uuid, entry
def startElementNS(self, name, qname, attrs): prefix, uri, local_name = qname.getPrefix( ) or None, qname.getNamespaceURI() or None, qname.getLocalPart() e = Element(local_name, prefix=prefix, namespace=uri, parent=self._current_el) while attrs.hasNext(): attr = attrs.next() qname = attr.getName() prefix, uri, local_name = qname.getPrefix(), qname.getNamespaceURI( ), qname.getLocalPart() Attribute(local_name, attr.getValue(), prefix or None, uri or None, e) self._current_el = e self._current_level = self._current_level + 1
def _next(self): """ Sends the next available stanza and register the `_handle_response` method to each expected stanza. """ self._last = 0 self.processing = True stanza_id, stanza_type = self.sendable.next() self.current_test_name, stanza = self.current[SEND_MODE][(stanza_id, stanza_type)] stanza = stanza.xml(omit_declaration=True).replace("$(hostname)", self.hostname) e = E.load(stanza).xml_root for stanza_id, stanza_type in self.current[EXPECT_MODE]: self.keys.append((stanza_id, stanza_type)) self.client.register_on_iq(self._handle_response, id=stanza_id, type=stanza_type, once=True) self._last = time.time() self.client.send_stanza(e)
def startElementNS(self, name, qname, attrs): #print "$%s%s: %f" % (" " * self._current_level, name, time()) uri, local_name = name prefix = None if uri and uri in self._current_context: prefix = self._current_context[uri] #print "$$%s%s: %f" % (" " * self._current_level, name, time()) e = Element(local_name, prefix=prefix, namespace=uri, parent=self._current_el) #print "$$$%s%s: %f" % (" " * self._current_level, name, time()) for name, value in iter(attrs.items()): (namespace, local_name) = name qname = attrs.getQNameByName(name) prefix = self._split_qname(qname)[0] Attribute(local_name, value, prefix, namespace, e) #print "$$$$%s%s: %f" % (" " * self._current_level, name, time()) self._current_el = e self._current_level = self._current_level + 1
def __deserialize_fragment(self, current, parent): if current.attributes: for key in iter(current.attributes.keys()): attr = current.attributes[key] Attribute(attr.localName, attr.value, attr.prefix, attr.namespaceURI, parent) children_num = len(current.childNodes) children = iter(current.childNodes) for child in children: nt = child.nodeType if nt == xd.Node.TEXT_NODE: data = escape(child.data) if children_num == 1: parent.xml_text = data else: parent.xml_children.append(data) elif nt == xd.Node.CDATA_SECTION_NODE: parent.as_cdata = True data = child.data if children_num == 1: parent.xml_text = data else: parent.xml_children.append(data) elif nt == xd.Node.COMMENT_NODE: Comment(data=unicode(child.data), parent=parent) elif nt == xd.Node.PROCESSING_INSTRUCTION_NODE: PI(target=unicode(child.target), data=unicode(child.data), parent=parent) elif nt == xd.Node.ELEMENT_NODE: element = Element(name=child.localName, prefix=child.prefix, namespace=child.namespaceURI, parent=parent) self.__deserialize_fragment(child, element)
def _next(self): """ Sends the next available stanza and register the `_handle_response` method to each expected stanza. """ self._last = 0 self.processing = True stanza_id, stanza_type = self.sendable.next() self.current_test_name, stanza = self.current[SEND_MODE][(stanza_id, stanza_type)] stanza = stanza.xml(omit_declaration=True).replace( "$(hostname)", self.hostname) e = E.load(stanza).xml_root for stanza_id, stanza_type in self.current[EXPECT_MODE]: self.keys.append((stanza_id, stanza_type)) self.client.register_on_iq(self._handle_response, id=stanza_id, type=stanza_type, once=True) self._last = time.time() self.client.send_stanza(e)
def load(path): """ Loads a cot script given by ``path`` and returns the parsed stanzas as a list of dictionaries. The dictionnaries have two numeric keys: * 0 which represents the stanzas to be sent * 1 representing the expected stanzas The values of the dictionnaries are also dictionaries with keys being tuples (stanza_id, stanza_type). For each key the associated value is a list of `bridge.Element` instances, each representing a stanza. """ script = file(path, 'r') mode = SEND_MODE inside_command = False buf = [] stanzas = [] current_test_name = None for line in script: line = line.strip() if not inside_command: if not line: continue if line.startswith('#'): continue m = command_regex.match(line) if not m: continue result = m.groups() command = result[0] or result[2] if command == 'send': current_test_name = result[1] stanzas.append({SEND_MODE: {}, EXPECT_MODE: {}}) mode = SEND_MODE elif command == 'expect': mode = EXPECT_MODE inside_command = True elif line.endswith('}') and inside_command: inside_command = False xml = u'<root xmlns="%s">' % XMPP_CLIENT_NS xml += ''.join(buf) xml += u'</root>' root = E.load(xml).xml_root for child in root.xml_children: child.xml_parent = None stanza_id = child.get_attribute_value('id') stanza_type = child.get_attribute_value('type') stanzas[-1][mode][(stanza_id, stanza_type)] = (current_test_name, child) buf = [] else: buf.append(line) script.close() return stanzas