def read_package(self, package): """reads the dom element package and sets internal state according to it""" # the standard std = self.paper and self.paper.standard or None self.name = package.getAttribute('name') if package.getAttribute('id'): self.id = package.getAttribute('id') for name, cls in { 'atom': atom, 'group': group, 'text': textatom, 'query': queryatom }.iteritems(): for a in dom_extensions.simpleXPathSearch(package, name): self.insert_atom(cls(standard=std, package=a, molecule=self)) self._id_map = [a.id for a in self.atoms] for b in dom_extensions.simpleXPathSearch(package, 'bond'): bnd = bond(standard=std, package=b, molecule=self) self.add_edge(bnd.atom1, bnd.atom2, bnd) # template related attributes temp = package.getElementsByTagName('template') if temp: temp = temp[0] self.t_atom = Store.id_manager.get_object_with_id( temp.getAttribute('atom')) if temp.getAttribute('bond_first') and temp.getAttribute( 'bond_second'): self.t_bond_first = Store.id_manager.get_object_with_id( temp.getAttribute('bond_first')) self.t_bond_second = Store.id_manager.get_object_with_id( temp.getAttribute('bond_second')) self.next_to_t_atom = self.t_atom.neighbors[0] # display form df = package.getElementsByTagName('display-form') if df: df = df[0] self.display_form = ''.join([e.toxml() for e in df.childNodes ]).encode('utf-8') # fragments for fel in dom_extensions.simpleXPathSearch(package, "fragment"): f = fragment() try: f.read_package(fel) except bkchem_exceptions.bkchem_fragment_error: pass else: self.fragments.add(f) ud = dom_extensions.getChildrenNamed(package, "user-data") if ud: self.user_data = [u.cloneNode(True) for u in ud] # final check of atoms valecies [ a.raise_valency_to_senseful_value() for a in self.vertices if isinstance(a, atom) ]
def read_package(self, doc): self.id = doc.getAttribute("id") self.type = doc.getAttribute("type") or "explicit" name = dom_ext.getFirstChildNamed(doc, "name") if name: self.name = dom_ext.getAllTextFromElement(name) for b in dom_ext.simpleXPathSearch(doc, "bond"): try: self.edges.add( Store.id_manager.get_object_with_id(b.getAttribute("id"))) except KeyError: raise bkchem_fragment_error("inconsistent", "") for v in dom_ext.simpleXPathSearch(doc, "vertex"): try: self.vertices.add( Store.id_manager.get_object_with_id(v.getAttribute("id"))) except KeyError: raise bkchem_fragment_error("inconsistent", "") for p in dom_ext.simpleXPathSearch(doc, "property"): k = p.getAttribute("name") v = p.getAttribute("value") t = p.getAttribute("type") typ = types.__dict__[t] self.properties[k] = typ(v)
def read_data_definition(self, filename): doc = dom.parse(filename) root = doc.childNodes[0] for ecls in dom_ext.simpleXPathSearch(root, "class"): cls = ecls.getAttribute('name') self.definitions[cls] = {} for eobj in dom_ext.simpleXPathSearch(ecls, "object"): obj = eobj.getAttribute('type') self.definitions[cls][obj] = {} for evalue in dom_ext.simpleXPathSearch(eobj, "value"): vname = evalue.getAttribute('name') vtype = evalue.getAttribute('type') # try to decode list style types if vtype.startswith("[") and vtype.endswith("]"): try: vtype = eval(vtype) except ValueError: pass text = dom_ext.getAllTextFromElement( dom_ext.getFirstChildNamed(evalue, "text")) self.definitions[cls][obj][vname] = { 'type': vtype, 'text': text } self.records[cls] = {}
def read_plugin_file( self, dir, name): doc = dom.parse( os.path.join( dir, name)) root = doc.childNodes[0] plugin_type = root.getAttribute( 'type') or 'script' sources = dom_ext.simpleXPathSearch( doc, "/plugin/source") if sources: source = sources[0] files = dom_ext.simpleXPathSearch( source, "file") names = dom_ext.simpleXPathSearch( source, "menu-text") descs = dom_ext.simpleXPathSearch( source, "/plugin/meta/description") menus = dom_ext.simpleXPathSearch( source, "menu") if files and names: file = dom_ext.getAllTextFromElement( files[0]) if not os.path.isabs( file): file = os.path.normpath( os.path.join( dir, file)) name = self._select_correct_text( names) if menus: menu = dom_ext.getAllTextFromElement( menus[0]) else: menu = "" plugin = plugin_handler( name, file, type=plugin_type, desc=self._select_correct_text( descs), menu=menu) self.plugins[ name] = plugin
def read_cdml(text): """returns the last molecule for now""" doc = dom.parseString(text) #if doc.childNodes()[0].nodeName == 'svg': # path = "/svg/cdml/molecule" #else: # path = "/cdml/molecule" path = "//molecule" do_not_continue_this_mol = 0 for mol_el in dom_ext.simpleXPathSearch(doc, path): atom_id_remap = {} mol = molecule() groups = [] for atom_el in dom_ext.simpleXPathSearch(mol_el, "atom"): name = atom_el.getAttribute('name') if not name: #print "this molecule has an invalid symbol" do_not_continue_this_mol = 1 break pos = dom_ext.simpleXPathSearch(atom_el, 'point')[0] x = cm_to_float_coord(pos.getAttribute('x')) y = cm_to_float_coord(pos.getAttribute('y')) z = cm_to_float_coord(pos.getAttribute('z')) if name in PT: # its really an atom a = atom(symbol=name, charge=atom_el.getAttribute('charge') and int(atom_el.getAttribute('charge')) or 0, coords=(x, y, z)) mol.add_vertex(v=a) elif name in cdml_to_smiles: # its a known group group = smiles.text_to_mol(cdml_to_smiles[name], calc_coords=0) a = group.vertices[0] a.x = x a.y = y a.z = z mol.insert_a_graph(group) atom_id_remap[atom_el.getAttribute('id')] = a if do_not_continue_this_mol: break for bond_el in dom_ext.simpleXPathSearch(mol_el, "bond"): type = bond_el.getAttribute('type') if type[1] == u'0': # we ignore bonds with order 0 continue v1 = atom_id_remap[bond_el.getAttribute('start')] v2 = atom_id_remap[bond_el.getAttribute('end')] e = bond(order=int(type[1]), type=type[0]) mol.add_edge(v1, v2, e=e) if mol.is_connected(): # this is here to handle diborane and similar weird things yield mol else: for comp in mol.get_disconnected_subgraphs(): yield comp
def read_cdml( text): """returns the last molecule for now""" doc = dom.parseString( text) #if doc.childNodes()[0].nodeName == 'svg': # path = "/svg/cdml/molecule" #else: # path = "/cdml/molecule" path = "//molecule" do_not_continue_this_mol = 0 for mol_el in dom_ext.simpleXPathSearch( doc, path): atom_id_remap = {} mol = molecule() groups = [] for atom_el in dom_ext.simpleXPathSearch( mol_el, "atom"): name = atom_el.getAttribute( 'name') if not name: #print "this molecule has an invalid symbol" do_not_continue_this_mol = 1 break pos = dom_ext.simpleXPathSearch( atom_el, 'point')[0] x = cm_to_float_coord( pos.getAttribute('x')) y = cm_to_float_coord( pos.getAttribute('y')) z = cm_to_float_coord( pos.getAttribute('z')) if name in PT: # its really an atom a = atom( symbol=name, charge=atom_el.getAttribute( 'charge') and int( atom_el.getAttribute( 'charge')) or 0, coords=( x, y, z)) mol.add_vertex( v=a) elif name in cdml_to_smiles: # its a known group group = smiles.text_to_mol( cdml_to_smiles[ name], calc_coords=0) a = group.vertices[0] a.x = x a.y = y a.z = z mol.insert_a_graph( group) atom_id_remap[ atom_el.getAttribute( 'id')] = a if do_not_continue_this_mol: break for bond_el in dom_ext.simpleXPathSearch( mol_el, "bond"): type = bond_el.getAttribute( 'type') if type[1] == u'0': # we ignore bonds with order 0 continue v1 = atom_id_remap[ bond_el.getAttribute( 'start')] v2 = atom_id_remap[ bond_el.getAttribute( 'end')] e = bond( order=int( type[1]), type=type[0]) mol.add_edge( v1, v2, e=e) if mol.is_connected(): # this is here to handle diborane and similar weird things yield mol else: for comp in mol.get_disconnected_subgraphs(): yield comp
def read_package( self, root): """reads the data from xml (CDML) format. Is not intended for reading of definition files, use read_data_definition instead""" for ecls in dom_ext.simpleXPathSearch( root, "class"): cls = ecls.getAttribute( 'name') if not cls in self.records.keys(): self.records[ cls] = {} for eobj in dom_ext.simpleXPathSearch( ecls, "object"): obj = Store.id_manager.get_object_with_id( eobj.getAttribute( 'ref')) for evalue in dom_ext.simpleXPathSearch( eobj, "value"): vcat = evalue.getAttribute( 'category') vvalue = evalue.getAttribute( 'value') self.set_data( cls, obj, vcat, vvalue)
def read_package( self, package): """reads the dom element package and sets internal state according to it""" # the standard std = self.paper and self.paper.standard or None self.name = package.getAttribute( 'name') if package.getAttribute( 'id'): self.id = package.getAttribute( 'id') for name, cls in {'atom': atom, 'group': group, 'text': textatom, 'query': queryatom}.items(): for a in dom_extensions.simpleXPathSearch( package, name): self.insert_atom( cls( standard=std, package=a, molecule=self)) self._id_map = [a.id for a in self.atoms] for b in dom_extensions.simpleXPathSearch( package, 'bond'): bnd = bond( standard=std, package=b, molecule=self) self.add_edge( bnd.atom1, bnd.atom2, bnd) # template related attributes temp = package.getElementsByTagName('template') if temp: temp = temp[0] self.t_atom = Store.id_manager.get_object_with_id( temp.getAttribute( 'atom')) if temp.getAttribute('bond_first') and temp.getAttribute('bond_second'): self.t_bond_first = Store.id_manager.get_object_with_id( temp.getAttribute( 'bond_first')) self.t_bond_second = Store.id_manager.get_object_with_id( temp.getAttribute( 'bond_second')) self.next_to_t_atom = self.t_atom.neighbors[0] # display form df = package.getElementsByTagName('display-form') if df: df = df[0] self.display_form = ''.join( [e.toxml('utf-8') for e in df.childNodes]) # fragments for fel in dom_extensions.simpleXPathSearch( package, "fragment"): f = fragment() try: f.read_package( fel) except bkchem_exceptions.bkchem_fragment_error: pass else: self.fragments.add( f) ud = dom_extensions.getChildrenNamed( package, "user-data") if ud: self.user_data = [u.cloneNode( True) for u in ud] # final check of atoms valecies [a.raise_valency_to_senseful_value() for a in self.vertices if isinstance( a, atom)]
def read_data_definition( self, filename): doc = dom.parse( filename) root = doc.childNodes[0] for ecls in dom_ext.simpleXPathSearch( root, "class"): cls = ecls.getAttribute( 'name') self.definitions[ cls] = {} for eobj in dom_ext.simpleXPathSearch( ecls, "object"): obj = eobj.getAttribute( 'type') self.definitions[ cls][ obj] = {} for evalue in dom_ext.simpleXPathSearch( eobj, "value"): vname = evalue.getAttribute( 'name') vtype = evalue.getAttribute( 'type') # try to decode list style types if vtype.startswith( "[") and vtype.endswith( "]"): try: vtype = eval( vtype) except ValueError: pass text = dom_ext.getAllTextFromElement( dom_ext.getFirstChildNamed( evalue, "text")) self.definitions[ cls][ obj][ vname] = {'type': vtype, 'text': text } self.records[ cls] = {}
def read_package( self, doc): self.id = doc.getAttribute( "id") self.type = doc.getAttribute( "type") or "explicit" name = dom_ext.getFirstChildNamed( doc, "name") if name: self.name = dom_ext.getAllTextFromElement( name) for b in dom_ext.simpleXPathSearch( doc, "bond"): try: self.edges.add( Store.id_manager.get_object_with_id( b.getAttribute( "id"))) except KeyError: raise bkchem_fragment_error( "inconsistent", "") for v in dom_ext.simpleXPathSearch( doc, "vertex"): try: self.vertices.add( Store.id_manager.get_object_with_id( v.getAttribute( "id"))) except KeyError: raise bkchem_fragment_error( "inconsistent", "") for p in dom_ext.simpleXPathSearch( doc, "property"): k = p.getAttribute( "name") v = p.getAttribute( "value") t = p.getAttribute( "type") typ = types.__dict__[ t] self.properties[k] = typ( v)