Use with block for file output

This commit is contained in:
Nick Hall 2016-06-15 21:48:17 +01:00
parent 2d2ee2939c
commit f172ca8672

View File

@ -201,55 +201,54 @@ class OptionListCollection:
""" """
Saves the current OptionListCollection to the associated file. Saves the current OptionListCollection to the associated file.
""" """
file = open(self.filename, "w", encoding="utf-8") with open(self.filename, "w", encoding="utf-8") as file:
file.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n") file.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
file.write('<options>\n') file.write('<options>\n')
self.write_common(file) self.write_common(file)
for module_name in sorted(self.get_module_names()): # enable a diff for module_name in sorted(self.get_module_names()): # enable a diff
option_list = self.get_option_list(module_name) option_list = self.get_option_list(module_name)
module_docgen_opts = {} module_docgen_opts = {}
for docgen_name in self.docgen_names: for docgen_name in self.docgen_names:
module_docgen_opts[docgen_name] = [] module_docgen_opts[docgen_name] = []
file.write('<module name=%s>\n' % quoteattr(module_name)) file.write('<module name=%s>\n' % quoteattr(module_name))
options = option_list.get_options() options = option_list.get_options()
for option_name in sorted(options.keys()): # enable a diff for option_name in sorted(options.keys()): # enable a diff
option_data = options[option_name] option_data = options[option_name]
if isinstance(option_data, (list, tuple)): if isinstance(option_data, (list, tuple)):
if option_data and option_data[0] in self.docgen_names: if option_data and option_data[0] in self.docgen_names:
module_docgen_opts[option_data[0]].append( module_docgen_opts[option_data[0]].append(
(option_name, option_data[1])) (option_name, option_data[1]))
else:
file.write(' <option name=%s '
'value="" length="%d">\n'
% (quoteattr(option_name),
len(option_data)))
for list_index, list_data in enumerate(option_data):
file.write(' <listitem '
'number="%d" value=%s/>\n'
% (list_index,
quoteattr(str(list_data))))
file.write(' </option>\n')
else: else:
file.write(' <option name=%s ' file.write(' <option name=%s value=%s/>\n'
'value="" length="%d">\n'
% (quoteattr(option_name), % (quoteattr(option_name),
len(option_data))) quoteattr(str(option_data))))
for list_index, list_data in enumerate(option_data): for docgen_name in self.docgen_names:
file.write(' <listitem ' if module_docgen_opts[docgen_name]:
'number="%d" value=%s/>\n' for idx, data in enumerate(
% (list_index, module_docgen_opts[docgen_name]):
quoteattr(str(list_data)))) file.write(' <docgen-option docgen=%s '
file.write(' </option>\n') 'name=%s value=%s/>\n'
else: % (quoteattr(docgen_name),
file.write(' <option name=%s value=%s/>\n' quoteattr(data[0]),
% (quoteattr(option_name), quoteattr(str(data[1]))))
quoteattr(str(option_data)))) self.write_module_common(file, option_list)
for docgen_name in self.docgen_names:
if module_docgen_opts[docgen_name]:
for idx, data in enumerate(
module_docgen_opts[docgen_name]):
file.write(' <docgen-option docgen=%s '
'name=%s value=%s/>\n'
% (quoteattr(docgen_name),
quoteattr(data[0]),
quoteattr(str(data[1]))))
self.write_module_common(file, option_list)
file.write('</module>\n') file.write('</module>\n')
file.write('</options>\n') file.write('</options>\n')
file.close()
def parse(self): def parse(self):
""" """