birdy.utils module

Birdy Utils Module.

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

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

Parameters

dataAny

Data to check.

Returns

Any

Single element or sequence.

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

valueAny

File path, URL, or file-like object.

mimetypestr, optional

Mimetype of the file.

encodingstr, optional

Encoding of the file.

Returns

tuple

Encoded content string and actual encoding.

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

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

Parameters

urlstr

URL or local path.

Returns

str

URL with a file:// scheme.

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

urlstr or Path

A path or URL to a file.

supportedlist or tuple

Supported mimetypes.

Returns

tuple

Mimetype and encoding.

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

Return True if path is a valid file.

Parameters

pathstr or Path

Path to a file.

Returns

bool

True if path is a valid file.

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

urlstr

URL.

Returns

bool

True if the URL is an OpenDAP URL.

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

urlstr

URL or local path.

Returns

bool

True if value is a valid URL.

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

namestr

Name to sanitize.

Returns

str

Sanitized name.