1+ """
2+ The RDF converter module provides conversion of odML documents to RDF and
3+ the conversion of odML flavored RDF to odML documents.
4+ """
5+
16import os
27import uuid
38
@@ -59,7 +64,7 @@ class RDFWriter(object):
5964
6065 def __init__ (self , odml_documents ):
6166 """
62- :param odml_documents: list of odml documents
67+ :param odml_documents: list of odML documents
6368 """
6469 if not isinstance (odml_documents , list ):
6570 odml_documents = [odml_documents ]
@@ -72,6 +77,13 @@ def __init__(self, odml_documents):
7277 self .section_subclasses = load_rdf_subclasses ()
7378
7479 def convert_to_rdf (self ):
80+ """
81+ convert_to_rdf converts all odML documents to RDF,
82+ connects them via a common "Hub" RDF node and
83+ returns the created RDF graph.
84+
85+ :return: An RDF graph.
86+ """
7587 self .hub_root = URIRef (ODML_NS .Hub )
7688 if self .docs :
7789 for doc in self .docs :
@@ -219,8 +231,12 @@ def save_element(self, odml_elem, node=None):
219231
220232 def _get_section_subclass (self , elem ):
221233 """
234+ _get_section_subclass checks whether the current odML element
235+ is of a type that can be converted into an RDF subclass of
236+ class Section.
237+
222238 :return: RDF identifier of section subclass type if present
223- in section_subclasses dict.
239+ in the section_subclasses dict.
224240 """
225241 sec_type = getattr (elem , "type" )
226242 if sec_type and sec_type in self .section_subclasses :
@@ -236,11 +252,13 @@ def __unicode__(self):
236252
237253 def get_rdf_str (self , rdf_format = "turtle" ):
238254 """
239- Get converted into one of the supported formats data
255+ Convert the current odML content of the parser to a common RDF graph
256+ and return the graph as a string object in the specified RDF format.
240257
241258 :param rdf_format: possible formats: 'xml', 'n3', 'turtle', 'nt', 'pretty-xml',
242259 'trix', 'trig', 'nquads', 'json-ld'.
243260 Full lists see in utils.RDFConversionFormats
261+
244262 :return: string object
245263 """
246264 if rdf_format not in RDFConversionFormats :
@@ -251,6 +269,16 @@ def get_rdf_str(self, rdf_format="turtle"):
251269 return self .convert_to_rdf ().serialize (format = rdf_format ).decode ("utf-8" )
252270
253271 def write_file (self , filename , rdf_format = "turtle" ):
272+ """
273+ Convert the current odML content of the parser to a common RDF graph
274+ and write the resulting graph to an output file using the provided
275+ RDF output format.
276+
277+ :param filename:
278+ :param rdf_format: Possible RDF output format. See utils.RDFConversionFormats
279+ for a full list of supported formats.
280+ Default format is 'turtle'.
281+ """
254282 data = self .get_rdf_str (rdf_format )
255283 filename_ext = filename
256284 if filename .find (RDFConversionFormats .get (rdf_format )) < 0 :
@@ -262,7 +290,7 @@ def write_file(self, filename, rdf_format="turtle"):
262290
263291class RDFReader (object ):
264292 """
265- A reader to parse odML RDF files or strings into odml documents.
293+ A reader to parse odML RDF files or strings into odML documents.
266294
267295 Usage:
268296 file = RDFReader().from_file("/path_to_input_rdf", "rdf_format")
@@ -271,13 +299,20 @@ class RDFReader(object):
271299 """
272300
273301 def __init__ (self , filename = None , doc_format = None ):
302+ """
303+ :param filename: Path of the input odML RDF file.
304+ :param doc_format: RDF format of the input odML RDF file.
305+ """
274306 self .docs = [] # list of parsed odml docs
275307 if filename and doc_format :
276308 self .graph = Graph ().parse (source = filename , format = doc_format )
277309
278310 def to_odml (self ):
279311 """
280- :return: list of converter odml documents
312+ to_odml converts all odML documents from a common RDF graph
313+ into individual odML documents.
314+
315+ :return: list of converted odML documents
281316 """
282317 docs_uris = list (self .graph .objects (subject = URIRef (ODML_NS .Hub ),
283318 predicate = ODML_NS .hasDocument ))
@@ -289,19 +324,41 @@ def to_odml(self):
289324 return self .docs
290325
291326 def from_file (self , filename , doc_format ):
327+ """
328+ from_file loads an odML RDF file and converts all odML documents
329+ from this RDF graph into individual odML documents.
330+
331+ :param filename: Path of the input odML RDF file.
332+ :param doc_format: RDF format of the input odML RDF file.
333+ :return: list of converted odML documents
334+ """
292335 self .graph = Graph ().parse (source = filename , format = doc_format )
293336 docs = self .to_odml ()
294337 for curr_doc in docs :
295338 # Provide original file name via the document
296339 curr_doc ._origin_file_name = os .path .basename (filename )
340+
297341 return docs
298342
299343 def from_string (self , file , doc_format ):
344+ """
345+ from_string loads an odML RDF file or string object and converts all
346+ odML documents from this RDF graph into individual odML documents.
347+
348+ :param file: Path of the input odML RDF file or an RDF graph string object.
349+ :param doc_format: RDF format of the input odML RDF graph.
350+ :return: list of converted odML documents
351+ """
300352 self .graph = Graph ().parse (source = StringIO (file ), format = doc_format )
301353 return self .to_odml ()
302354
303- # TODO check mandatory attrs
304355 def parse_document (self , doc_uri ):
356+ """
357+ parse_document parses an odML RDF Document node into an odML Document.
358+
359+ :param doc_uri: RDF URI of an odML Document node within an RDF graph.
360+ :return: dict containing an odML Document
361+ """
305362 doc_attrs = {}
306363 for attr in Document .rdf_map_items :
307364 elems = list (self .graph .objects (subject = doc_uri , predicate = attr [1 ]))
@@ -316,8 +373,13 @@ def parse_document(self, doc_uri):
316373
317374 return {'Document' : doc_attrs , 'odml-version' : FORMAT_VERSION }
318375
319- # TODO section subclass conversion
320376 def parse_section (self , sec_uri ):
377+ """
378+ parse_section parses an odML RDF Section node into an odML Section.
379+
380+ :param sec_uri: RDF URI of an odML Section node within an RDF graph.
381+ :return: dict containing an odML Section
382+ """
321383 sec_attrs = {}
322384 for attr in Section .rdf_map_items :
323385 elems = list (self .graph .objects (subject = sec_uri , predicate = attr [1 ]))
@@ -338,6 +400,12 @@ def parse_section(self, sec_uri):
338400 return sec_attrs
339401
340402 def parse_property (self , prop_uri ):
403+ """
404+ parse_property parses an odML RDF Property node into an odML Property.
405+
406+ :param prop_uri: RDF URI of an odML Property node within an RDF graph.
407+ :return: dict containing an odML Property
408+ """
341409 prop_attrs = {}
342410 for attr in Property .rdf_map_items :
343411 elems = list (self .graph .objects (subject = prop_uri , predicate = attr [1 ]))
@@ -366,10 +434,16 @@ def parse_property(self, prop_uri):
366434 return prop_attrs
367435
368436 @staticmethod
369- def _check_mandatory_attrs (attrs ):
370- if "name" not in attrs :
437+ def _check_mandatory_attrs (odml_entity ):
438+ """
439+ _check_mandatory_attrs checks whether a passed odML entity contains
440+ the required "name" attribute and raises a ParserException otherwise.
441+
442+ :param odml_entity: dict containing an odmL entity
443+ """
444+ if "name" not in odml_entity :
371445 msg = "Entity missing required 'name' attribute"
372- if "id" in attrs :
373- msg = "%s id:'%s'" % (msg , attrs ["id" ])
446+ if "id" in odml_entity :
447+ msg = "%s id:'%s'" % (msg , odml_entity ["id" ])
374448
375449 raise ParserException (msg )
0 commit comments