@@ -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,30 +76,44 @@ 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 , ** kwargs ))
8393
84- def to_string (self , odml_document ):
94+ def to_string (self , odml_document , ** kwargs ):
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.
101+ :param kwargs: Writer backend keyword arguments e.g. for adding specific
102+ stylesheets for xml documents or specifying an RDF format.
103+ Refer to the documentation of the available parsers to check
104+ which arguments are supported.
105+
91106 :return: string containing the content of the odml.Document in the
92107 specified format.
93108 """
94109 string_doc = ''
95110
96- if self .parser == 'XML' :
97- string_doc = unicode (xmlparser .XMLWriter (odml_document ))
98- elif self .parser == "RDF" :
99- # Use XML as default output format for now.
100- string_doc = RDFWriter (odml_document ).get_rdf_str ("xml" )
111+ if self .parser == "RDF" :
112+ rdf_format = "xml"
113+ if "rdf_format" in kwargs and isinstance (kwargs ["rdf_format" ], str ):
114+ rdf_format = kwargs ["rdf_format" ]
115+
116+ string_doc = RDFWriter (odml_document ).get_rdf_str (rdf_format )
101117 else :
102118 self .parsed_doc = DictWriter ().to_dict (odml_document )
103119
0 commit comments