dbrownell-parserlib 0.2.0__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {dbrownell_parserlib-0.2.0 → dbrownell_parserlib-0.4.0}/PKG-INFO +1 -1
- {dbrownell_parserlib-0.2.0 → dbrownell_parserlib-0.4.0}/pyproject.toml +1 -1
- {dbrownell_parserlib-0.2.0 → dbrownell_parserlib-0.4.0}/src/dbrownell_ParserLib/__init__.py +5 -1
- dbrownell_parserlib-0.4.0/src/dbrownell_ParserLib/errors.py +112 -0
- dbrownell_parserlib-0.4.0/src/dbrownell_ParserLib/region.py +112 -0
- {dbrownell_parserlib-0.2.0 → dbrownell_parserlib-0.4.0}/README.md +0 -0
- {dbrownell_parserlib-0.2.0 → dbrownell_parserlib-0.4.0}/src/dbrownell_ParserLib/location.py +0 -0
- {dbrownell_parserlib-0.2.0 → dbrownell_parserlib-0.4.0}/src/dbrownell_ParserLib/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "dbrownell-parserlib"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.4.0"
|
|
4
4
|
# ^^^^^
|
|
5
5
|
# Wheel names will be generated according to this value. Do not manually modify this value; instead
|
|
6
6
|
# update it according to committed changes by running this command from the root of the repository:
|
|
@@ -2,12 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
from importlib.metadata import version
|
|
4
4
|
|
|
5
|
+
from dbrownell_ParserLib.errors import Error, PythonError
|
|
5
6
|
from dbrownell_ParserLib.location import Location
|
|
6
|
-
|
|
7
|
+
from dbrownell_ParserLib.region import Region
|
|
7
8
|
|
|
8
9
|
# ----------------------------------------------------------------------
|
|
9
10
|
__all__ = [
|
|
11
|
+
"Error",
|
|
10
12
|
"Location",
|
|
13
|
+
"PythonError",
|
|
14
|
+
"Region",
|
|
11
15
|
"__version__",
|
|
12
16
|
]
|
|
13
17
|
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""Contains functionality associated with errors and exceptions."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
import textwrap
|
|
5
|
+
import traceback
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass, field, InitVar
|
|
8
|
+
from functools import cached_property
|
|
9
|
+
from io import StringIO
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Self
|
|
12
|
+
|
|
13
|
+
from dbrownell_ParserLib.region import Location, Region
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# ----------------------------------------------------------------------
|
|
17
|
+
@dataclass(frozen=True)
|
|
18
|
+
class Error:
|
|
19
|
+
"""Base class for all errors generated by this library."""
|
|
20
|
+
|
|
21
|
+
# ----------------------------------------------------------------------
|
|
22
|
+
message: str
|
|
23
|
+
|
|
24
|
+
region_or_regions: InitVar[Region | list[Region]]
|
|
25
|
+
regions: list[Region] = field(init=False)
|
|
26
|
+
|
|
27
|
+
# ----------------------------------------------------------------------
|
|
28
|
+
def __post_init__(self, region_or_regions: Region | list[Region]) -> None:
|
|
29
|
+
regions = [region_or_regions] if isinstance(region_or_regions, Region) else region_or_regions
|
|
30
|
+
|
|
31
|
+
if not regions:
|
|
32
|
+
msg = "At least one region must be provided"
|
|
33
|
+
raise ValueError(msg)
|
|
34
|
+
|
|
35
|
+
object.__setattr__(self, "regions", regions)
|
|
36
|
+
|
|
37
|
+
# ----------------------------------------------------------------------
|
|
38
|
+
def __str__(self) -> str:
|
|
39
|
+
return self._string
|
|
40
|
+
|
|
41
|
+
# ----------------------------------------------------------------------
|
|
42
|
+
# ----------------------------------------------------------------------
|
|
43
|
+
# ----------------------------------------------------------------------
|
|
44
|
+
@cached_property
|
|
45
|
+
def _string(self) -> str:
|
|
46
|
+
if len(self.regions) == 1 and "\n" not in self.message:
|
|
47
|
+
return f"{self.message} ({self.regions[0]})"
|
|
48
|
+
|
|
49
|
+
return textwrap.dedent(
|
|
50
|
+
"""\
|
|
51
|
+
{}
|
|
52
|
+
|
|
53
|
+
{}
|
|
54
|
+
""",
|
|
55
|
+
).format(self.message.rstrip(), "\n".join(f" - {region}" for region in self.regions))
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# ----------------------------------------------------------------------
|
|
59
|
+
@dataclass(frozen=True)
|
|
60
|
+
class PythonError(Error):
|
|
61
|
+
"""An `Error` based on a Python exception."""
|
|
62
|
+
|
|
63
|
+
# ----------------------------------------------------------------------
|
|
64
|
+
ex: Exception
|
|
65
|
+
|
|
66
|
+
# ----------------------------------------------------------------------
|
|
67
|
+
@classmethod
|
|
68
|
+
def Create(
|
|
69
|
+
cls,
|
|
70
|
+
ex: Exception,
|
|
71
|
+
) -> Self:
|
|
72
|
+
"""Create a `PythonError` from a Python exception."""
|
|
73
|
+
|
|
74
|
+
regions: list[Region] = []
|
|
75
|
+
|
|
76
|
+
# I haven't been able to find a reliable way to get the exception's callstack so it is being
|
|
77
|
+
# parsed manually here. There has got to be a better way to do this.
|
|
78
|
+
sink = StringIO()
|
|
79
|
+
|
|
80
|
+
traceback.print_exception(ex, file=sink)
|
|
81
|
+
sink_str = sink.getvalue()
|
|
82
|
+
|
|
83
|
+
regex = re.compile(r"^\s*File \"(?P<filename>.+?)\", line (?P<line>\d+),.+$", re.MULTILINE)
|
|
84
|
+
|
|
85
|
+
for line in sink_str.splitlines():
|
|
86
|
+
match = regex.match(line)
|
|
87
|
+
if not match:
|
|
88
|
+
continue
|
|
89
|
+
|
|
90
|
+
line_number = int(match.group("line"))
|
|
91
|
+
|
|
92
|
+
regions.append(
|
|
93
|
+
Region(
|
|
94
|
+
Path(match.group("filename")),
|
|
95
|
+
Location(line_number, 1),
|
|
96
|
+
Location(line_number, 1),
|
|
97
|
+
),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
regions.reverse()
|
|
101
|
+
|
|
102
|
+
return cls(
|
|
103
|
+
textwrap.dedent(
|
|
104
|
+
"""\
|
|
105
|
+
Python Exception
|
|
106
|
+
----------------
|
|
107
|
+
{}
|
|
108
|
+
""",
|
|
109
|
+
).format(str(ex)),
|
|
110
|
+
regions,
|
|
111
|
+
ex,
|
|
112
|
+
)
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# noqa: D100
|
|
2
|
+
import inspect
|
|
3
|
+
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from functools import cached_property
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Self
|
|
8
|
+
|
|
9
|
+
from dbrownell_ParserLib.location import Location
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# ----------------------------------------------------------------------
|
|
13
|
+
@dataclass(frozen=True)
|
|
14
|
+
class Region:
|
|
15
|
+
"""A region within a source file."""
|
|
16
|
+
|
|
17
|
+
# ----------------------------------------------------------------------
|
|
18
|
+
filename: Path
|
|
19
|
+
begin: Location
|
|
20
|
+
end: Location
|
|
21
|
+
|
|
22
|
+
# ----------------------------------------------------------------------
|
|
23
|
+
@classmethod
|
|
24
|
+
def CreateFromCode(
|
|
25
|
+
cls,
|
|
26
|
+
*,
|
|
27
|
+
callstack_offset: int = 0,
|
|
28
|
+
) -> Self:
|
|
29
|
+
"""Create a `Region` from the current code location."""
|
|
30
|
+
|
|
31
|
+
frame = inspect.stack()[callstack_offset + 1][0]
|
|
32
|
+
location = Location(frame.f_lineno, frame.f_lineno)
|
|
33
|
+
|
|
34
|
+
return cls(Path(frame.f_code.co_filename), location, location)
|
|
35
|
+
|
|
36
|
+
# ----------------------------------------------------------------------
|
|
37
|
+
def __post_init__(self) -> None:
|
|
38
|
+
if self.end < self.begin:
|
|
39
|
+
msg = "Invalid region"
|
|
40
|
+
raise ValueError(msg)
|
|
41
|
+
|
|
42
|
+
# ----------------------------------------------------------------------
|
|
43
|
+
def __str__(self) -> str:
|
|
44
|
+
return self._string
|
|
45
|
+
|
|
46
|
+
# ----------------------------------------------------------------------
|
|
47
|
+
@staticmethod
|
|
48
|
+
def Compare(
|
|
49
|
+
this: Region,
|
|
50
|
+
that: Region,
|
|
51
|
+
) -> int:
|
|
52
|
+
"""Compare two `Region` instances."""
|
|
53
|
+
|
|
54
|
+
if this.filename != that.filename:
|
|
55
|
+
return -1 if this.filename < that.filename else 1
|
|
56
|
+
|
|
57
|
+
result = Location.Compare(this.begin, that.begin)
|
|
58
|
+
if result != 0:
|
|
59
|
+
return result
|
|
60
|
+
|
|
61
|
+
result = Location.Compare(this.end, that.end)
|
|
62
|
+
if result != 0:
|
|
63
|
+
return result
|
|
64
|
+
|
|
65
|
+
return 0
|
|
66
|
+
|
|
67
|
+
# ----------------------------------------------------------------------
|
|
68
|
+
def __hash__(self) -> int:
|
|
69
|
+
return hash((self.filename, self.begin, self.end))
|
|
70
|
+
|
|
71
|
+
def __eq__(self, other: object) -> bool:
|
|
72
|
+
return isinstance(other, Region) and self.__class__.Compare(self, other) == 0
|
|
73
|
+
|
|
74
|
+
def __ne__(self, other: object) -> bool:
|
|
75
|
+
return not isinstance(other, Region) or self.__class__.Compare(self, other) != 0
|
|
76
|
+
|
|
77
|
+
def __lt__(self, other: object) -> bool:
|
|
78
|
+
return isinstance(other, Region) and self.__class__.Compare(self, other) < 0
|
|
79
|
+
|
|
80
|
+
def __le__(self, other: object) -> bool:
|
|
81
|
+
return isinstance(other, Region) and self.__class__.Compare(self, other) <= 0
|
|
82
|
+
|
|
83
|
+
def __gt__(self, other: object) -> bool:
|
|
84
|
+
return isinstance(other, Region) and self.__class__.Compare(self, other) > 0
|
|
85
|
+
|
|
86
|
+
def __ge__(self, other: object) -> bool:
|
|
87
|
+
return isinstance(other, Region) and self.__class__.Compare(self, other) >= 0
|
|
88
|
+
|
|
89
|
+
# ----------------------------------------------------------------------
|
|
90
|
+
def __contains__(
|
|
91
|
+
self,
|
|
92
|
+
location_or_region: Location | Region,
|
|
93
|
+
) -> bool:
|
|
94
|
+
if isinstance(location_or_region, Location):
|
|
95
|
+
return self.begin <= location_or_region <= self.end
|
|
96
|
+
|
|
97
|
+
region = location_or_region
|
|
98
|
+
|
|
99
|
+
if self.filename != region.filename:
|
|
100
|
+
return False
|
|
101
|
+
|
|
102
|
+
return self.begin <= region.begin and region.end <= self.end
|
|
103
|
+
|
|
104
|
+
# ----------------------------------------------------------------------
|
|
105
|
+
# ----------------------------------------------------------------------
|
|
106
|
+
# ----------------------------------------------------------------------
|
|
107
|
+
@cached_property
|
|
108
|
+
def _string(self) -> str:
|
|
109
|
+
if self.end == self.begin:
|
|
110
|
+
return f"{self.filename.as_posix()}, {self.begin}"
|
|
111
|
+
|
|
112
|
+
return f"{self.filename.as_posix()}, {self.begin} - {self.end}"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|