Repository navigation
Expand file tree
/
Copy pathmodel.py
More file actions
88 lines (63 loc) · 2.8 KB
/
Copy pathmodel.py
File metadata and controls
88 lines (63 loc) · 2.8 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
87
"""Immutable source files and locations shared by engine layers."""
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
@dataclass(frozen=True, order=True)
class SourceId:
"""Stable identity for a source unit within an analysis invocation."""
value: str
def __post_init__(self) -> None:
if not self.value:
raise ValueError("source ID cannot be empty")
def __str__(self) -> str:
return self.value
@dataclass(frozen=True)
class SourceSpan:
"""One-based, half-open source range."""
source_id: SourceId
start_line: int
start_column: int
end_line: int | None = None
end_column: int | None = None
def __post_init__(self) -> None:
if self.start_line < 1 or self.start_column < 1:
raise ValueError("source span start must be one-based")
if (self.end_line is None) != (self.end_column is None):
raise ValueError("source span end line and column must be provided together")
if self.end_line is not None and self.end_column is not None:
if self.end_line < self.start_line:
raise ValueError("source span cannot end before it starts")
if self.end_line == self.start_line and self.end_column < self.start_column:
raise ValueError("source span cannot end before it starts")
def display(self) -> str:
return f"{self.source_id}:{self.start_line}:{self.start_column}"
@dataclass(frozen=True)
class FileLocation:
"""A place in a file whose line could not be established: the file, and the JSON
pointer of the value in it (``/a/api_token``). It is never given a guessed line,
which a report or an inline suppression would then take for a verified one."""
source_id: SourceId
pointer: str
# A digest of the value (``sha256:…``), never the value: what the baseline compares
# where it would compare the text of a line. ``positions.file_location`` builds both.
digest: str
def display(self) -> str:
return f"{self.source_id}{self.pointer and '#' + self.pointer}"
# Where a finding is: a verified range of lines, or a file location.
Location = SourceSpan | FileLocation
@dataclass(frozen=True)
class ImportName:
"""A name a module can be imported by, with the directory an import of that name
starts from: the entry of the import path under which the module has that name."""
name: str
root: Path
@dataclass(frozen=True)
class SourceFile:
"""Decoded source text, its identity and its dotted module name; for a file on disk,
the names it can be imported by (``import_names``), its module name first."""
source_id: SourceId
text: str
module_name: str
path: Path | None = None
is_package: bool = False
import_names: tuple[ImportName, ...] = ()