Exemplo n.º 1
0
    def _init_subclass(cls,
                       serializer: str = None,
                       namespace: str = None,
                       include_metadata: bool = None,
                       isodates: bool = None,
                       abstract: bool = False,
                       allow_blessed_key: bool = None,
                       decimals: bool = None) -> None:
        if abstract:
            # Custom base classes can set this to skip class initialization.
            cls.__is_abstract__ = True
            return
        cls.__is_abstract__ = False

        # Can set serializer/namespace/etc. using:
        #    class X(Record, serializer='json', namespace='com.vandelay.X'):
        #        ...
        try:
            custom_options = cls.Options
        except AttributeError:
            custom_options = None
        else:
            delattr(cls, 'Options')
        options = getattr(cls, '_options', None)
        if options is None:
            options = ModelOptions()
        else:
            options = options.clone_defaults()
        if custom_options:
            options.__dict__.update(custom_options.__dict__)
        if serializer is not None:
            options.serializer = serializer
        if include_metadata is not None:
            options.include_metadata = include_metadata
        if isodates is not None:
            options.isodates = isodates
        if decimals is not None:
            options.decimals = decimals
        if allow_blessed_key is not None:
            options.allow_blessed_key = allow_blessed_key
        options.namespace = namespace or canoname(cls)

        # Add introspection capabilities
        cls._contribute_to_options(options)
        # Add FieldDescriptors for every field.
        cls._contribute_field_descriptors(cls, options)

        # Store options on new subclass.
        cls._options = options

        cls._contribute_methods()

        # Register in the global registry, so we can look up
        # models by namespace.
        registry[options.namespace] = cls

        cls._model_init = cls._BUILD_init()
        if '__init__' not in cls.__dict__:
            cls.__init__ = cls._model_init
Exemplo n.º 2
0
def test_canoname():
    class X:
        ...

    X.__module__ = "__main__"
    x = X()

    class Y:
        ...

    y = Y()

    assert canoname(X, main_name="faust") == "faust.test_canoname.<locals>.X"
    assert canoname(x, main_name="faust") == "faust.test_canoname.<locals>.X"
    assert canoname(Y, main_name="faust") == ".".join([
        __name__,
        "test_canoname.<locals>.Y",
    ])
    assert canoname(y, main_name="faust") == ".".join([
        __name__,
        "test_canoname.<locals>.Y",
    ])
Exemplo n.º 3
0
def test_canoname():
    class X:
        ...

    X.__module__ = '__main__'
    x = X()

    class Y:
        ...

    y = Y()

    assert canoname(X, main_name='faust') == 'faust.test_canoname.<locals>.X'
    assert canoname(x, main_name='faust') == 'faust.test_canoname.<locals>.X'
    assert canoname(Y, main_name='faust') == '.'.join([
        __name__,
        'test_canoname.<locals>.Y',
    ])
    assert canoname(y, main_name='faust') == '.'.join([
        __name__,
        'test_canoname.<locals>.Y',
    ])
Exemplo n.º 4
0
 def __init__(self,
              fun: AgentFun,
              *,
              app: AppT,
              name: str = None,
              channel: Union[str, ChannelT] = None,
              concurrency: int = 1,
              sink: Iterable[SinkT] = None,
              on_error: AgentErrorHandler = None,
              supervisor_strategy: Type[SupervisorStrategyT] = None,
              help: str = None,
              key_type: ModelArg = None,
              value_type: ModelArg = None,
              isolated_partitions: bool = False,
              **kwargs: Any) -> None:
     self.app = app
     self.fun: AgentFun = fun
     self.name = name or canoname(self.fun)
     # key-type/value_type arguments only apply when a channel
     # is not set
     if key_type is not None:
         assert channel is None or isinstance(channel, str)
     self._key_type = key_type
     if value_type is not None:
         assert channel is None or isinstance(channel, str)
     self._value_type = value_type
     self._channel_arg = channel
     self._channel_kwargs = kwargs
     self.concurrency = concurrency or 1
     self.isolated_partitions = isolated_partitions
     self.help = help or ''
     self._sinks = list(sink) if sink is not None else []
     self._on_error: Optional[AgentErrorHandler] = on_error
     self.supervisor_strategy = supervisor_strategy
     self._actors = WeakSet()
     self._actor_by_partition = WeakValueDictionary()
     if self.isolated_partitions and self.concurrency > 1:
         raise ImproperlyConfigured(
             'Agent concurrency must be 1 when using isolated partitions')
     ServiceProxy.__init__(self)
Exemplo n.º 5
0
    def _init_subclass(
        cls,
        serializer: str = None,
        namespace: str = None,
        include_metadata: bool = None,
        isodates: bool = None,
        abstract: bool = False,
        allow_blessed_key: bool = None,
        decimals: bool = None,
        coerce: bool = None,
        coercions: CoercionMapping = None,
        polymorphic_fields: bool = None,
        validation: bool = None,
        date_parser: Callable[[Any], datetime] = None,
    ) -> None:
        # Can set serializer/namespace/etc. using:
        #    class X(Record, serializer='json', namespace='com.vandelay.X'):
        #        ...
        try:
            custom_options = cls.Options
        except AttributeError:
            custom_options = None
        else:
            delattr(cls, "Options")
        options = getattr(cls, "_options", None)
        if options is None:
            options = ModelOptions()
            options.coercions = {}
            options.defaults = {}
        else:
            options = options.clone_defaults()
        if custom_options:
            options.__dict__.update(custom_options.__dict__)
        if coerce is not None:
            options.coerce = coerce
        if coercions is not None:
            options.coercions.update(coercions)
        if serializer is not None:
            options.serializer = serializer
        if include_metadata is not None:
            options.include_metadata = include_metadata
        if isodates is not None:
            options.isodates = isodates
        if decimals is not None:
            options.decimals = decimals
        if allow_blessed_key is not None:
            options.allow_blessed_key = allow_blessed_key
        if polymorphic_fields is not None:
            options.polymorphic_fields = polymorphic_fields
        if validation is not None:
            options.validation = validation
            options.coerce = True  # validation implies coerce
        if date_parser is not None:
            options.date_parser = date_parser

        options.namespace = namespace or canoname(cls)

        if abstract:
            # Custom base classes can set this to skip class initialization.
            cls.__is_abstract__ = True
            cls._options = options
            cls.__init__ = cls.__abstract_init__  # type: ignore
            return
        cls.__is_abstract__ = False

        # Add introspection capabilities
        cls._contribute_to_options(options)
        # Add FieldDescriptors for every field.
        options.descriptors = cls._contribute_field_descriptors(cls, options)

        # Store options on new subclass.
        cls._options = options

        cls._contribute_methods()

        # Register in the global registry, so we can look up
        # models by namespace.
        registry[options.namespace] = cls

        codegens = [
            ("__init__", cls._BUILD_init, "_model_init"),
            ("__hash__", cls._BUILD_hash, "_model_hash"),
            ("__eq__", cls._BUILD_eq, "_model_eq"),
            ("__ne__", cls._BUILD_ne, "_model_ne"),
            ("__gt__", cls._BUILD_gt, "_model_gt"),
            ("__ge__", cls._BUILD_ge, "_model_ge"),
            ("__lt__", cls._BUILD_lt, "_model_lt"),
            ("__le__", cls._BUILD_le, "_model_le"),
        ]

        for meth_name, meth_gen, attr_name in codegens:
            # self._model_init = cls._BUILD_init()
            # if '__init__' not in cls.__dict__:
            #     cls.__init__ = self._model_init
            meth = meth_gen()
            setattr(cls, attr_name, meth)
            if meth_name not in cls.__dict__:
                setattr(cls, meth_name, meth)