Exemplo n.º 1
0
 def _get_date_value(self, data):
     """
     Takes a dictionary of data and attempts to return the date field.
     If none exists, returns None.
     """
     if self.datetime:
         return get_dict_value(self.datetime, data)
     elif self.date_string:
         return get_dict_value(self.date_string, data)
Exemplo n.º 2
0
 def test_value_from_nested_dict(self):
     """
     Tests method for getting a value from a nested dictionary.
     """
     doc = {'a': {'b': {'c': 100}}, 'd': {'e': {'f': 200}}}
     field = 'a.b.c'
     self.assertEqual(parserutils.get_dict_value(field, doc), 100)
Exemplo n.º 3
0
 def test_value_from_simple_dict(self):
     """
     Tests method for getting a value from a nested dictionary.
     """
     doc = {'a': 100}
     field = 'a'
     self.assertEqual(parserutils.get_dict_value(field, doc), 100)
Exemplo n.º 4
0
 def test_value_from_empty_dict(self):
     """
     Tests method for getting a value from a nested dictionary.
     """
     doc = {}
     field = 'a.b.c'
     self.assertEqual(parserutils.get_dict_value(field, doc), None)
Exemplo n.º 5
0
 def test_get_array_item(self):
     """
     Tests method for getting a value from an array in a nested dictionary.
     """
     doc = {'a': {'b': [{'c': [100, [15, 20]]}, {'d': 40}], 'e': 10}}
     field = 'a.b[0].c[1][1]'
     self.assertEqual(parserutils.get_dict_value(field, doc), 20)
Exemplo n.º 6
0
    def get_result(self, data):
        """Analayze data according to a Protocol.

        Analyzes a data dictionary using the Procedure's
        :attr:`~Procedure.protocol`. If the Procedure has a
        :attr:`~Procedure.field_name`, only the corresponding field
        within the data dictionary is analyzed. Otherwise, the entire
        data dictionary is analyzed with the protocol.

        Parameters
        ----------
        data : dict
            The data to analyze.

        Returns
        -------
        any
            The results of the analysis.

        Notes
        -----
        This method should have the same name as the corresponding
        method in an Inspection.

        """
        if self.field_name:
            value = parserutils.get_dict_value(self.field_name, data)
            return self._analyze(value)
        else:
            return self._analyze(data)
Exemplo n.º 7
0
 def test_wrong_value_from_dict(self):
     """
     Tests method for getting a nonexistent value from a nested dictionary.
     """
     doc = {'a': {'b': {'c': 100}}}
     field = 'a.c'
     self.assertEqual(parserutils.get_dict_value(field, doc), None)
Exemplo n.º 8
0
 def _get_field_values(self, muzzle):
     """
     Get a string of field names and field values associated with the
     Muzzle of the Watchdog that created the Alert.
     """
     field_values = [
         ':'.join((field, get_dict_value(field, self.data) or ''))
         for field in sorted(muzzle.get_fields())
     ]
     return ','.join(field_values)
Exemplo n.º 9
0
 def create_fieldset(self, data):
     """
     Takes a dictionary of data that was distilled by the Context's
     Focal Distillery. Returns a QueryFieldset representing a
     query expression for the ContextFilter.
     """
     value = get_dict_value(self.value_field, data)
     field_type = get_field_type(self.search_field)
     return QueryFieldset(field_name=self.search_field,
                          field_type=field_type,
                          operator=self.operator,
                          value=value)
Exemplo n.º 10
0
    def is_match(self, alert):
        """
        Takes an Alert and returns a Boolean indicating whether the
        Alert duplicates a previous Alert within the Muzzle's time frame.
        """
        fields = self._get_fields()
        alerts = self._get_filtered_alerts(alert)
        new_data = alert.data
        for old_alert in alerts:
            match = True
            old_data = old_alert.data
            for field in fields:
                new_data_val = get_dict_value(field, new_data)
                old_data_val = get_dict_value(field, old_data)
                if new_data_val != old_data_val:
                    match = False
                    break
            if match:
                old_alert.add_incident()
                return True

        return False
Exemplo n.º 11
0
    def _get_values(self, doc):
        """
        Takes a dictionary and returns values for the keys specified by the
        source_fields.
        """
        fields = self.source_fields.split(',')
        values = []

        for field in fields:
            key = field.strip()  # in case there were spaces after commas
            value = parserutils.get_dict_value(key, doc)
            values.append(value)

        return values
Exemplo n.º 12
0
    def get_result(self, data):
        """
        Takes a data dictionary and analyzes it using the Procedure's protocol.
        If the Procedure has a field_name, only the corresponding field within
        the data dictionary is analyzed. Otherwise, the entire data dictionary
        is analyzed with the protocol.

        Notes
        -----
        This method should have the same name as the corresponding
        method in an Inspection.
        """
        if self.field_name:
            value = parserutils.get_dict_value(self.field_name, data)
            return self._analyze(value)
        else:
            return self._analyze(data)
Exemplo n.º 13
0
    def get_location(self, data):
        """Return the value of the location field.

        Parameters
        ----------
        data : |dict|
            A document from which to extract the date field.

        Returns
        -------
        |Point| or |None|
            The value of the location field.

        """
        location = get_dict_value(self.location, data)
        if location:
            return convert_to_point(location, self.location_format)
Exemplo n.º 14
0
 def _get_collection_name(data):
     """
     Takes a dictionary of distilled data and returns a string
     representing the Collection where the data is stored.
     """
     if data is not None:
         raw_data = get_dict_value(_DISTILLERY_SETTINGS['RAW_DATA_KEY'],
                                   data)
         if raw_data:
             try:
                 return '.'.join([
                     raw_data.get(_DISTILLERY_SETTINGS['BACKEND_KEY']),
                     raw_data.get(_DISTILLERY_SETTINGS['WAREHOUSE_KEY']),
                     raw_data.get(_DISTILLERY_SETTINGS['COLLECTION_KEY'])
                 ])
             except TypeError:
                 return None
         else:
             return None
Exemplo n.º 15
0
    def _get_teaser_data(self, data):
        """
        Takes a dictionary of distilled data and returns a dictionary of
        data that can be used to show a teaser (taste) of that data.
        """
        sample = {}
        field_dict = self._to_dict()

        for key in self.TEXT_FIELDS:
            field_name = field_dict[key]
            if field_name:
                sample[key] = get_dict_value(field_name, data)
            else:
                sample[key] = None

        sample['date'] = self.get_date(data)
        sample['location'] = self.get_location(data)
        sample['collection'] = self._get_collection_name(data)

        return sample
Exemplo n.º 16
0
 def _get_value(self, data):
     """
     Takes a dictionary and returns the value for the key specified by the
     field_name.
     """
     return get_dict_value(self.field_name, data)
Exemplo n.º 17
0
 def _get_value(self, alert):
     """Return a lowercase string from an Alert's data field."""
     value = get_dict_value(self.field_name, alert.data)
     if isinstance(value, str):
         return value.lower()