Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/utils.py
Show First 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | Returns: | ||||
and second member the content mime type | and second member the content mime type | ||||
Raises: | Raises: | ||||
NotFoundExc if the content is not found | NotFoundExc if the content is not found | ||||
""" | """ | ||||
content_data = service.lookup_content(query_string) | content_data = service.lookup_content(query_string) | ||||
filetype = None | filetype = None | ||||
language = None | language = None | ||||
license = None | |||||
# requests to the indexer db may fail so properly handle | # requests to the indexer db may fail so properly handle | ||||
# those cases in order to avoid content display errors | # those cases in order to avoid content display errors | ||||
try: | try: | ||||
filetype = service.lookup_content_filetype(query_string) | filetype = service.lookup_content_filetype(query_string) | ||||
language = service.lookup_content_language(query_string) | language = service.lookup_content_language(query_string) | ||||
license = service.lookup_content_license(query_string) | |||||
except Exception as exc: | except Exception as exc: | ||||
sentry_sdk.capture_exception(exc) | sentry_sdk.capture_exception(exc) | ||||
mimetype = "unknown" | mimetype = "unknown" | ||||
encoding = "unknown" | encoding = "unknown" | ||||
if filetype: | if filetype: | ||||
mimetype = filetype["mimetype"] | mimetype = filetype["mimetype"] | ||||
encoding = filetype["encoding"] | encoding = filetype["encoding"] | ||||
# workaround when encountering corrupted data due to implicit | # workaround when encountering corrupted data due to implicit | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | ): | ||||
content_data["mimetype"] = mimetype | content_data["mimetype"] = mimetype | ||||
content_data["encoding"] = encoding | content_data["encoding"] = encoding | ||||
if language: | if language: | ||||
content_data["language"] = language["lang"] | content_data["language"] = language["lang"] | ||||
else: | else: | ||||
content_data["language"] = "not detected" | content_data["language"] = "not detected" | ||||
if license: | |||||
content_data["licenses"] = ", ".join(license["facts"][0]["licenses"]) | |||||
else: | |||||
content_data["licenses"] = "not detected" | |||||
return content_data | return content_data | ||||
def prepare_content_for_display(content_data, mime_type, path): | def prepare_content_for_display(content_data, mime_type, path): | ||||
"""Function that prepares a content for HTML display. | """Function that prepares a content for HTML display. | ||||
The function tries to associate a programming language to a | The function tries to associate a programming language to a | ||||
▲ Show 20 Lines • Show All 511 Lines • Show Last 20 Lines |