Ticket #549: unicode_support.diff

File unicode_support.diff, 5.5 KB (added by [email protected]…, 15 years ago)

suggested patch to resolve the utf-8 encoding

  • src/lib/Server/Plugins/Cfg.py

     
    7777        if entry.get('encoding') == 'base64': 
    7878            entry.text = binascii.b2a_base64(data) 
    7979        else: 
    80             entry.text = data             
     80            entry.text = unicode(data, "utf-8") 
    8181        if entry.text in ['', None]: 
    8282            entry.set('empty', 'true') 
    8383 
  • src/lib/Server/Plugins/TCheetah.py

     
    3434        self.template.path = entry.get('realname', entry.get('name')) 
    3535         
    3636        try: 
    37             entry.text = str(self.template) 
     37            entry.text = unicode(str(self.template),'UTF-8') 
    3838        except: 
    3939            (a, b, c) = sys.exc_info() 
    4040            msg = traceback.format_exception(a, b, c, limit=2)[-1][:-1] 
  • src/lib/Client/Tools/POSIX.py

     
    320320                self.logger.error("Cannot verify incomplete ConfigFile %s" % (entry.get('name'))) 
    321321                return False 
    322322            tempdata = entry.text 
     323            if type(tempdata) == unicode: 
     324                tempdata = tempdata.encode('UTF-8') 
    323325        try: 
    324326            content = open(entry.get('name')).read() 
    325327        except IOError, error: 
    326328            self.logger.error("Failed to read %s: %s" % (error.filename, error.strerror)) 
    327329            return False 
     330        # comparaison should be done with figerprints or md5sum so it would be faster  
     331        # for big binary files 
    328332        contentStatus = content == tempdata 
    329333        if not contentStatus: 
    330334            if tbin or not isString(content): 
     
    348352                        break 
    349353                if do_diff: 
    350354                    diff = '\n'.join(rawdiff) 
    351                     entry.set("current_bdiff", binascii.b2a_base64(diff)) 
     355#                    entry.set("current_bdiff", binascii.b2a_base64(diff)) 
     356#                    entry.set("current_diff", diff) 
    352357                    udiff = '\n'.join([x for x in \ 
    353358                                       difflib.unified_diff(content.split('\n'), \ 
    354359                                                            tempdata.split('\n'))]) 
     
    356361                        eudiff = udiff.encode('ascii') 
    357362                    except: 
    358363                        eudiff = "Binary file: no diff printed" 
     364  
    359365                    nqtext = entry.get('qtext', '') 
    360366 
    361367                    if nqtext: 
     
    417423            elif entry.get('empty', 'false') == 'true': 
    418424                filedata = '' 
    419425            else: 
    420                 filedata = entry.text 
     426                if type(entry.text) == unicode: 
     427                    filedata = entry.text.encode('UTF-8') 
     428                else: 
     429                    filedata = entry.text 
    421430            newfile.write(filedata) 
    422431            newfile.close() 
    423432            try: 
  • src/sbin/bcfg2-server

     
    103103            for plugin in self.Core.plugins.values(): 
    104104                for probe in plugin.GetProbes(meta): 
    105105                    resp.append(probe) 
    106             return tostring(resp) 
     106            data = "<?xml version='1.0' encoding='UTF-8' ?>\n" 
     107            data += tostring(resp, encoding='UTF-8') 
     108            return data 
    107109        except Bcfg2.Server.Plugins.Metadata.MetadataConsistencyError: 
    108110            warning = 'Client metadata resolution error for %s' % address[0] 
    109111            self.logger.warning(warning) 
     
    157159        '''Build config for a client''' 
    158160        try: 
    159161            client = self.Core.metadata.resolve_client(address) 
    160             return tostring(self.Core.BuildConfiguration(client)) 
     162            return "<?xml version='1.0' encoding='UTF-8' ?>\n" + \ 
     163                   tostring(self.Core.BuildConfiguration(client), encoding='UTF-8') 
    161164        except Bcfg2.Server.Plugins.Metadata.MetadataConsistencyError: 
    162165            self.logger.warning("Metadata consistency failure for %s" % (address)) 
    163166            raise Fault, (6, "Metadata consistency failure") 
  • src/sbin/bcfg2-info

     
    9797        if len(args.split()) == 2: 
    9898            client, ofile = args.split() 
    9999            output = open(ofile, 'w') 
    100             data = lxml.etree.tostring(self.BuildConfiguration(client)) 
     100            output.write("<?xml version='1.0' encoding='UTF-8'?>") 
     101            data = lxml.etree.tostring(self.BuildConfiguration(client), encoding='UTF-8') 
    101102            output.write(data) 
    102103            output.close() 
    103104        else: 
     
    121122            entry = lxml.etree.Element('ConfigFile', name=fname) 
    122123            metadata = self.metadata.get_metadata(client) 
    123124            self.Bind(entry, metadata) 
    124             print lxml.etree.tostring(entry) 
     125            print lxml.etree.tostring(entry, encoding="UTF-8") 
    125126        else: 
    126127            print 'Usage: buildfile filename hostname' 
    127128