doc.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8
  2. import odml.dtypes as dtypes
  3. import odml.base as base
  4. import odml.format as format
  5. import odml.terminology as terminology
  6. from odml.tools.doc_inherit import inherit_docstring, allow_inherit_docstring
  7. class Document(base._baseobj):
  8. pass
  9. @allow_inherit_docstring
  10. class BaseDocument(base.sectionable, Document):
  11. """
  12. A represenation of an odML document in memory.
  13. Its odml attributes are: *author*, *date*, *version* and *repository*.
  14. A Document behaves very much like a section, except that it cannot hold
  15. properties.
  16. """
  17. _format = format.Document
  18. def __init__(self, author=None, date=None, version=None, repository=None):
  19. super(BaseDocument, self).__init__()
  20. self._author = author
  21. self._date = date # date must be a datetime
  22. self._version = version
  23. self._repository = repository
  24. @property
  25. def author(self):
  26. """
  27. The author of the document.
  28. """
  29. return self._author
  30. @author.setter
  31. def author(self, new_value):
  32. self._author = new_value
  33. @property
  34. def version(self):
  35. """
  36. A personal version-specifier that can be used to track different
  37. versions of the same document.
  38. """
  39. return self._version
  40. @version.setter
  41. def version(self, new_value):
  42. self._version = new_value
  43. @property
  44. def date(self):
  45. """
  46. The date the document was created.
  47. """
  48. return dtypes.set(self._date, "date")
  49. @date.setter
  50. def date(self, new_value):
  51. self._date = dtypes.get(new_value, "date")
  52. @property
  53. def parent(self):
  54. """The parent of a document is always None."""
  55. return None
  56. def __repr__(self):
  57. return "<Doc %s by %s (%d sections)>" % (self._version, self._author, len(self._sections))
  58. def finalize(self):
  59. """
  60. This needs to be called after the document is set up from parsing
  61. it will perform additional operations, that need the complete document.
  62. In particular, this method will resolve all *link* and *include* attributes
  63. accordingly.
  64. """
  65. # we could not fill out links while parsing (referenced sections where not known),
  66. # so try to set them now, where the document is complete
  67. for sec in self.itersections(recursive=True):
  68. if sec._link is not None:
  69. sec.link = sec._link
  70. if sec._include is not None:
  71. sec.include = sec._include
  72. @inherit_docstring
  73. def get_terminology_equivalent(self):
  74. if self.repository is None: return None
  75. term = terminology.load(self.repository)
  76. return term