swh.web.browse.utils module

swh.web.browse.utils.get_directory_entries(sha1_git)[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)[source]

Function that returns the mime type and the encoding associated to a content buffer using the magic module under the hood.

Parameters

content (bytes) – a content buffer

Returns

A tuple (mimetype, encoding), for instance (‘text/plain’, ‘us-ascii’), associated to the provided content.

swh.web.browse.utils.request_content(query_string, max_size=5242880, raise_if_unavailable=True, re_encode=True)[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, or blake2s256 (default to sha1) and HASH the hexadecimal representation of the hash value

  • max_size – the maximum size for a content to retrieve (default to 1MB, no size limit if None)

Returns

A tuple whose first member corresponds to the content raw bytes and second member the content mime type

Raises

NotFoundExc if the content is not found

swh.web.browse.utils.prepare_content_for_display(content_data, mime_type, path)[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 (bytes) – raw bytes of the content

  • mime_type (string) – mime type of the content

  • path (string) – 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’.

Utility function for generating an HTML link to insert in Django templates.

Parameters
  • url (str) – an url

  • link_text (str) – optional text for the produced link, if not provided the url will be used

  • link_attrs (dict) – 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, snapshot_context=None)[source]

Utility function for generating an url to a revision.

Parameters
  • revision_id (str) – a revision id

  • snapshot_context (dict) – if provided, generate snapshot-dependent browsing url

Returns

The url to browse the revision

Return type

str

Utility function for generating a link to a revision HTML view to insert in Django templates.

Parameters
  • revision_id (str) – a revision id

  • shorten_id (boolean) – whether to shorten the revision id to 7 characters for the link text

  • snapshot_context (dict) – if provided, generate snapshot-dependent browsing link

  • link_text (str) – optional text for the generated link (the revision id will be used by default)

  • link_attrs (dict) – 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

str

Utility function for generating a link to a directory HTML view to insert in Django templates.

Parameters
  • sha1_git (str) – directory identifier

  • link_text (str) – optional text for the generated link (the directory id will be used by default)

  • link_attrs (dict) – 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>’

Utility function for generating a link to a snapshot HTML view to insert in Django templates.

Parameters
  • snapshot_id (str) – snapshot identifier

  • link_text (str) – optional text for the generated link (the snapshot id will be used by default)

  • link_attrs (dict) – 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>’

Utility function for generating a link to a content HTML view to insert in Django templates.

Parameters
  • sha1_git (str) – content identifier

  • link_text (str) – optional text for the generated link (the content sha1_git will be used by default)

  • link_attrs (dict) – 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, snapshot_context=None)[source]

Utility function for getting the URL for a revision log HTML view (possibly in the context of an origin).

Parameters
  • revision_id (str) – revision identifier the history heads to

  • snapshot_context (dict) – if provided, generate snapshot-dependent browsing link

Returns

The revision log view URL

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 (str) – revision identifier the history heads to

  • snapshot_context (dict) – if provided, generate snapshot-dependent browsing link

  • link_text (str) – optional text to use for the generated link (the revision id will be used by default)

  • link_attrs (dict) – 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>’

Utility function for generating a mail link to a person to insert in Django templates.

Parameters
  • person (dict) – dictionary containing person data (name, email, fullname)

  • link_text (str) – 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

str

Utility function for generating a link to a release HTML view to insert in Django templates.

Parameters
  • sha1_git (str) – release identifier

  • link_text (str) – optional text for the generated link (the release id will be used by default)

  • link_attrs (dict) – 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, per_page, snapshot_context=None)[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 (list) – raw revision log as returned by the swh-web api

  • per_page (int) – number of log entries per page

  • snapshot_context (dict) – if provided, generate snapshot-dependent browsing link

swh.web.browse.utils.get_readme_to_display(readmes)[source]

Process a list of readme files found in a directory in order to find the adequate one to display.

Parameters

readmes – a list of dict where keys are readme file names and values are readme sha1s

Returns

A tuple (readme_name, readme_sha1)