Changeset 2264

Show
Ignore:
Timestamp:
05/19/05 23:26:05 (19 years ago)
Author:
jerome
Message:

Improved data dumper : speed and correctness

Location:
pykota/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/NEWS

    r2262 r2264  
    2424    - 1.23alpha3 : 
    2525     
     26        - Data dumper improved : it's faster and now datas are correctly  
     27          escaped. 
     28         
    2629        - The URL to the logo displayed in the CGI scripts is configurable. 
    2730         
  • pykota/trunk/pykota/dumper.py

    r2219 r2264  
    2525import os 
    2626import pwd 
     27from xml.sax import saxutils 
     28 
    2729from mx import DateTime 
    2830 
     
    113115        """Dumps datas with a separator.""" 
    114116        for entry in entries : 
    115             line = separator.join([ '"%s"' % field for field in entry ]) 
     117            line = [] 
     118            for value in entry : 
     119                if type(value).__name__ in ("str", "NoneType") : 
     120                    line.append('"%s"' % str(value).replace(separator, "\\%s" % separator).replace('"', '\\"')) 
     121                else :     
     122                    line.append(str(value)) 
    116123            try : 
    117                 self.outfile.write("%s\n" % line) 
     124                self.outfile.write("%s\n" % separator.join(line)) 
    118125            except IOError, msg :     
    119126                sys.stderr.write("%s : %s\n" % (_("PyKota data dumper failed : I/O error"), msg)) 
     
    165172            x._push() 
    166173            x.entry() 
    167             for i in range(len(entry)) : 
    168                 value = str(entry[i]) 
    169                 typval = type(entry[i]).__name__ 
    170                 try : 
    171                     value = unicode(value, self.getCharset()).encode("UTF-8") 
    172                 except UnicodeError :     
    173                     pass 
    174                 x.attribute(value, type=typval, name=headers[i]) 
     174            for (header, value) in zip(headers, entry) : 
     175                strvalue = str(value) 
     176                typval = type(value).__name__ 
     177                if header in ("filename", "title", "options", "billingcode") \ 
     178                          and (typval == "str") : 
     179                    try : 
     180                        strvalue = unicode(strvalue, self.getCharset()).encode("UTF-8") 
     181                    except UnicodeError :     
     182                        pass 
     183                    strvalue = saxutils.escape(strvalue, { "'" : "'", \ 
     184                                                           '"' : """ }) 
     185                x.attribute(strvalue, type=typval, name=header) 
    175186            x._pop()     
    176187        x._output(self.outfile) 
  • pykota/trunk/TODO

    r2262 r2264  
    2626          Winboxes it seems) 
    2727 
    28         - Ensures that all special characters in strings are properly  
    29           escaped when dumping datas : quotes, xml entities, etc... 
    30          
    3128        - Add date filtering options to dumpykota, something like  
    3229                date=+20050101 date=-20050201