Commits

Anonymous committed b2d1eb0

Add Orgnode.setParent, Orgnode.Parent, and inher option to Orgnode.Tags.
Test for inher option is passed.

  • Participants
  • Parent commits 712ed51

Comments (0)

Files changed (3)

             n.setPriority(prtysrch.group(1))
             n.setHeading(prtysrch.group(2))
 
+    # set parent of nodes
+    ancestors = [None]
+    n1 = nodelist[0]
+    l1 = n1.Level()
+    for n2 in nodelist:
+        # n1, l1: previous node and its level
+        # n2, l2: this node and its level
+        l2 = n2.Level()
+        if l1 < l2:
+            ancestors.append(n1)
+        else:
+            while len(ancestors) > l2:
+                ancestors.pop()
+        n2.setParent(ancestors[-1])
+        n1 = n2
+        l1 = l2
+
     return nodelist
 
 ######################
         self.properties = dict()
         self.datelist = []
         self.rangelist = []
+        self.parent = None
         for t in alltags:
             self.tags[t] = ''
 
         """
         return self.tag
 
-    def Tags(self):
+    def Tags(self, inher=False):
         """
         Returns a list of all tags
         For example, :HOME:COMPUTER: would return ['HOME', 'COMPUTER']
+        If `inher` is True, then all tags from ancestors is included.
         """
-        return self.tags.keys()
+        if inher and self.parent:
+            return set(self.tags.keys()) | set(self.parent.Tags(True))
+        else:
+            return set(self.tags.keys())
 
     def hasTag(self, srch):
         """
                 bool(self.datelist) or
                 bool(self.rangelist) )
 
+    def setParent(self, parent):
+        """
+        Set parent node
+        """
+        self.parent = parent
+
+    def Parent(self):
+        """
+        Return parent node if exist else None.
+        """
+        return self.parent
+
     def __repr__(self):
         """
         Print the level, heading text and tag of a node and the body

File test/data_00.py

-keys1 = ['level', 'heading', 'tags', 'todo']
+keys1 = ['level', 'heading', 'todo', 'tags', 'tags_inher']
 keys2 = ['level', 'heading']
+
+def tags(nums):
+    return set(['TAG%d'%i for i in nums])
+
 data = [
     dict(zip(keys1, vals)) for vals in
-    [[1, 'Heading (Level 1) 1', ['TAG1'], 'TODO1'],
-     [2, 'Heading (Level 2) 1', ['TAG2'], 'TODO2'],
-     [3, 'Heading (Level 3) 1', ['TAG3'], 'TODO3'],
-     [4, 'Heading (Level 4) 1', ['TAG4'], 'TODO4'],
+    [[1, 'Heading (Level 1) 1', 'TODO1', tags([1]), tags(range(1,2))],
+     [2, 'Heading (Level 2) 1', 'TODO2', tags([2]), tags(range(1,3))],
+     [3, 'Heading (Level 3) 1', 'TODO3', tags([3]), tags(range(1,4))],
+     [4, 'Heading (Level 4) 1', 'TODO4', tags([4]), tags(range(1,5))],
      ]
     ] + [
     dict(zip(keys2, vals)) for vals in

File test/test.py

 
 TESTDIR = os.path.dirname(__file__)
 
+def value_from_data_key(node, key):
+    """
+    Helper function for check_data. Get value from Orgnode by key.
+    """
+    if key == 'tags_inher':
+        return node.Tags(inher=True)
+    else:
+        return node.__getattribute__(key.title())()
+
+
 def check_data(dataname):
     """Helper function for test_data"""
     oname = os.path.join(TESTDIR, dataname + '.org')
 
     for (i, (node, kwds)) in enumerate(zip(nodelist, data)):
         for key in kwds:
-            val = node.__getattribute__(key.title())()
+            val = value_from_data_key(node, key)
             eq_(kwds[key], val,
                 msg=('check value of %d-th node of key "%s" from "%s". '
                      'Orgnode.%s() = %s != %s.'