Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

format.py 4.6 KB

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