Correct a bug with file opening and convert to with

This commit is contained in:
Sengian 2011-01-09 19:26:39 +01:00
parent 4043009433
commit 5ffbfc89b5
2 changed files with 26 additions and 42 deletions

View File

@ -78,7 +78,7 @@ class CombineBorders:
self.add_to_border_desc(line) self.add_to_border_desc(line)
def combine_borders(self): def combine_borders(self):
with open(self.__file, 'r') as read_obj, with open(self.__file, 'r') as read_obj, \
open(self.__write_to, 'w') as write_obj: open(self.__write_to, 'w') as write_obj:
for line in read_obj: for line in read_obj:
self.__first_five = line[0:5] self.__first_five = line[0:5]

View File

@ -119,14 +119,11 @@ class Footnote:
bottom of the main file. bottom of the main file.
""" """
self.__initiate_sep_values() self.__initiate_sep_values()
self.__write_obj = open(self.__write_to, 'w') self.__footnote_holder = tempfile.mktemp()
with open(self.__file) as read_obj: with open(self.__file) as read_obj, \
self.__footnote_holder = tempfile.mktemp() open(self.__write_to, 'w') as self.__write_obj, \
self.__write_to_foot_obj = open(self.__footnote_holder, 'w') open(self.__footnote_holder, 'w') as self.__write_to_foot_obj:
line_to_read = 1 for line in read_obj:
while line_to_read:
line_to_read = read_obj.readline()
line = line_to_read
self.__token_info = line[:16] self.__token_info = line[:16]
# keep track of opening and closing brackets # keep track of opening and closing brackets
if self.__token_info == 'ob<nu<open-brack': if self.__token_info == 'ob<nu<open-brack':
@ -139,9 +136,7 @@ class Footnote:
# not in the middle of footnote text # not in the middle of footnote text
else: else:
self.__default_sep(line) self.__default_sep(line)
self.__write_obj.close() with open(self.__footnote_holder, 'r') as read_obj, \
self.__write_to_foot_obj.close()
with open(self.__footnote_holder, 'r') as read_obj,
open(self.__write_to, 'a') as write_obj: open(self.__write_to, 'a') as write_obj:
write_obj.write( write_obj.write(
'mi<mk<sect-close\n' 'mi<mk<sect-close\n'
@ -195,21 +190,15 @@ class Footnote:
These two functions do the work of separating the footnotes form the These two functions do the work of separating the footnotes form the
body. body.
""" """
read_obj = open(self.__file) with open(self.__file) as read_obj, \
self.__write_obj = open(self.__write_to, 'w') open(self.__write_to, 'w') as self.__write_obj, \
# self.__write_to = "footnote_info.data" open(self.__footnote_holder, 'w') as self.__write_to_foot_obj:
self.__write_to_foot_obj = open(self.__footnote_holder, 'w') for line in read_obj:
line = 1 self.__token_info = line[:16]
while line: if self.__state == 'body':
line = read_obj.readline() self.__get_foot_body_func(line)
self.__token_info = line[:16] elif self.__state == 'foot':
if self.__state == 'body': self.__get_foot_foot_func(line)
self.__get_foot_body_func(line)
elif self.__state == 'foot':
self.__get_foot_foot_func(line)
read_obj.close()
self.__write_obj.close()
self.__write_to_foot_obj.close()
def __get_foot_from_temp(self, num): def __get_foot_from_temp(self, num):
""" """
@ -221,9 +210,7 @@ class Footnote:
look_for = 'mi<mk<footnt-ope<' + num + '\n' look_for = 'mi<mk<footnt-ope<' + num + '\n'
found_foot = 0 found_foot = 0
string_to_return = '' string_to_return = ''
line = 1 for line in self.__read_from_foot_obj:
while line:
line = self.__read_from_foot_obj.readline()
if found_foot: if found_foot:
if line == 'mi<mk<footnt-clo\n': if line == 'mi<mk<footnt-clo\n':
return string_to_return return string_to_return
@ -241,16 +228,13 @@ class Footnote:
print out to the third file. print out to the third file.
If no footnote marker is found, simply print out the token (line). If no footnote marker is found, simply print out the token (line).
""" """
self.__read_from_foot_obj = open(self.__footnote_holder, 'r') with open(self.__footnote_holder, 'r') as self.__read_from_foot_obj, \
read_obj = open(self.__write_to, 'r') open(self.__write_to, 'r') as read_obj, \
self.__write_obj = open(self.__write_to2, 'w') open(self.__write_to2, 'w') as self.__write_obj:
line = 1 for line in read_obj:
while line: if line[:16] == 'mi<mk<footnt-ind':
line = read_obj.readline() line = self.__get_foot_from_temp(line[17:-1])
if line[:16] == 'mi<mk<footnt-ind': self.__write_obj.write(line)
line = self.__get_foot_from_temp(line[17:-1])
self.__write_obj.write(line)
read_obj.close()
def join_footnotes(self): def join_footnotes(self):
""" """
@ -268,8 +252,8 @@ class Footnote:
self.__state = 'body' self.__state = 'body'
self.__get_footnotes() self.__get_footnotes()
self.__join_from_temp() self.__join_from_temp()
self.__write_obj.close() # self.__write_obj.close()
self.__read_from_foot_obj.close() # self.__read_from_foot_obj.close()
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_to2, "footnote_joined.data") copy_obj.copy_file(self.__write_to2, "footnote_joined.data")