/test_simple-agenda.py

https://github.com/bhutley/PyOrgMode
Python | 57 lines | 53 code | 3 blank | 1 comment | 2 complexity | f346bd963628dc48261b4abb91ae2edb MD5 | raw file
  1. import PyOrgMode
  2. import copy
  3. try:
  4. import unittest2 as unittest
  5. except ImportError:
  6. import unittest
  7. def Get_Scheduled_Elements(element, data=[]):
  8. """
  9. Grab the data from all scheduled elements for all the tree defined by 'element' recursively.
  10. Returns all the elements as an array.
  11. """
  12. if hasattr(element,"content"):
  13. for child in element.content:
  14. if hasattr(child,"TYPE"):
  15. if child.TYPE == "SCHEDULE_ELEMENT":
  16. # This element is scheduled, we are creating a copy of it
  17. data.append(copy.deepcopy(child.parent))
  18. Get_Scheduled_Elements(child,data)
  19. return data
  20. class TestAgenda(unittest.TestCase):
  21. def test_agenda(self):
  22. # Creating the input and output files data structures
  23. input_file = PyOrgMode.OrgDataStructure()
  24. output_file = PyOrgMode.OrgDataStructure()
  25. # Loading from agenda.org file
  26. input_file.load_from_file("agenda.org")
  27. # Get the scheduled elements (those with SCHEDULE, DEADLINE in them, not in the node name)
  28. scheduled_elements = Get_Scheduled_Elements(input_file.root)
  29. # Assign these element to the root (reparent each elements recursively, relevel them cleanly)
  30. output_file.root.append_clean(scheduled_elements)
  31. output_file.save_to_file("test_scheduled_output.org")
  32. saved = open("test_scheduled_output.org").readlines()
  33. self.assertEqual(saved, ['* Element 1\n',
  34. ' SCHEDULED: <2011-02-08>\n',
  35. '* Element 3\n',
  36. ' DEADLINE: <2011-02-08>\n',
  37. '** Test\n',
  38. '** Element 4\n',
  39. ' SCHEDULED: <2011-02-08>\n',
  40. '*** Couic\n',
  41. '* Element 4\n',
  42. ' SCHEDULED: <2011-02-08>\n',
  43. '** Couic\n'])
  44. if __name__ == '__main__':
  45. unittest.main()