Add pict.rtf if debugging + simplify extract images in RTFinput

This commit is contained in:
Sengian 2011-01-09 20:46:15 +01:00
parent 5ffbfc89b5
commit a335d86cd5
2 changed files with 37 additions and 18 deletions

View File

@ -127,35 +127,52 @@ class RTFInput(InputFormatPlugin):
def extract_images(self, picts):
self.log('Extracting images...')
count = 0
raw = open(picts, 'rb').read()
starts = []
for match in re.finditer(r'\{\\pict([^}]+)\}', raw):
starts.append(match.start(1))
picts = filter(len, re.findall(r'\{\\pict([^}]+)\}', raw))
hex = re.compile(r'[^a-zA-Z0-9]')
encs = [hex.sub('', pict) for pict in picts]
count = 0
imap = {}
for start in starts:
pos, bc = start, 1
while bc > 0:
if raw[pos] == '}': bc -= 1
elif raw[pos] == '{': bc += 1
pos += 1
pict = raw[start:pos+1]
enc = re.sub(r'[^a-zA-Z0-9]', '', pict)
for enc in encs:
if len(enc) % 2 == 1:
enc = enc[:-1]
data = enc.decode('hex')
count += 1
name = (('%4d'%count).replace(' ', '0'))+'.wmf'
name = '%04d.wmf' % count
open(name, 'wb').write(data)
imap[count] = name
#open(name+'.hex', 'wb').write(enc)
return self.convert_images(imap)
# count = 0
# raw = open(picts, 'rb').read()
# starts = []
# for match in re.finditer(r'\{\\pict([^}]+)\}', raw):
# starts.append(match.start(1))
# imap = {}
# for start in starts:
# pos, bc = start, 1
# while bc > 0:
# if raw[pos] == '}': bc -= 1
# elif raw[pos] == '{': bc += 1
# pos += 1
# pict = raw[start:pos+1]
# enc = re.sub(r'[^a-zA-Z0-9]', '', pict)
# if len(enc) % 2 == 1:
# enc = enc[:-1]
# data = enc.decode('hex')
# count += 1
# name = (('%4d'%count).replace(' ', '0'))+'.wmf'
# open(name, 'wb').write(data)
# imap[count] = name
# #open(name+'.hex', 'wb').write(enc)
# return self.convert_images(imap)
def convert_images(self, imap):
for count, val in imap.items():
for count, val in imap.iteritems():
try:
imap[count] = self.convert_image(val)
except:

View File

@ -146,7 +146,8 @@ class Pict:
def process_pict(self):
self.__make_dir()
with open(self.__file) as read_obj, open(self.__write_to, 'w') as write_obj:
with open(self.__file) as read_obj, \
open(self.__write_to, 'w') as write_obj:
for line in read_obj:
self.__token_info = line[:16]
if self.__token_info == 'ob<nu<open-brack':
@ -167,6 +168,7 @@ class Pict:
copy_obj = copy.Copy(bug_handler = self.__bug_handler)
if self.__copy:
copy_obj.copy_file(self.__write_to, "pict.data")
copy_obj.copy_file(self.__pict_file, "pict.rtf")
copy_obj.rename(self.__write_to, self.__file)
os.remove(self.__write_to)
if self.__pict_count == 0: