Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/service.py
Show First 20 Lines • Show All 86 Lines • ▼ Show 20 Lines | def lookup_expression(expression, last_sha1, per_page): | ||||
) | ) | ||||
for ctag in ctags: | for ctag in ctags: | ||||
ctag = converters.from_swh(ctag, hashess={"id"}) | ctag = converters.from_swh(ctag, hashess={"id"}) | ||||
ctag["sha1"] = ctag["id"] | ctag["sha1"] = ctag["id"] | ||||
ctag.pop("id") | ctag.pop("id") | ||||
yield ctag | yield ctag | ||||
def lookup_hash(q): | def lookup_hash(q: str) -> Dict[str, Any]: | ||||
"""Checks if the storage contains a given content checksum | """Check if the storage contains a given content checksum and return it if found. | ||||
Args: query string of the form <hash_algo:hash> | Args: | ||||
q: query string of the form <hash_algo:hash> | |||||
Returns: Dict with key found containing the hash info if the | Returns: | ||||
Dict with key found containing the hash info if the | |||||
hash is present, None if not. | hash is present, None if not. | ||||
""" | """ | ||||
algo, hash = query.parse_hash(q) | algo, hash_ = query.parse_hash(q) | ||||
found = _first_element(storage.content_find({algo: hash})) | found = _first_element(storage.content_find({algo: hash_})) | ||||
return {"found": converters.from_content(found), "algo": algo} | if found: | ||||
content = converters.from_content(found.to_dict()) | |||||
else: | |||||
content = None | |||||
return {"found": content, "algo": algo} | |||||
def search_hash(q): | def search_hash(q: str) -> Dict[str, bool]: | ||||
"""Checks if the storage contains a given content checksum | """Search storage for a given content checksum. | ||||
Args: query string of the form <hash_algo:hash> | Args: | ||||
q: query string of the form <hash_algo:hash> | |||||
Returns: Dict with key found to True or False, according to | Returns: | ||||
Dict with key found to True or False, according to | |||||
whether the checksum is present or not | whether the checksum is present or not | ||||
""" | """ | ||||
algo, hash = query.parse_hash(q) | algo, hash_ = query.parse_hash(q) | ||||
found = _first_element(storage.content_find({algo: hash})) | found = _first_element(storage.content_find({algo: hash_})) | ||||
return {"found": found is not None} | return {"found": found is not None} | ||||
def _lookup_content_sha1(q): | def _lookup_content_sha1(q: str) -> Optional[bytes]: | ||||
"""Given a possible input, query for the content's sha1. | """Given a possible input, query for the content's sha1. | ||||
Args: | Args: | ||||
q: query string of the form <hash_algo:hash> | q: query string of the form <hash_algo:hash> | ||||
Returns: | Returns: | ||||
binary sha1 if found or None | binary sha1 if found or None | ||||
""" | """ | ||||
algo, hash = query.parse_hash(q) | algo, hash_ = query.parse_hash(q) | ||||
if algo != "sha1": | if algo != "sha1": | ||||
hashes = _first_element(storage.content_find({algo: hash})) | hashes = _first_element(storage.content_find({algo: hash_})) | ||||
if not hashes: | if not hashes: | ||||
return None | return None | ||||
return hashes["sha1"] | return hashes.sha1 | ||||
return hash | return hash_ | ||||
def lookup_content_ctags(q): | def lookup_content_ctags(q): | ||||
"""Return ctags information from a specified content. | """Return ctags information from a specified content. | ||||
Args: | Args: | ||||
q: query string of the form <hash_algo:hash> | q: query string of the form <hash_algo:hash> | ||||
▲ Show 20 Lines • Show All 640 Lines • ▼ Show 20 Lines | if entity["type"] == "dir": | ||||
"type": "dir", | "type": "dir", | ||||
"path": "." if not dir_path else dir_path, | "path": "." if not dir_path else dir_path, | ||||
"revision": sha1_git, | "revision": sha1_git, | ||||
"content": list(map(converters.from_directory_entry, directory_entries)), | "content": list(map(converters.from_directory_entry, directory_entries)), | ||||
} | } | ||||
elif entity["type"] == "file": # content | elif entity["type"] == "file": # content | ||||
content = _first_element(storage.content_find({"sha1_git": entity["target"]})) | content = _first_element(storage.content_find({"sha1_git": entity["target"]})) | ||||
if not content: | if not content: | ||||
raise NotFoundExc("Content not found for revision %s" % sha1_git) | raise NotFoundExc(f"Content not found for revision {sha1_git}") | ||||
content_d = content.to_dict() | |||||
if with_data: | if with_data: | ||||
c = _first_element(storage.content_get([content["sha1"]])) | c = _first_element(storage.content_get([content.sha1])) | ||||
content["data"] = c["data"] | content_d["data"] = c["data"] | ||||
return { | return { | ||||
"type": "file", | "type": "file", | ||||
"path": "." if not dir_path else dir_path, | "path": "." if not dir_path else dir_path, | ||||
"revision": sha1_git, | "revision": sha1_git, | ||||
"content": converters.from_content(content), | "content": converters.from_content(content_d), | ||||
} | } | ||||
elif entity["type"] == "rev": # revision | elif entity["type"] == "rev": # revision | ||||
revision = next(storage.revision_get([entity["target"]])) | revision = next(storage.revision_get([entity["target"]])) | ||||
return { | return { | ||||
"type": "rev", | "type": "rev", | ||||
"path": "." if not dir_path else dir_path, | "path": "." if not dir_path else dir_path, | ||||
"revision": sha1_git, | "revision": sha1_git, | ||||
"content": converters.from_revision(revision), | "content": converters.from_revision(revision), | ||||
} | } | ||||
else: | else: | ||||
raise NotImplementedError("Entity of type %s not implemented." % entity["type"]) | raise NotImplementedError("Entity of type %s not implemented." % entity["type"]) | ||||
def lookup_content(q): | def lookup_content(q: str) -> Dict[str, Any]: | ||||
"""Lookup the content designed by q. | """Lookup the content designed by q. | ||||
Args: | Args: | ||||
q: The release's sha1 as hexadecimal | q: The release's sha1 as hexadecimal | ||||
Raises: | Raises: | ||||
NotFoundExc if the requested content is not found | NotFoundExc if the requested content is not found | ||||
""" | """ | ||||
algo, hash = query.parse_hash(q) | algo, hash_ = query.parse_hash(q) | ||||
c = _first_element(storage.content_find({algo: hash})) | c = _first_element(storage.content_find({algo: hash_})) | ||||
ardumont: This follows a recent discussion about name clashes with standard keyword message [1]
[1]… | |||||
Not Done Inline ActionsI am good with that convention. There must be plenty of other ones to fix (like dir) in swh-web codebase. anlambert: I am good with that convention. There must be plenty of other ones to fix (like `dir`) in swh… | |||||
Done Inline Actionsok, so i'll change the other impacted functions with this as well then (if you still agree with that thinking ;) ardumont: ok, so i'll change the other impacted functions with this as well then (if you still agree with… | |||||
Not Done Inline Actionshash is a builtin function, not a keyword, though vlorentz: `hash` is a builtin function, not a keyword, though | |||||
Done Inline Actionsdoes that change anything? (it's highlighted as a keyword by my editor with plain hash, now it's better ;) ardumont: does that change anything?
(it's highlighted as a keyword by my editor with plain `hash`, now… | |||||
Not Done Inline Actions
Same behavior with my editor too, that highlighting is confusing. anlambert: > it's highlighted as a keyword by my editor with plain hash, now it's better ;)
Same behavior… | |||||
Not Done Inline ActionsThe difference is that it's only variable shadowing instead of a SyntaxError. vlorentz: The difference is that it's only variable shadowing instead of a SyntaxError. | |||||
if not c: | if not c: | ||||
raise NotFoundExc( | hhex = hashutil.hash_to_hex(hash_) | ||||
"Content with %s checksum equals to %s not found!" | raise NotFoundExc(f"Content with {algo} checksum equals to {hhex} not found!") | ||||
% (algo, hashutil.hash_to_hex(hash)) | return converters.from_content(c.to_dict()) | ||||
) | |||||
return converters.from_content(c) | |||||
def lookup_content_raw(q): | def lookup_content_raw(q): | ||||
"""Lookup the content defined by q. | """Lookup the content defined by q. | ||||
Args: | Args: | ||||
q: query string of the form <hash_algo:hash> | q: query string of the form <hash_algo:hash> | ||||
▲ Show 20 Lines • Show All 487 Lines • Show Last 20 Lines |
This follows a recent discussion about name clashes with standard keyword message [1]
[1] https://forge.softwareheritage.org/D3684#inline-25462
Tell me if you disagree (I see i did not modify everywhere so i can always revert, and it's not per say the subject of the diff ;)