From 92078617fcffb694babd7c0efe2aea8d7bf0812a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Sun, 1 Sep 2024 23:46:10 +0200 Subject: [PATCH] Add tests and implement `org-tags-exclude-from-inheritance`. --- org_rw/org_rw.py | 10 ++++++++-- tests/test_org.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index 52257f9..c139fa3 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -752,8 +752,13 @@ class Headline: return times @property - def tags(self): - return list(self.shallow_tags) + self.parent.tags + def tags(self) -> list[str]: + parent_tags = self.parent.tags + if self.doc.environment.get('org-tags-exclude-from-inheritance'): + for tag in self.doc.environment.get('org-tags-exclude-from-inheritance'): + if tag in parent_tags: + parent_tags.remove(tag) + return list(self.shallow_tags) + parent_tags def add_tag(self, tag: str): self.shallow_tags.append(tag) @@ -2234,6 +2239,7 @@ class OrgDoc: ): self.todo_keywords = [HeadlineState(name=kw) for kw in DEFAULT_TODO_KEYWORDS] self.done_keywords = [HeadlineState(name=kw) for kw in DEFAULT_DONE_KEYWORDS] + self.environment = environment keywords_set_in_file = False for keyword in keywords: diff --git a/tests/test_org.py b/tests/test_org.py index d515168..662a472 100644 --- a/tests/test_org.py +++ b/tests/test_org.py @@ -906,6 +906,25 @@ class TestSerde(unittest.TestCase): h1_2_h2 = h1_2.children[0] self.assertEqual(sorted(h1_2_h2.shallow_tags), ['otherh2tag']) + def test_exclude_tags_from_inheritance_property_read_13(self): + with open(os.path.join(DIR, "13-tags.org")) as f: + orig = f.read() + doc = loads(orig, { + 'org-tags-exclude-from-inheritance': ('h1tag', 'otherh2tag') + }) + + self.assertEqual(doc.tags, ['filetag']) + + h1_1, h1_2 = doc.getTopHeadlines() + self.assertEqual(sorted(h1_1.tags), ['filetag', 'h1tag']) + self.assertEqual(sorted(h1_2.tags), ['filetag', 'otherh1tag']) + + h1_1_h2 = h1_1.children[0] + self.assertEqual(sorted(h1_1_h2.tags), ['filetag', 'h2tag']) + + h1_2_h2 = h1_2.children[0] + self.assertEqual(sorted(h1_2_h2.tags), ['filetag', 'otherh1tag', 'otherh2tag']) + def print_tree(tree, indentation=0, headline=None): for element in tree: print(" " * indentation * 2, "EL:", element)