swh.web.browse.utils module#
- swh.web.browse.utils.get_directory_entries(sha1_git: str) Tuple[List[Dict[str, Any]], List[Dict[str, Any]]] [source]#
Function that retrieves the content of a directory from the archive.
The directories entries are first sorted in lexicographical order. Sub-directories and regular files are then extracted.
- Parameters:
sha1_git – sha1_git identifier of the directory
- Returns:
A tuple whose first member corresponds to the sub-directories list and second member the regular files list
- Raises:
NotFoundExc if the directory is not found –
- swh.web.browse.utils.get_mimetype_and_encoding_for_content(content: bytes) Tuple[str, str] [source]#
Function that returns the mime type and the encoding associated to a content buffer using the magic module under the hood.
- Parameters:
content – a content buffer
- Returns:
A tuple (mimetype, encoding), for instance (‘text/plain’, ‘us-ascii’), associated to the provided content.
- swh.web.browse.utils.re_encode_content(mimetype: str, encoding: str, content_data: bytes) Tuple[str, str, bytes] [source]#
Try to re-encode textual content if it is not encoded to UTF-8 for proper display in the browse Web UI.
- Parameters:
mimetype – content mimetype as detected by python-magic
encoding – content encoding as detected by python-magic
content_data – raw content bytes
- Returns:
content mimetype, content encoding (possibly updated after processing), content raw bytes (possibly reencoded to UTF-8)
- Return type:
A tuple with 3 members
- swh.web.browse.utils.request_content(query_string: str, max_size: int | None = 5242880, re_encode: bool = True) Dict[str, Any] [source]#
Function that retrieves a content from the archive.
Raw bytes content is first retrieved, then the content mime type. If the mime type is not stored in the archive, it will be computed using Python magic module.
- Parameters:
query_string – a string of the form “[ALGO_HASH:]HASH” where optional ALGO_HASH can be either
sha1
,sha1_git
,sha256
, orblake2s256
(default tosha1
) and HASH the hexadecimal representation of the hash valuemax_size – the maximum size for a content to retrieve (default to 1MB, no size limit if None)
- Returns:
A dict filled with content info.
- Raises:
NotFoundExc if the content is not found –
- swh.web.browse.utils.prepare_content_for_display(content_data: bytes, mime_type: str, path: str | None) Dict[str, Any] [source]#
Function that prepares a content for HTML display.
The function tries to associate a programming language to a content in order to perform syntax highlighting client-side using highlightjs. The language is determined using either the content filename or its mime type. If the mime type corresponds to an image format supported by web browsers, the content will be encoded in base64 for displaying the image.
- Parameters:
content_data – raw bytes of the content
mime_type – mime type of the content
path – path of the content including filename
- Returns:
A dict containing the content bytes (possibly different from the one provided as parameter if it is an image) under the key ‘content_data and the corresponding highlightjs language class under the key ‘language’.
- swh.web.browse.utils.gen_link(url: str, link_text: str | None = None, link_attrs: Dict[str, str] | None = None) str [source]#
Utility function for generating an HTML link to insert in Django templates.
- Parameters:
url – an url
link_text – optional text for the produced link, if not provided the url will be used
link_attrs – optional attributes (e.g. class) to add to the link
- Returns:
An HTML link in the form ‘<a href=”url”>link_text</a>’
- swh.web.browse.utils.gen_revision_url(revision_id: str, snapshot_context: SnapshotContext | None = None) str [source]#
Utility function for generating an url to a revision.
- Parameters:
revision_id – a revision id
snapshot_context – if provided, generate snapshot-dependent browsing url
- Returns:
The url to browse the revision
- Return type:
- swh.web.browse.utils.gen_revision_link(revision_id: str, shorten_id: bool = False, snapshot_context: SnapshotContext | None = None, link_text: str | None = 'Browse', link_attrs: Dict[str, str] | None = {'class': 'btn btn-secondary btn-sm', 'role': 'button'}) str | None [source]#
Utility function for generating a link to a revision HTML view to insert in Django templates.
- Parameters:
revision_id – a revision id
shorten_id – whether to shorten the revision id to 7 characters for the link text
snapshot_context – if provided, generate snapshot-dependent browsing link
link_text – optional text for the generated link (the revision id will be used by default)
link_attrs – optional attributes (e.g. class) to add to the link
- Returns:
An HTML link in the form ‘<a href=”revision_url”>revision_id</a>’
- Return type:
- swh.web.browse.utils.gen_directory_link(sha1_git: str, snapshot_context: SnapshotContext | None = None, link_text: str | None = 'Browse', link_attrs: Dict[str, str] | None = {'class': 'btn btn-secondary btn-sm', 'role': 'button'}) str | None [source]#
Utility function for generating a link to a directory HTML view to insert in Django templates.
- Parameters:
sha1_git – directory identifier
link_text – optional text for the generated link (the directory id will be used by default)
link_attrs – optional attributes (e.g. class) to add to the link
- Returns:
An HTML link in the form ‘<a href=”directory_view_url”>link_text</a>’
- swh.web.browse.utils.gen_snapshot_link(snapshot_id: str, snapshot_context: SnapshotContext | None = None, link_text: str | None = 'Browse', link_attrs: Dict[str, str] | None = {'class': 'btn btn-secondary btn-sm', 'role': 'button'}) str [source]#
Utility function for generating a link to a snapshot HTML view to insert in Django templates.
- Parameters:
snapshot_id – snapshot identifier
link_text – optional text for the generated link (the snapshot id will be used by default)
link_attrs – optional attributes (e.g. class) to add to the link
- Returns:
An HTML link in the form ‘<a href=”snapshot_view_url”>link_text</a>’
- swh.web.browse.utils.gen_content_link(sha1_git: str, snapshot_context: SnapshotContext | None = None, link_text: str | None = 'Browse', link_attrs: Dict[str, str] | None = {'class': 'btn btn-secondary btn-sm', 'role': 'button'}) str | None [source]#
Utility function for generating a link to a content HTML view to insert in Django templates.
- Parameters:
sha1_git – content identifier
link_text – optional text for the generated link (the content sha1_git will be used by default)
link_attrs – optional attributes (e.g. class) to add to the link
- Returns:
An HTML link in the form ‘<a href=”content_view_url”>link_text</a>’
- swh.web.browse.utils.get_revision_log_url(revision_id: str, snapshot_context: SnapshotContext | None = None) str [source]#
Utility function for getting the URL for a revision log HTML view (possibly in the context of an origin).
- Parameters:
revision_id – revision identifier the history heads to
snapshot_context – if provided, generate snapshot-dependent browsing link
- Returns:
The revision log view URL
- swh.web.browse.utils.gen_revision_log_link(revision_id: str, snapshot_context: SnapshotContext | None = None, link_text: str | None = 'Browse', link_attrs: Dict[str, str] | None = {'class': 'btn btn-secondary btn-sm', 'role': 'button'}) str | None [source]#
Utility function for generating a link to a revision log HTML view (possibly in the context of an origin) to insert in Django templates.
- Parameters:
revision_id – revision identifier the history heads to
snapshot_context – if provided, generate snapshot-dependent browsing link
link_text – optional text to use for the generated link (the revision id will be used by default)
link_attrs – optional attributes (e.g. class) to add to the link
- Returns:
An HTML link in the form ‘<a href=”revision_log_view_url”>link_text</a>’
- swh.web.browse.utils.gen_person_mail_link(person: Dict[str, Any], link_text: str | None = None) str [source]#
Utility function for generating a mail link to a person to insert in Django templates.
- Parameters:
person – dictionary containing person data (name, email, fullname)
link_text – optional text to use for the generated mail link (the person name will be used by default)
- Returns:
- A mail link to the person or the person name if no email is
present in person data
- Return type:
- swh.web.browse.utils.gen_release_link(sha1_git: str, snapshot_context: SnapshotContext | None = None, link_text: str | None = 'Browse', link_attrs: Dict[str, str] | None = {'class': 'btn btn-secondary btn-sm', 'role': 'button'}) str [source]#
Utility function for generating a link to a release HTML view to insert in Django templates.
- Parameters:
sha1_git – release identifier
link_text – optional text for the generated link (the release id will be used by default)
link_attrs – optional attributes (e.g. class) to add to the link
- Returns:
An HTML link in the form ‘<a href=”release_view_url”>link_text</a>’
- swh.web.browse.utils.format_log_entries(revision_log: Iterator[Dict[str, Any] | None], per_page: int, snapshot_context: SnapshotContext | None = None) List[Dict[str, str]] [source]#
Utility functions that process raw revision log data for HTML display. Its purpose is to:
add links to relevant browse views
format date in human readable format
truncate the message log
- Parameters:
revision_log – raw revision log as returned by the swh-web api
per_page – number of log entries per page
snapshot_context – if provided, generate snapshot-dependent browsing link
- swh.web.browse.utils.get_readme_to_display(readmes: Dict[str, str]) Tuple[str | None, str | None, str | None] [source]#
Process a list of readme files found in a directory in order to find the adequate one to display.
- Parameters:
readmes – a dict where keys are readme file names and values are readme sha1_gits
- Returns:
A tuple (readme_name, readme_url, readme_html)