Skip to content

Commit

Permalink
represent all paths internally as PurePath
Browse files Browse the repository at this point in the history
  • Loading branch information
jhidding committed Mar 5, 2025
1 parent 49b1813 commit bdd86a3
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 19 deletions.
8 changes: 4 additions & 4 deletions entangled/code_reader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass, field
from pathlib import Path
from pathlib import Path, PurePath

import mawk
import re
Expand All @@ -18,9 +18,9 @@ class Frame:
class CodeReader(mawk.RuleSet):
"""Reads an annotated code file."""

def __init__(self, path: str, refs: ReferenceMap):
def __init__(self, path: PurePath, refs: ReferenceMap):
self.location = TextLocation(path, 0)
self.stack: list[Frame] = [Frame(ReferenceId("#root#", "", -1), "")]
self.stack: list[Frame] = [Frame(ReferenceId("#root#", PurePath("-"), -1), "")]
self.refs: ReferenceMap = refs

@property
Expand Down Expand Up @@ -56,7 +56,7 @@ def on_block_begin(self, m: re.Match):

self.stack.append(
Frame(
ReferenceId(m["ref_name"], m["source"], ref_count), m["indent"], content
ReferenceId(m["ref_name"], PurePath(m["source"]), ref_count), m["indent"], content
)
)
return []
Expand Down
2 changes: 1 addition & 1 deletion entangled/commands/stitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def stitch(*, force: bool = False, show: bool = False):
logging.debug("reading `%s`", path)
t.update(path)
with open(path, "r") as f:
CodeReader(str(path), refs).run(f.read())
CodeReader(path, refs).run(f.read())

for path in input_file_list:
t.write(path, stitch_markdown(refs, content[path]), [])
Expand Down
5 changes: 3 additions & 2 deletions entangled/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections import defaultdict
from functools import singledispatchmethod
from itertools import chain
from pathlib import PurePath

from .config import Language, AnnotationMethod, config
from .properties import Property, get_attribute
Expand All @@ -17,7 +18,7 @@ def length(iter: Iterable[Any]) -> int:
@dataclass
class ReferenceId:
name: str
file: str
file: PurePath
ref_count: int

def __hash__(self):
Expand Down Expand Up @@ -71,7 +72,7 @@ def by_name(self, n: str) -> Iterable[CodeBlock]:

return (self.map[r] for r in self.index[n])

def new_id(self, filename: str, name: str) -> ReferenceId:
def new_id(self, filename: PurePath, name: str) -> ReferenceId:
c = length(filter(lambda r: r.file == filename, self.index[name]))
return ReferenceId(name, filename, c)

Expand Down
12 changes: 6 additions & 6 deletions entangled/markdown_reader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional
from copy import copy
from pathlib import Path
from pathlib import Path, PurePath

import re
import mawk
Expand All @@ -20,8 +20,8 @@ class MarkdownLexer(mawk.RuleSet):
content."""
def __init__(
self,
filename: str
):
filename: Path
):
self.location = TextLocation(filename)
self.raw_content: list[RawContent] = []
self.inside_codeblock: bool = False
Expand Down Expand Up @@ -122,13 +122,13 @@ def read_markdown_file(
-> tuple[ReferenceMap, list[Content]]:

with open(path, "r") as f:
path_str = str(path.resolve().relative_to(Path.cwd()))
return read_markdown_string(f.read(), path_str, refs, hooks)
rel_path = path.resolve().relative_to(Path.cwd())
return read_markdown_string(f.read(), rel_path, refs, hooks)


def read_markdown_string(
text: str,
path_str: str = "-",
path_str: Path = Path("-"),
refs: ReferenceMap | None = None,
hooks: list[HookBase] | None = None) \
-> tuple[ReferenceMap, list[Content]]:
Expand Down
2 changes: 1 addition & 1 deletion entangled/tangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def on_begin(self):
if self.cb.header is not None:
result.append(self.cb.header)
result.append(
f"{self.cb.language.comment.open} ~/~ begin <<{self.ref.file}#{self.ref.name}>>[{count}]{self.close_comment}"
f"{self.cb.language.comment.open} ~/~ begin <<{self.ref.file.as_posix()}#{self.ref.name}>>[{count}]{self.close_comment}"
)
return result

Expand Down
3 changes: 2 additions & 1 deletion entangled/text_location.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from dataclasses import dataclass
from pathlib import PurePath


@dataclass
class TextLocation:
filename: str
filename: PurePath
line_number: int = 0

def __str__(self):
Expand Down
4 changes: 2 additions & 2 deletions test/test_os_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from entangled.tangle import tangle_ref
from entangled.code_reader import CodeReader

from pathlib import Path
from pathlib import Path, PurePath
import os
from shutil import copytree, move
from contextlib import chdir
Expand All @@ -13,7 +13,7 @@ def test_tangle_ref(data, tmp_path):
with chdir(tmp_path / "os_interop"):
refs, _ = read_markdown_file(Path("doc/index.md"))
tangled, deps = tangle_ref(refs, "src/euler_number.c")
assert deps == {"doc/index.md"}
assert deps == {PurePath("doc/index.md")}
with open("src/euler_number.c", "r") as f:
assert f.read().strip() == tangled.strip()

Expand Down
4 changes: 2 additions & 2 deletions test/test_tangle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from entangled.markdown_reader import read_markdown_file
from entangled.tangle import tangle_ref
from entangled.code_reader import CodeReader
from pathlib import Path
from pathlib import Path, PurePath
import os
from shutil import copytree, move
from contextlib import chdir
Expand All @@ -12,7 +12,7 @@ def test_tangle_ref(data, tmp_path):
with chdir(tmp_path / "hello-world"):
refs, _ = read_markdown_file(Path("hello-world.md"))
tangled, deps = tangle_ref(refs, "hello_world.cc")
assert deps == {"hello-world.md"}
assert deps == {PurePath("hello-world.md")}
with open("hello_world.cc", "r") as f:
assert f.read() == tangled

Expand Down

0 comments on commit bdd86a3

Please sign in to comment.