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())