swh.web.utils.converters module#

swh.web.utils.converters.fmap(f, data)[source]#

Map f to data at each level.

This must keep the origin data structure type: - map -> map - dict -> dict - list -> list - None -> None

Parameters:
  • f – function that expects one argument.

  • data – data to traverse to apply the f function. list, map, dict or bare value.

Returns:

The same data-structure with modified values by the f function.

swh.web.utils.converters.from_swh(dict_swh, hashess={}, bytess={}, dates={}, blacklist={}, removables_if_empty={}, empty_dict={}, empty_list={}, convert={}, convert_fn=<function <lambda>>)[source]#

Convert from a swh dictionary to something reasonably json serializable.

Parameters:
  • dict_swh – the origin dictionary needed to be transformed

  • hashess – list/set of keys representing hashes values (sha1, sha256, sha1_git, etc…) as bytes. Those need to be transformed in hexadecimal string

  • bytess – list/set of keys representing bytes values which needs to be decoded

  • blacklist – set of keys to filter out from the conversion

  • convert – set of keys whose associated values need to be converted using convert_fn

  • convert_fn – the conversion function to apply on the value of key in ‘convert’

The remaining keys are copied as is in the output.

Returns:

dictionary equivalent as dict_swh only with its keys converted.

swh.web.utils.converters.from_origin(origin: Dict[str, Any]) OriginInfo[source]#

Convert from a swh origin to an origin dictionary.

swh.web.utils.converters.from_release(release: Release) Dict[str, Any][source]#

Convert from a swh release to a json serializable release dictionary.

Parameters:

release – A release model object

Returns:

release dictionary with the following keys

  • id: hexadecimal sha1 (string)

  • revision: hexadecimal sha1 (string)

  • comment: release’s comment message (string)

  • name: release’s name (string)

  • author: release’s author identifier (swh’s id)

  • synthetic: the synthetic property (boolean)

class swh.web.utils.converters.SWHDjangoJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]#

Bases: DjangoJSONEncoder

Wrapper around DjangoJSONEncoder to serialize SWH-specific types found in swh.web.utils.typing.SWHObjectInfo.

Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.

If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.

If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an RecursionError). Otherwise, no such check takes place.

If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (’, ‘, ‘: ‘) if indent is None and (‘,’, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.

If specified, default is a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError.

default(o)[source]#

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
class swh.web.utils.converters.SWHMetadataEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]#

Bases: JSONEncoder

Special json encoder for metadata field which can contain bytes encoded value.

Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.

If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.

If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an RecursionError). Otherwise, no such check takes place.

If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (’, ‘, ‘: ‘) if indent is None and (‘,’, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.

If specified, default is a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError.

default(obj)[source]#

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
swh.web.utils.converters.convert_metadata(metadata)[source]#

Convert json specific dict to a json serializable one.

swh.web.utils.converters.from_revision(revision: Dict[str, Any] | Revision) Dict[str, Any][source]#

Convert swh revision model object to a json serializable revision dictionary.

Parameters:

revision – revision model object

Returns:

Revision dictionary with the same keys as inputs, except:

  • sha1s are in hexadecimal strings (id, directory)

  • bytes are decoded in string (author_name, committer_name, author_email, committer_email)

Remaining keys are left as is

Return type:

dict

swh.web.utils.converters.from_raw_extrinsic_metadata(metadata: Dict[str, Any] | RawExtrinsicMetadata) Dict[str, Any][source]#

Convert RawExtrinsicMetadata model object to a json serializable dictionary.

swh.web.utils.converters.from_content(content)[source]#

Convert swh content to serializable content dictionary.

swh.web.utils.converters.from_person(person)[source]#

Convert swh person to serializable person dictionary.

swh.web.utils.converters.from_origin_visit(visit: Dict[str, Any]) OriginVisitInfo[source]#

Convert swh origin_visit to serializable origin_visit dictionary.

swh.web.utils.converters.from_snapshot(snapshot)[source]#

Convert swh snapshot to serializable (partial) snapshot dictionary.

swh.web.utils.converters.from_partial_branches(branches: PartialBranches)[source]#

Convert PartialBranches to serializable partial snapshot dictionary

swh.web.utils.converters.from_directory_entry(dir_entry)[source]#

Convert swh directory to serializable directory dictionary.

swh.web.utils.converters.from_filetype(content_entry)[source]#

Convert swh content to serializable dictionary containing keys ‘id’, ‘encoding’, and ‘mimetype’.