birdy.utils module

birdy.utils._encode(content, mimetype, encoding)[source]

Encode in base64 if mimetype is a binary type.

birdy.utils.delist(data: Any) Any[source]

If data is a sequence with a single element, returns this element, otherwise return the sequence.

Parameters:

data (Any) – Data to check.

Returns:

Single element or sequence.

Return type:

Any

birdy.utils.embed(value: Any, mimetype: str | None = None, encoding: str | None = None) tuple[bytes, str] | tuple[str, str | Any] | tuple[Any, str | Any][source]

Return the content of the file, either as a string or base64 bytes.

Parameters:
  • value (Any) – File path, URL, or file-like object.

  • mimetype (str, optional) – Mimetype of the file.

  • encoding (str, optional) – Encoding of the file.

Returns:

Encoded content string and actual encoding.

Return type:

tuple

birdy.utils.fix_url(url: str) str[source]

If url is a local path, add a file:// scheme.

Parameters:

url (str) – URL or local path.

Returns:

URL with a file:// scheme.

Return type:

str

birdy.utils.guess_type(url: str | Path, supported: list[str] | tuple[str]) tuple[str, str][source]

Guess the mime type of the file link.

If the mimetype is not recognized, default to the first supported value.

Parameters:
  • url (str or Path) – A path or URL to a file.

  • supported (list or tuple) – Supported mimetypes.

Returns:

Mimetype and encoding.

Return type:

tuple

birdy.utils.is_file(path: str | None) bool[source]

Return True if path is a valid file.

Parameters:

path (str or Path) – Path to a file.

Returns:

True if path is a valid file.

Return type:

bool

birdy.utils.is_opendap_url(url: str) bool[source]

Check if a provided url is an OpenDAP url.

The DAP Standard specifies that a specific tag must be included in the Content-Description header of every request. This tag is one of: “dods-dds” | “dods-das” | “dods-data” | “dods-error”

So we can check if the header starts with dods.

Parameters:

url (str) – URL.

Returns:

True if the URL is an OpenDAP URL.

Return type:

bool

Notes

This might not work with every DAP server implementation.

birdy.utils.is_url(url: str | None) bool[source]

Return whether value is a valid URL.

Parameters:

url (str) – URL or local path.

Returns:

True if value is a valid URL.

Return type:

bool

birdy.utils.sanitize(name: str) str[source]

Lower-case name and replace all non-ascii chars by _.

If name is a Python keyword (like return) then add a trailing _.

Parameters:

name (str) – Name to sanitize.

Returns:

Sanitized name.

Return type:

str