Compare commits
No commits in common. "7e44bce145468f90ce481442df2732de32fc830e" and "b174405c902cd5c1782c3fc0036431d5d349462d" have entirely different histories.
7e44bce145
...
b174405c90
633
org_rw/org_rw.py
633
org_rw/org_rw.py
File diff suppressed because it is too large
Load Diff
@ -1,7 +1,6 @@
|
|||||||
import re
|
import re
|
||||||
from typing import List, TypedDict
|
from typing import List, TypedDict
|
||||||
|
|
||||||
|
|
||||||
class HeadlineDict(TypedDict):
|
class HeadlineDict(TypedDict):
|
||||||
linenum: int
|
linenum: int
|
||||||
orig: re.Match
|
orig: re.Match
|
||||||
|
@ -1,23 +1,14 @@
|
|||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
from datetime import date
|
||||||
from datetime import datetime as DT
|
from datetime import datetime as DT
|
||||||
|
|
||||||
from org_rw import MarkerToken, MarkerType, Timestamp, dumps, load, loads, dom
|
from org_rw import MarkerToken, MarkerType, Timestamp, dumps, load, loads, dom
|
||||||
import org_rw
|
import org_rw
|
||||||
|
|
||||||
from utils.assertions import (
|
from utils.assertions import (BOLD, CODE, HL, ITALIC, SPAN, STRIKE, UNDERLINED,
|
||||||
BOLD,
|
VERBATIM, WEB_LINK, Doc, Tokens)
|
||||||
CODE,
|
|
||||||
HL,
|
|
||||||
ITALIC,
|
|
||||||
SPAN,
|
|
||||||
STRIKE,
|
|
||||||
UNDERLINED,
|
|
||||||
VERBATIM,
|
|
||||||
WEB_LINK,
|
|
||||||
Doc,
|
|
||||||
Tokens,
|
|
||||||
)
|
|
||||||
|
|
||||||
DIR = os.path.dirname(os.path.abspath(__file__))
|
DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
@ -292,19 +283,13 @@ class TestSerde(unittest.TestCase):
|
|||||||
SPAN("\n"),
|
SPAN("\n"),
|
||||||
SPAN(
|
SPAN(
|
||||||
" This is a ",
|
" This is a ",
|
||||||
WEB_LINK(
|
WEB_LINK("[tricky web link]\u200b", "https://codigoparallevar.com/4"),
|
||||||
"[tricky web link]\u200b",
|
|
||||||
"https://codigoparallevar.com/4",
|
|
||||||
),
|
|
||||||
" followed up with some text.\n",
|
" followed up with some text.\n",
|
||||||
),
|
),
|
||||||
SPAN("\n"),
|
SPAN("\n"),
|
||||||
SPAN(
|
SPAN(
|
||||||
" This is [",
|
" This is [",
|
||||||
WEB_LINK(
|
WEB_LINK("another tricky web link", "https://codigoparallevar.com/5"),
|
||||||
"another tricky web link",
|
|
||||||
"https://codigoparallevar.com/5",
|
|
||||||
),
|
|
||||||
"] followed up with some text.\n",
|
"] followed up with some text.\n",
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@ -321,7 +306,7 @@ class TestSerde(unittest.TestCase):
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
ex.assert_matches(self, doc)
|
ex.assert_matches(self, doc)
|
||||||
@ -486,9 +471,7 @@ class TestSerde(unittest.TestCase):
|
|||||||
+ 'echo "with two lines"\n'
|
+ 'echo "with two lines"\n'
|
||||||
+ "exit 0 # Exit successfully",
|
+ "exit 0 # Exit successfully",
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(snippets[0].arguments.split(), ['shell', ':results', 'verbatim'])
|
||||||
snippets[0].arguments.split(), ["shell", ":results", "verbatim"]
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
snippets[0].result,
|
snippets[0].result,
|
||||||
"This is a test\n" + "with two lines",
|
"This is a test\n" + "with two lines",
|
||||||
@ -506,10 +489,10 @@ class TestSerde(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
snippets[2].content,
|
snippets[2].content,
|
||||||
"/* This code has to be escaped to\n"
|
'/* This code has to be escaped to\n'
|
||||||
+ " * avoid confusion with new headlines.\n"
|
+ ' * avoid confusion with new headlines.\n'
|
||||||
+ " */\n"
|
+ ' */\n'
|
||||||
+ "main(){}",
|
+ 'main(){}',
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_mimic_write_file_05(self):
|
def test_mimic_write_file_05(self):
|
||||||
@ -547,7 +530,7 @@ class TestSerde(unittest.TestCase):
|
|||||||
hl_schedule_range = hl.children[1]
|
hl_schedule_range = hl.children[1]
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
hl_schedule_range.scheduled.time,
|
hl_schedule_range.scheduled.time,
|
||||||
Timestamp(True, 2020, 12, 15, "Mar", 0, 5, "++1w"),
|
Timestamp(True, 2020, 12, 15, "Mar", 0, 5, '++1w')
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
hl_schedule_range.scheduled.end_time,
|
hl_schedule_range.scheduled.end_time,
|
||||||
@ -555,7 +538,7 @@ class TestSerde(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
hl_schedule_range.scheduled.repetition,
|
hl_schedule_range.scheduled.repetition,
|
||||||
"++1w",
|
'++1w',
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_update_info_file_05(self):
|
def test_update_info_file_05(self):
|
||||||
@ -608,8 +591,7 @@ class TestSerde(unittest.TestCase):
|
|||||||
MarkerToken(closing=False, tok_type=MarkerType.UNDERLINED_MODE),
|
MarkerToken(closing=False, tok_type=MarkerType.UNDERLINED_MODE),
|
||||||
"markup",
|
"markup",
|
||||||
MarkerToken(closing=True, tok_type=MarkerType.UNDERLINED_MODE),
|
MarkerToken(closing=True, tok_type=MarkerType.UNDERLINED_MODE),
|
||||||
".",
|
".", "\n"
|
||||||
"\n",
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -643,24 +625,12 @@ class TestSerde(unittest.TestCase):
|
|||||||
print(lists4)
|
print(lists4)
|
||||||
self.assertEqual(len(lists4), 2)
|
self.assertEqual(len(lists4), 2)
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(lists4[0][0].content, ["This is a list item...", "\n that spans multiple lines", "\n"])
|
||||||
lists4[0][0].content,
|
|
||||||
["This is a list item...", "\n that spans multiple lines", "\n"],
|
|
||||||
)
|
|
||||||
self.assertEqual(lists4[0][0].bullet, "-")
|
self.assertEqual(lists4[0][0].bullet, "-")
|
||||||
self.assertEqual(
|
self.assertEqual(lists4[0][1].content, ["This is another list item...", "\n that has content on multiple lines", "\n"])
|
||||||
lists4[0][1].content,
|
|
||||||
[
|
|
||||||
"This is another list item...",
|
|
||||||
"\n that has content on multiple lines",
|
|
||||||
"\n",
|
|
||||||
],
|
|
||||||
)
|
|
||||||
self.assertEqual(lists4[0][1].bullet, "-")
|
self.assertEqual(lists4[0][1].bullet, "-")
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(lists4[1][0].content, ["This is another", "\n multiline list", "\n"])
|
||||||
lists4[1][0].content, ["This is another", "\n multiline list", "\n"]
|
|
||||||
)
|
|
||||||
self.assertEqual(lists4[1][0].bullet, "-")
|
self.assertEqual(lists4[1][0].bullet, "-")
|
||||||
|
|
||||||
def test_org_roam_07(self):
|
def test_org_roam_07(self):
|
||||||
@ -704,22 +674,20 @@ class TestSerde(unittest.TestCase):
|
|||||||
""".strip(),
|
""".strip(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_markup_file_09(self):
|
def test_markup_file_09(self):
|
||||||
with open(os.path.join(DIR, "09-markup-on-headline.org")) as f:
|
with open(os.path.join(DIR, "09-markup-on-headline.org")) as f:
|
||||||
doc = load(f)
|
doc = load(f)
|
||||||
|
|
||||||
hl = doc.getTopHeadlines()[0]
|
hl = doc.getTopHeadlines()[0]
|
||||||
print(hl.title)
|
print(hl.title)
|
||||||
self.assertEqual(
|
self.assertEqual(hl.title.contents, [
|
||||||
hl.title.contents,
|
'Headline ',
|
||||||
[
|
|
||||||
"Headline ",
|
|
||||||
MarkerToken(closing=False, tok_type=MarkerType.UNDERLINED_MODE),
|
MarkerToken(closing=False, tok_type=MarkerType.UNDERLINED_MODE),
|
||||||
"with",
|
'with',
|
||||||
MarkerToken(closing=True, tok_type=MarkerType.UNDERLINED_MODE),
|
MarkerToken(closing=True, tok_type=MarkerType.UNDERLINED_MODE),
|
||||||
" markup",
|
' markup',
|
||||||
],
|
])
|
||||||
)
|
|
||||||
|
|
||||||
def test_mimic_write_file_10(self):
|
def test_mimic_write_file_10(self):
|
||||||
with open(os.path.join(DIR, "10-tables.org")) as f:
|
with open(os.path.join(DIR, "10-tables.org")) as f:
|
||||||
@ -740,9 +708,9 @@ class TestSerde(unittest.TestCase):
|
|||||||
|
|
||||||
print(first_table[0])
|
print(first_table[0])
|
||||||
self.assertEqual(len(first_table[0].cells), 3)
|
self.assertEqual(len(first_table[0].cells), 3)
|
||||||
self.assertEqual(first_table[0].cells[0].strip(), "Header1")
|
self.assertEqual(first_table[0].cells[0].strip(), 'Header1')
|
||||||
self.assertEqual(first_table[0].cells[1].strip(), "Header2")
|
self.assertEqual(first_table[0].cells[1].strip(), 'Header2')
|
||||||
self.assertEqual(first_table[0].cells[2].strip(), "Header3")
|
self.assertEqual(first_table[0].cells[2].strip(), 'Header3')
|
||||||
|
|
||||||
hl = hl.children[0]
|
hl = hl.children[0]
|
||||||
|
|
||||||
@ -752,9 +720,9 @@ class TestSerde(unittest.TestCase):
|
|||||||
|
|
||||||
print(first_table[0])
|
print(first_table[0])
|
||||||
self.assertEqual(len(first_table[0].cells), 3)
|
self.assertEqual(len(first_table[0].cells), 3)
|
||||||
self.assertEqual(first_table[0].cells[0].strip(), "Header1")
|
self.assertEqual(first_table[0].cells[0].strip(), 'Header1')
|
||||||
self.assertEqual(first_table[0].cells[1].strip(), "Header2")
|
self.assertEqual(first_table[0].cells[1].strip(), 'Header2')
|
||||||
self.assertEqual(first_table[0].cells[2].strip(), "Header3")
|
self.assertEqual(first_table[0].cells[2].strip(), 'Header3')
|
||||||
|
|
||||||
def test_tables_html_file_10(self):
|
def test_tables_html_file_10(self):
|
||||||
with open(os.path.join(DIR, "10-tables.org")) as f:
|
with open(os.path.join(DIR, "10-tables.org")) as f:
|
||||||
@ -764,26 +732,27 @@ class TestSerde(unittest.TestCase):
|
|||||||
|
|
||||||
tree = hl.as_dom()
|
tree = hl.as_dom()
|
||||||
non_props = [
|
non_props = [
|
||||||
item for item in tree if not isinstance(item, dom.PropertyDrawerNode)
|
item
|
||||||
|
for item in tree
|
||||||
|
if not isinstance(item, dom.PropertyDrawerNode)
|
||||||
]
|
]
|
||||||
self.assertTrue(
|
self.assertTrue(isinstance(non_props[0], dom.Text)
|
||||||
isinstance(non_props[0], dom.Text)
|
|
||||||
and isinstance(non_props[1], dom.TableNode)
|
and isinstance(non_props[1], dom.TableNode)
|
||||||
and isinstance(non_props[2], dom.Text),
|
and isinstance(non_props[2], dom.Text),
|
||||||
"Expected <Text><Table><Text>",
|
'Expected <Text><Table><Text>')
|
||||||
)
|
|
||||||
|
|
||||||
hl = hl.children[0]
|
hl = hl.children[0]
|
||||||
tree = hl.as_dom()
|
tree = hl.as_dom()
|
||||||
non_props = [
|
non_props = [
|
||||||
item
|
item
|
||||||
for item in tree
|
for item in tree
|
||||||
if not (
|
if not (isinstance(item, dom.PropertyDrawerNode)
|
||||||
isinstance(item, dom.PropertyDrawerNode) or isinstance(item, dom.Text)
|
or isinstance(item, dom.Text))
|
||||||
)
|
|
||||||
]
|
]
|
||||||
print_tree(non_props)
|
print_tree(non_props)
|
||||||
self.assertTrue(len(non_props) == 1, "Expected <List>, with only (1) element")
|
self.assertTrue(len(non_props) == 1,
|
||||||
|
'Expected <List>, with only (1) element')
|
||||||
|
|
||||||
def test_nested_lists_html_file_11(self):
|
def test_nested_lists_html_file_11(self):
|
||||||
with open(os.path.join(DIR, "11-nested-lists.org")) as f:
|
with open(os.path.join(DIR, "11-nested-lists.org")) as f:
|
||||||
@ -793,38 +762,30 @@ class TestSerde(unittest.TestCase):
|
|||||||
|
|
||||||
tree = hl.as_dom()
|
tree = hl.as_dom()
|
||||||
non_props = [
|
non_props = [
|
||||||
item for item in tree if not isinstance(item, dom.PropertyDrawerNode)
|
item
|
||||||
|
for item in tree
|
||||||
|
if not isinstance(item, dom.PropertyDrawerNode)
|
||||||
]
|
]
|
||||||
print_tree(non_props)
|
print_tree(non_props)
|
||||||
self.assertTrue(
|
self.assertTrue((len(non_props) == 1) and (isinstance(non_props[0], dom.ListGroupNode)),
|
||||||
(len(non_props) == 1) and (isinstance(non_props[0], dom.ListGroupNode)),
|
'Expected only <List> as top level')
|
||||||
"Expected only <List> as top level",
|
|
||||||
)
|
|
||||||
|
|
||||||
dom_list = non_props[0]
|
dom_list = non_props[0]
|
||||||
children = dom_list.children
|
children = dom_list.children
|
||||||
self.assertTrue(
|
self.assertTrue(len(children) == 5, 'Expected 5 items inside <List>, 3 texts and 2 sublists')
|
||||||
len(children) == 5, "Expected 5 items inside <List>, 3 texts and 2 sublists"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Assert texts
|
# Assert texts
|
||||||
self.assertEqual(children[0].content, ["1"])
|
self.assertEqual(children[0].content, ['1'])
|
||||||
self.assertEqual(children[2].content, ["2"])
|
self.assertEqual(children[2].content, ['2'])
|
||||||
self.assertEqual(
|
self.assertEqual(children[4].content[0], '3') # Might be ['3', '\n'] but shouldn't be a breaking change
|
||||||
children[4].content[0], "3"
|
|
||||||
) # Might be ['3', '\n'] but shouldn't be a breaking change
|
|
||||||
|
|
||||||
# Assert lists
|
# Assert lists
|
||||||
self.assertTrue(
|
self.assertTrue(isinstance(children[1], dom.ListGroupNode), 'Expected sublist inside "1"')
|
||||||
isinstance(children[1], dom.ListGroupNode), 'Expected sublist inside "1"'
|
self.assertEqual(children[1].children[0].content, ['1.1'])
|
||||||
)
|
self.assertEqual(children[1].children[1].content, ['1.2'])
|
||||||
self.assertEqual(children[1].children[0].content, ["1.1"])
|
self.assertTrue(isinstance(children[3], dom.ListGroupNode), 'Expected sublist inside "2"')
|
||||||
self.assertEqual(children[1].children[1].content, ["1.2"])
|
self.assertEqual(children[3].children[0].content, ['2.1'])
|
||||||
self.assertTrue(
|
self.assertEqual(children[3].children[1].content, ['2.2'])
|
||||||
isinstance(children[3], dom.ListGroupNode), 'Expected sublist inside "2"'
|
|
||||||
)
|
|
||||||
self.assertEqual(children[3].children[0].content, ["2.1"])
|
|
||||||
self.assertEqual(children[3].children[1].content, ["2.2"])
|
|
||||||
|
|
||||||
def test_mimic_write_file_12(self):
|
def test_mimic_write_file_12(self):
|
||||||
with open(os.path.join(DIR, "12-headlines-with-skip-levels.org")) as f:
|
with open(os.path.join(DIR, "12-headlines-with-skip-levels.org")) as f:
|
||||||
@ -883,10 +844,6 @@ def print_element(element, indentation, headline):
|
|||||||
if isinstance(element, org_rw.Link):
|
if isinstance(element, org_rw.Link):
|
||||||
print(" " * indentation * 2, "Link:", element.get_raw())
|
print(" " * indentation * 2, "Link:", element.get_raw())
|
||||||
elif isinstance(element, str):
|
elif isinstance(element, str):
|
||||||
print(
|
print(" " * indentation * 2, "Str[" + element.replace('\n', '<NL>') + "]", type(element))
|
||||||
" " * indentation * 2,
|
|
||||||
"Str[" + element.replace("\n", "<NL>") + "]",
|
|
||||||
type(element),
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
print_tree(element, indentation, headline)
|
print_tree(element, indentation, headline)
|
||||||
|
@ -1,84 +0,0 @@
|
|||||||
"""Test the Timestamp object."""
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
from datetime import date, datetime
|
|
||||||
from org_rw import Timestamp
|
|
||||||
|
|
||||||
|
|
||||||
def test_init_with_datetime() -> None:
|
|
||||||
datetime_obj: datetime = datetime(2024, 7, 20, 15, 45)
|
|
||||||
|
|
||||||
ts: Timestamp = Timestamp(active=True, datetime_=datetime_obj)
|
|
||||||
|
|
||||||
assert ts.active is True
|
|
||||||
assert ts._year == 2024
|
|
||||||
assert ts._month == 7
|
|
||||||
assert ts._day == 20
|
|
||||||
assert ts.hour == 15
|
|
||||||
assert ts.minute == 45
|
|
||||||
assert ts.dow is None
|
|
||||||
assert ts.repetition is None
|
|
||||||
|
|
||||||
|
|
||||||
def test_init_with_date() -> None:
|
|
||||||
date_obj: date = date(2024, 7, 20)
|
|
||||||
|
|
||||||
ts: Timestamp = Timestamp(active=True, datetime_=date_obj)
|
|
||||||
|
|
||||||
assert ts.active is True
|
|
||||||
assert ts._year == 2024
|
|
||||||
assert ts._month == 7
|
|
||||||
assert ts._day == 20
|
|
||||||
assert ts.hour is None
|
|
||||||
assert ts.minute is None
|
|
||||||
assert ts.dow is None
|
|
||||||
assert ts.repetition is None
|
|
||||||
|
|
||||||
|
|
||||||
def test_init_with_year_month_day() -> None:
|
|
||||||
ts: Timestamp = Timestamp(
|
|
||||||
active=True,
|
|
||||||
year=2024,
|
|
||||||
month=7,
|
|
||||||
day=20,
|
|
||||||
hour=15,
|
|
||||||
minute=45,
|
|
||||||
dow="Saturday",
|
|
||||||
repetition=".+1d",
|
|
||||||
)
|
|
||||||
|
|
||||||
assert ts.active is True
|
|
||||||
assert ts._year == 2024
|
|
||||||
assert ts._month == 7
|
|
||||||
assert ts._day == 20
|
|
||||||
assert ts.hour == 15
|
|
||||||
assert ts.minute == 45
|
|
||||||
assert ts.dow == "Saturday"
|
|
||||||
assert ts.repetition == ".+1d"
|
|
||||||
|
|
||||||
|
|
||||||
def test_init_without_required_arguments() -> None:
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
Timestamp(active=True)
|
|
||||||
|
|
||||||
|
|
||||||
def test_init_with_partial_date_info() -> None:
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
Timestamp(active=True, year=2024, month=7)
|
|
||||||
|
|
||||||
|
|
||||||
def test_init_with_datetime_overrides_date_info() -> None:
|
|
||||||
datetime_obj: datetime = datetime(2024, 7, 20, 15, 45)
|
|
||||||
|
|
||||||
ts: Timestamp = Timestamp(
|
|
||||||
active=True, year=2020, month=1, day=1, datetime_=datetime_obj
|
|
||||||
)
|
|
||||||
|
|
||||||
assert ts.active is True
|
|
||||||
assert ts._year == 2024
|
|
||||||
assert ts._month == 7
|
|
||||||
assert ts._day == 20
|
|
||||||
assert ts.hour == 15
|
|
||||||
assert ts.minute == 45
|
|
||||||
assert ts.dow is None
|
|
||||||
assert ts.repetition is None
|
|
Loading…
Reference in New Issue
Block a user