format.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. """
  2. The module provides general format information and mappings of
  3. XML and RDF attributes to their Python class equivalents.
  4. """
  5. import sys
  6. from rdflib import Namespace
  7. import odml
  8. class Format(object):
  9. """
  10. Base format class for all odML object formats. The formats are required
  11. when the corresponding odML objects are serialized to or loaded from files.
  12. """
  13. _name = ""
  14. _args = {}
  15. _map = {}
  16. _rev_map = None
  17. _rdf_map = {}
  18. _rdf_type = None
  19. _ns = Namespace("https://g-node.org/odml-rdf#")
  20. @property
  21. def name(self):
  22. """Returns the name of the current odML format"""
  23. return self._name
  24. @property
  25. def arguments(self):
  26. """Returns all items in the current odML format argument dict"""
  27. return self._args.items()
  28. @property
  29. def arguments_keys(self):
  30. """Returns all keys of the current odML format argument dict"""
  31. return self._args.keys()
  32. def map(self, name):
  33. """ Maps an odml name to a python name """
  34. return self._map.get(name, name)
  35. @property
  36. def map_keys(self):
  37. """Returns all keys of the current odML format map dict"""
  38. return self._map.keys()
  39. def rdf_map(self, name):
  40. """ Maps a python name to a odml rdf namespace """
  41. return self._rdf_map.get(name, name)
  42. @property
  43. def rdf_map_keys(self):
  44. """Returns all keys of the current odML format RDF map dict"""
  45. return self._rdf_map.keys()
  46. @property
  47. def rdf_map_items(self):
  48. """Returns all items of the current odML format RDF map dict"""
  49. return self._rdf_map.items()
  50. @property
  51. def rdf_type(self):
  52. """ Return rdf type of an object """
  53. return self._rdf_type
  54. @staticmethod
  55. def namespace():
  56. """ Return current link to current odml namespace"""
  57. return Format._ns
  58. def revmap(self, name):
  59. """ Maps a python name to an odml name """
  60. if self._rev_map is None:
  61. # create the reverse map only if requested
  62. self._rev_map = {}
  63. if sys.version_info < (3, 0):
  64. for k, val in self._map.iteritems():
  65. self._rev_map[val] = k
  66. else:
  67. for k, val in self._map.items():
  68. self._rev_map[val] = k
  69. return self._rev_map.get(name)
  70. def __iter__(self):
  71. """ Iterates each python property name """
  72. for k in self._args:
  73. yield self.map(k)
  74. def create(self, *args, **kargs):
  75. """
  76. This method will call the init method of the odML class implementation
  77. corresponding to the specific format odML class and return the initialised
  78. class instance. e.g. format.Document.create() will return an initialised
  79. odml.Document instance.
  80. """
  81. return getattr(odml, self.__class__.__name__)(*args, **kargs)
  82. class Property(Format):
  83. """
  84. The format class for the odml Property class.
  85. """
  86. _name = "property"
  87. _ns = Format._ns
  88. _rdf_type = _ns.Property
  89. _args = {
  90. 'id': 0,
  91. 'name': 1,
  92. 'value': 0,
  93. 'unit': 0,
  94. 'definition': 0,
  95. 'dependency': 0,
  96. 'dependencyvalue': 0,
  97. 'uncertainty': 0,
  98. 'reference': 0,
  99. 'type': 0,
  100. 'value_origin': 0,
  101. 'val_cardinality': 0
  102. }
  103. _map = {
  104. 'dependencyvalue': 'dependency_value',
  105. 'type': 'dtype',
  106. 'id': 'oid',
  107. 'value': 'values'
  108. }
  109. _rdf_map = {
  110. 'id': _ns.hasId,
  111. 'name': _ns.hasName,
  112. 'definition': _ns.hasDefinition,
  113. 'dtype': _ns.hasDtype,
  114. 'unit': _ns.hasUnit,
  115. 'uncertainty': _ns.hasUncertainty,
  116. 'reference': _ns.hasReference,
  117. 'value': _ns.hasValue,
  118. 'value_origin': _ns.hasValueOrigin
  119. }
  120. class Section(Format):
  121. """
  122. The format class for the odml Section class.
  123. """
  124. _name = "section"
  125. _ns = Format._ns
  126. _rdf_type = _ns.Section
  127. _args = {
  128. 'id': 0,
  129. 'type': 1,
  130. 'name': 1,
  131. 'definition': 0,
  132. 'reference': 0,
  133. 'link': 0,
  134. 'repository': 0,
  135. 'section': 0,
  136. 'include': 0,
  137. 'property': 0,
  138. 'sec_cardinality': 0,
  139. 'prop_cardinality': 0
  140. }
  141. _map = {
  142. 'section': 'sections',
  143. 'property': 'properties',
  144. 'id': 'oid'
  145. }
  146. _rdf_map = {
  147. 'id': _ns.hasId,
  148. 'name': _ns.hasName,
  149. 'definition': _ns.hasDefinition,
  150. 'type': _ns.hasType,
  151. 'repository': _ns.hasTerminology,
  152. 'reference': _ns.hasReference,
  153. 'sections': _ns.hasSection,
  154. 'properties': _ns.hasProperty,
  155. }
  156. class Document(Format):
  157. """
  158. The format class for the odml Document class.
  159. """
  160. _name = "odML"
  161. _ns = Format._ns
  162. _rdf_type = _ns.Document
  163. _args = {
  164. 'id': 0,
  165. 'version': 0,
  166. 'author': 0,
  167. 'date': 0,
  168. 'section': 0,
  169. 'repository': 0,
  170. }
  171. _map = {
  172. 'section': 'sections',
  173. 'id': 'oid'
  174. }
  175. _rdf_map = {
  176. 'id': _ns.hasId,
  177. 'author': _ns.hasAuthor,
  178. 'date': _ns.hasDate,
  179. 'version': _ns.hasDocVersion,
  180. 'repository': _ns.hasTerminology,
  181. 'sections': _ns.hasSection
  182. }
  183. Document = Document()
  184. Section = Section()
  185. Property = Property()
  186. __all__ = [Document, Section, Property]