def UpdateValuesInFile()

in Providers/Scripts/2.4x-2.5x/Scripts/nxIPAddress.py [0:0]


    def UpdateValuesInFile(self,fname,src_dict,re_dict,Ensure):
        if len(src_dict) == 0:
            return [0]
        removing=False
        if self.inet in src_dict.keys() and Ensure=='Absent': # we are trying to remove
            removing=True
        if removing == False and os.path.exists(fname) != True:
            # if this file is not here - we will create it
            try:
                F = open(fname,'w+')
                F.write('# Created by nxIPAddress DSC PRovider\n')
                F.close()
            except:
                raise
        try:
            F = open(fname,'r')
            txt=F.read()
            if 'iface ' in src_dict.keys():
                srch=r'(^auto '+src_dict['iface ']+'$.*?^iface '+src_dict['iface ']+'.*?$|^iface '+src_dict['iface ']+'.*?$).*?((^auto )|(^iface )|(^$))'
            updated=''
            r=re.search(srch,txt,flags=re.S|re.M)
            if r == None:
                if removing:  #nothing to remove
                    return [0]
                else : # append values to the end
                    l='auto ' + src_dict['iface '] + '\niface '+src_dict['iface '] + ' ' + self.inet+src_dict[self.inet] + '\n'
                    if len(updated) > 0 and updated[-1] != '\n':
                        updated+='\n'
                    updated+=l
                    re_dict['iface ']=None
                    re_dict[self.inet]=None
                    for k in re_dict:
                        if re_dict[k] != None and len(src_dict[k]) > 0 :
                            l=k+src_dict[k]+'\n'
                            updated+=l
                    txt=txt+updated
            else:  #matched      
                if removing:
                    tail=''
                    rpl=re.compile(r.group(0),flags=re.S|re.M)
                    txt=rpl.sub(tail,txt)
                    if txt[-2:] == '\n\n':
                        txt=txt[:-1]
                else : # replace tags - preserve unknown tags
                    t=r.group(0)
                    for l in t.splitlines():
                        if len(l)>1:
                            l+='\n'
                        else:
                            continue
                        if 'iface ' in re_dict.keys() and re_dict['iface '] != None :
                                if re.match(re_dict['iface '],l) :
                                    l='iface '+src_dict['iface '] + ' ' + self.inet+src_dict[self.inet] + '\n'
                                    re_dict['iface ']=None
                                    re_dict[self.inet]=None
                                    updated+=l
                                    continue
                        for k in re_dict.keys():
                            if re_dict[k]!=None:
                                if re.match(re_dict[k],l): # re.match is anchored to the line start.
                                    if len(src_dict[k])==0 :
                                        l=''
                                    else:
                                        l=re.sub(re_dict[k],k+src_dict[k],l)
                                        if len(l)>0 and l[-1]!='\n':
                                            l+='\n'
                                    re_dict[k]=None
                                    break
                        if len(l)>2:
                            updated+=l
    
                    for k in re_dict:
                        if re_dict[k] != None and len(src_dict[k]) > 0 :
                            l=k+src_dict[k]+'\n'
                            updated+=l
                    tail=''
                    if updated[-1] != '\n':
                        tail='\n'
                    updated+=tail
                    rpl=re.compile(r.group(0),flags=re.S|re.M)
                    txt=rpl.sub(updated,txt)
                    if txt[-2:] == '\n\n':
                        txt=txt[:-1]
            F.close()
        except:
            raise
        ReplaceFileContentsAtomic(fname,txt)
        return [0]