format.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import odml
  2. """
  3. A module providing general format information
  4. and mappings of xml-attributes to their python class equivalents
  5. """
  6. class Format(object):
  7. _map = {}
  8. _rev_map = None
  9. def map(self, name):
  10. """maps an odml name to a python name"""
  11. return self._map.get(name, name)
  12. def revmap(self, name):
  13. """maps a python name to an odml name"""
  14. if self._rev_map is None:
  15. # create the reverse map only if requested
  16. self._rev_map = {}
  17. for k, v in self._map.iteritems():
  18. self._rev_map[v] = k
  19. return self._rev_map.get(name, name)
  20. def __iter__(self):
  21. """iterates each python property name"""
  22. for k in self._args:
  23. yield self.map(k)
  24. def create(self, *args, **kargs):
  25. return getattr(odml, self.__class__.__name__)(*args, **kargs)
  26. class Value(Format):
  27. _name = "value"
  28. _args = {
  29. 'value': 0,
  30. 'uncertainty': 0,
  31. 'unit': 0,
  32. 'type': 0,
  33. 'definition': 0,
  34. 'reference': 0,
  35. 'filename': 0,
  36. 'checksum': 0,
  37. 'encoder': 0
  38. }
  39. _map = {'type': 'dtype'}
  40. class Property(Format):
  41. _name = "property"
  42. _args = {
  43. 'name': 1,
  44. 'value': 1,
  45. 'definition': 0,
  46. 'mapping': 0,
  47. 'dependency': 0,
  48. 'dependencyvalue': 0
  49. }
  50. _map = {
  51. 'value': 'values',
  52. 'dependencyvalue': 'dependency_value',
  53. 'type': 'dtype'
  54. }
  55. class Section(Format):
  56. _name = "section"
  57. _args = {
  58. 'type': 1,
  59. 'name': 0,
  60. 'definition': 0,
  61. 'reference': 0,
  62. 'link': 0,
  63. 'repository': 0,
  64. 'mapping': 0,
  65. 'section': 0,
  66. 'include': 0,
  67. 'property': 0
  68. }
  69. _map = {
  70. 'section': 'sections',
  71. 'property': 'properties',
  72. }
  73. class Document(Format):
  74. _name = "odML"
  75. _args = {
  76. 'version': 0,
  77. 'author': 0,
  78. 'date': 0,
  79. 'section': 0,
  80. 'repository': 0,
  81. }
  82. _map = {
  83. 'section': 'sections'
  84. }
  85. Document = Document()
  86. Section = Section()
  87. Value = Value()
  88. Property = Property()
  89. __all__ = [Document, Section, Property, Value]