Skip to main content

infrahub_sdk.utils

Functions

generate_short_id

generate_short_id() -> str

Generate a short unique ID

base36encode

base36encode(number: int) -> str

base36decode

base36decode(data: str) -> int

base16decode

base16decode(data: str) -> int

base16encode

base16encode(number: int) -> str

get_fixtures_dir

get_fixtures_dir() -> Path

Get the directory which stores fixtures that are common to multiple unit/integration tests.

is_valid_uuid

is_valid_uuid(value: Any) -> bool

Check if the input is a valid UUID.

decode_json

decode_json(response: httpx.Response) -> dict

generate_uuid

generate_uuid() -> str

duplicates

duplicates(input_list: list) -> list

Identify and return all the duplicates in a list.

intersection

intersection(list1: list[Any], list2: list[Any]) -> list

Calculate the intersection between 2 lists.

compare_lists

compare_lists(list1: list[Any], list2: list[Any]) -> tuple[list[Any], list[Any], list[Any]]

Compare 2 lists and return :

  • the intersection of both
  • the item present only in list1
  • the item present only in list2

deep_merge_dict

deep_merge_dict(dicta: dict, dictb: dict, path: list | None = None) -> dict

Deep Merge Dictionary B into Dictionary A. Code is inspired by https://stackoverflow.com/a/7205107

str_to_bool

str_to_bool(value: str) -> bool

Convert a String to a Boolean

get_flat_value

get_flat_value(obj: Any, key: str, separator: str = '__') -> Any

Query recursively an value defined in a flat notation (string), on a hierarchy of objects

Examples:

name__value module.object.value

generate_request_filename

generate_request_filename(request: httpx.Request) -> str

Return a filename for a request sent to the Infrahub API

This function is used when recording and playing back requests, as Infrahub is using a GraphQL API it's not possible to rely on the URL endpoint alone to separate one request from another, for this reason a hash of the payload is included in a filename.

is_valid_url

is_valid_url(url: str) -> bool

get_branch

get_branch(branch: str | None = None, directory: str | Path = '.') -> str

If branch isn't provide, return the name of the local Git branch.

dict_hash

dict_hash(dictionary: dict[str, Any]) -> str

MD5 hash of a dictionary.

calculate_dict_depth

calculate_dict_depth(data: dict, level: int = 1) -> int

Calculate the depth of a nested Dictionary recursively.

calculate_dict_height

calculate_dict_height(data: dict, cnt: int = 0) -> int

Calculate the number of fields (height) in a nested Dictionary recursively.

extract_fields

extract_fields(selection_set: SelectionSetNode | None) -> dict[str, dict] | None

This function extract all the requested fields in a tree of Dict from a SelectionSetNode

The goal of this function is to limit the fields that we need to query from the backend.

Currently the function support Fields and InlineFragments but in a combined tree where the fragments are merged together This implementation may seam counter intuitive but in the current implementation it's better to have slightly more information at time passed to the query manager.

In the future we'll probably need to redesign how we read GraphQL queries to generate better Database query.

extract_fields_first_node

extract_fields_first_node(info: GraphQLResolveInfo) -> dict[str, dict]

write_to_file

write_to_file(path: Path, value: Any) -> bool

Write a given value into a file and return if the operation was successful.

If the file does not exist, the function will attempt to create it.

read_file

read_file(file_path: Path) -> str

get_user_permissions

get_user_permissions(data: list[dict]) -> dict

calculate_time_diff

calculate_time_diff(value: str) -> str | None

Calculate the time in human format between a timedate in string format and now.