From 134d872ca6f81fafed3a8f880d6e05877c9680ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Mon, 22 Jul 2024 23:31:19 +0100 Subject: [PATCH 1/8] wip: First approach to get going. --- org_rw/org_rw.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index c0a1244..2120011 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -772,6 +772,20 @@ class Headline: else: raise NotImplementedError() + def update_raw_contents(self, new_contents): + # @TODO: Properly re-parse elements + self.keywords = [] + self.contents = [] + self.list_items = [] + self.table_rows = [] + self.properties = [] + self.structural = [] + self.delimiters = [] + for line in new_contents.split('\n'): + self.contents.append( + RawLine(linenum=0, line=line) + ) + def get_element_in_line(self, linenum): for line in self.contents: if linenum == line.linenum: -- 2.45.2 From 071004ea7a7f816733000b5a7de27b6faf02835a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Mon, 29 Jul 2024 15:36:57 +0100 Subject: [PATCH 2/8] Quick fix for handling date data. --- org_rw/org_rw.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index 86b233f..1d21c98 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -829,6 +829,10 @@ class Headline: self.properties = [] self.structural = [] self.delimiters = [] + self.scheduled = None + self.deadline = None + self.closed = None + for line in new_contents.split('\n'): self.contents.append( RawLine(linenum=0, line=line) -- 2.45.2 From 5de339570d675b1c7812483981205004ea582895 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Tue, 30 Jul 2024 10:21:44 +0200 Subject: [PATCH 3/8] Add (failing) test for reparse on headline update. --- tests/test_org.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/test_org.py b/tests/test_org.py index e49c6cf..c7cfd6e 100644 --- a/tests/test_org.py +++ b/tests/test_org.py @@ -865,6 +865,46 @@ class TestSerde(unittest.TestCase): self.assertEqual(dumps(doc), orig) + def test_update_reparse(self): + with open(os.path.join(DIR, "01-simple.org")) as f: + doc = load(f) + + hl = doc.getTopHeadlines()[0] + ex = HL( + "First level", + props=[ + ("ID", "01-simple-first-level-id"), + ("CREATED", DT(2020, 1, 1, 1, 1)), + ], + content=" First level content\n", + children=[ + HL( + "Second level", + props=[("ID", "01-simple-second-level-id")], + content="\n Second level content\n", + children=[ + HL( + "Third level", + props=[("ID", "01-simple-third-level-id")], + content="\n Third level content\n", + ) + ], + ) + ], + ) + + # Ground check + ex.assert_matches(self, hl) + + # Update + lines = list(doc.dump_headline(hl, recursive=False)) + assert lines[0].startswith('* ') # Title, skip it + content = '\n'.join(lines[1:]) + hl.update_raw_contents(content) + + # Check after update + ex.assert_matches(self, hl) + def print_tree(tree, indentation=0, headline=None): for element in tree: -- 2.45.2 From d0498d2f5ba476a18955ae0979caaaa0d7718867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Tue, 30 Jul 2024 10:52:06 +0200 Subject: [PATCH 4/8] Add support for data parse on headline content update. --- org_rw/org_rw.py | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index 1d21c98..acca21d 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -1,5 +1,4 @@ from __future__ import annotations -from typing import Optional from datetime import timedelta import collections import difflib @@ -9,7 +8,7 @@ import re import sys from datetime import date, datetime, timedelta from enum import Enum -from typing import cast, Iterator, List, Literal, Optional, Tuple, TypedDict, Union +from typing import Any, cast, Iterator, List, Literal, Optional, Tuple, TypedDict, TypeVar, Union from .types import HeadlineDict @@ -821,7 +820,7 @@ class Headline: raise NotImplementedError() def update_raw_contents(self, new_contents): - # @TODO: Properly re-parse elements + # Clear elements self.keywords = [] self.contents = [] self.list_items = [] @@ -833,10 +832,31 @@ class Headline: self.deadline = None self.closed = None - for line in new_contents.split('\n'): - self.contents.append( - RawLine(linenum=0, line=line) - ) + reader = OrgDocReader(environment=self.doc.environment) + reader.read(new_contents) + + # No need to finalize as we can take the data from the reader instead of from a doc + if len(reader.headlines) > 0: + # Probably can be done by just adding the headlines to this one's children + raise NotImplementedError('new headlines on raw contents not supported yet. This probably should be simple, see comment on code.') + + for kw in reader.keywords: + self.keywords.append(offset_linenum(self.start_line + 1, kw)) + + for content in reader.contents: + self.contents.append(offset_linenum(self.start_line + 1, content)) + + for list_item in reader.list_items: + self.list_items.append(offset_linenum(self.start_line + 1, list_item)) + + for struct_item in reader.structural: + self.structural.append(offset_linenum(self.start_line + 1, struct_item)) + + for prop in reader.properties: + self.properties.append(offset_linenum(self.start_line + 1, prop)) + + # Environment is not used, as it's known + def get_element_in_line(self, linenum): for line in self.contents: @@ -1054,6 +1074,19 @@ TableRow = collections.namedtuple( ), ) +ItemWithLineNum = Union[Keyword, RawLine, Property, ListItem, tuple[int, Any]] +def offset_linenum(offset: int, item: ItemWithLineNum) -> ItemWithLineNum: + if isinstance(item, tuple) and len(item) == 2 and isinstance(item[0], int): + return item + + if isinstance(item, ListItem): + item.linenum += offset + return item + + assert isinstance(item, (Keyword, RawLine, Property)), \ + "Expected (Keyword|RawLine|Property), found {}".format(item) + return item._replace(linenum=item.linenum + offset) + # @TODO How are [YYYY-MM-DD HH:mm--HH:mm] and ([... HH:mm]--[... HH:mm]) differentiated ? # @TODO Consider recurrence annotations @@ -2258,6 +2291,7 @@ class OrgDoc: self.headlines: List[Headline] = list( map(lambda hl: parse_headline(hl, self, self), headlines) ) + self.environment = environment @property def id(self): -- 2.45.2 From ef615feac5e7fc178d3a6888d4e817a1891a62f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Tue, 30 Jul 2024 10:52:37 +0200 Subject: [PATCH 5/8] Allow trailing whitespace changes on raw content update. --- tests/test_org.py | 2 +- tests/utils/assertions.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_org.py b/tests/test_org.py index c7cfd6e..e66e338 100644 --- a/tests/test_org.py +++ b/tests/test_org.py @@ -903,7 +903,7 @@ class TestSerde(unittest.TestCase): hl.update_raw_contents(content) # Check after update - ex.assert_matches(self, hl) + ex.assert_matches(self, hl, accept_trailing_whitespace_changes=True) def print_tree(tree, indentation=0, headline=None): diff --git a/tests/utils/assertions.py b/tests/utils/assertions.py index 59dc658..732bc37 100644 --- a/tests/utils/assertions.py +++ b/tests/utils/assertions.py @@ -58,7 +58,7 @@ class HL: self.content = content self.children = children - def assert_matches(self, test_case: unittest.TestCase, doc): + def assert_matches(self, test_case: unittest.TestCase, doc, accept_trailing_whitespace_changes=False): test_case.assertEqual(self.title, get_raw(doc.title)) # Check properties @@ -75,7 +75,10 @@ class HL: timestamp_to_datetime(doc_props[i].value), prop[1] ) - test_case.assertEqual(get_raw_contents(doc), self.get_raw()) + if accept_trailing_whitespace_changes: + test_case.assertEqual(get_raw_contents(doc).rstrip(), self.get_raw().rstrip()) + else: + test_case.assertEqual(get_raw_contents(doc), self.get_raw()) # Check children if self.children is None: -- 2.45.2 From c7f78e0a6cc7b25ddbe7bb7555b72cdda70db7bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Tue, 30 Jul 2024 15:33:49 +0200 Subject: [PATCH 6/8] Add reparse test line ordering of reparsing. --- tests/13-update-reparse-test.org | 22 +++++++++++++++++ tests/test_org.py | 42 +++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/13-update-reparse-test.org diff --git a/tests/13-update-reparse-test.org b/tests/13-update-reparse-test.org new file mode 100644 index 0000000..97eee86 --- /dev/null +++ b/tests/13-update-reparse-test.org @@ -0,0 +1,22 @@ +#+TITLE: 13-Update reparse +#+DESCRIPTION: Update-Reparse org file +#+TODO: TODO(t) PAUSED(p) | DONE(d) + + +* First level + :PROPERTIES: + :ID: 13-update-reparse-first-level-id + :CREATED: [2020-01-01 Wed 01:01] + :END: + First level content + + - A list of items :: + - With a sublist + + Something after the list. + +** Second level + :PROPERTIES: + :ID: 13-update-reparse-second-level-id + :END: + Second level content diff --git a/tests/test_org.py b/tests/test_org.py index e66e338..96e0181 100644 --- a/tests/test_org.py +++ b/tests/test_org.py @@ -1,4 +1,5 @@ import os +import tempfile import unittest from datetime import datetime as DT @@ -865,7 +866,7 @@ class TestSerde(unittest.TestCase): self.assertEqual(dumps(doc), orig) - def test_update_reparse(self): + def test_update_reparse_same_structure(self): with open(os.path.join(DIR, "01-simple.org")) as f: doc = load(f) @@ -905,6 +906,45 @@ class TestSerde(unittest.TestCase): # Check after update ex.assert_matches(self, hl, accept_trailing_whitespace_changes=True) + def test_update_reparse_same_values(self): + with open(os.path.join(DIR, "13-update-reparse-test.org")) as f: + doc = load(f) + + expected_hl_contents = ''' :PROPERTIES: + :ID: 13-update-reparse-first-level-id + :CREATED: [2020-01-01 Wed 01:01] + :END: + First level content + + - A list of items :: + - With a sublist + + Something after the list. +''' + + hl = doc.getTopHeadlines()[0] + lines = list(doc.dump_headline(hl, recursive=False)) + assert lines[0].startswith('* ') # Title, skip it + content = '\n'.join(lines[1:]) + self.assertEqual(content, expected_hl_contents) + + # Check after update + hl.update_raw_contents(content) + self.assertEqual(content, expected_hl_contents) + + # Check after dump and reload + with tempfile.NamedTemporaryFile('wt') as f: + save = org_rw.dumps(doc) + f.write(save) + f.flush() + + with open(f.name, 'rt') as reader: + reloaded = org_rw.load(reader) + re_hl = reloaded.getTopHeadlines()[0] + lines = list(doc.dump_headline(hl, recursive=False)) + assert lines[0].startswith('* ') # Title, skip it + content = '\n'.join(lines[1:]) + self.assertEqual(content, expected_hl_contents) def print_tree(tree, indentation=0, headline=None): for element in tree: -- 2.45.2 From 97db5686d80ad4fe9164560e6ab5ea255c19e83e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Tue, 30 Jul 2024 17:43:46 +0200 Subject: [PATCH 7/8] Tag structural elements. --- org_rw/org_rw.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index acca21d..c8836e1 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -1026,6 +1026,9 @@ Keyword = collections.namedtuple( Property = collections.namedtuple( "Property", ("linenum", "match", "key", "value", "options") ) +Structural = collections.namedtuple( + "Structural", ("linenum", "line") +) class ListItem: @@ -1074,17 +1077,14 @@ TableRow = collections.namedtuple( ), ) -ItemWithLineNum = Union[Keyword, RawLine, Property, ListItem, tuple[int, Any]] +ItemWithLineNum = Union[Keyword, RawLine, Property, ListItem, Structural] def offset_linenum(offset: int, item: ItemWithLineNum) -> ItemWithLineNum: - if isinstance(item, tuple) and len(item) == 2 and isinstance(item[0], int): - return item - if isinstance(item, ListItem): item.linenum += offset return item - assert isinstance(item, (Keyword, RawLine, Property)), \ - "Expected (Keyword|RawLine|Property), found {}".format(item) + assert isinstance(item, (Keyword, RawLine, Property, Structural)), \ + "Expected (Keyword|RawLine|Property|Structural), found {}".format(item) return item._replace(linenum=item.linenum + offset) @@ -2465,8 +2465,8 @@ class OrgDocReader: self.delimiters: List[DelimiterLine] = [] self.list_items: List[ListItem] = [] self.table_rows: List[TableRow] = [] - self.structural: List = [] - self.properties: List = [] + self.structural: List[Structural] = [] + self.properties: List[Property] = [] self.current_drawer: Optional[List] = None self.environment = environment @@ -2648,7 +2648,7 @@ class OrgDocReader: def add_property_drawer_line(self, linenum: int, line: str, match: re.Match): if len(self.headline_hierarchy) == 0: self.current_drawer = self.properties - self.structural.append((linenum, line)) + self.structural.append(Structural(linenum, line)) else: assert self.headline_hierarchy[-1] is not None self.current_drawer = self.headline_hierarchy[-1]["properties"] @@ -2667,7 +2667,7 @@ class OrgDocReader: def add_drawer_end_line(self, linenum: int, line: str, match: re.Match): self.current_drawer = None if len(self.headline_hierarchy) == 0: - self.structural.append((linenum, line)) + self.structural.append(Structural(linenum, line)) else: assert self.headline_hierarchy[-1] is not None self.headline_hierarchy[-1]["structural"].append((linenum, line)) -- 2.45.2 From 15af4212aea4bc0b27d984adf444801712d9a015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Mon, 7 Oct 2024 19:48:16 +0200 Subject: [PATCH 8/8] Apply formatting scripts. --- org_rw/org_rw.py | 18 ++++++++++-------- tests/test_org.py | 20 ++++++++++---------- tests/utils/assertions.py | 11 +++++++++-- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index 7133ad9..ddb88ec 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -1,4 +1,5 @@ from __future__ import annotations + import collections import difflib import logging @@ -7,7 +8,6 @@ import re import sys from datetime import date, datetime, timedelta from enum import Enum - from typing import ( Dict, Iterator, @@ -860,7 +860,9 @@ class Headline: # No need to finalize as we can take the data from the reader instead of from a doc if len(reader.headlines) > 0: # Probably can be done by just adding the headlines to this one's children - raise NotImplementedError('new headlines on raw contents not supported yet. This probably should be simple, see comment on code.') + raise NotImplementedError( + "new headlines on raw contents not supported yet. This probably should be simple, see comment on code." + ) for kw in reader.keywords: self.keywords.append(offset_linenum(self.start_line + 1, kw)) @@ -879,7 +881,6 @@ class Headline: # Environment is not used, as it's known - def get_element_in_line(self, linenum): for line in self.contents: if linenum == line.linenum: @@ -1077,9 +1078,7 @@ Keyword = collections.namedtuple( Property = collections.namedtuple( "Property", ("linenum", "match", "key", "value", "options") ) -Structural = collections.namedtuple( - "Structural", ("linenum", "line") -) +Structural = collections.namedtuple("Structural", ("linenum", "line")) class ListItem: @@ -1129,13 +1128,16 @@ TableRow = collections.namedtuple( ) ItemWithLineNum = Union[Keyword, RawLine, Property, ListItem, Structural] + + def offset_linenum(offset: int, item: ItemWithLineNum) -> ItemWithLineNum: if isinstance(item, ListItem): item.linenum += offset return item - assert isinstance(item, (Keyword, RawLine, Property, Structural)), \ - "Expected (Keyword|RawLine|Property|Structural), found {}".format(item) + assert isinstance( + item, (Keyword, RawLine, Property, Structural) + ), "Expected (Keyword|RawLine|Property|Structural), found {}".format(item) return item._replace(linenum=item.linenum + offset) diff --git a/tests/test_org.py b/tests/test_org.py index 959d968..cb204f6 100644 --- a/tests/test_org.py +++ b/tests/test_org.py @@ -903,8 +903,8 @@ class TestSerde(unittest.TestCase): # Update lines = list(doc.dump_headline(hl, recursive=False)) - assert lines[0].startswith('* ') # Title, skip it - content = '\n'.join(lines[1:]) + assert lines[0].startswith("* ") # Title, skip it + content = "\n".join(lines[1:]) hl.update_raw_contents(content) # Check after update @@ -914,7 +914,7 @@ class TestSerde(unittest.TestCase): with open(os.path.join(DIR, "13-update-reparse-test.org")) as f: doc = load(f) - expected_hl_contents = ''' :PROPERTIES: + expected_hl_contents = """ :PROPERTIES: :ID: 13-update-reparse-first-level-id :CREATED: [2020-01-01 Wed 01:01] :END: @@ -924,12 +924,12 @@ class TestSerde(unittest.TestCase): - With a sublist Something after the list. -''' +""" hl = doc.getTopHeadlines()[0] lines = list(doc.dump_headline(hl, recursive=False)) - assert lines[0].startswith('* ') # Title, skip it - content = '\n'.join(lines[1:]) + assert lines[0].startswith("* ") # Title, skip it + content = "\n".join(lines[1:]) self.assertEqual(content, expected_hl_contents) # Check after update @@ -937,17 +937,17 @@ class TestSerde(unittest.TestCase): self.assertEqual(content, expected_hl_contents) # Check after dump and reload - with tempfile.NamedTemporaryFile('wt') as f: + with tempfile.NamedTemporaryFile("wt") as f: save = org_rw.dumps(doc) f.write(save) f.flush() - with open(f.name, 'rt') as reader: + with open(f.name, "rt") as reader: reloaded = org_rw.load(reader) re_hl = reloaded.getTopHeadlines()[0] lines = list(doc.dump_headline(hl, recursive=False)) - assert lines[0].startswith('* ') # Title, skip it - content = '\n'.join(lines[1:]) + assert lines[0].startswith("* ") # Title, skip it + content = "\n".join(lines[1:]) self.assertEqual(content, expected_hl_contents) def test_mimic_write_file_13(self): diff --git a/tests/utils/assertions.py b/tests/utils/assertions.py index 011c255..47ab637 100644 --- a/tests/utils/assertions.py +++ b/tests/utils/assertions.py @@ -67,7 +67,12 @@ class HL: self.content = content self.children = children - def assert_matches(self, test_case: unittest.TestCase, doc, accept_trailing_whitespace_changes=False): + def assert_matches( + self, + test_case: unittest.TestCase, + doc, + accept_trailing_whitespace_changes=False, + ): test_case.assertEqual(self.title, get_raw(doc.title)) # Check properties @@ -85,7 +90,9 @@ class HL: ) if accept_trailing_whitespace_changes: - test_case.assertEqual(get_raw_contents(doc).rstrip(), self.get_raw().rstrip()) + test_case.assertEqual( + get_raw_contents(doc).rstrip(), self.get_raw().rstrip() + ) else: test_case.assertEqual(get_raw_contents(doc), self.get_raw()) -- 2.45.2