Guest User

decode.py

a guest
Jun 29th, 2011
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. # An adapter that takes CSV as input, performs a MIME/utf-8 decoding, then returns the CSV results
  2. import csv,sys,commands,socket,email.header,re,codecs,traceback
  3.  
  4. def decode_string(encoded_string):
  5.     match = re.search('(=\?.*?\?(Q|B)\?)', encoded_string)
  6.     if not match:
  7.         return encoded_string
  8.    
  9.     try:
  10.       header_string = email.header.decode_header(encoded_string)
  11.     except:
  12.       return encoded_string
  13.  
  14.     try:
  15.       codecs.lookup(header_string[0][1])
  16.     except:
  17.       return "Codec not found"
  18.  
  19.     decoded = header_string[0][0].decode(header_string[0][1],"replace")
  20.  
  21.     decoded_utf8 = decoded.encode('utf-8')
  22.     return decoded_utf8
  23.  
  24. def main():
  25.     if len(sys.argv) != 3:
  26.         print "Usage: python decode_string.py [encoded field] [decoded field]"
  27.         sys.exit(0)
  28.  
  29.     encoded_stringf = sys.argv[1]
  30.     decoded_stringf = sys.argv[2]
  31.     r = csv.reader(sys.stdin)
  32.     w = None
  33.     header = []
  34.     first = True
  35.  
  36.     for line in r:
  37.         if first:
  38.             header = line
  39.             if encoded_stringf not in header or decoded_stringf not in header:
  40.                 print "Encoded string field and decoded string field must exist in CSV data"
  41.                 sys.exit(0)
  42.             csv.writer(sys.stdout).writerow(header)
  43.             w = csv.DictWriter(sys.stdout, header)
  44.             first = False
  45.             continue
  46.  
  47.         # Read the result
  48.         result = {}
  49.         i = 0
  50.         while i < len(header):
  51.             if i < len(line):
  52.                 result[header[i]] = line[i]
  53.             else:
  54.                 result[header[i]] = ''
  55.             i += 1
  56.  
  57.         if len(result[encoded_stringf]):
  58.             result[decoded_stringf] = decode_string(result[encoded_stringf])
  59.             if len(result[decoded_stringf]):
  60.                 w.writerow(result)
  61.  
  62. if __name__=="__main__":
  63.     try:
  64.         main()
  65.     except:
  66.         traceback.print_exc(file=open("/tmp/errlog.txt","a"))
  67.         sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment