def __connect(self, username, password, caller): payload = ( '<?xml version="1.0"?>\n' '<xf>\n' f' <request caller="{caller}">\n' f' <statement command="connect" username="******" password="******"/>\n' ' </request>\n' '</xf>') session = Session() response = session.post(self._url, payload) debug(response.text) root = XML(response.content) if next(root.getiterator('status')).text == 'OK': return session, next(root.getiterator('sessionid')).text raise Exception( next(root.getiterator('message')).text.split(';', 1)[0])
def extractNamespaces2(doc): """Extract xmlns:prefix="namespace" pairs from the root element of the XML document and return a dictionary. The default namespace (xmlns="ns") is also extracted and saved under the null ('') prefix. """ ns = {} tree = XML(doc) for elt in tree.getiterator(): for key, ns in elt.attrib.items(): if key.startswith('xmlns'): if key.find(':') > 0: junk, prefix = key.split(':') else: prefix = '_default' ns[prefix] = ns return ns
def __call__(self, session, response): debug(response.text) root = XML(response.content) res = xml_dict(next(root.getiterator('reactions')), ['sup', 'sub', 'i', 'b'])['reaction'] response.data = data = [] if isinstance(res, list): for x in res: try: data.append(self.__parser(x)) except (KeyError, StopIteration, ValueError) as e: print(e) else: try: data.append(self.__parser(res)) except (KeyError, StopIteration, ValueError) as e: print(e)
def search(self, query, context=None, dbname='RX', no_coresult=False, worker=False, single_step=False, without_trash=True, **kwargs): options = [] if no_coresult: options.append('NO_CORESULT') if worker: options.append('WORKER') if isinstance(query, MoleculeContainer): query = self.__mol2query(query, kwargs) if context == 'R': options.extend(self.__options(kwargs)) else: if context is None: context = 'S' elif context != 'S': raise ValueError('Invalid context') if single_step: raise ValueError( 'single step applicable for reactions only') if without_trash: without_trash = False elif isinstance(query, ReactionContainer): context = 'R' query = self.__rxn2query(query, kwargs) elif not isinstance(query, str): raise ValueError('Invalid query') elif context not in ('S', 'R'): raise ValueError('Invalid context value') elif context == 'S': if without_trash: without_trash = False if single_step: raise ValueError('single step applicable for reactions only') payload = ( '<?xml version="1.0" encoding="UTF-8"?>\n' '<xf>\n' f' <request caller="{self._caller}" sessionid="{self._sessionid}">\n' ' <statement command="select"/>\n' ' <select_list><select_item/></select_list>\n' f' <from_clause dbname="{dbname}" context="{context}"/>\n' f' <where_clause>{query}{single_step and " and RXD.STP=1" or ""}</where_clause>\n' ' <order_by_clause></order_by_clause>\n' f' <options>{",".join(options)}</options>\n' ' </request>\n' '</xf>') response = self._session.post(self._url, payload) debug(response.text) root = XML(response.content) resultname = next(root.getiterator('resultname')).text resultsize = int(next(root.getiterator('resultsize')).text) if resultsize: return (Structure if context == 'S' else Reaction)(self, resultname, resultsize, single_step, without_trash)