:tocdepth: 3 :py:mod:`gws.core.util` ======================= .. py:module:: gws.core.util .. autoapi-nested-parse:: Core utilities Most common function which are needed everywhere. This module is available as ``gws.u`` everywhere. **Source code:** :source:`gws.core.util` Module Contents --------------- .. py:function:: action_url_path(name: str, **kwargs) -> str .. py:function:: app_lock(name='') .. py:class:: cached_property(fn) Decorator for a cached property. .. py:function:: collect(pairs) .. py:function:: compact(x) Remove all None values from a collection. .. py:function:: deep_merge(x, y, concat_lists=True) Deeply merge dicts/Datas into a nested dict/Data. Latter vales overwrite former ones unless None. :param x: dict or Data. :param y: dict or Data. :param concat_lists: if true, list will be concatenated, otherwise merged :returns: A new object (dict or Data). .. py:function:: delete_app_global(name) .. py:function:: dirname(path) .. py:function:: ensure_dir(dir_path: str, base_dir: str = None, mode: int = 493, user: int = None, group: int = None) -> str Check if a (possibly nested) directory exists and create if it does not. :param dir_path: Path to a directory. :param base_dir: Base directory. :param mode: Directory creation mode. :param user: Directory user (defaults to gws.c.UID) :param group: Directory group (defaults to gws.c.GID) :returns: The absolute path to the directory. .. py:function:: ensure_system_dirs() .. py:function:: exit(code: int = 255) Exit the application. :param code: Exit code. .. py:function:: first(it) .. py:function:: first_not_none(*args) .. py:function:: format_map(fmt: str, x: Union[dict, Data], default: str = '') -> str .. py:function:: get(x, key, default=None) Get a nested value/attribute from a structure. :param x: A dict, list or Data. :param key: A list or a dot separated string of nested keys. :param default: The default value. :returns: The value if it exists and the default otherwise. .. py:function:: get_app_global(name, init_fn) .. py:function:: get_cached_object(name: str, life_time: int, init_fn) .. py:function:: get_server_global(name: str, init_fn) .. py:function:: has(x, key) -> bool True if a nested value/attribute exists in a structure. :param x: A dict, list or Data. :param key: A list or a dot separated string of nested keys. :returns: True if a key exists .. py:function:: is_atom(x) .. py:function:: is_bytes(x) .. py:function:: is_data_object(x) -> bool .. py:function:: is_dict(x) .. py:function:: is_dir(path) .. py:function:: is_empty(x) -> bool Check if the value is empty (None, empty list/dict/object). .. py:function:: is_file(path) .. py:function:: is_list(x) .. py:function:: join_uid(parent_uid, object_uid) .. py:function:: merge(*args, **kwargs) -> Union[dict, Data] Create a new dict/Data object by merging values from dicts/Datas or kwargs. Latter vales overwrite former ones unless None. :param \*args: dicts or Datas. :param \*\*kwargs: Keyword args. :returns: A new object (dict or Data). .. py:function:: mstime() -> int Unix time as an integer number of milliseconds. .. py:function:: omit(x, *keys) .. py:function:: parse_acl(acl) Parse an ACL config into an ACL. :param acl: an ACL config. Can be given as a string ``allow X, allow Y, deny Z``, or as a list of dicts ``{ role X type allow }, { role Y type deny }``, or it can already be an ACL ``[1 X], [0 Y]``, or it can be None. :returns: Access list. .. py:function:: pick(x, *keys) .. py:function:: pop(x, key, default=None) .. py:function:: printtemp(name: str) -> str Return a transient path name in the print directory. .. py:function:: random_string(size: int) -> str Generate a random string of length `size`. .. py:function:: read_file(path: str) -> str .. py:function:: read_file_b(path: str) -> bytes .. py:function:: serialize_to_path(obj, path) .. py:function:: server_lock(uid) .. py:function:: set_app_global(name, value) .. py:function:: sha256(x) .. py:function:: sleep(n: float) Sleep for n seconds. .. py:function:: split_uid(joined_uid: str) -> tuple[str, str] .. py:function:: stime() -> int Unix time as an integer number of seconds. .. py:function:: strip(x) Strip all strings and remove empty values from a collection. .. py:function:: to_bytes(x, encoding='utf8') -> bytes Convert a value to bytes by converting it to string and encoding. .. py:function:: to_data_object(x) -> bool .. py:function:: to_dict(x) -> dict Convert a value to a dict. If the argument is an object, return its `dict`. .. py:function:: to_float(x) -> float Convert a value to a float or 0.0 if this fails. .. py:function:: to_int(x) -> int Convert a value to an int or 0 if this fails. .. py:function:: to_lines(txt: str, comment: str = None) -> list[str] Convert a multiline string into a list of strings. Strip each line, skip empty lines, if `comment` is given, also remove lines starting with it. .. py:function:: to_list(x, delimiter: str = ',') -> list Convert a value to a list. :param x: A value. Is it's a string, split it by the delimiter :param delimiter: :returns: A list. .. py:function:: to_lower_dict(x) -> dict .. py:function:: to_rounded_int(x) -> int Round and convert a value to an int or 0 if this fails. .. py:function:: to_str(x, encodings: list[str] = None) -> str Convert a value to a string. :param x: Value. :param encodings: A list of acceptable encodings. If the value is bytes, try each encoding, and return the first one which passes without errors. :returns: A string. .. py:function:: to_uid(x) -> str Convert a value to an uid (alphanumeric string). .. py:function:: to_upper_dict(x) -> dict .. py:data:: UID_DELIMITER :value: '::' .. py:function:: uniq(x) Remove duplicate elements from a collection. .. py:function:: unserialize_from_path(path) .. py:function:: utime() -> float Unix time as a float number. .. py:function:: write_file(path: str, s: str, user: int = None, group: int = None) .. py:function:: write_file_b(path: str, s: bytes, user: int = None, group: int = None)