# flake8: noqaq
import inspect
import re
import importlib
import warnings
from collections import OrderedDict
from functools import lru_cache
from html import escape as html_escape

# String that separates a name from its unique ID
_UNIQUE_SEPARATOR = "_"
_IDENTITY_LENGTH = 8
_IDENTITY_TABLE = {}

# If a property ends with this key, it is a pseudo-property which exists to
# hold the data dependencies of another property value
_DATA_DEPENDENCY_KEY_SUFFIX = "_PyConfDataDependencies"

_FLOW_GRAPH_NODE_COLOUR = "aliceblue"
_FLOW_GRAPH_INPUT_COLOUR = "deepskyblue1"
_FLOW_GRAPH_OUTPUT_COLOUR = "coral1"


def unique_name_ext_re():
    return "(?:%s[1234567890abcdef]{%s})?" % (_UNIQUE_SEPARATOR, _IDENTITY_LENGTH)


def findRootObjByName(rootFile, name):
    """
    Finds the object with given name in the given root file,
    where name is a regular expression or a set of them separated by '/' in case directories are used
    """
    curObj = rootFile
    for n in name.split("/"):
        matches = [
            k.GetName() for k in curObj.GetListOfKeys() if re.fullmatch(n, k.GetName())
        ]
        if len(matches) > 1:
            raise Exception(
                "Collision of names in Root : found several objects with name matching %s inside %s : %s"
                % (n, curObj.GetName(), matches)
            )
        if len(matches) == 0:
            raise Exception(
                "Failed to find object with name %s inside %s" % (n, curObj.GetName())
            )
        curObj = curObj.Get(matches[0])
    return curObj
