예제 #1
0
 def test_sample_data5(self):
     self.assertEqual(
         1, get_occurrence_of_key(self.sample5, 'total_number_of_cores'))
     self.assertEqual(1, get_occurrence_of_key(self.sample5, 'memory'))
     # Add key 'memory' and verify
     self.sample5['memory'] = 0
     self.assertEqual(2, get_occurrence_of_key(self.sample5, 'memory'))
def non_essential_count(base_json):
    #base_json = non_essential_json(base_path, cc_id)
    return {
        "total": get_occurrence_of_key(base_json, key="Touchstatus"),
        "essential": get_occurrence_of_value(base_json, value=True),
        "non-essential": get_occurrence_of_value(base_json, value=False)
    }
예제 #3
0
    def __get(self, b_dict: dict, key: str, prefix: str = ""):
        """Get preference.

        Args:
            b_dict (dict): Dictionary to search.
            key (str): Preference to peek. Can be full path preference.
            prefix (:obj:`str`, optional): Prefix defining which sub-config to search.
                For example, to access the key ``"visualization/rcParams"``,
                this parameter should be ``"visualization"``.

        Returns:
            The preference value, sub-dictionary to search
        """
        p_keys = self._get_prefixes(key)
        key = p_keys[-1]
        k_prefixes = list(p_keys[:-1])

        prefixes = list(self._get_prefixes(prefix))
        prefixes.extend(k_prefixes)

        subdict = b_dict
        for p in prefixes:
            subdict = subdict[p]

        num_keys = nested_lookup.get_occurrence_of_key(subdict, key)

        if num_keys == 0:
            raise KeyError("Key not '%s ' found in prefix '%s'" %
                           (key, prefix))
        if num_keys > 1:
            raise KeyError(
                "Multiple keys '%s 'found in prefix '%s'. Provide a more specific prefix."
                % (key, prefix))

        return nested_lookup.nested_lookup(key, subdict)[0], subdict
예제 #4
0
    def test_set(self):
        a = PreferencesMock()

        # Raise error when key doesn't exist.
        with self.assertRaises(KeyError):
            a.set("sample-key", "bar")

        # Raise error when key is not specific.
        assert nested_lookup.get_occurrence_of_key(a.config,
                                                   "foo") > 1, "%s." % a.config
        with self.assertRaises(KeyError):
            a.set("foo", 100)

        # Raise error when value is not the same
        with self.assertRaises(TypeError):
            a.set("testing1/foo", "bar")

        # Success when using full path or prefix kwarg
        a.set("testing1/foo", 50)
        assert a.get(
            "testing1/foo") == 50, "Value mismatch: got %s, expected %s" % (
                a.get("testing1/foo"),
                50,
            )

        a.set("foo", 100, "testing1")
        assert a.get(
            "testing1/foo") == 100, "Value mismatch: got %s, expected %s" % (
                a.get("testing1/foo"),
                100,
            )
예제 #5
0
    def _verify_key_exists(self, key, lookup_dict):
        """
        Return True if the specified key exists in our lookup_dict.

        This is a protected method and should not be used outside of the public
        convert_it method. This method is responsible for checking to see if we
        get a hit on a specified key in a lookup on our dict. This helps us
        ensure that during conversion, if any labels annotations or otherwise
        will be unaffected by the conversion and thusly not return empty lists.

        Parameters
        ----------
        key : str
            The key we are looking up in lookup_dict.

        lookup_dict : str
            The lookup_dict is in our case an alert from an arbitrary alerting
            system to be converted to an alert that Alert manager will accept.


        Returns
        -------
        exists : boolean
            Simple flag, True or False, to let us know if our search was
            successful.


        """
        exists = False
        if get_occurrence_of_key(lookup_dict, key) > 0:
            exists = True
        return exists
예제 #6
0
 def test_sample_data4(self):
     result = get_occurrence_of_key(self.sample4, "checks")
     self.assertEqual(1, result)
     result = get_occurrence_of_value(self.sample4, "mziad")
     self.assertEqual(1, result)
     # Add one more value in key "monitoring_zones" and verify
     self.sample4["values"][0]["checks"][0]["monitoring_zones"].append("mziad")
     self.assertEqual(2, get_occurrence_of_value(self.sample4, "mziad"))
예제 #7
0
def non_essential_count(base_json, filter="workspace"):
    #base_json = non_essential_json(base_path, cc_id)
    return {
        "folder": filter,
        "total": get_occurrence_of_key(base_json, key="Touchstatus"),
        "essential": get_occurrence_of_value(base_json, value=True),
        "non-essential": get_occurrence_of_value(base_json, value=False)
    }
예제 #8
0
 def test_sample_data4(self):
     result = get_occurrence_of_key(self.sample4, 'checks')
     self.assertEqual(1, result)
     result = get_occurrence_of_value(self.sample4, 'mziad')
     self.assertEqual(1, result)
     # Add one more value in key "monitoring_zones" and verify
     self.sample4['values'][0]['checks'][0]['monitoring_zones'].append(
         'mziad')
     self.assertEqual(2, get_occurrence_of_value(self.sample4, 'mziad'))
예제 #9
0
    def test_get(self):
        a = PreferencesMock()

        # Raise error when key doesn't exist.
        with self.assertRaises(KeyError):
            a.get("sample-key")

        # Raise error when key is not specific.
        assert nested_lookup.get_occurrence_of_key(a.config, "foo") > 1
        with self.assertRaises(KeyError):
            a.get("foo")

        # No error when lookup is specific.
        a.get("testing1/foo")
def filter_line(stat, filter_dict, or_and=True, include_absent=True):
    """
    stat - dict which represents JSONL line
    filter_dict - simple dict to use for filtering
    or_and - True for OR logic else AND
    include_absent - if True and filter finds nothing return True

    returns True if line has entry from filter_dict
    Currently support simple keys and values
    """
    found = [filter_dict[k] in nl.nested_lookup(k, stat) for k in filter_dict]
    num_exist = [nl.get_occurrence_of_key(stat, k) for k in filter_dict]

    return (include_absent and sum(num_exist)
            == 0) or (or_and and any(found)) or (not or_and and all(found))
예제 #11
0
 def test_sample_data3(self):
     result = get_occurrence_of_key(self.sample3, 'total_number_of_cores')
     self.assertEqual(3, result)
     result = get_occurrence_of_value(self.sample3, '4')
     self.assertEqual(3, result)
예제 #12
0
 def test_sample_data2(self):
     result = get_occurrence_of_key(self.sample2, 'core_details')
     self.assertEqual(1, result)
     result = get_occurrence_of_value(self.sample2, '4')
     self.assertEqual(3, result)
예제 #13
0
 def test_sample_data1(self):
     result = get_occurrence_of_key(self.sample1, 'build_version')
     self.assertEqual(4, result)
     result = get_occurrence_of_value(self.sample1, '256 KB')
     self.assertEqual(2, result)