def getSettingInjectionListFromSettingValue(settingValue):
    if ObjectHelper.isList(settingValue):
        allElements = []
        for element in settingValue:
            elements = getSettingInjectionListFromSettingValue(element)
            if ObjectHelper.isList(elements):
                allElements += elements
        return allElements
    elif ObjectHelper.isNotNone(settingValue) and StringHelper.isNotBlank(
            settingValue):
        splitedSettingValue = settingValue.split(OPEN_SETTING_INJECTION)
        settingValueList = []
        completeSettingValue = c.NOTHING
        for segment in splitedSettingValue if settingValue.startswith(
                OPEN_SETTING_INJECTION) else splitedSettingValue[1:]:
            if ObjectHelper.isNotNone(segment) and StringHelper.isNotBlank(
                    segment):
                if ObjectHelper.isNotNone(segment.count(
                        c.OPEN_DICTIONARY)) and not segment.count(
                            c.OPEN_DICTIONARY) == segment.count(
                                c.CLOSE_DICTIONARY) and 0 < segment.count(
                                    c.OPEN_DICTIONARY):
                    completeSettingValue += segment
                else:
                    splitedSegment = segment.split(CLOSE_SETTING_INJECTION)
                    completeSettingValue += splitedSegment[0]
                    if ObjectHelper.isNotNone(
                            completeSettingValue) and StringHelper.isNotBlank(
                                completeSettingValue):
                        settingValueList.append(
                            f'{OPEN_SETTING_INJECTION}{completeSettingValue}{CLOSE_SETTING_INJECTION}'
                        )
                    completeSettingValue = c.NOTHING
        return settingValueList
    return []
def sampleCollection(collection, collectionClass, length=None) :
    if ObjectHelper.isNotNone(length) :
        if ObjectHelper.isList(collection) :
            return collectionClass(random.sample(collection, length))
        return collectionClass(random.sample(list(collection), length))
    if ObjectHelper.isList(collection) :
        return random.choice(collection)
    return random.choice(list(collection))
def sample(collection, length=None):
    if ObjectHelper.isCollection(collection):
        if ObjectHelper.isDictionary(collection):
            if ObjectHelper.isNone(length):
                key = RandomHelperHelper.sampleCollection(
                    list(collection.keys()), list)
                return {key: collection[key]}
            sampleDictionaryKeys = RandomHelperHelper.sampleCollection(
                list(collection.keys()), list, length=length)
            sampleDictionary = {}
            for key in sampleDictionaryKeys:
                sampleDictionary[key] = collection[key]
            return sampleDictionary
        elif ObjectHelper.isList(collection):
            return RandomHelperHelper.sampleCollection(collection,
                                                       list,
                                                       length=length)
        elif ObjectHelper.isSet(collection):
            return RandomHelperHelper.sampleCollection(collection,
                                                       set,
                                                       length=length)
        elif ObjectHelper.isTuple(collection):
            return RandomHelperHelper.sampleCollection(collection,
                                                       tuple,
                                                       length=length)
    raise Exception(f'The "{collection}" argument is not a collection')
def containsValidSettingInjection(settingValue):
    if ObjectHelper.isNotNone(settingValue) and StringHelper.isNotBlank(
            settingValue) and 0 < settingValue.count(
                OPEN_SETTING_INJECTION) and settingValue.count(
                    c.OPEN_DICTIONARY) == settingValue.count(
                        c.CLOSE_DICTIONARY):
        splitedSettingValue = settingValue.split(OPEN_SETTING_INJECTION)
        settingValueList = []
        completeSettingValue = c.NOTHING
        for segment in splitedSettingValue if settingValue.startswith(
                OPEN_SETTING_INJECTION) else splitedSettingValue[1:]:
            if ObjectHelper.isNotNone(segment):
                if ObjectHelper.isNotNone(segment.count(
                        c.OPEN_DICTIONARY)) and not segment.count(
                            c.OPEN_DICTIONARY) == segment.count(
                                c.CLOSE_DICTIONARY) and 0 < segment.count(
                                    c.OPEN_DICTIONARY):
                    completeSettingValue += segment
                else:
                    splitedSegment = segment.split(CLOSE_SETTING_INJECTION)
                    completeSettingValue += splitedSegment[0]
                    settingValueList.append(
                        f'{OPEN_SETTING_INJECTION}{completeSettingValue}{CLOSE_SETTING_INJECTION}'
                    )
                    completeSettingValue = c.NOTHING
        return len(splitedSettingValue) == len(
            settingValueList) if settingValue.startswith(
                OPEN_SETTING_INJECTION) else len(
                    splitedSettingValue) == len(settingValueList) + 1
    elif ObjectHelper.isList(settingValue):
        return containsValidSettingInjection(str(settingValue))
    return False
def handleSettingInjectionListByCallingHandler(done, stuck,
                                               isSettingInjectionCount,
                                               containsSettingInjectionCount,
                                               settingInjection, settingTree,
                                               settingInjectionList,
                                               fallbackSettingTree, handler):
    try:
        if isSettingInjection(settingInjection[SETTING_VALUE]):
            settingInjection[SETTING_VALUE] = handler(
                settingInjection[SETTING_KEY],
                settingInjection[SETTING_VALUE],
                settingInjection[SETTING_NODE_KEY],
                settingTree,
                fallbackSettingTree=fallbackSettingTree)
            settingInjectionArgs = list(
                settingInjection.values()) + [settingTree]
            updateSettingValue(*settingInjectionArgs, isSettingInjection=True)
            if not containsSettingInjection(settingInjection[SETTING_VALUE]):
                settingInjectionList.remove(settingInjection)
                isSettingInjectionCount += 1
            stuck = False
        elif containsSettingInjection(settingInjection[SETTING_VALUE]):
            settingInjectionListFromSettingValue = getSettingInjectionListFromSettingValue(
                settingInjection[SETTING_VALUE])
            newSettingInjection = settingInjection[SETTING_VALUE]
            for settingValue in settingInjectionListFromSettingValue:
                newSettingValue = handler(
                    settingInjection[SETTING_KEY],
                    settingValue,
                    settingInjection[SETTING_NODE_KEY],
                    settingTree,
                    fallbackSettingTree=fallbackSettingTree)
                if ObjectHelper.isList(newSettingInjection):
                    for index, element in enumerate(newSettingInjection):
                        if settingValue == element:
                            newSettingInjection[index] = newSettingValue
                else:
                    newSettingInjection = newSettingInjection.replace(
                        settingValue, str(newSettingValue))
            settingInjection[SETTING_VALUE] = newSettingInjection
            settingInjectionArgs = list(
                settingInjection.values()) + [settingTree]
            updateSettingValue(*settingInjectionArgs, isSettingInjection=True)
            if not containsSettingInjection(settingInjection[SETTING_VALUE]):
                settingInjectionList.remove(settingInjection)
                containsSettingInjectionCount += 1
            stuck = False
    except Exception as exception:
        LogHelper.log(
            handleSettingInjectionListByCallingHandler,
            f'Ignored exception while handling {StringHelper.prettyPython(settingInjection)} setting injection list',
            exception=exception)
    return done, stuck, isSettingInjectionCount, containsSettingInjectionCount