17 from dataclasses
import dataclass
18 from typing
import Optional
28 @dataclass(frozen=True)
44 end_lineno: Optional[int]
48 end_col_offset: Optional[int]
62 'rel_path': self.rel_path,
63 'lineno': self.lineno,
64 'col_offset': self.col_offset,
65 'end_lineno': self.end_lineno,
66 'end_col_offset': self.end_col_offset,
67 'normalized_call': self.normalized_call,
76 separators=(
',',
':'),
78 return hashlib.sha256(payload).hexdigest()
86 rel_slug = re.sub(
r'[^0-9A-Za-z]+',
'_', str(self.rel_path)).strip(
'_').lower()
87 rel_slug = re.sub(
r'_+',
'_', rel_slug)[:MAX_REL].strip(
'_')
or 'source'
89 call_name = str(self.normalized_call).split(
'(', 1)[0]
90 call_slug = re.sub(
r'[^0-9A-Za-z]+',
'_', call_name).strip(
'_').lower()
91 call_slug = re.sub(
r'_+',
'_', call_slug)[:MAX_CALL].strip(
'_')
or 'call'
92 return f
'{rel_slug}__L{self.lineno}C{self.col_offset}__{call_slug}__{self.artifactHash()}'
102 @dataclass(frozen=True)
106 identity: CallsiteIdentity
125 'id': self.artifact_id,
126 'artifact_hash': self.identity.artifactHash(),
127 'rel_path': self.identity.rel_path,
128 'lineno': self.identity.lineno,
129 'col_offset': self.identity.col_offset,
130 'end_lineno': self.identity.end_lineno,
131 'end_col_offset': self.identity.end_col_offset,
132 'call_text': self.identity.call_text,
133 'normalized_call': self.identity.normalized_call,
134 'format_api': self.format_api,
135 'parameters': self.parameters,
145 return call_text.replace(
' ',
'').replace(
'"',
'').replace(
"'",
'')
155 normalized_file = os.path.abspath(file_path).replace(
'\\',
'/')
157 normalized_root = os.path.abspath(proj_path).replace(
'\\',
'/').rstrip(
'/')
159 rel_path = os.path.relpath(normalized_file, normalized_root).replace(
'\\',
'/')
160 if not rel_path.startswith(
'..'):
164 return normalized_file
194 col_offset=int(col_offset),
195 end_lineno=end_lineno,
196 end_col_offset=end_col_offset,
202 artifact_id=identity.artifactId(),
203 format_api=format_api,
204 parameters=parameters,
206 return record.toDict()
Callsite identity class 调用点身份类
def artifactPayload(self)
The relative path from project root 相对于项目根目录的文件路径
def artifactId(self)
Return stable readable artifact id for a callsite 返回调用点稳定可读运行产物id.
def artifactHash(self)
Return stable artifact hash for a callsite 返回调用点稳定运行产物hash.
Callsite record class 调用点记录类
def toDict(self)
The structured callsite identity 结构化调用点身份
def normalizeRelPath(file_path, proj_path=None)
Normalize source file path to project relative path 将源码路径归一化为项目相对路径
def normalizeCallText(call_text)
Normalize call text for identity generation 归一化调用文本,用于生成调用点身份
def makeCallsiteRecord(file_path, call_text, format_api, parameters, lineno, col_offset, end_lineno=None, end_col_offset=None, proj_path=None)
Make callsite record 构造调用点记录