@@ -46,7 +46,7 @@ def __init__(self, parser='XML'):
46
46
47
47
self .parser = parser
48
48
49
- def write_file (self , odml_document , filename ):
49
+ def write_file (self , odml_document , filename , ** kwargs ):
50
50
"""
51
51
Writes an odml.Document to a file using the format
52
52
defined in the ODMLWriter.parser property. Supported formats are
@@ -55,6 +55,8 @@ def write_file(self, odml_document, filename):
55
55
56
56
:param odml_document: odml.Document.
57
57
: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.
58
60
"""
59
61
60
62
# Write document only if it does not contain validation errors.
@@ -74,30 +76,44 @@ def write_file(self, odml_document, filename):
74
76
msg += " Run the Documents 'validate' method to access them.\n %s" % report
75
77
warnings .warn (msg )
76
78
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 ))
83
93
84
- def to_string (self , odml_document ):
94
+ def to_string (self , odml_document , ** kwargs ):
85
95
"""
86
96
Parses an odml.Document to a string in the file format
87
97
defined in the ODMLWriter.parser property. Supported formats are
88
- JSON, XML, YAML and RDF.
98
+ JSON, YAML and RDF.
89
99
90
100
: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
+
91
106
:return: string containing the content of the odml.Document in the
92
107
specified format.
93
108
"""
94
109
string_doc = ''
95
110
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 )
101
117
else :
102
118
self .parsed_doc = DictWriter ().to_dict (odml_document )
103
119
0 commit comments