Esempio n. 1
0
 def test_derivation_dict_3(self):
     # But test it notices there is a dict already over intermediate type.
     DerivedType3 = (AbstractValueObject.derive_type(
         'DerivedType1',
         dict=True).derive_type('DerivedType2').derive_type('DerivedType3',
                                                            dict=True))
     DerivedType3().this_attribute_isnt_declared_but_there_is_a_dict = None
Esempio n. 2
0
 def test_derivation_dict_2(self):
     # Test that derivation mechanism gracefully ignores second `dict'.
     DerivedType2 = (AbstractValueObject.derive_type('DerivedType1',
                                                     dict=True).derive_type(
                                                         'DerivedType2',
                                                         dict=True))
     DerivedType2().this_attribute_isnt_declared_but_there_is_a_dict = None
Esempio n. 3
0
    def _generate_derived_type_dictionary (cls, options):
        allowed_values      = options.get ('allowed_values')
        allowed_value_types = options.get ('allowed_value_types')

        if allowed_value_types is not None:
            if not isinstance (allowed_value_types, tuple):
                allowed_value_types = (allowed_value_types,)

            for allowed_type in allowed_value_types:
                if not isinstance (allowed_type, ClassTypes):
                    raise TypeError ("'allowed_value_types' must be a tuple of types and classes")

        for attribute in (super (AbstractValueTrackingVariable, cls)
                          ._generate_derived_type_dictionary (options)):
            if attribute[0] not in ('get', 'set'):
                yield attribute

        functions        = {}
        object           = options.get ('object')
        filtered_options = AbstractValueObject._filter_options (options,
                                                                'cls',
                                                                'allowed_values',
                                                                'allowed_value_types',
                                                                'getter', 'setter',
                                                                'default_value')

        if allowed_values is not None or allowed_value_types is not None:
            if allowed_values is not None:
                if allowed_value_types is not None:
                    execute ('def is_allowed_value (self, value):\n'
                             '    return value in allowed_values\n'
                             '           and isinstance (value, allowed_value_types)',
                             filtered_options, functions)
                else:
                    execute ('def is_allowed_value (self, value):\n'
                             '    return value in allowed_values\n',
                             filtered_options, functions)
            else:
                execute ('def is_allowed_value (self, value):\n'
                         '    return isinstance (value, allowed_value_types)',
                         filtered_options, functions)

        if 'getter' in options:
            if object is not None:
                execute (('def __init__(self, %s):\n'
                          '    cls.__init__(self, getter (%s))\n'
                          '    %s = %s')
                         % (object, object, AbstractValueObject._get_object (options), object),
                         filtered_options, functions)
            else:
                execute ('def __init__(self):\n'
                         '    cls.__init__(self, getter (self))\n',
                         filtered_options, functions)

            execute (('def resynchronize_with_backend (self):\n'
                      '    self._set (getter (%s))')
                     % AbstractValueObject._get_object (options),
                     filtered_options, functions)

        else:
            if 'default_value' in options:
                if (    'is_allowed_value' in functions
                    and not functions['is_allowed_value'] (None, options['default_value'])):
                   raise ValueError ("'default_value' of '%s' is not within allowed value set"
                                     % (options['default_value'],))

                initial_default = ' = default_value'

            # Since our is_allowed_value() implementation never accesses `self', it is OK
            # to pass None as that.
            elif 'is_allowed_value' not in functions or functions['is_allowed_value'] (None, None):
                initial_default = ' = None'

            else:
                initial_default = ''

            if 'setter' in options:
                setter_statement = ('setter (%s, initial_value)'
                                    % AbstractValueObject._get_object (options))
            else:
                setter_statement = ''

            if object is not None:
                execute (('def __init__(self, %s, initial_value%s):\n'
                          '    cls.__init__(self, initial_value)\n'
                          '    %s = %s\n'
                          '    %s\n')
                         % (object, initial_default,
                            AbstractValueObject._get_object (options), object, setter_statement),
                         filtered_options, functions)
            else:
                execute (('def __init__(self, initial_value%s):\n'
                          '    cls.__init__(self, initial_value)\n'
                          '    %s\n')
                         % (initial_default, setter_statement),
                         filtered_options, functions)

        if 'setter' in options:
            execute (('def _set (self, value):\n'
                      '    if self.get () != value:\n'
                      '        if not self.is_allowed_value (value):\n'
                      '            raise ValueError \\\n'
                      '                ("\'%%s\' is not allowed as value of the variable" %% value)\n'
                      '        setter (%s, value)\n'
                      '        self._AbstractValueTrackingVariable__value = value\n'
                      '        return self._value_changed (value)\n'
                      '    else:\n'
                      '        return False')
                     % AbstractValueObject._get_object (options),
                     filtered_options, functions)

            execute ('def set (self, value): return self._set (value)', functions)

        for function in functions.items ():
            yield function
Esempio n. 4
0
    def _generate_derived_type_dictionary(cls, options):
        allowed_values = options.get('allowed_values')
        allowed_value_types = options.get('allowed_value_types')

        if allowed_value_types is not None:
            if not isinstance(allowed_value_types, tuple):
                allowed_value_types = (allowed_value_types, )

            for allowed_type in allowed_value_types:
                if not isinstance(allowed_type, ClassTypes):
                    raise TypeError(
                        "'allowed_value_types' must be a tuple of types and classes"
                    )

        for attribute in (super(
                AbstractValueTrackingVariable,
                cls)._generate_derived_type_dictionary(options)):
            if attribute[0] not in ('get', 'set'):
                yield attribute

        functions = {}
        object = options.get('object')
        filtered_options = AbstractValueObject._filter_options(
            options, 'cls', 'allowed_values', 'allowed_value_types', 'getter',
            'setter', 'default_value')

        if allowed_values is not None or allowed_value_types is not None:
            if allowed_values is not None:
                if allowed_value_types is not None:
                    execute(
                        'def is_allowed_value (self, value):\n'
                        '    return value in allowed_values\n'
                        '           and isinstance (value, allowed_value_types)',
                        filtered_options, functions)
                else:
                    execute(
                        'def is_allowed_value (self, value):\n'
                        '    return value in allowed_values\n',
                        filtered_options, functions)
            else:
                execute(
                    'def is_allowed_value (self, value):\n'
                    '    return isinstance (value, allowed_value_types)',
                    filtered_options, functions)

        if 'getter' in options:
            if object is not None:
                execute(('def __init__(self, %s):\n'
                         '    cls.__init__(self, getter (%s))\n'
                         '    %s = %s') %
                        (object, object,
                         AbstractValueObject._get_object(options), object),
                        filtered_options, functions)
            else:
                execute(
                    'def __init__(self):\n'
                    '    cls.__init__(self, getter (self))\n',
                    filtered_options, functions)

            execute(('def resynchronize_with_backend (self):\n'
                     '    self._set (getter (%s))') %
                    AbstractValueObject._get_object(options), filtered_options,
                    functions)

        else:
            if 'default_value' in options:
                if ('is_allowed_value' in functions
                        and not functions['is_allowed_value'](
                            None, options['default_value'])):
                    raise ValueError(
                        "'default_value' of '%s' is not within allowed value set"
                        % (options['default_value'], ))

                initial_default = ' = default_value'

            # Since our is_allowed_value() implementation never accesses `self', it is OK
            # to pass None as that.
            elif 'is_allowed_value' not in functions or functions[
                    'is_allowed_value'](None, None):
                initial_default = ' = None'

            else:
                initial_default = ''

            if 'setter' in options:
                setter_statement = ('setter (%s, initial_value)' %
                                    AbstractValueObject._get_object(options))
            else:
                setter_statement = ''

            if object is not None:
                execute(
                    ('def __init__(self, %s, initial_value%s):\n'
                     '    cls.__init__(self, initial_value)\n'
                     '    %s = %s\n'
                     '    %s\n') % (object, initial_default,
                                    AbstractValueObject._get_object(options),
                                    object, setter_statement),
                    filtered_options, functions)
            else:
                execute(('def __init__(self, initial_value%s):\n'
                         '    cls.__init__(self, initial_value)\n'
                         '    %s\n') % (initial_default, setter_statement),
                        filtered_options, functions)

        if 'setter' in options:
            execute((
                'def _set (self, value):\n'
                '    if self.get () != value:\n'
                '        if not self.is_allowed_value (value):\n'
                '            raise ValueError \\\n'
                '                ("\'%%s\' is not allowed as value of the variable" %% value)\n'
                '        setter (%s, value)\n'
                '        self._AbstractValueTrackingVariable__value = value\n'
                '        return self._value_changed (value)\n'
                '    else:\n'
                '        return False') %
                    AbstractValueObject._get_object(options), filtered_options,
                    functions)

            execute('def set (self, value): return self._set (value)',
                    functions)

        for function in functions.items():
            yield function
Esempio n. 5
0
    def _generate_derived_type_dictionary(cls, options):
        for attribute in (super(
                AbstractStateTrackingCondition,
                cls)._generate_derived_type_dictionary(options)):
            if attribute[0] not in ('get', 'set'):
                yield attribute

        functions = {}
        object = options.get('object')
        filtered_options = AbstractValueObject._filter_options(
            options, 'cls', 'getter', 'setter')

        if 'getter' in options:
            if object is not None:
                execute(('def __init__(self, %s):\n'
                         '    cls.__init__(self, getter (%s))\n'
                         '    %s = %s') %
                        (object, object,
                         AbstractValueObject._get_object(options), object),
                        filtered_options, functions)
            else:
                execute(
                    'def __init__(self):\n'
                    '    cls.__init__(self, getter (self))\n',
                    filtered_options, functions)

            execute(('def resynchronize_with_backend (self):\n'
                     '    self._set (getter (%s))') %
                    AbstractValueObject._get_object(options), filtered_options,
                    functions)

        else:
            if 'setter' in options:
                setter_statement = ('setter (%s, initial_state)' %
                                    AbstractValueObject._get_object(options))
            else:
                setter_statement = ''

            if object is not None:
                execute(('def __init__(self, %s, initial_state):\n'
                         '    cls.__init__(self, initial_state)\n'
                         '    %s = %s\n'
                         '    %s\n') %
                        (object, AbstractValueObject._get_object(options),
                         object, setter_statement), filtered_options,
                        functions)
            else:
                execute(('def __init__(self, initial_state):\n'
                         '    cls.__init__(self, initial_state)\n'
                         '    %s\n') % setter_statement, filtered_options,
                        functions)

        if 'setter' in options:
            execute((
                'def _set (self, value):\n'
                '    state = bool (value)\n'
                '    if self.get () != state:\n'
                '        setter (%s, state)\n'
                '        self._AbstractStateTrackingCondition__state = state\n'
                '        return self._value_changed (state)\n'
                '    else:\n'
                '        return False') %
                    AbstractValueObject._get_object(options), filtered_options,
                    functions)

            execute('def set (self, value): return self._set (value)',
                    functions)

        for function in functions.items():
            yield function
Esempio n. 6
0
 def test_derivation_dict_1 (self):
     DerivedType1 = AbstractValueObject.derive_type ('DerivedType1', dict = True)
     DerivedType1 ().this_attribute_isnt_declared_but_there_is_a_dict = None
Esempio n. 7
0
 def test_derivation_slots (self):
     DerivedType = AbstractValueObject.derive_type ('DerivedType')
     self.assertRaises (AttributeError, self.non_existing_attribute_setter (DerivedType ()))
Esempio n. 8
0
    def _generate_derived_type_dictionary (cls, options):
        for attribute in (super (AbstractStateTrackingCondition, cls)
                          ._generate_derived_type_dictionary (options)):
            if attribute[0] not in ('get', 'set'):
                yield attribute

        functions        = {}
        object           = options.get ('object')
        filtered_options = AbstractValueObject._filter_options (options, 'cls', 'getter', 'setter')

        if 'getter' in options:
            if object is not None:
                execute (('def __init__(self, %s):\n'
                          '    cls.__init__(self, getter (%s))\n'
                          '    %s = %s')
                         % (object, object, AbstractValueObject._get_object (options), object),
                         filtered_options, functions)
            else:
                execute ('def __init__(self):\n'
                         '    cls.__init__(self, getter (self))\n',
                         filtered_options, functions)

            execute (('def resynchronize_with_backend (self):\n'
                      '    self._set (getter (%s))')
                     % AbstractValueObject._get_object (options),
                     filtered_options, functions)

        else:
            if 'setter' in options:
                setter_statement = ('setter (%s, initial_state)'
                                    % AbstractValueObject._get_object (options))
            else:
                setter_statement = ''

            if object is not None:
                execute (('def __init__(self, %s, initial_state):\n'
                          '    cls.__init__(self, initial_state)\n'
                          '    %s = %s\n'
                          '    %s\n')
                         % (object, AbstractValueObject._get_object (options), object,
                            setter_statement),
                         filtered_options, functions)
            else:
                execute (('def __init__(self, initial_state):\n'
                          '    cls.__init__(self, initial_state)\n'
                          '    %s\n')
                         % setter_statement,
                         filtered_options, functions)

        if 'setter' in options:
            execute (('def _set (self, value):\n'
                      '    state = bool (value)\n'
                      '    if self.get () != state:\n'
                      '        setter (%s, state)\n'
                      '        self._AbstractStateTrackingCondition__state = state\n'
                      '        return self._value_changed (state)\n'
                      '    else:\n'
                      '        return False')
                     % AbstractValueObject._get_object (options),
                     filtered_options, functions)

            execute ('def set (self, value): return self._set (value)', functions)

        for function in functions.items ():
            yield function
Esempio n. 9
0
 def test_derivation_dict_1(self):
     DerivedType1 = AbstractValueObject.derive_type('DerivedType1',
                                                    dict=True)
     DerivedType1().this_attribute_isnt_declared_but_there_is_a_dict = None
Esempio n. 10
0
 def test_derivation_slots(self):
     DerivedType = AbstractValueObject.derive_type('DerivedType')
     self.assertRaises(AttributeError,
                       self.non_existing_attribute_setter(DerivedType()))