swh.core.utils module

swh.core.utils.cwd(path)[source]

Contextually change the working directory to do thy bidding. Then gets back to the original location.

swh.core.utils.grouper(iterable, n)[source]
Collect data into fixed-length size iterables. The last block might

contain less elements as it will hold only the remaining number of elements.

The invariant here is that the number of elements in the input iterable and the sum of the number of elements of all iterables generated from this function should be equal.

Parameters
  • iterable (Iterable) – an iterable

  • n (int) – size of block to slice the iterable into

Yields

fixed-length blocks as iterables. As mentioned, the last iterable might be less populated.

swh.core.utils.backslashescape_errors(exception)[source]
swh.core.utils.encode_with_unescape(value)[source]

Encode an unicode string containing x<hex> backslash escapes

swh.core.utils.decode_with_escape(value)[source]

Decode a bytestring as utf-8, escaping the bytes of invalid utf-8 sequences as x<hex value>. We also escape NUL bytes as they are invalid in JSON strings.

swh.core.utils.commonname(path0, path1, as_str=False)[source]

Compute the commonname between the path0 and path1.

swh.core.utils.numfile_sortkey(fname)[source]

Simple function to sort filenames of the form:

nnxxx.ext

where nn is a number according to the numbers.

Typically used to sort sql/nn-swh-xxx.sql files.