Changeset 153

Show
Ignore:
Timestamp:
Sun Mar 18 14:41:59 2007
Author:
manatlan
Message:

- matej patch for non pc-data chars from jhead output in tools.py
(correct)

Files:

Legend:

Unmodified
Added
Removed
Modified
  • trunk/tools.py

    r151 r153  
    24 24 import tempfile  
    25 25  
      26  
      27 # Protect against crappy output of some unnamed (ehm,  
      28 # ehm, jhead) programs. Prepare tables for later use.  
      29 nonPCData = ''  
      30  
      31 # additional procedure for working with Unicode -- normal  
      32 # string.maketrans doesn't work with Unicode strings.  
      33 # originally from  
      34 # http://groups.google.com/group/comp.lang.python/msg/4dbebae9e040a7b3  
      35 def maketransU(s1, s2, todel=""):  
      36     trans_tab = dict( zip( map(ord, s1), map(ord, s2) ) )  
      37     trans_tab.update( (ord(c),None) for c in todel )  
      38     return trans_tab  
      39  
      40 # These are codes of characters which are not PCDATA  
      41 # and so they cannot happen in XML file.  
      42 nonPCDataRange = range(0x00,0x08)+[0x0b,0x0c]+range(0x0e,0x19)  
      43 # The following ones are stricly speaking not incorrect (and XML  
      44 # parse won't choke on them), but they are unprintable control  
      45 # characters, so they will probably never happen in metadata.  
      46 nonPCDataRange += range(0x7f,0x9f)  
      47 for i in nonPCDataRange:  
      48    nonPCData += chr(i)  
      49 allchars = maketransU('','',nonPCData)  
      50  
      51  
    26 52 def cd2d(f): #yyyymmddhhiiss -> datetime  
    27 53    return datetime.datetime(int(f[:4]),int(f[4:6]), int(f[6:8]),int(f[8:10]),int(f[10:12]),int(f[12:14]))  
     
    106 132         outerr = string.join(p.stderr.readlines() ).strip()  
    107 133  
    108    
    109           # ======================================================== patch matej  
    110           # Protect against crappy output of some unnamed (ehm,  
    111           # ehm, jhead) programs.  
    112           nonPCData = ''  
    113           # These are codes of characters which are not PCDATA  
    114           # and so they cannot happen in XML file.  
    115           nonPCDataRange = range(0x00,0x08)+[0x0b,0x0c]+range(0x0e,0x19)  
    116           # The following ones are stricly speaking not incorrect (and XML parse  
    117           # won't choke on them), but they are unprintable control  
    118           # characters, so they will probably never happen in  
    119           # metadata.  
    120           nonPCDataRange += range(0x7f,0x9f)  
    121           for i in nonPCDataRange:  
    122              nonPCData += chr(i)  
    123           allchars = string.maketrans('','')  
    124           out = out.translate(allchars,nonPCData)  
    125           outerr = outerr.translate(allchars,nonPCData)  
    126           # ======================================================== patch matej  
    127    
    128    
    129 134         if "jhead" in cmdline:  
    130 135            if "Nonfatal Error" in outerr:  
     
    150 155                       raise CommandException( cmd +"\n decoding trouble")  
    151 156  
    152              return out #unicode  
      157            # Protect against crappy output of some unnamed (ehm,  
      158            # ehm, jhead) programs. Use tables from the top of this  
      159            # module.  
      160            out = out.translate(allchars)  
    153 161  
      162            return out #unicode  
    154 163  
    155 164