-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodels.py
More file actions
86 lines (63 loc) · 2.13 KB
/
models.py
File metadata and controls
86 lines (63 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import TypedDict
from tree_sitter import Node as TreeSitterNode
class MarkedContentType(str, Enum):
need = "need"
need_id_refs = "need-id-refs"
rst = "rst"
class SourceComment:
def __init__(self, node: TreeSitterNode) -> None:
self.node: TreeSitterNode = node
self.source_file: SourceFile | None = None
class SourceFile:
def __init__(self, filepath: Path) -> None:
self.filepath: Path = filepath
self.src_comments: list[SourceComment] = []
def add_comment(self, comment: SourceComment) -> None:
self.src_comments.append(comment)
comment.source_file = self
def add_comments(self, comments: list[SourceComment]) -> None:
for comment in comments:
self.add_comment(comment)
class Position(TypedDict):
row: int
column: int
class SourceMap(TypedDict):
start: Position
end: Position
@dataclass
class Metadata:
filepath: Path
remote_url: str | None
source_map: SourceMap
source_comment: SourceComment
tagged_scope: TreeSitterNode | None
type: MarkedContentType
def to_dict(self) -> dict[str, str | int | list[str]]:
obj = self.__dict__.copy()
obj["filepath"] = str(self.filepath)
obj["tagged_scope"] = (
str(self.tagged_scope.text.decode("utf-8"))
if self.tagged_scope and self.tagged_scope.text
else None
)
obj["type"] = self.type.value
del obj["source_comment"]
return obj
def add_src_comment(self, src_comment: SourceComment) -> None:
self.source_comment = src_comment
@dataclass
class NeedIdRefs(Metadata):
need_ids: list[str]
marker: str
type: MarkedContentType = field(init=False, default=MarkedContentType.need_id_refs)
@dataclass
class OneLineNeed(Metadata):
need: dict[str, str | list[str]]
type: MarkedContentType = field(init=False, default=MarkedContentType.need)
@dataclass
class MarkedRst(Metadata):
rst: str
type: MarkedContentType = field(init=False, default=MarkedContentType.rst)