Ejemplo n.º 1
0
 def include_files(self):
     pair = '<include', '/>'
     qpair = '<?', '?>'
     ir = 0
     while ir != -1:
         il, ir = find_matching_pair(self.xml, pair, ir)
         if ir != -1:
             cont = self.xml[il:ir].strip(pair[0]).rstrip(pair[1])
             fname = cont.split('=', 1)[1].strip().strip('"')
             fobj = open(os.path.join(self.base_path, fname), 'r')
             fcont = fobj.read()
             fcont = remove_pair_sections(fcont, qpair)
             fobj.close()
             self.xml = self.xml.replace(self.xml[il:ir], fcont)
         #end if
     #end while
     return
Ejemplo n.º 2
0
 def include_files(self):
     pair = '<include','/>'
     qpair = '<?','?>'
     ir=0
     while ir!=-1:
         il,ir = find_matching_pair(self.xml,pair,ir)
         if ir!=-1:
             cont = self.xml[il:ir].strip(pair[0]).rstrip(pair[1])
             fname = cont.split('=',1)[1].strip().strip('"')
             fobj = open(os.path.join(self.base_path,fname),'r')
             fcont = fobj.read()
             fcont = remove_pair_sections(fcont,qpair)
             fobj.close()
             self.xml = self.xml.replace(self.xml[il:ir],fcont)
         #end if
     #end while
     return
Ejemplo n.º 3
0
    def __init__(self,
                 fpath=None,
                 element_joins=None,
                 element_aliases=None,
                 contract_names=False,
                 strip_prefix=None,
                 warn=True,
                 xml=None):
        if element_joins is None:
            element_joins = []
        if element_aliases is None:
            element_aliases = {}

        #assign values
        self.fpath = fpath
        if fpath is None:
            self.base_path = None
        else:
            self.base_path = os.path.split(fpath)[0]
        #end if
        self.element_joins = set(element_joins)
        self.element_aliases = element_aliases
        self.contract_names = contract_names
        self.strip_prefix = strip_prefix
        self.warn = warn

        #create the parser
        self.parser = expat.ParserCreate()
        self.parser.StartElementHandler = self.found_element_start
        self.parser.EndElementHandler = self.found_element_end
        self.parser.CharacterDataHandler = self.found_text
        self.parser.AttlistDeclHandler = self.found_attribute
        #self.parser.returns_unicode = 0

        #read in xml file
        if xml is None:
            fobj = open(fpath, 'r')
            self.xml = fobj.read()
        else:
            self.xml = xml
        #end if
        #remove all comments
        pair = '<!--', '-->'
        self.xml = remove_pair_sections(self.xml, pair)
        #process includes
        while self.xml.find('<include') != -1:
            self.include_files()
            self.xml = remove_pair_sections(self.xml, pair)
        #end while
        #remove empty lines
        self.xml = remove_empty_lines(self.xml)
        #print self.xml

        #parse the xml and build the dynamic object
        self.nlevels = 1
        self.ilevel = 0
        self.pad = ''
        #  Set the current xml element
        self.obj = XMLelement()
        self.cur = [self.obj]

        self.parser.Parse(self.xml, True)

        #the expat parser is troublesome in that it
        # -does not have typical class members
        # -is unpickleable
        # therefore it is removed after the dynamic object is built
        del self.parser

        return
Ejemplo n.º 4
0
    def __init__(self,fpath=None,element_joins=None,element_aliases=None,contract_names=False,strip_prefix=None,warn=True,xml=None):
        if element_joins is None:
            element_joins = []
        if element_aliases is None:
            element_aliases = {}

        #assign values
        self.fpath=fpath
        if fpath is None:
            self.base_path = None
        else:
            self.base_path = os.path.split(fpath)[0]
        #end if
        self.element_joins = set(element_joins)
        self.element_aliases = element_aliases
        self.contract_names = contract_names
        self.strip_prefix = strip_prefix
        self.warn = warn
        
        #create the parser
        self.parser = expat.ParserCreate()
        self.parser.StartElementHandler  = self.found_element_start
        self.parser.EndElementHandler    = self.found_element_end
        self.parser.CharacterDataHandler = self.found_text
        self.parser.AttlistDeclHandler   = self.found_attribute
        self.parser.returns_unicode = 0

        #read in xml file
        if xml is None:
            fobj = open(fpath,'r')
            self.xml = fobj.read()
        else:
            self.xml = xml
        #end if
        #remove all comments
        pair='<!--','-->'
        self.xml = remove_pair_sections(self.xml,pair)
        #process includes
        while self.xml.find('<include')!=-1:
            self.include_files()
            self.xml = remove_pair_sections(self.xml,pair)
        #end while
        #remove empty lines
        self.xml = remove_empty_lines(self.xml)
        #print self.xml

        #parse the xml and build the dynamic object
        self.nlevels=1
        self.ilevel=0
        self.pad=''
        #  Set the current xml element
        self.obj = XMLelement()
        self.cur=[self.obj]

        self.parser.Parse(self.xml,True)

        #the expat parser is troublesome in that it
        # -does not have typical class members
        # -is unpickleable
        # therefore it is removed after the dynamic object is built
        del self.parser

        return
Ejemplo n.º 5
0
def test_remove_pair_sections():
    from superstring import remove_pair_sections

    s = '''
        <simulation>
           <project id="vmc_hf_noj" series="0">
              <application name="qmcapp" role="molecu" class="serial" version="1.0"/>
           </project>
           <qmcsystem>
              <simulationcell>
                ...
              </simulationcell>
              <particleset>
                ...
              </particleset>
              <wavefunction>
                ...
              </wavefunction>
              <hamiltonian>
                ...
              </hamiltonian>
           </qmcsystem>
           <qmc method="vmc" move="pbyp">
              ...
           </qmc>
        </simulation>
        '''

    s = remove_pair_sections(s,('<hamiltonian>','</hamiltonian>'))

    s_no_h = '''
        <simulation>
           <project id="vmc_hf_noj" series="0">
              <application name="qmcapp" role="molecu" class="serial" version="1.0"/>
           </project>
           <qmcsystem>
              <simulationcell>
                ...
              </simulationcell>
              <particleset>
                ...
              </particleset>
              <wavefunction>
                ...
              </wavefunction>
              
           </qmcsystem>
           <qmc method="vmc" move="pbyp">
              ...
           </qmc>
        </simulation>
        '''
    assert(s==s_no_h)


    s = remove_pair_sections(s,('<qmcsystem>','</qmcsystem>'))

    s_no_sys = '''
        <simulation>
           <project id="vmc_hf_noj" series="0">
              <application name="qmcapp" role="molecu" class="serial" version="1.0"/>
           </project>
           
           <qmc method="vmc" move="pbyp">
              ...
           </qmc>
        </simulation>
        '''
    assert(s==s_no_sys)
    

    s = remove_pair_sections(s,('<simulation>','</simulation>'))

    assert(s.strip()=='')