def getOrganisations(self): """ Returns the list of organisations """ from xml.etree.cElementTree import XMLParser # need some error handling here (see equivalent function in # medin portal) xml = self.client.service.ws_edmo_get_list() parser = XMLParser() parser.feed(xml) tree = parser.close() organisations = [] for element in tree: n_code = int(element.findtext('n_code')) name = element.findtext('name') organisation = Organisation(n_code, name) for attr in ('address', 'city', 'zipcode', 'state', 'email', 'phone', 'fax', 'website'): value = element.findtext(attr).strip() if value: setattr(organisation, attr, value) organisation.country = element.findtext('c_country') organisations.append(organisation) return organisations
def main(): super_concat = False options, args = interface() xml = ElementTree().parse(options.input, parser=XMLParser(target=MyTreeBuilder())) # delete the older subs. models from the xml file for node in [ 'HKYModel', 'gtrModel', 'siteModel', 'patterns', 'treeLikelihood' ]: xml = delete_node(xml, node, 1) # delete the kappa and frequency parameters in 'operators' delete = [ 'kappa', 'frequencies', 'alpha', 'pInv', 'ac', 'ag', 'at', 'cg', 'gt' ] xml = delete_children_from_node(xml, xml.find('operators'), delete) xml = delete_children_from_node( xml, xml.find('mcmc').find('posterior').find('prior'), delete) # there are 2 log tags, disambiguated w/ id params. delete elements from # the one we want (fileLog) xml = delete_children_from_node(xml, get_file_log_section(xml), delete) # jettison some comments xml = comment_remover(xml, [ 'The general time reversible', 'HKY substitution model', 'site model', 'The unique patterns from 1 to end', 'npatterns=' ]) # load our substitution model information substitution_models = cPickle.load(open(options.subs)) snippets = ElementTree().parse(options.params, parser=XMLParser(target=MyTreeBuilder())) # insert patterns on a per locus basis insert_position = get_position(xml, 'alignment') xml = insert_patterns_for_locus(xml, substitution_models, insert_position, snippets) # insert substitution models on a per locus basis insert_position = get_position(xml, 'strictClockBranchRates') xml, insert_position = insert_models_for_locus(xml, substitution_models, insert_position, snippets) # insert site models on a per locus basis xml, insert_position = insert_site_models_for_locus( xml, substitution_models, insert_position, snippets) # insert tree likelihoods on a per locus basis xml, insert_position = insert_tree_likelihoods_for_locus( xml, substitution_models, insert_position, snippets) # insert operators xml = insert_operators_for_locus(xml, substitution_models, snippets) write(xml, options.output) pdb.set_trace() """model_names, site_names = get_xml_model_names(set(sub_models_from_modeltest.values()))
def open_data_file(path): with open(path, 'rb') as encoding_detect_file: file_text = encoding_detect_file.read() encoding = chardet.detect(file_text)['encoding'] parser = XMLParser(encoding=encoding) tree = parse(path, parser=parser) root = tree.getroot() return root
def _query(self, target, qtype): if qtype not in (QUERY_IPV4, QUERY_IPV6): return None endpoint = self._endpoint.format(target) r = requests.get(endpoint) r.raise_for_status() xp = XMLParser(encoding="utf-8") xp.feed(r.text.encode("utf-8")) root = xp.close() data = {} for e in root: if e.text == None: continue tag = e.tag.decode("utf-8") value = e.text.decode("utf-8").strip() if len(value) != 0: data[tag] = value return self._parse(data)
def xml(self, cmd, **kwargs): # NOTE: darcs is currently encoding agnostic and will print # patch metadata byte-for-byte, even in the XML changelog. etree = ElementTree() # While we are decoding the XML as latin-1 to be as liberal as # possible, etree will still raise an exception if any # non-printable characters are in the XML changelog. parser = XMLParser(encoding='latin-1') fp = self._run(cmd, **kwargs) etree.parse(fp, parser=parser) self.checkexit(fp.close()) return etree.getroot()
def open(self): register_namespace("mrng", "http://mremoteng.org") xmlp = XMLParser(encoding="utf-8") with open(self.filePath, 'r') as xml_file: tree = ET.parse(xml_file, parser=xmlp) self.root = tree.getroot()
def connect(self): self.__parser = XMLParser(target=XmppHandler(self)) self.send(('<?xml version=\'1.0\'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" to="%s" version="1.0">' % (_escape_attrib(self.__jid.domain()),)).encode(self.__encoding)) self.__stream_open = True
class XmppStream: def __init__(self, jid, handlers, encoding='utf-8', password=None, prefer_tls=False): self.__jid, self.__password = jid, password self.__prefer_tls = prefer_tls self.__handlers, self.__encoding = handlers, encoding self.__synced_feeder, self.__parser, self.__stream_open = None, None, False self.__iq_handlers = {} self.__iq_handler_sync = threading.Lock() def register_iq_handler(self, regid, handler): if regid != None: self.__iq_handler_sync.acquire() try: self.__iq_handlers[regid] = handler finally: self.__iq_handler_sync.release() def __do_callback(self, cb, args=()): result = cb(*args) if type(result) == types.GeneratorType: self.register_iq_handler(result.send(None), result) else: return result def _call_handler(self, stanza): key = (stanza.tag(), stanza.get_type()) if key in (('iq', 'result'), ('iq', 'error')): msgid = stanza.get_id() iq_handler = None try: with self.__iq_handler_sync: iq_handler = self.__iq_handlers[msgid] del self.__iq_handlers[msgid] except KeyError: pass if iq_handler != None: try: if stanza.get_type() == 'result': reg = iq_handler.send(stanza) else: reg = iq_handler.throw(IqError(stanza)) self.register_iq_handler(reg, iq_handler) except StopIteration: pass return if key[0] == 'iq': child = stanza.get_child() if child != None: key = key + (child.tag,) handler = None for i in range(len(key), 0, -1): try: handler = self.__handlers[key[:i]] except KeyError: pass if handler != None: self.__do_callback(handler, (stanza,)) break if handler == None: self.unhandled_stanza(stanza) def connect(self): self.__parser = XMLParser(target=XmppHandler(self)) self.send(('<?xml version=\'1.0\'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" to="%s" version="1.0">' % (_escape_attrib(self.__jid.domain()),)).encode(self.__encoding)) self.__stream_open = True def disconnect(self): if self.__stream_open: self.__stream_open = False self.send('</stream:stream>'.encode(self.__encoding)) def feed(self, data): self.__parser.feed(data) def close(self): self.__parser.close() if self.__parser != None: self.__synced_feeder, self.__parser = None, None self.closed() def _stream_start(self, elem): self.stream_start(elem) def __bind_and_start_session(self, elem): if elem.find('{urn:ietf:params:xml:ns:xmpp-bind}bind') != None: iq = Stanza.Iq(type='set', id='xmpplify_bind') bind = Element('{urn:ietf:params:xml:ns:xmpp-bind}bind') resource = Element('{urn:ietf:params:xml:ns:xmpp-bind}resource') resource.text = self.__jid.resource() if not resource.text: resource.text = 'xmpplify' bind.append(resource) iq.xmlnode().append(bind) self.send(iq.asbytes(self.__encoding)) stanza = yield 'xmpplify_bind' if elem.find('{urn:ietf:params:xml:ns:xmpp-session}session') != None: iq = Stanza.Iq(type='set', id='xmpplify_session') session = Element('{urn:ietf:params:xml:ns:xmpp-session}session') iq.xmlnode().append(session) self.send(iq.asbytes(self._encoding)) stanza = yield 'xmpplify_session' self.__do_callback(self.session_start) return def _stream_features(self, elem): sasl_mechanisms = [mech.text for mech in elem.findall('{urn:ietf:params:xml:ns:xmpp-sasl}mechanisms/{urn:ietf:params:xml:ns:xmpp-sasl}mechanism')] starttls = elem.find('{urn:ietf:params:xml:ns:xmpp-tls}starttls') if starttls != None: required = starttls.find('{urn:ietf:params:xml:ns:xmpp-tls}required') if required != None or self.__prefer_tls: starttls = Element('{urn:ietf:params:xml:ns:xmpp-tls}starttls') self.send(tobytes(starttls, self.__encoding)) return if ('PLAIN' in sasl_mechanisms) and (self.__password != None): # try SASL PLAIN authentication auth = Element('{urn:ietf:params:xml:ns:xmpp-sasl}auth') auth.set('mechanism', 'PLAIN') auth.text = base64.b64encode(('\x00%s\x00%s' % (self.__jid.user(), self.__password)).encode('utf-8')).decode('ascii').strip() self.send(tobytes(auth, self.__encoding)) return self.__do_callback(self.__bind_and_start_session, (elem,)) self.__do_callback(self.stream_features, (elem,)) def _stream_error(self, elem): self.stream_error(elem) self.disconnect() self.shutdown() def _stream_end(self, elem): self.stream_end(elem) self.disconnect() self.shutdown() def _sasl_challenge(self, elem): self.sasl_challenge(elem) def _sasl_failure(self, elem): self.sasl_failure(elem) def _sasl_success(self, elem): self.sasl_success(elem) XmppStream.connect(self) def _starttls_proceed(self, elem): self.starttls_proceed(elem) def _starttls_failure(self, elem): self.starttls_failure(elem) def stream_start(self, elem): pass def stream_features(self, elem): pass def stream_error(self, elem): pass def stream_end(self, elem): pass def sasl_challenge(self, elem): pass def sasl_failure(self, elem): pass def sasl_success(self, elem): pass def session_start(self): pass def starttls_proceed(self, elem): pass def starttls_failure(self, elem): pass def send(self, data): assert False, 'send method not implemented' def shutdown(self): pass def closed(self): pass def unhandled_stanza(self, stanza): pass
def connect(self): self.__parser = XMLParser(target=XmppHandler(self)) self.send(( '<?xml version=\'1.0\'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" to="%s" version="1.0">' % (_escape_attrib(self.__jid.domain()), )).encode(self.__encoding)) self.__stream_open = True
class XmppStream: def __init__(self, jid, handlers, encoding='utf-8', password=None, prefer_tls=False): self.__jid, self.__password = jid, password self.__prefer_tls = prefer_tls self.__handlers, self.__encoding = handlers, encoding self.__synced_feeder, self.__parser, self.__stream_open = None, None, False self.__iq_handlers = {} self.__iq_handler_sync = threading.Lock() def register_iq_handler(self, regid, handler): if regid != None: self.__iq_handler_sync.acquire() try: self.__iq_handlers[regid] = handler finally: self.__iq_handler_sync.release() def __do_callback(self, cb, args=()): result = cb(*args) if type(result) == types.GeneratorType: self.register_iq_handler(result.send(None), result) else: return result def _call_handler(self, stanza): key = (stanza.tag(), stanza.get_type()) if key in (('iq', 'result'), ('iq', 'error')): msgid = stanza.get_id() iq_handler = None try: with self.__iq_handler_sync: iq_handler = self.__iq_handlers[msgid] del self.__iq_handlers[msgid] except KeyError: pass if iq_handler != None: try: if stanza.get_type() == 'result': reg = iq_handler.send(stanza) else: reg = iq_handler.throw(IqError(stanza)) self.register_iq_handler(reg, iq_handler) except StopIteration: pass return if key[0] == 'iq': child = stanza.get_child() if child != None: key = key + (child.tag, ) handler = None for i in range(len(key), 0, -1): try: handler = self.__handlers[key[:i]] except KeyError: pass if handler != None: self.__do_callback(handler, (stanza, )) break if handler == None: self.unhandled_stanza(stanza) def connect(self): self.__parser = XMLParser(target=XmppHandler(self)) self.send(( '<?xml version=\'1.0\'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" to="%s" version="1.0">' % (_escape_attrib(self.__jid.domain()), )).encode(self.__encoding)) self.__stream_open = True def disconnect(self): if self.__stream_open: self.__stream_open = False self.send('</stream:stream>'.encode(self.__encoding)) def feed(self, data): self.__parser.feed(data) def close(self): self.__parser.close() if self.__parser != None: self.__synced_feeder, self.__parser = None, None self.closed() def _stream_start(self, elem): self.stream_start(elem) def __bind_and_start_session(self, elem): if elem.find('{urn:ietf:params:xml:ns:xmpp-bind}bind') != None: iq = Stanza.Iq(type='set', id='xmpplify_bind') bind = Element('{urn:ietf:params:xml:ns:xmpp-bind}bind') resource = Element('{urn:ietf:params:xml:ns:xmpp-bind}resource') resource.text = self.__jid.resource() if not resource.text: resource.text = 'xmpplify' bind.append(resource) iq.xmlnode().append(bind) self.send(iq.asbytes(self.__encoding)) stanza = yield 'xmpplify_bind' if elem.find('{urn:ietf:params:xml:ns:xmpp-session}session') != None: iq = Stanza.Iq(type='set', id='xmpplify_session') session = Element('{urn:ietf:params:xml:ns:xmpp-session}session') iq.xmlnode().append(session) self.send(iq.asbytes(self._encoding)) stanza = yield 'xmpplify_session' self.__do_callback(self.session_start) return def _stream_features(self, elem): sasl_mechanisms = [ mech.text for mech in elem.findall( '{urn:ietf:params:xml:ns:xmpp-sasl}mechanisms/{urn:ietf:params:xml:ns:xmpp-sasl}mechanism' ) ] starttls = elem.find('{urn:ietf:params:xml:ns:xmpp-tls}starttls') if starttls != None: required = starttls.find( '{urn:ietf:params:xml:ns:xmpp-tls}required') if required != None or self.__prefer_tls: starttls = Element('{urn:ietf:params:xml:ns:xmpp-tls}starttls') self.send(tobytes(starttls, self.__encoding)) return if ('PLAIN' in sasl_mechanisms) and (self.__password != None): # try SASL PLAIN authentication auth = Element('{urn:ietf:params:xml:ns:xmpp-sasl}auth') auth.set('mechanism', 'PLAIN') auth.text = base64.b64encode( ('\x00%s\x00%s' % (self.__jid.user(), self.__password) ).encode('utf-8')).decode('ascii').strip() self.send(tobytes(auth, self.__encoding)) return self.__do_callback(self.__bind_and_start_session, (elem, )) self.__do_callback(self.stream_features, (elem, )) def _stream_error(self, elem): self.stream_error(elem) self.disconnect() self.shutdown() def _stream_end(self, elem): self.stream_end(elem) self.disconnect() self.shutdown() def _sasl_challenge(self, elem): self.sasl_challenge(elem) def _sasl_failure(self, elem): self.sasl_failure(elem) def _sasl_success(self, elem): self.sasl_success(elem) XmppStream.connect(self) def _starttls_proceed(self, elem): self.starttls_proceed(elem) def _starttls_failure(self, elem): self.starttls_failure(elem) def stream_start(self, elem): pass def stream_features(self, elem): pass def stream_error(self, elem): pass def stream_end(self, elem): pass def sasl_challenge(self, elem): pass def sasl_failure(self, elem): pass def sasl_success(self, elem): pass def session_start(self): pass def starttls_proceed(self, elem): pass def starttls_failure(self, elem): pass def send(self, data): assert False, 'send method not implemented' def shutdown(self): pass def closed(self): pass def unhandled_stanza(self, stanza): pass