1 #! /usr/bin/env python 2 3 # Reads RMAIL email file and outputs JPEG files (tested with ``eeyes'' 4 # on Debian GNU/Linux). Why create this script in the first place? 5 # Emacs19 did not have base64- interactive functions. 6 7 8 info = {} 9 info['CONTENT_TYPE'] = 'image/jpeg' 10 info['RMAIL_pathfile'] = '~/RMAIL_marxists' 11 info['PY_NAME'] = 'RMAIL_base64' 12 info['PY_FILE'] = info['PY_NAME'] + '.py' 13 14 # ....................................................... 15 import base64, fileinput, os 16 import re, string, sys, tempfile, time 17 # ....................................................... 18 class Main: 19 """___""" 20 21 def __init__(self, info): 22 """___""" 23 24 # 25 self.info = info 26 self.when = time.strftime('%Y%m%d+%S', time.localtime(time.time())) 27 self.n_decode = 0 28 29 # 30 self.tempfile_pathfile = tempfile.mktemp() 31 # 32 print self.tempfile_pathfile 33 print '' 34 # /var/tmp/@2684.1 35 36 # 37 self.switch = None 38 self.switch_store_next = None 39 40 # Using CPython mode in Emacs. 41 if sys.argv[0] == '': 42 sys.argv[0] = self.info['PY_FILE'] 43 pass 44 45 # 46 self.info['RMAIL_pathfile'] = os.path.expanduser( 47 self.info['RMAIL_pathfile']) 48 49 # 50 self.info['TARGET_SUBDIRECTORY'] = os.path.abspath(os.path.join( 51 os.getcwd(), self.info['PY_NAME'])) 52 if not os.path.isdir(self.info['TARGET_SUBDIRECTORY']): 53 os.mkdir(self.info['TARGET_SUBDIRECTORY']) 54 pass 55 56 # 57 self.regexp = {} 58 data_regexp_sub = '([^\n]+)\n?$' 59 self.regexp[ 60 'content-transfer-encoding'] = re.compile( 61 '^Content-transfer-encoding: ' + data_regexp_sub, re.I) 62 self.regexp[ 63 'content-type'] = re.compile( 64 '^Content-type: ' + data_regexp_sub, re.I) 65 self.regexp[ 66 'content-type-type'] = re.compile('^([^;]+);', re.I) 67 self.regexp[ 68 'content-type-name'] = re.compile('name="([^"]+)"', re.I) 69 self.regexp[ 70 'blank-line'] = re.compile('\s*$', re.I) 71 72 pass 73 74 75 def __called__(self): 76 """___""" 77 os.remove(self.tempfile_pathfile) 78 pass 79 80 81 def __call__(self): 82 """___""" 83 84 for line in fileinput.input(self.info['RMAIL_pathfile']): 85 86 # Stop before end of last file. 87 if fileinput.lineno() > 20000: 88 raise 'Stopping.' 89 90 # 91 match_obj_a = self.regexp['content-transfer-encoding'].match(line) 92 match_obj_b = self.regexp['content-type'].match(line) 93 match_obj__blank = self.regexp['blank-line'].match(line) 94 95 # ................................. 96 if self.switch_store_next: 97 if match_obj__blank == None: 98 self.tempfile.write(line) 99 pass 100 pass 101 102 # ................................. 103 if not match_obj_a == None: 104 encoding = match_obj_a.group(1) 105 pass 106 107 elif not match_obj_b == None: 108 type = match_obj_b.group(1) 109 110 # 111 match_obj = self.regexp['content-type-type'].match(type) 112 if not match_obj == None: 113 type_type = match_obj.group(1) 114 115 # 116 if type_type == self.info['CONTENT_TYPE']: 117 self.switch = 't' 118 pass 119 pass 120 else: 121 type_type = None 122 pass 123 # 124 match_obj = self.regexp['content-type-name'].search(type) 125 if not match_obj == None: 126 type_name = match_obj.group(1) 127 pass 128 else: 129 type_name = None 130 pass 131 132 print '---' + type 133 print type_type 134 print type_name 135 pass 136 137 elif not match_obj__blank == None: 138 # 139 # Blank line. 140 if self.switch_store_next: 141 self.switch = None 142 self.switch_store_next = None 143 # 144 print 'Closing tempfile: ' + self.tempfile_pathfile 145 self.tempfile.close() 146 # 147 self.n_decode = self.n_decode + 1 148 self.decoded_pathfile = os.path.join( 149 self.info['TARGET_SUBDIRECTORY'], 150 string.join(string.split(type_name, '.')[:-1], '.') + 151 '__' + self.when + 152 '__' + string.zfill(self.n_decode, 5) + 153 '__' + type_name) 154 155 # 156 self.tempfile = open(self.tempfile_pathfile, 'r') 157 self.decoded = open(self.decoded_pathfile, 'w') 158 base64.decode(self.tempfile, self.decoded) 159 self.decoded.close() 160 self.tempfile.close() 161 162 # 163 type = None 164 type_name = None 165 type_type = None 166 167 pass 168 elif self.switch: 169 self.switch_store_next = 't' 170 # ```````````````````````````````````````````` 171 self.tempfile = open(self.tempfile_pathfile, 'w') 172 pass 173 pass 174 175 pass 176 self.__called__() 177 pass 178 pass 179 180 main = Main(info) 181 main() 182 183 ### 184 # Local variables: 185 # py-indent-offset: 4 186 # End: 187 # |