Example #1
0
 def format(self, record):
     msg = super(ZMQHandler, self).format(record)
     if self.log_prefix:
         msg = u"[{}] {}".format(
             to_unicode(self.log_prefix),
             to_unicode(msg),
         )
     return msg
    def _get_grain_value(value, depth):
        """
        Process different value types, recursing lists if necessary
        """

        # Ignore dicts. Creating attributes from this type is not useful.
        if isinstance(value, dict):
            raise TypeError

        # Return string value
        if isinstance(value, six.string_types):
            return stringutils.to_unicode(value)

        # Return the first element of a list
        if hasattr(value, "__iter__"):
            if isinstance(value, list) and len(value) > 0:
                return ResourceGenerator._get_grain_value(value[0], depth + 1)
            raise TypeError

        return value
    def _tags_from_value(value, depth):
        """Add tags from a grain value

        Args:
            value (any): The grain value from which to create tag(s)
            depth (int): The recursion depth. This is not currently used,
                         but may be in the future to support a maximum
                         recursion depth setting.

        Returns:
            set: Set of tags to create from this value

        Raises:
            TypeError: Raised when the type of the value is not supported
        """
        tags = set()

        # Ignore None values
        if value is None:
            return tags

        # Ignore numbers, booleans, and dicts. Creating tags
        # from these types is not useful.
        if isinstance(value, (int, float, bool, dict)):
            raise TypeError

        # Create tags from string types
        if isinstance(value, six.string_types):
            tags.add(stringutils.to_unicode(value))

        # Create tags from binary types
        elif isinstance(value, six.binary_type):
            tags.add(value)

        # If the type is iterable, add each element
        elif hasattr(value, "__iter__"):
            for item in value:
                tags.update(ResourceGenerator._tags_from_value(
                    item, depth + 1))

        return tags