Value Normalization partis.pyproj.norms#

class scalar_list(vals=None)[source]#

Bases: valid_list

class str_list(vals=None)[source]#

Bases: valid_list

class nonempty_str_list(vals=None)[source]#

Bases: valid_list

norm_data(data)[source]#

Normalize data for writing into a distribution

Parameters:

data (bytes | str | io.IOBase) –

Returns:

data (bytes) –

  • If data is bytes, it will be returned un-modified.

  • If data is a str, it will be encoded as ‘utf-8’.

  • If data is a stream, it will be read to EOF and apply one of the above.

norm_path(path)[source]#

Normalizes a file path for writing into a distribution archive

Note

  • Must be a valid path

  • Must be relative, and no reference to parent any directory (e.g. ‘..’)

  • May not contain any white-space in path components

  • All slashes replaced with forward slashes

norm_path_to_os(path)[source]#

Converts a normalized or OS path to be an OS path

norm_mode(mode=None)[source]#

Normalizes file permission mode for distribution archive

Parameters:

mode (None | int) – POSIX permission mode

Returns:

int

Note

The returned mode is either 0o644 (rw-r–r–), or 0o755 (rwxr-xr-x) if mode & stat.S_IXUSR == True

Example

from partis.pyproj import norm_mode

print( norm_mode( 0o000 ) == 0o644 )
print( norm_mode( 0o100 ) == 0o755 )
norm_zip_external_attr(mode=None)[source]#

Converts the unix integer mode to zip external_attr

The returned value follows the 4 byte format |mm|mm|xx|dx|

The file permission mode is masked and shifted to the two most significant bytes. If specified as a directory, the second bit is set.

Parameters:

mode (int) –

Returns:

external_attr (int)

b64_nopad(data)[source]#

Encodes hash as urlsafe base64 encoding with no trailing ‘=’ (PEP 427)

Parameters:

data (bytes) –

Returns:

str

hash_sha256(stream)[source]#

Computes SHA-256 hash

Parameters:

stream (bytes | io.BytesIO) –

Returns:

str, int – urlsafe base64 encoded hash, and size (in bytes) of the hashed data

See also

hashlib.sha256()

email_encode_items(headers, payload=None)[source]#

Encodes a list of headers and a payload

Parameters:
Returns:

bytes

See also

email.message