Skip to content

Commit 95db028

Browse files
committed
[tools] Set origin_file_name on file load
1 parent db5aeda commit 95db028

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

odml/tools/odmlparser.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import sys
1111
import yaml
1212

13+
from os.path import basename
14+
1315
from . import xmlparser
1416
from .dict_parser import DictWriter, DictReader
1517
from ..info import FORMAT_VERSION
@@ -131,6 +133,8 @@ def from_file(self, file, doc_format=None):
131133
return
132134

133135
self.doc = DictReader().to_odml(self.parsed_doc)
136+
# Provide original file name via the in memory document
137+
self.doc._origin_file_name = basename(file)
134138
return self.doc
135139

136140
elif self.parser == 'JSON':
@@ -142,6 +146,8 @@ def from_file(self, file, doc_format=None):
142146
return
143147

144148
self.doc = DictReader().to_odml(self.parsed_doc)
149+
# Provide original file name via the in memory document
150+
self.doc._origin_file_name = basename(file)
145151
return self.doc
146152

147153
elif self.parser == 'RDF':

odml/tools/rdf_converter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,11 @@ def to_odml(self):
222222

223223
def from_file(self, filename, doc_format):
224224
self.g = Graph().parse(source=filename, format=doc_format)
225-
return self.to_odml()
225+
docs = self.to_odml()
226+
for d in docs:
227+
# Provide original file name via the document
228+
d._origin_file_name = os.path.basename(filename)
229+
return docs
226230

227231
def from_string(self, file, doc_format):
228232
self.g = Graph().parse(source=StringIO(file), format=doc_format)

odml/tools/xmlparser.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from lxml.builder import E
1111
# this is needed for py2exe to include lxml completely
1212
from lxml import _elementpath as _dummy
13+
from os.path import basename
1314

1415
try:
1516
from StringIO import StringIO
@@ -187,7 +188,12 @@ def from_file(self, xml_file):
187188
raise ParserException(e.msg)
188189

189190
self._handle_version(root)
190-
return self.parse_element(root)
191+
doc = self.parse_element(root)
192+
193+
# Provide original file name via the in memory document
194+
if isinstance(xml_file, unicode):
195+
doc._origin_file_name = basename(xml_file)
196+
return doc
191197

192198
def from_string(self, string):
193199
try:

0 commit comments

Comments
 (0)