Skip to content

Commit

Permalink
Merge pull request #1 from ccpgames/feature/add-servicer-class-to-api
Browse files Browse the repository at this point in the history
Version 5.1.0 - Added Servicer Class to API
  • Loading branch information
CCP-Zeulix authored Apr 30, 2024
2 parents 37df50e + ccf0992 commit 6cbd3a5
Show file tree
Hide file tree
Showing 22 changed files with 142 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ venv*/
/build/
/tests/res/build/
!/tests/res/build/empty.txt
/_sandbox/
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [5.1.0] - 2024-04-30

### Added

- A class attribute called `__servicer_cls__` to any service interfaces (for
a gRPC build) with reference to that services `GrpcServicer` class
- Added a bunch of useful sub-tests to unittests

### Fixed

- A bunch of unittesting issues

## [5.0.0] - 2024-04-15

### Changed

- Moved this entire project over to Github
- Bumped the version in order to not confuse older stuff that doesn't expect
protoplasm to exist in Pypi.org (if we end up migriting this there and
protoplasm to exist in Pypi.org (if we end up migrating this there and
just open-sourcing the whole thing)
- Also in case something changes in the API while migrating, cause I tend
to fiddle with the code and tidy up and refactor when moving stuff
17 changes: 17 additions & 0 deletions _sandbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from neobuilder.neobuilder import NeoBuilder


if __name__ == '__main__':
#n = NeoBuilder(
# package='efrit',
# protopath='./_sandbox/proto/',
# build_root='./_sandbox/build/',
# verbose=True,
#)
n = NeoBuilder(
package='sandbox',
protopath=r'D:\Code\github\ccpgames\neobuilder\tests\res\proto',
build_root='./_sandbox/build/',
verbose=True,
)
n.build()
2 changes: 1 addition & 1 deletion neobuilder/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '5.0.0'
__version__ = '5.1.0'
1 change: 1 addition & 0 deletions neobuilder/data/templates/grpc_receiver.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ from protoplasm import plasm
{{ imports }}
if TYPE_CHECKING:
from grpc import ServicerContext
{{ api_import }}

import logging
log = logging.getLogger(__name__)
Expand Down
4 changes: 4 additions & 0 deletions neobuilder/data/templates/parts/_interface_service.jinja2
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{% from 'macros/indentclude.jinja2' import indentclude with context %}
class {{ service_name }}Interface:
{% if is_grpc %}
__servicer_cls__ = {{ service_name }}GrpcServicer

{% endif %}
{% for method in method_list %}
{{ indentclude('parts/_interface_method.jinja2', (indent_spaces + 4), method) }}

Expand Down
3 changes: 2 additions & 1 deletion neobuilder/generators/servicebuilders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_template_context(self) -> Dict:
}

def render_top(self) -> str:
# TODO(thordurm@ccpgames.com) 2022-06-24: Remove!
# TODO(thordurm@ccpgames.com) 2022-06-24: Remove... why?
return ('# Auto-Generated file - DO NOT EDIT!\n'
f'# Source module: {self.module.get_module_full_name()}\n'
f'# Generated at: {datetime.datetime.now().isoformat()}\n')
Expand Down Expand Up @@ -49,6 +49,7 @@ def __init__(self, service: symbols_service.ProtoService, indent_level: int = 0)

def get_template_context(self) -> Dict:
return {
'is_grpc': self.service.module.is_grpc_file(),
'service': self.service,
'service_name': self.service.service_descriptor.name,
'_indent_level': self.indent_level,
Expand Down
4 changes: 2 additions & 2 deletions neobuilder/generators/servicebuilders/grpc_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def render_imports(self) -> str:
f'from {self.module.get_package()} import {self.module.get_module_name()} as pb2',
f'from {self.module.get_package()} import {self.module.get_module_name()}_grpc as pb2_grpc',
f'from {self.module.get_package()} import {self.module.get_module_name()[:-3]}dc as dc',
f'from {self.module.get_package()} import {self.module.get_module_name()[:-3]}api as api',
}

for s in self.module.service_map.values():
Expand All @@ -42,6 +41,7 @@ def render_imports(self) -> str:
def get_template_context(self) -> Dict:
d = super().get_template_context()
d.update({
'api_import': f'from {self.module.get_package()} import {self.module.get_module_name()[:-3]}api as api',
'services': self.render_services(),
'imports': self.render_imports(),
'all_list': [f'{svc.service_descriptor.name}GrpcServicer' for svc in self.module.service_map.values()],
Expand All @@ -60,7 +60,7 @@ def render(self) -> str:
i = self.base_indent
return (
f'{i}class {self.service.service_descriptor.name}GrpcServicer(plasm.BaseGrpcServicer, pb2_grpc.{self.service.service_descriptor.name}Servicer):\n'
f'{i}{__}def __init__(self, implementation: api.{self.service.service_descriptor.name}Interface):\n'
f"{i}{__}def __init__(self, implementation: 'api.{self.service.service_descriptor.name}Interface'):\n"
f'{i}{__}{__}super().__init__(implementation)\n'
'\n'
f'{i}{__}def add_to_server(self, server):\n'
Expand Down
4 changes: 4 additions & 0 deletions neobuilder/generators/servicebuilders/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def get_import_lines(self) -> List[str]:
f'from {self.module.get_package()} import {self.module.get_module_name()[:-3]}dc as dc'
}
for s in self.module.service_map.values():

if self.module.is_grpc_file():
import_set.add(f'from {self.module.get_package()}.{self.module.get_module_name()[:-3]}grpc_receiver import {s.service_descriptor.name}GrpcServicer')

for m in s.method_map.values():
# From 3.2
if m.input.self_import.get_package() != 'google.protobuf':
Expand Down
4 changes: 4 additions & 0 deletions neobuilder/generators/symbols/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def render_dataclass_file(self):
})

def write_dataclass_file(self):
log.debug(f' - - write_dataclass_file() {self.get_render_file_name()}')
self.write_file(self.get_render_file_name(), self.render_dataclass_file())

#
Expand All @@ -182,6 +183,7 @@ def render_grpc_server_file(self):

def write_api_file(self):
b = servicebuilders.get_module_builder('interface')(self)
log.debug(f' - - write_api_file() {b.get_render_filename()}')
self.write_file(b.get_render_filename(), b.render())

def render_api_file(self):
Expand All @@ -193,6 +195,7 @@ def render_api_file(self):

def write_grpc_file(self):
b = servicebuilders.get_module_builder('grpc_receiver')(self)
log.debug(f' - - write_grpc_file() {b.get_render_filename()}')
self.write_file(b.get_render_filename(), b.render())

def render_grpc_file(self):
Expand All @@ -204,6 +207,7 @@ def render_grpc_file(self):

def write_grpc_impl_file(self):
b = servicebuilders.get_module_builder('grpc_sender')(self)
log.debug(f' - - write_grpc_impl_file() {b.get_render_filename()}')
self.write_file(b.get_render_filename(), b.render())

def render_grpc_impl_file(self):
Expand Down
6 changes: 6 additions & 0 deletions neobuilder/neobuilder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ def __init__(self,
format='%(levelname)8s - %(message)s')
log.info(f'Initializing Neobuilder {self.neobuilder_version()}')

log.debug(f'{self.package=}')
log.debug(f'{self.protopath=}')
log.debug(f'{self.build_root=}')
log.debug(f'{self.proto_include=}')

@staticmethod
def _get_basic_proto_path() -> str:
if _PY_3_9_PLUS:
Expand Down Expand Up @@ -337,6 +342,7 @@ def plasm_build(self, protofile: ProtoFile):
else:
try:
p = modules.ProtoModule(m)
log.debug(f' - Writing ProtoModule: {p.get_module_full_name()}')
p.write_rendered_file()
except Exception as ex:
log.exception(f'ERROR! Bad stuff happened! %r' % ex)
Expand Down
3 changes: 3 additions & 0 deletions tests/res/expected/sandbox/test/illnamedservice_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
from protoplasm import plasm

from sandbox.test import illnamedservice_dc as dc
from sandbox.test.illnamedservice_grpc_receiver import ServiceWithBadRequestNamesGrpcServicer

import logging
log = logging.getLogger(__name__)


class ServiceWithBadRequestNamesInterface:
__servicer_cls__ = ServiceWithBadRequestNamesGrpcServicer

def do_something(self, foo: str = None) -> str:
raise plasm.Unimplemented()

Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
]
from typing import *
from protoplasm import plasm
from sandbox.test import illnamedservice_api as api
from sandbox.test import illnamedservice_dc as dc
from sandbox.test import illnamedservice_pb2 as pb2
from sandbox.test import illnamedservice_pb2_grpc as pb2_grpc
if TYPE_CHECKING:
from grpc import ServicerContext
from sandbox.test import illnamedservice_api as api

import logging
log = logging.getLogger(__name__)


class ServiceWithBadRequestNamesGrpcServicer(plasm.BaseGrpcServicer, pb2_grpc.ServiceWithBadRequestNamesServicer):
def __init__(self, implementation: api.ServiceWithBadRequestNamesInterface):
def __init__(self, implementation: 'api.ServiceWithBadRequestNamesInterface'):
super().__init__(implementation)

def add_to_server(self, server):
Expand Down
3 changes: 3 additions & 0 deletions tests/res/expected/sandbox/test/river_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
from protoplasm import plasm

from sandbox.test import river_dc as dc
from sandbox.test.river_grpc_receiver import StreamingServiceGrpcServicer

import logging
log = logging.getLogger(__name__)


class StreamingServiceInterface:
__servicer_cls__ = StreamingServiceGrpcServicer

def reverse_my_shit(self, shit: str = None) -> str:
raise plasm.Unimplemented()

Expand Down
4 changes: 2 additions & 2 deletions tests/res/expected/sandbox/test/river_grpc_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
]
from typing import *
from protoplasm import plasm
from sandbox.test import river_api as api
from sandbox.test import river_dc as dc
from sandbox.test import river_pb2 as pb2
from sandbox.test import river_pb2_grpc as pb2_grpc
if TYPE_CHECKING:
from grpc import ServicerContext
from sandbox.test import river_api as api

import logging
log = logging.getLogger(__name__)


class StreamingServiceGrpcServicer(plasm.BaseGrpcServicer, pb2_grpc.StreamingServiceServicer):
def __init__(self, implementation: api.StreamingServiceInterface):
def __init__(self, implementation: 'api.StreamingServiceInterface'):
super().__init__(implementation)

def add_to_server(self, server):
Expand Down
6 changes: 6 additions & 0 deletions tests/res/expected/sandbox/test/service_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
from protoplasm import plasm

from sandbox.test import service_dc as dc
from sandbox.test.service_grpc_receiver import MathGrpcServicer
from sandbox.test.service_grpc_receiver import SimpleServiceGrpcServicer

import logging
log = logging.getLogger(__name__)


class SimpleServiceInterface:
__servicer_cls__ = SimpleServiceGrpcServicer

def hello(self, greeting: str = None) -> str:
raise plasm.Unimplemented()

Expand All @@ -28,6 +32,8 @@ def empty_hello(self) -> NoReturn:


class MathInterface:
__servicer_cls__ = MathGrpcServicer

def add(self, x: int = None, y: int = None) -> int:
raise plasm.Unimplemented()

Expand Down
7 changes: 4 additions & 3 deletions tests/res/expected/sandbox/test/service_grpc_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@
]
from typing import *
from protoplasm import plasm
from sandbox.test import service_api as api

from sandbox.test import service_dc as dc
from sandbox.test import service_pb2 as pb2
from sandbox.test import service_pb2_grpc as pb2_grpc
if TYPE_CHECKING:
from grpc import ServicerContext
from sandbox.test import service_api as api

import logging
log = logging.getLogger(__name__)


class SimpleServiceGrpcServicer(plasm.BaseGrpcServicer, pb2_grpc.SimpleServiceServicer):
def __init__(self, implementation: api.SimpleServiceInterface):
def __init__(self, implementation: 'api.SimpleServiceInterface'):
super().__init__(implementation)

def add_to_server(self, server):
Expand All @@ -37,7 +38,7 @@ def EmptyHello(self, request: dc.pb2.EmptyHelloRequest, context: 'ServicerContex


class MathGrpcServicer(plasm.BaseGrpcServicer, pb2_grpc.MathServicer):
def __init__(self, implementation: api.MathInterface):
def __init__(self, implementation: 'api.MathInterface'):
super().__init__(implementation)

def add_to_server(self, server):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
from sandbox.test import nested_dc as sandbox__test__nested_dc
from sandbox.test import rainbow_dc as sandbox__test__rainbow_dc
from sandbox.test import service_with_imported_io_dc as dc
from sandbox.test.service_with_imported_io_grpc_receiver import ServiceWithImportedInputAndOutputGrpcServicer

import logging
log = logging.getLogger(__name__)


class ServiceWithImportedInputAndOutputInterface:
__servicer_cls__ = ServiceWithImportedInputAndOutputGrpcServicer

def simple(self, my_string: str = None, my_number: int = None, my_level_three_message: sandbox__test__beta_dc.BetaMessage = None) -> Tuple[str, str]:
raise plasm.Unimplemented()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
from sandbox.test import delta_dc as sandbox__test__delta_dc
from sandbox.test import nested_dc as sandbox__test__nested_dc
from sandbox.test import rainbow_dc as sandbox__test__rainbow_dc
from sandbox.test import service_with_imported_io_api as api
from sandbox.test import service_with_imported_io_dc as dc
from sandbox.test import service_with_imported_io_pb2 as pb2
from sandbox.test import service_with_imported_io_pb2_grpc as pb2_grpc
if TYPE_CHECKING:
from grpc import ServicerContext
from sandbox.test import service_with_imported_io_api as api

import logging
log = logging.getLogger(__name__)


class ServiceWithImportedInputAndOutputGrpcServicer(plasm.BaseGrpcServicer, pb2_grpc.ServiceWithImportedInputAndOutputServicer):
def __init__(self, implementation: api.ServiceWithImportedInputAndOutputInterface):
def __init__(self, implementation: 'api.ServiceWithImportedInputAndOutputInterface'):
super().__init__(implementation)

def add_to_server(self, server):
Expand Down
3 changes: 3 additions & 0 deletions tests/res/expected/sandbox/test/service_with_oneof_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
from protoplasm import plasm

from sandbox.test import service_with_oneof_dc as dc
from sandbox.test.service_with_oneof_grpc_receiver import SimpleOneOfServiceGrpcServicer

import logging
log = logging.getLogger(__name__)


class SimpleOneOfServiceInterface:
__servicer_cls__ = SimpleOneOfServiceGrpcServicer

def hello_again(self, greeting: str = None) -> Tuple[str, int]:
raise plasm.Unimplemented()

Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
]
from typing import *
from protoplasm import plasm
from sandbox.test import service_with_oneof_api as api
from sandbox.test import service_with_oneof_dc as dc
from sandbox.test import service_with_oneof_pb2 as pb2
from sandbox.test import service_with_oneof_pb2_grpc as pb2_grpc
if TYPE_CHECKING:
from grpc import ServicerContext
from sandbox.test import service_with_oneof_api as api

import logging
log = logging.getLogger(__name__)


class SimpleOneOfServiceGrpcServicer(plasm.BaseGrpcServicer, pb2_grpc.SimpleOneOfServiceServicer):
def __init__(self, implementation: api.SimpleOneOfServiceInterface):
def __init__(self, implementation: 'api.SimpleOneOfServiceInterface'):
super().__init__(implementation)

def add_to_server(self, server):
Expand Down
Loading

0 comments on commit 6cbd3a5

Please sign in to comment.