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,
73 payload = json.dumps(self.
artifactPayloadartifactPayload(),sort_keys=
True,separators=(
',',
':')).encode(
'utf-8')
74 return hashlib.sha256(payload).hexdigest()
82 rel_slug = re.sub(
r'[^0-9A-Za-z]+',
'_', str(self.rel_path)).strip(
'_').lower()
83 rel_slug = re.sub(
r'_+',
'_', rel_slug)[:MAX_REL].strip(
'_')
or 'source'
85 call_name = str(self.normalized_call).split(
'(', 1)[0]
86 call_slug = re.sub(
r'[^0-9A-Za-z]+',
'_', call_name).strip(
'_').lower()
87 call_slug = re.sub(
r'_+',
'_', call_slug)[:MAX_CALL].strip(
'_')
or 'call'
88 return f
'{rel_slug}__L{self.lineno}C{self.col_offset}__{call_slug}__{self.artifactHash()}'
98 @dataclass(frozen=True)
102 identity: CallsiteIdentity
121 'id': self.artifact_id,
122 'artifact_hash': self.identity.artifactHash(),
123 'rel_path': self.identity.rel_path,
124 'lineno': self.identity.lineno,
125 'col_offset': self.identity.col_offset,
126 'end_lineno': self.identity.end_lineno,
127 'end_col_offset': self.identity.end_col_offset,
128 'call_text': self.identity.call_text,
129 'normalized_call': self.identity.normalized_call,
130 'format_api': self.format_api,
131 'parameters': self.parameters,
141 return call_text.replace(
' ',
'').replace(
'"',
'').replace(
"'",
'')
151 normalized_file = os.path.abspath(file_path).replace(
'\\',
'/')
153 normalized_root = os.path.abspath(proj_path).replace(
'\\',
'/').rstrip(
'/')
155 rel_path = os.path.relpath(normalized_file, normalized_root).replace(
'\\',
'/')
156 if not rel_path.startswith(
'..'):
160 return normalized_file
188 record =
CallsiteRecord(identity=identity,artifact_id=identity.artifactId(),format_api=format_api,parameters=parameters)
189 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 构造调用点记录