Ejemplo n.º 1
0
  def testInitialization(self):
    """Tests the __init__ function."""
    event_timestamp = 1467835655123456
    time_slice = time_slices.TimeSlice(event_timestamp)

    self.assertEqual(time_slice.event_timestamp, event_timestamp)
    self.assertEqual(time_slice.duration, 5)
    self.assertEqual(time_slice.end_timestamp, 1467835955123456)
    self.assertEqual(time_slice.start_timestamp, 1467835355123456)

    calculated_duration, _ = divmod(
        time_slice.end_timestamp - event_timestamp, 60 * 1000000)
    self.assertEqual(calculated_duration, 5)

    calculated_duration, _ = divmod(
        event_timestamp - time_slice.start_timestamp, 60 * 1000000)
    self.assertEqual(calculated_duration, 5)
Ejemplo n.º 2
0
    def _ParseFilterOptions(self, options):
        """Parses the filter options.

    Args:
      options (argparse.Namespace): command line arguments.

    Raises:
      BadConfigOption: if the options are invalid.
    """
        self._event_filter_expression = self.ParseStringOption(
            options, 'filter')
        if self._event_filter_expression:
            self._event_filter = event_filter.EventObjectFilter()

            try:
                self._event_filter.CompileFilter(self._event_filter_expression)
            except errors.ParseError as exception:
                raise errors.BadConfigOption(
                    ('Unable to compile filter expression with error: '
                     '{0!s}').format(exception))

        time_slice_event_time_string = getattr(options, 'slice', None)
        time_slice_duration = getattr(options, 'slice_size', 5)
        self._use_time_slicer = getattr(options, 'slicer', False)

        # The slice and slicer cannot be set at the same time.
        if time_slice_event_time_string and self._use_time_slicer:
            raise errors.BadConfigOption(
                'Time slice and slicer cannot be used at the same time.')

        time_slice_event_timestamp = None
        if time_slice_event_time_string:
            # Note self._preferred_time_zone is None when not set but represents UTC.
            preferred_time_zone = self._preferred_time_zone or 'UTC'
            timezone = pytz.timezone(preferred_time_zone)
            time_slice_event_timestamp = timelib.Timestamp.FromTimeString(
                time_slice_event_time_string, timezone=timezone)
            if time_slice_event_timestamp is None:
                raise errors.BadConfigOption(
                    'Unsupported time slice event date and time: {0:s}'.format(
                        time_slice_event_time_string))

        if time_slice_event_timestamp is not None or self._use_time_slicer:
            # Note that time slicer uses the time slice to determine the duration.
            self._time_slice = time_slices.TimeSlice(
                time_slice_event_timestamp, duration=time_slice_duration)
Ejemplo n.º 3
0
    def _ParseFilterOptions(self, options):
        """Parses the filter options.

    Args:
      options (argparse.Namespace): command line arguments.

    Raises:
      BadConfigOption: if the options are invalid.
    """
        self._event_filter_expression = self.ParseStringOption(
            options, u'filter')
        if self._event_filter_expression:
            self._event_filter = filters_manager.FiltersManager.GetFilterObject(
                self._event_filter_expression)
            if not self._event_filter:
                raise errors.BadConfigOption(
                    u'Invalid filter expression: {0:s}'.format(
                        self._event_filter_expression))

        time_slice_event_time_string = getattr(options, u'slice', None)
        time_slice_duration = getattr(options, u'slice_size', 5)
        self._use_time_slicer = getattr(options, u'slicer', False)

        # The slice and slicer cannot be set at the same time.
        if time_slice_event_time_string and self._use_time_slicer:
            raise errors.BadConfigOption(
                u'Time slice and slicer cannot be used at the same time.')

        time_slice_event_timestamp = None
        if time_slice_event_time_string:
            preferred_time_zone = self._preferred_time_zone or u'UTC'
            timezone = pytz.timezone(preferred_time_zone)
            time_slice_event_timestamp = timelib.Timestamp.FromTimeString(
                time_slice_event_time_string, timezone=timezone)
            if time_slice_event_timestamp is None:
                raise errors.BadConfigOption(
                    u'Unsupported time slice event date and time: {0:s}'.
                    format(time_slice_event_time_string))

        if time_slice_event_timestamp is not None or self._use_time_slicer:
            # Note that time slicer uses the time slice to determine the duration.
            self._time_slice = time_slices.TimeSlice(
                time_slice_event_timestamp, duration=time_slice_duration)
Ejemplo n.º 4
0
    def ParseOptions(cls, options, configuration_object):
        """Parses and validates options.

    Args:
      options (argparse.Namespace): parser options.
      configuration_object (CLITool): object to be configured by the argument
          helper.

    Raises:
      BadConfigObject: when the configuration object is of the wrong type.
      BadConfigOption: when a configuration parameter fails validation.
    """
        if not isinstance(configuration_object, tools.CLITool):
            raise errors.BadConfigObject(
                'Configuration object is not an instance of CLITool')

        filter_expression = cls._ParseStringOption(options, 'filter')

        filter_object = None
        if filter_expression:
            filter_object = event_filter.EventObjectFilter()

            try:
                filter_object.CompileFilter(filter_expression)
            except errors.ParseError as exception:
                raise errors.BadConfigOption(
                    ('Unable to compile filter expression with error: '
                     '{0!s}').format(exception))

        time_slice_event_time_string = getattr(options, 'slice', None)
        time_slice_duration = getattr(options, 'slice_size', 5)
        use_time_slicer = getattr(options, 'slicer', False)

        # The slice and slicer cannot be set at the same time.
        if time_slice_event_time_string and use_time_slicer:
            raise errors.BadConfigOption(
                'Time slice and slicer cannot be used at the same time.')

        time_slice_event_timestamp = None
        if time_slice_event_time_string:
            # Note self._preferred_time_zone is None when not set but represents UTC.
            preferred_time_zone = getattr(
                configuration_object, '_preferred_time_zone', None) or 'UTC'
            timezone = pytz.timezone(preferred_time_zone)
            time_slice_event_timestamp = timelib.Timestamp.FromTimeString(
                time_slice_event_time_string, timezone=timezone)
            if time_slice_event_timestamp is None:
                raise errors.BadConfigOption(
                    'Unsupported time slice event date and time: {0:s}'.format(
                        time_slice_event_time_string))

        setattr(configuration_object, '_event_filter_expression',
                filter_expression)

        if filter_object:
            setattr(configuration_object, '_event_filter', filter_object)

        setattr(configuration_object, '_use_time_slicer', use_time_slicer)

        if time_slice_event_timestamp is not None or use_time_slicer:
            # Note that time slicer uses the time slice to determine the duration.
            # TODO: refactor TimeSlice to filters.
            time_slice = time_slices.TimeSlice(time_slice_event_timestamp,
                                               duration=time_slice_duration)
            setattr(configuration_object, '_time_slice', time_slice)
Ejemplo n.º 5
0
    def ParseOptions(cls, options, configuration_object):
        """Parses and validates options.

    Args:
      options (argparse.Namespace): parser options.
      configuration_object (CLITool): object to be configured by the argument
          helper.

    Raises:
      BadConfigObject: when the configuration object is of the wrong type.
      BadConfigOption: when a configuration parameter fails validation.
    """
        if not isinstance(configuration_object, tools.CLITool):
            raise errors.BadConfigObject(
                'Configuration object is not an instance of CLITool')

        filter_expression = cls._ParseStringOption(options, 'filter')

        filter_object = None
        if filter_expression:
            filter_object = event_filter.EventObjectFilter()

            try:
                filter_object.CompileFilter(filter_expression)
            except errors.ParseError as exception:
                raise errors.BadConfigOption(
                    ('Unable to compile filter expression with error: '
                     '{0!s}').format(exception))

        time_slice_event_time_string = getattr(options, 'slice', None)
        time_slice_duration = getattr(options, 'slice_size', 5)
        use_time_slicer = getattr(options, 'slicer', False)

        # The slice and slicer cannot be set at the same time.
        if time_slice_event_time_string and use_time_slicer:
            raise errors.BadConfigOption(
                'Time slice and slicer cannot be used at the same time.')

        time_slice_event_timestamp = None
        if time_slice_event_time_string:
            if ' ' in time_slice_event_time_string:
                raise errors.BadConfigOption(
                    'Time slice date and time must be defined in ISO 8601 format, '
                    'for example: 20200619T20:09:23+02:00.')

            date_time = dfdatetime_time_elements.TimeElements()

            try:
                date_time.CopyFromStringISO8601(time_slice_event_time_string)
            except ValueError:
                raise errors.BadConfigOption((
                    'Unsupported time slice date and time: {0:s}. The date and time '
                    'must be defined in ISO 8601 format, for example: '
                    '20200619T20:09:23+02:00'
                ).format(time_slice_event_time_string))

            # TODO: directly use dfDateTime objects in time slice.
            time_slice_event_timestamp = date_time.GetPlasoTimestamp()

        setattr(configuration_object, '_event_filter_expression',
                filter_expression)

        if filter_object:
            setattr(configuration_object, '_event_filter', filter_object)

        setattr(configuration_object, '_use_time_slicer', use_time_slicer)

        if time_slice_event_timestamp is not None or use_time_slicer:
            # Note that time slicer uses the time slice to determine the duration.
            # TODO: refactor TimeSlice to filters.
            time_slice = time_slices.TimeSlice(time_slice_event_timestamp,
                                               duration=time_slice_duration)
            setattr(configuration_object, '_time_slice', time_slice)
Ejemplo n.º 6
0
  def ParseOptions(cls, options, configuration_object):
    """Parses and validates options.

    Args:
      options (argparse.Namespace): parser options.
      configuration_object (CLITool): object to be configured by the argument
          helper.

    Raises:
      BadConfigObject: when the configuration object is of the wrong type.
      BadConfigOption: when a configuration parameter fails validation.
    """
    if not isinstance(configuration_object, tools.CLITool):
      raise errors.BadConfigObject(
          u'Configuration object is not an instance of CLITool')

    filter_expression = cls._ParseStringOption(options, u'filter')

    event_filter = None
    if filter_expression:
      event_filter = filters_manager.FiltersManager.GetFilterObject(
          filter_expression)
      if not event_filter:
        raise errors.BadConfigOption(u'Invalid filter expression: {0:s}'.format(
            filter_expression))

    time_slice_event_time_string = getattr(options, u'slice', None)
    time_slice_duration = getattr(options, u'slice_size', 5)
    use_time_slicer = getattr(options, u'slicer', False)

    # The slice and slicer cannot be set at the same time.
    if time_slice_event_time_string and use_time_slicer:
      raise errors.BadConfigOption(
          u'Time slice and slicer cannot be used at the same time.')

    time_slice_event_timestamp = None
    if time_slice_event_time_string:
      preferred_time_zone = getattr(
          configuration_object, u'_preferred_time_zone', u'UTC')
      timezone = pytz.timezone(preferred_time_zone)
      time_slice_event_timestamp = timelib.Timestamp.FromTimeString(
          time_slice_event_time_string, timezone=timezone)
      if time_slice_event_timestamp is None:
        raise errors.BadConfigOption(
            u'Unsupported time slice event date and time: {0:s}'.format(
                time_slice_event_time_string))

    setattr(configuration_object, u'_event_filter_expression',
            filter_expression)

    if event_filter:
      setattr(configuration_object, u'_event_filter', event_filter)

    setattr(configuration_object, u'_use_time_slicer', use_time_slicer)

    if time_slice_event_timestamp is not None or use_time_slicer:
      # Note that time slicer uses the time slice to determine the duration.
      # TODO: refactor TimeSlice to filters.
      time_slice = time_slices.TimeSlice(
          time_slice_event_timestamp, duration=time_slice_duration)
      setattr(configuration_object, u'_time_slice', time_slice)