Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan Misiurev committed Jan 25, 2019
1 parent 28a8b1c commit 602c0cd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion django_grpc/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.4'
__version__ = '0.1.5'
3 changes: 2 additions & 1 deletion django_grpc/management/commands/grpcserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ def _serve(self, max_workers, port, *args, **kwargs):
self.stdout.write("Server is listening port %s" % port)

if kwargs['list_handlers'] is True:
self.stdout.write("Registered handlers:")
for handler in extract_handlers(server):
self.stdout.write(self.style.INFO(handler))
self.stdout.write("* %s" % handler)

# since server.start() will not block,
# a sleep-loop is added to keep alive
Expand Down
11 changes: 9 additions & 2 deletions django_grpc/serializers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def find_for_model(cls, instance, serializers: Iterable):
@classmethod
def serialize_model(cls, message_class, instance: 'Model', serializers):
serializer = cls.find_for_model(instance, serializers)
serializer.serializers = serializers
return message_class(**serializer._to_dict(message_class.DESCRIPTOR.fields_by_name.items(), instance))

@classmethod
Expand Down Expand Up @@ -77,16 +78,22 @@ def _message_value(val):
"""
Check if nested values need to be deserialized
"""
class_name = val.__class__.__name__
# List of structures
# Convert repeated
# if isinstance(val, RepeatedCompositeContainer):
if val.__class__.__name__ == 'RepeatedCompositeContainer':
if class_name == 'RepeatedCompositeContainer':
return [
message_to_python(it)
for it in val
]
# List of simple types
if class_name == 'RepeatedScalarContainer':
return list(val)

# Convert complex types
# Convert single complex type (structure)
if isinstance(val, Message):
return message_to_python(val)

# Simple type (str, int, bool)
return val
21 changes: 20 additions & 1 deletion django_grpc/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from abc import ABCMeta
import logging
from concurrent import futures

import grpc
from django.utils.module_loading import import_string


logger = logging.getLogger(__name__)


def create_server(max_workers, port):
# create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers))
Expand All @@ -19,10 +24,24 @@ def add_servicers(server):
"""
from django.conf import settings
for path in settings.GRPC_SERVICERS:
logger.debug("Adding servicers from %s", path)
callback = import_string(path)
callback(server)


def extract_handlers(server):
for it in server._state.generic_handlers[0]._method_handlers.values():
yield it.unary_unary.__qualname__
unary = it.unary_unary
if unary is None:
logger.warning("%s is invalid", it)
continue

code = it.unary_unary.__code__
abstract = ''
if isinstance(it.__class__, ABCMeta):
abstract = 'NOT IMPLEMENTED'
yield "{name}({params}) {abstract}".format(
name=code.co_name,
params=", ".join(code.co_varnames),
abstract=abstract
)
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.1.4
current_version = 0.1.5
commit = True
tag = True

Expand Down

0 comments on commit 602c0cd

Please sign in to comment.