Esempio n. 1
0
    def __init__(self, configuration_path=None, configuration_file=None,
                 defaults=None):
        raw_defaults = None
        if defaults:
            raw_defaults = {}
            for key, value in defaults.items():
                raw_defaults[key] = json.dumps(value)

        self._raw_config = ConfigParser.RawConfigParser(raw_defaults)
        self._configuration_path = configuration_path
        if configuration_path:
            configuration_segments = local_filesystem.getSegmentsFromRealPath(
                configuration_path)
            if not local_filesystem.isFile(configuration_segments):
                raise UtilsError(u'1011', _(
                    u'Configuration file "%s" does not exists.' % (
                        configuration_path)))
            try:
                self._configuration_file = (
                    local_filesystem.openFileForReading(
                         configuration_segments, utf8=True))
            except IOError:
                raise UtilsError(u'1012', _(
                    u'Server process could not read the configuration file '
                    u'"%s".' % (configuration_path))
                    )
        elif configuration_file:
            self._configuration_file = configuration_file
        else:
            raise AssertionError('You must specify a path or a file.')
Esempio n. 2
0
    def generate(self, key_type=crypto.TYPE_RSA, key_size=DEFAULT_KEY_SIZE):
        '''Create the key data.'''
        if key_type not in [crypto.TYPE_RSA, crypto.TYPE_DSA]:
            raise UtilsError(u'1003',
                _('Unknown key type "%s".' % (key_type)))

        key = None
        key_class = KEY_CLASSES[key_type]
        try:
            key = key_class.generate(bits=key_size, randfunc=rand.bytes)
        except ValueError, error:
            raise UtilsError(u'1004',
                _(u'Wrong key size "%d". %s.' % (key_size, unicode(error))))
Esempio n. 3
0
 def getEventGroupDefinition(self, name):
     """
     See `IEventsDefinition`.
     """
     try:
         event_group = self._group_definitions[name]
     except KeyError:
         raise UtilsError(u'1031',
             _('No EventGroupDefinition with name "%s"' % (name)))
     else:
         return event_group
Esempio n. 4
0
 def getEventDefinition(self, id):
     """
     See `IEventsDefinition`.
     """
     try:
         event_definition = self._event_definitions[id]
     except KeyError:
         raise UtilsError(u'1030',
             _('No EventDefinition with id "%s"' % (id)))
     else:
         return event_definition
Esempio n. 5
0
 def load(self):
     '''Load configuration from input file.'''
     try:
         self._raw_config.readfp(self._configuration_file)
     except (ConfigParser.ParsingError, AttributeError), error:
         self._configuration_file = None
         message = error.message
         if not isinstance(message, unicode):
             message = message.decode('utf-8')
         raise UtilsError(u'1002', _(
             u'Could not parse the configuration file. %s' % (message))
             )
Esempio n. 6
0
    def _fileRotateEachToHumanReadable(self, value):
        """
        Return the human readable representation for file_rotate_each
        tuple provided as `value'.

        (2, 's') returns 2 seconds
        """
        mapping = {
            u's': u'second',
            u'm': u'minute',
            u'h': u'hour',
            u'd': u'day',
            u'midnight': u'midnight',
            u'w0': u'monday',
            u'w1': u'tuesday',
            u'w2': u'wednesday',
            u'w3': u'thursday',
            u'w4': u'friday',
            u'w5': u'saturday',
            u'w6': u'sunday',
            }
        try:
            frequency = int(value[0])
        except ValueError:
            raise self._fileRotateEachError(
                _(u'Bad interval count. Got: "%s"' % (value[0])))

        if frequency < 0:
            raise self._fileRotateEachError(
                _(u'Interval should not be less than 0'))

        try:
            when = mapping[value[1]]
        except KeyError:
            raise self._fileRotateEachError(
                _(u'Unknown interval type. Got: "%s"' % (value[1])))

        return u'%s %s' % (frequency, when)
Esempio n. 7
0
 def _get(self, method, section, option, type_name=''):
     """
     Helper to get a value for a specific type.
     """
     try:
         return method(section, option)
     except ValueError, error:
         raise UtilsError(u'1000', _(
             u'Wrong %(type)s value for option "%(option)s" in '
             u'section "%(section)s". %(error_details)s' % {
                 'type': type_name,
                 'option': option,
                 'section': section,
                 'error_details': unicode(error),
                 }))
Esempio n. 8
0
 def _set(self, converter, section, option, value, type_name=''):
     """
     Helper to set a value for a specific type.
     """
     try:
         converted_value = converter(value)
     except (ValueError, TypeError), error:
         raise UtilsError(u'1001', _(
             u'Cannot set %(type)s value %(value)s for option %(option)s '
             u'in %(section)s. %(error)s') % {
                 'type': type_name,
                 'value': value,
                 'option': option,
                 'section': section,
                 'error': str(error),
                 }
             )
Esempio n. 9
0
 def _fileRotateEachError(self, details):
     return UtilsError(u'1023',
         _(u'Wrong value for logger rotation based on time interval. '
           u'%s' % (details)))
Esempio n. 10
0
    def _fileRotateEachToMachineReadable(self, value):
        """
        Return the machine readable format for `value`.

        Inside the configuration file, the value is stored as a human
        readable format like::

            1 hour | 2 seconds | 2 midnight | 3 Monday | Disabled

        When reading the value, it will return::

            (1, 'h') | (2, 's') | (2, 'midnight') | (3, 'w0') | None
        """

        mapping = {
            u'second': u's',
            u'seconds': u's',
            u'minute': u'm',
            u'minutes': u'm',
            u'hour': u'h',
            u'hours': u'h',
            u'day': u'd',
            u'days': u'd',
            u'midnight': u'midnight',
            u'midnights': u'midnight',
            u'monday': u'w0',
            u'mondays': u'w0',
            u'tuesday': u'w1',
            u'tuesdays': u'w1',
            u'wednesday': u'w2',
            u'wednesdays': u'w2',
            u'thursday': u'w3',
            u'thursdays': u'w3',
            u'friday': u'w4',
            u'fridays': u'w4',
            u'saturday': u'w5',
            u'saturdays': u'w5',
            u'sunday': u'w6',
            u'sundays': u'w6',
            }

        if not value:
            return None

        tokens = re.split('\W+', value)

        if len(tokens) != 2:
            raise self._fileRotateEachError(_(u'Got: "%s"' % (value)))

        try:
            interval = int(tokens[0])
        except ValueError:
            raise self._fileRotateEachError(
                _(u'Interval is not an integer. Got: "%s"' % (tokens[0])))

        if interval < 0:
            raise self._fileRotateEachError(
                _(u'Interval should not be less than 0'))

        when = tokens[1].lower()
        try:
            when = mapping[when]
        except KeyError:
            raise self._fileRotateEachError(
                _(u'Unknown interval type. Got: "%s"' % (tokens[1])))
        return (interval, when)