Skip to content

Commit

Permalink
feat: no idea how I got here
Browse files Browse the repository at this point in the history
  • Loading branch information
raozixuan committed Feb 4, 2022
1 parent 310cab8 commit 8b4fe19
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 80 deletions.
6 changes: 5 additions & 1 deletion onto/attrs/attribute_new.py
Expand Up @@ -294,10 +294,14 @@ def _graphql_field_constructor(self):
from functools import partial
field_base = graphql.GraphQLField

def resolve_info(obj, context):
from graphql import GraphQLResolveInfo
def resolve_info(obj, context: GraphQLResolveInfo, **kwargs):
value = getattr(obj, self.properties.name, None)
if isinstance(value, enum.Enum):
value = value.value
elif isinstance(value, Callable):
f = value
value = f(obj, **kwargs)
return value

# def resolver(attributed, resolve_info):
Expand Down
72 changes: 52 additions & 20 deletions onto/attrs/unit.py
@@ -1,6 +1,5 @@
import contextlib


# Attribute
# - Serialization/Deserialization instructions
# - Validation
Expand All @@ -13,6 +12,7 @@

from onto.common import _NA


class _ModelRegistry(type):
"""
Expand All @@ -36,7 +36,7 @@ def __new__(mcs, name, bases, attrs):
if new_cls.__name__ in mcs._REGISTRY:
raise ValueError(
"Class with name {} is declared more than once. "
.format(new_cls.__name__)
.format(new_cls.__name__)
)
mcs._REGISTRY[new_cls.__name__] = new_cls

Expand Down Expand Up @@ -90,11 +90,11 @@ def _get_root(cls):

# def __mul__(cls, cls_b):
# import types
# cls_r = types.new_class(
# name=cls.__name__ + cls_b.__name__,
# bases=(cls, cls_b),
# )
# return cls_r
# cls_r = types.new_class(
# name=cls.__name__ + cls_b.__name__,
# bases=(cls, cls_b),
# )
# return cls_r

@property
def properties(self):
Expand Down Expand Up @@ -130,6 +130,7 @@ def __getattr__(self, item):

def output_wrapper(decorated):
return self_cls(decor=decorated)

try:
f = decorator_cls.easy(decor=decor, output_wrapper=output_wrapper)
except AttributeError:
Expand All @@ -143,19 +144,20 @@ def descendant_of(self, cs):


class MonadContext(Monad, contextlib.ContextDecorator):

stack = list()

@classmethod
def context(cls):
return cls(decor=root_decor.get())

def __enter__(self):
self.stack.append( root_decor.set(self.decor) )
self.stack.append(root_decor.set(self.decor))

def __exit__(self, exc_type, exc_val, exc_tb):
root_decor.reset(self.stack.pop())
return False


#
#
# class CurSelfContext(contextlib.ContextDecorator):
Expand Down Expand Up @@ -195,6 +197,7 @@ def _constructor(_self):
)

return field_obj

return _constructor

@property
Expand All @@ -210,20 +213,24 @@ def _marshmallow_field_cls(self):
def _marshmallow_field_override(self):
yield from ()


class GraphqlCapableMixin:

@property
def _graphql_object_type(self):
yield from ()


class DefaultDecoratorMixin(MarshmallowCapableBaseMixin, GraphqlCapableMixin):
is_internal = False


import contextvars

root_decor = contextvars.ContextVar('root_decor', default=DefaultDecoratorMixin())
cur_self = contextvars.ContextVar('cur_self', default=None)


def whichever_order(li: list, operation):
"""
Hack: try operation on all permutative orders of li until the one order does not trigger an error
Expand All @@ -240,6 +247,7 @@ def whichever_order(li: list, operation):
else:
raise Exception(f'All possible combinations failed {str(errors)}')


class DecoratorBase(metaclass=_ModelRegistry):
"""
Can only create new state in self.
Expand Down Expand Up @@ -326,6 +334,7 @@ def op(typ):

return whichever_order(typ, op)


# _vals_

# class BindClass_(DecoratorBase):
Expand Down Expand Up @@ -360,7 +369,7 @@ def __init__(self, annotation, *args, decorated, **kwargs):
if origin is list:
if arguments := typing.get_args(annotation):
element_type = next(iter(arguments))
if element_type in (str, bool, float, int, ):
if element_type in (str, bool, float, int,):
decorated = List(value=lambda a: a.of_type(element_type), decorated=decorated)
else:
decorated = OfType(type_cls=origin, decorated=decorated)
Expand All @@ -381,7 +390,6 @@ def __init__(self):


class Nothing(DecoratorBase):

"""
Use attr.nothing
Expand All @@ -393,7 +401,6 @@ class A:
b.name == c.name == 'c'
"""


@classmethod
def easy(cls, *args, **kwargs):
return cls.easy_property(*args, **kwargs)
Expand All @@ -414,13 +421,13 @@ def _marshmallow_field_kwargs(self):
yield from self.decorated._marshmallow_field_kwargs
yield 'missing', self.default_value


# class DefaultParamsMixin:
# import_enabled = True
# export_enabled = True


class ImportRequired(DecoratorBase):

import_required = True

@property
Expand Down Expand Up @@ -458,8 +465,8 @@ def _graphql_object_type(self):
yield from self.decorated._graphql_object_type
# pass

class Optional(DecoratorBase):

class Optional(DecoratorBase):
import_required = False
export_required = False

Expand Down Expand Up @@ -642,7 +649,6 @@ class Int(Integer):


class IntegerTimestamp(Integer):

_long_type = None

@classmethod
Expand Down Expand Up @@ -677,6 +683,7 @@ def easy(cls, *args, **kwargs):
def __init__(self, *args, **kwargs):
super().__init__(*args, type_cls=bool, **kwargs)


#
# import contextvars
# current_self = contextvars.ContextVar('current_self', default=list()) # TODO: note mutable default
Expand All @@ -699,6 +706,7 @@ def _get_default_fget(self, *, name):
def fget(_self_obj):
inner = getattr(_self_obj, _ATTRIBUTE_STORE_NAME)
return getattr(inner, name)

return fget

def __init__(self, fget=_NA, *args, **kwargs):
Expand All @@ -718,6 +726,7 @@ def _get_default_fset(self, *, name):
def fset(_self_obj, value):
inner = getattr(_self_obj, _ATTRIBUTE_STORE_NAME)
return setattr(inner, name, value)

return fset

def __init__(self, fset=_NA, *args, **kwargs):
Expand All @@ -737,6 +746,7 @@ def _get_default_fdel(self, *, name):
def fdel(_self_obj):
inner = getattr(_self_obj, _ATTRIBUTE_STORE_NAME)
return delattr(inner, name)

return fdel

def __init__(self, fdel=_NA, *args, **kwargs):
Expand Down Expand Up @@ -777,20 +787,21 @@ def make_init(self, name=_NA):
def _init(_self_obj):
inner = getattr(_self_obj, _ATTRIBUTE_STORE_NAME)
return setattr(inner, name, self._easy_initializer(_self_obj))

return _init

def __init__(self, easy_initializer, *args, **kwargs):
super().__init__(*args, **kwargs)
self._easy_initializer = easy_initializer



class Dict(Init):

def __init__(self, *args, **kwargs):
def _dict_initializer(_self):
attr_name = self.name # TODO: fix
setattr(_self, attr_name, dict())

super().__init__(
*args,
initializer=_dict_initializer,
Expand Down Expand Up @@ -829,7 +840,6 @@ def _graphql_object_type(self):


class Enum(OfType):

from enum import Enum as _Enum

def __init__(self, enum_cls: typing.Type[_Enum], *args, **kwargs):
Expand Down Expand Up @@ -872,6 +882,7 @@ def new(cls, *args, **kwargs):
"""
Dispatch to subclass when required
"""

def fget(self):
return self.doc_ref.id

Expand Down Expand Up @@ -899,7 +910,6 @@ def _marshmallow_field_cls(self):
return fields.DocIdField



class NodeId(DecoratorBase):
"""
For GraphQL ID
Expand Down Expand Up @@ -964,12 +974,14 @@ class NoneAsMissing(DecoratorBase):
@property
def _marshmallow_field_override(self):
yield from self.decorated._marshmallow_field_override

def _deserialize(_self, value, attr, data, **kwargs):
if value is None:
from marshmallow.fields import missing_
return missing_
else:
return super(_self.__class__, _self)._deserialize(value, attr, data, **kwargs)

yield ('_deserialize', _deserialize)

@property
Expand Down Expand Up @@ -1085,7 +1097,27 @@ def _graphql_field_kwargs(self):
yield 'description', self.doc


class AsRoot(DecoratorBase):
class Executable(Getter):
"""
可以被执行的 attribute 用于 graphql
"""

is_root = True
def __init__(self, f=_NA, *args, **kwargs):
self._f = f
super().__init__(lambda *_: f, *args, **kwargs)

def _make_graphql_args(self):
from onto.sink.graphql import GraphQLSink
parameters = GraphQLSink._parameters_for(self._f)
for name, annotated_type in parameters:
yield name, GraphQLSink._param_to_graphql_arg(annotated_type=annotated_type)
yield from ()

@property
def _graphql_field_kwargs(self):
yield from self.decorated._graphql_field_kwargs
yield 'args', dict(self._make_graphql_args())


class AsRoot(DecoratorBase):
is_root = True
35 changes: 19 additions & 16 deletions onto/database/kafka.py
Expand Up @@ -36,35 +36,38 @@ def _document_path(self):
return str(self)


class KafkaDatabase(Database):
class KafkaReadDatabase(Database):

@classmethod
def listener(cls):
from onto.database.utils import GenericListener
return GenericListener

bootstrap_servers = None

d = dict()

@classmethod
@functools.lru_cache(maxsize=None)
def kafka_producer(cls) -> kafka.KafkaProducer:
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers=[cls.bootstrap_servers])
return producer
def _onto_set(cls, ref: Reference, snapshot: Snapshot, transaction=_NA):
cls.d[str(ref)] = snapshot.to_dict()
cls.listener()._pub(reference=ref, snapshot=snapshot)

@classmethod
def set(cls, ref: Reference, snapshot: Snapshot, transaction=_NA,
**kwargs):
d = snapshot.to_dict()
import json
s = json.dumps(d)
b = s.encode(encoding='utf-8')
bk = ref.id.encode(encoding='utf-8')
cls.kafka_producer().send(topic=ref.collection, key=bk, value=b, **kwargs)
def get(cls, ref: Reference, transaction=_NA):
return Snapshot(cls.d[str(ref)])

update = set
create = set

@classmethod
def delete(cls, ref: Reference, transaction=_NA):
cls.kafka_producer().send(topic=ref.collection, key=ref.id, value=None)
""" Note: this only deletes one instance that has _doc_id == ref.last
ref = KafkaReference()
:param ref:
:param transaction:
:return:
"""
del cls.d[str(ref)]


class KafkaSnapshot(Snapshot):
Expand Down

0 comments on commit 8b4fe19

Please sign in to comment.