RTFInput: Problem with old RTF bracket addition

This commit is contained in:
Sengian 2012-05-12 20:59:59 +02:00
parent 16a9c42edc
commit 8bcdacf1ce

View File

@ -101,6 +101,14 @@ class AddBrackets:
def __in_body_func(self, line): def __in_body_func(self, line):
""" """
Select what action to take in body:
1-At the end of the file close the braket if a bracket was opened
This happens if there is achange
2-If an open bracket is found the code inside is ignore
(written without modifications)
3-If an accepted control word is found put the line
in a buffer then chage state to after cw
4-Else simply write the line
""" """
if line == 'cb<nu<clos-brack<0001\n' and self.__open_bracket: if line == 'cb<nu<clos-brack<0001\n' and self.__open_bracket:
self.__write_obj.write( self.__write_obj.write(
@ -120,6 +128,10 @@ class AddBrackets:
def __after_control_word_func(self, line): def __after_control_word_func(self, line):
""" """
After a cw either add next allowed cw to temporary list or
change groupe and write it.
If the token leading to an exit is an open bracket go to
ignore otherwise goto in body
""" """
if self.__token_info in self.__accept: if self.__token_info in self.__accept:
self.__temp_group.append(line) self.__temp_group.append(line)
@ -135,42 +147,47 @@ class AddBrackets:
def __write_group(self): def __write_group(self):
""" """
Write a tempory group after accepted control words end
But this is mostly useless in my opinion as there is no list of rejected cw
This may be a way to implement future old rtf processing for cw
Utility: open a group to just put brackets but why be so complicated?
Scheme: open brackets, write cw then go to body and back with cw after
""" """
if self.__open_bracket: if self.__open_bracket:
self.__write_obj.write( self.__write_obj.write(
'cb<nu<clos-brack<0003\n' 'cb<nu<clos-brack<0003\n'
) )
self.__open_bracket = False self.__open_bracket = False
inline_string = ''
the_keys = self.__inline.keys() inline_string = ''.join(['%s<nu<%s\n' % (k, v) \
for the_key in the_keys: for k, v in self.__inline.iteritems() \
value = self.__inline[the_key] if v != 'false'])
if value != 'false':
inline_string += '%s<nu<%s\n' % (the_key, value)
if inline_string: if inline_string:
self.__write_obj.write('ob<nu<open-brack<0003\n') self.__write_obj.write('ob<nu<open-brack<0003\n'
self.__write_obj.write(inline_string) '%s' % inline_string)
self.__open_bracket = True self.__open_bracket = True
self.__temp_group = [] self.__temp_group = []
def __change_permanent_group(self): def __change_permanent_group(self):
""" """
use temp group to change permanent group Use temp group to change permanent group
If the control word is not accepted remove it
What is the interest as it is build to accept only accepted cw
in __after_control_word_func?
""" """
for line in self.__temp_group: self.__inline = {line[:16] : line[20:-1]\
token_info = line[:16] for line in self.__temp_group\
if token_info in self.__accept: # Is this really necessary?
att = line[20:-1] if line[:16] in self.__accept}
self.__inline[token_info] = att
def __ignore_func(self, line): def __ignore_func(self, line):
""" """
Don't add any brackets while inside of brackets RTF has already Just copy data inside of RTF brackets already here.
added.
""" """
self.__write_obj.write(line) self.__write_obj.write(line)
if self.__token_info == 'cb<nu<clos-brack'and\ if self.__token_info == 'cb<nu<clos-brack'\
self.__cb_count == self.__ignore_count: and self.__cb_count == self.__ignore_count:
self.__state = 'in_body' self.__state = 'in_body'
def __check_brackets(self, in_file): def __check_brackets(self, in_file):
@ -204,10 +221,10 @@ class AddBrackets:
copy_obj = copy.Copy(bug_handler = self.__bug_handler) copy_obj = copy.Copy(bug_handler = self.__bug_handler)
if self.__copy: if self.__copy:
copy_obj.copy_file(self.__write_to, "add_brackets.data") copy_obj.copy_file(self.__write_to, "add_brackets.data")
copy_obj.rename(self.__write_to, self.__file) copy_obj.rename(self.__write_to, self.__file)
else: else:
if self.__run_level > 0: if self.__run_level > 0:
sys.stderr.write( sys.stderr.write(
'Sorry, but this files has a mix of old and new RTF.\n' 'Sorry, but this files has a mix of old and new RTF.\n'
'Some characteristics cannot be converted.\n') 'Some characteristics cannot be converted.\n')
os.remove(self.__write_to) os.remove(self.__write_to)