1
1

parser_utils.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. Utility file to provide constants, exceptions and functions
  3. commonly used by the odML tools parsers and converters.
  4. """
  5. SUPPORTED_PARSERS = ['XML', 'YAML', 'JSON', 'RDF']
  6. RDF_CONVERSION_FORMATS = {
  7. # rdflib version "4.2.2" serialization formats
  8. 'xml': '.rdf',
  9. 'pretty-xml': '.rdf',
  10. 'trix': '.rdf',
  11. 'n3': '.n3',
  12. 'turtle': '.ttl',
  13. 'ttl': '.ttl',
  14. 'ntriples': '.nt',
  15. 'nt': '.nt',
  16. 'nt11': '.nt',
  17. 'trig': '.trig',
  18. 'json-ld': '.jsonld'
  19. }
  20. class ParserException(Exception):
  21. """
  22. Exception wrapper used by various odML parsers.
  23. """
  24. class InvalidVersionException(ParserException):
  25. """
  26. Exception wrapper to indicate a non-compatible odML version.
  27. """
  28. def odml_tuple_export(odml_tuples):
  29. """
  30. Converts odml style tuples to a parsable string representation.
  31. Every tuple is represented by brackets '()'. The individual elements of a tuple are
  32. separated by a semicolon ';'. The individual tuples are separated by a comma ','.
  33. An odml 3-tuple list of 2 tuples would be serialized to: "[(11;12;13),(21;22;23)]".
  34. :param odml_tuples: List of odml style tuples.
  35. :return: string
  36. """
  37. str_tuples = ""
  38. for val in odml_tuples:
  39. str_val = ";".join(val)
  40. if str_tuples:
  41. str_tuples = "%s,(%s)" % (str_tuples, str_val)
  42. else:
  43. str_tuples = "(%s)" % str_val
  44. return "[%s]" % str_tuples