Skip to content

Commit cfc9a58

Browse files
committed
[fileio/odmlparser] Add xmlparser kwargs option
To allow passing custom stylesheets for xml documents also via the odml.save function, kwargs are now supported in odml.save and the odmlparser has been adjusted accordingly.
1 parent 24d7552 commit cfc9a58

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

odml/fileio.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,21 @@ def load(filename, backend="xml", show_warnings=True):
2525
return reader.from_file(filename)
2626

2727

28-
def save(obj, filename, backend="xml"):
28+
def save(obj, filename, backend="xml", **kwargs):
2929
"""
3030
Save an open odML document to file of a specified format.
3131
:param obj: odML document do be saved.
3232
:param filename: Filename and path where the odML document
3333
should be saved.
3434
:param backend: Format in which the odML document is to be saved.
3535
The default format is XML.
36+
:param kwargs: Writer backend keyword arguments. Refer to the documentation
37+
of the available parsers to check which arguments are supported.
3638
"""
3739
writer = ODMLWriter(backend)
3840
if "." not in filename.split(os.pathsep)[-1]:
3941
filename = filename + ".%s" % backend
40-
return writer.write_file(obj, filename)
42+
return writer.write_file(obj, filename, **kwargs)
4143

4244

4345
def display(obj, backend="xml"):

odml/tools/odmlparser.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(self, parser='XML'):
4646

4747
self.parser = parser
4848

49-
def write_file(self, odml_document, filename):
49+
def write_file(self, odml_document, filename, **kwargs):
5050
"""
5151
Writes an odml.Document to a file using the format
5252
defined in the ODMLWriter.parser property. Supported formats are
@@ -55,6 +55,8 @@ def write_file(self, odml_document, filename):
5555
5656
:param odml_document: odml.Document.
5757
:param filename: path and filename of the output file.
58+
:param kwargs: Writer backend keyword arguments. Refer to the documentation
59+
of the available parsers to check which arguments are supported.
5860
"""
5961

6062
# Write document only if it does not contain validation errors.
@@ -74,28 +76,34 @@ def write_file(self, odml_document, filename):
7476
msg += " Run the Documents 'validate' method to access them.\n%s" % report
7577
warnings.warn(msg)
7678

77-
with open(filename, 'w') as file:
78-
# Add XML header to support odML stylesheets.
79-
if self.parser == 'XML':
80-
file.write(xmlparser.XMLWriter.header)
81-
82-
file.write(self.to_string(odml_document))
79+
# Allow kwargs when writing XML documents to support individual style sheets
80+
if self.parser == 'XML':
81+
local_style = False
82+
custom_template = None
83+
84+
if "local_style" in kwargs and isinstance(kwargs["local_style"], bool):
85+
local_style = kwargs["local_style"]
86+
if "custom_template" in kwargs and isinstance(kwargs["custom_template"], str):
87+
custom_template = kwargs["custom_template"]
88+
xmlparser.XMLWriter(odml_document).write_file(filename, local_style=local_style,
89+
custom_template=custom_template)
90+
else:
91+
with open(filename, 'w') as file:
92+
file.write(self.to_string(odml_document))
8393

8494
def to_string(self, odml_document):
8595
"""
8696
Parses an odml.Document to a string in the file format
8797
defined in the ODMLWriter.parser property. Supported formats are
88-
JSON, XML, YAML and RDF.
98+
JSON, YAML and RDF.
8999
90100
:param odml_document: odml.Document.
91101
:return: string containing the content of the odml.Document in the
92102
specified format.
93103
"""
94104
string_doc = ''
95105

96-
if self.parser == 'XML':
97-
string_doc = unicode(xmlparser.XMLWriter(odml_document))
98-
elif self.parser == "RDF":
106+
if self.parser == "RDF":
99107
# Use XML as default output format for now.
100108
string_doc = RDFWriter(odml_document).get_rdf_str("xml")
101109
else:

0 commit comments

Comments
 (0)