示例#1
0
 def __call__(self, value):
     if value is None:
         return None
     try:
         value = re.compile(six.text_type(value))
     except re.error as error:
         raise ValueError('{}: {}'.format(
             six.text_type(error).capitalize(), value))
     return value
    def __call__(self, o):

        if isfunction(o):
            # We must wait to finalize configuration as the class containing this function is under construction
            # at the time this call to decorate a member function. This will be handled in the call to
            # o.ConfigurationSettings.fix_up(o) in the elif clause of this code block.
            o._settings = self.settings
        elif isclass(o):

            # Set command name

            name = o.__name__
            if name.endswith('Command'):
                name = name[:-len('Command')]
            o.name = six.text_type(name.lower())

            # Construct ConfigurationSettings instance for the command class

            o.ConfigurationSettings = ConfigurationSettingsType(
                module=o.__module__ + '.' + o.__name__,
                name='ConfigurationSettings',
                bases=(o.ConfigurationSettings,))

            ConfigurationSetting.fix_up(o.ConfigurationSettings, self.settings)
            o.ConfigurationSettings.fix_up(o)
            Option.fix_up(o)
        else:
            raise TypeError('Incorrect usage: Configuration decorator applied to {0}'.format(type(o), o.__name__))

        return o
示例#3
0
 def __call__(self, value):
     if not (value is None or isinstance(value, bool)):
         value = six.text_type(value).lower()
         if value not in Boolean.truth_values:
             raise ValueError('Unrecognized truth value: {0}'.format(value))
         value = Boolean.truth_values[value]
     return value
示例#4
0
 def __call__(self, value):
     if value is None:
         return None
     value = six.text_type(value)
     if value not in self.membership:
         raise ValueError('Unrecognized value: {}'.format(value))
     return value
示例#5
0
 def __call__(self, value):
     if value is not None:
         value = six.text_type(value)
         if OptionName.pattern.match(value) is None:
             raise ValueError(
                 'Illegal characters in option name: {}'.format(value))
     return value
示例#6
0
 def __call__(self, value):
     if value is None:
         return None
     try:
         return Code.object(compile(value, 'string', self._mode),
                            six.text_type(value))
     except (SyntaxError, TypeError) as error:
         raise ValueError(error.message)
示例#7
0
 def __call__(self, value):
     if value is None:
         return None
     value = six.text_type(value)
     if self.pattern.match(value) is None:
         raise ValueError('Expected {}, not {}'.format(
             self.name, json_encode_string(value)))
     return value
 def execute(self):
     # noinspection PyBroadException
     try:
         if self._argv is None:
             self._argv = os.path.splitext(os.path.basename(self._path))[0]
         self._execute(self._path, self._argv, self._environ)
     except:
         error_type, error, tb = sys.exc_info()
         message = 'Command execution failed: ' + six.text_type(error)
         self._logger.error(message + '\nTraceback:\n' +
                            ''.join(traceback.format_tb(tb)))
         sys.exit(1)
    def __init__(self, path, argv=None, environ=None):

        if not isinstance(path, (bytes, six.text_type)):
            raise ValueError('Expected a string value for path, not {}'.format(
                repr(path)))

        self._logger = getLogger(self.__class__.__name__)
        self._path = six.text_type(path)
        self._argv = None
        self._environ = None

        self.argv = argv
        self.environ = environ
示例#10
0
    def prepare(self):

        phase = self.phase

        if phase == 'map':
            # noinspection PyUnresolvedReferences
            self._configuration = self.map.ConfigurationSettings(self)
            return

        if phase == 'reduce':
            streaming_preop = chain(
                (self.name, 'phase="map"', str(self._options)),
                self.fieldnames)
            self._configuration.streaming_preop = ' '.join(streaming_preop)
            return

        raise RuntimeError('Unrecognized reporting command phase: {}'.format(
            json_encode_string(six.text_type(phase))))
示例#11
0
    def __call__(self, value):

        if value is None:
            return value

        path = six.text_type(value)

        if not os.path.isabs(path):
            path = os.path.join(self.directory, path)

        try:
            value = open(path, self.mode) if self.buffering is None else open(
                path, self.mode, self.buffering)
        except IOError as error:
            raise ValueError(
                'Cannot open {0} with mode={1} and buffering={2}: {3}'.format(
                    value, self.mode, self.buffering, error))

        return value
示例#12
0
    def fix_up(cls, values):

        is_configuration_setting = lambda attribute: isinstance(attribute, ConfigurationSetting)
        definitions = getmembers(cls, is_configuration_setting)
        i = 0

        for name, setting in definitions:

            if setting._name is None:
                setting._name = name = six.text_type(name)
            else:
                name = setting._name

            validate, specification = setting._get_specification()
            backing_field_name = '_' + name

            if setting.fget is None and setting.fset is None and setting.fdel is None:

                value = setting._value

                if setting._readonly or value is not None:
                    validate(specification, name, value)

                def fget(bfn, value):
                    return lambda this: getattr(this, bfn, value)

                setting = setting.getter(fget(backing_field_name, value))

                if not setting._readonly:

                    def fset(bfn, validate, specification, name):
                        return lambda this, value: setattr(this, bfn, validate(specification, name, value))

                    setting = setting.setter(fset(backing_field_name, validate, specification, name))

                setattr(cls, name, setting)

            def is_supported_by_protocol(supporting_protocols):

                def is_supported_by_protocol(version):
                    return version in supporting_protocols

                return is_supported_by_protocol

            del setting._name, setting._value, setting._readonly

            setting.is_supported_by_protocol = is_supported_by_protocol(specification.supporting_protocols)
            setting.supporting_protocols = specification.supporting_protocols
            setting.backing_field_name = backing_field_name
            definitions[i] = setting
            setting.name = name

            i += 1

            try:
                value = values[name]
            except KeyError:
                continue

            if setting.fset is None:
                raise ValueError('The value of configuration setting {} is fixed'.format(name))

            setattr(cls, backing_field_name, validate(specification, name, value))
            del values[name]

        if len(values) > 0:
            settings = sorted(list(six.iteritems(values)))
            settings = imap(lambda n_v: '{}={}'.format(n_v[0], repr(n_v[1])), settings)
            raise AttributeError('Inapplicable configuration settings: ' + ', '.join(settings))

        cls.configuration_setting_definitions = definitions
示例#13
0
 def format(self, value):
     return None if value is None else six.text_type(value)
示例#14
0
 def __init__(self, name, pattern, flags=0):
     self.name = six.text_type(name)
     self.pattern = re.compile(pattern, flags)