Skip to content

Commit 8f8b721

Browse files
authored
Merge pull request #92 from dahlia/transport
Introduce transport and deprecate nirum.rpc.*
2 parents ae1fc85 + 5ca2d9e commit 8f8b721

File tree

10 files changed

+335
-136
lines changed

10 files changed

+335
-136
lines changed

CHANGES.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,64 @@ Version 0.6.0
66

77
To be released.
88

9+
- Deprecated ``nirum.rpc`` module.
10+
11+
This module and all it has provided are deprecated or obsolete. The most
12+
of them are now distributed as separated packages, or replaced by a newer
13+
concept. See also the below for details.
14+
15+
It will be completely obsolete at version 0.7.0.
16+
17+
- Client transport layer. [`#79`_]
18+
19+
- Added ``nirum.transport.Transport`` interface.
20+
21+
The recent builds of Nirum compiler became to generate ``*_Client`` classes
22+
taking a ``nirum.transport.Transport`` instance through their constructor.
23+
24+
Use nirum-python-http_ (PyPI handle: ``nirum-http``) instead for HTTP
25+
client of services e.g.:
26+
27+
.. code-block:: python
28+
29+
from yourservice import YourService_Client
30+
from nirum_http import HttpTransport
31+
32+
transport = HttpTransport('https://service-host/')
33+
client = YourService_Client(transport)
34+
35+
- Deprecated ``nirum.rpc.Client`` type. The recent builds of Nirum compiler
36+
became to generate ``*_Client`` classes for services without subclassing
37+
``nirum.rpc.Client``.
38+
39+
The deprecated ``nirum.rpc.Client`` will be completely obsolete at
40+
version 0.7.0.
41+
42+
- ``nirum.rpc.Service`` was moved to ``nirum.service.Service``.
43+
44+
The recent builds of Nirum compiler became to generate service classes
45+
that inherit ``nirum.service.Service`` instead of ``nirum.rpc.Service``.
46+
47+
The deprecated ``nirum.rpc.Service`` will be completely obsolete at
48+
version 0.7.0.
49+
50+
- Deprecated ``nirum.rpc.WsgiApp``. This will be completely obsolete at
51+
version 0.7.0.
52+
53+
Use nirum-python-wsgi_ (PyPI handle: ``nirum-wsgi``) instead.
54+
55+
- ``nirum-server`` command is obsolete. The same command is now provided
56+
by nirum-python-wsgi_ (PyPI handle: ``nirum-wsgi``), a separated package.
57+
58+
- ``nirum.func.import_string()`` function and ``nirum.func.IMPORT_RE`` constant
59+
are obsolete.
60+
961
- Fixed ``NameError`` raised from forward references. [`compiler #138`_]
1062

63+
.. _#79: https://github.com/spoqa/nirum-python/issues/79
1164
.. _compiler #138: https://github.com/spoqa/nirum/issues/138
65+
.. _nirum-python-http: https://github.com/spoqa/nirum-python-http
66+
.. _nirum-python-wsgi: https://github.com/spoqa/nirum-python-wsgi
1267

1368

1469
Version 0.5.3

nirum/func.py

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1-
import re
1+
from six.moves import urllib
22

3-
from six.moves import urllib, reduce
4-
5-
__all__ = 'IMPORT_RE', 'import_string', 'url_endswith_slash'
6-
IMPORT_RE = re.compile(
7-
r'''^
8-
(?P<modname> (?!\d) [\w]+
9-
(?: \. (?!\d)[\w]+ )*
10-
)
11-
:
12-
(?P<clsexp> (?P<clsname> (?!\d) \w+ )
13-
(?: \(.*\) )?
14-
)
15-
$''',
16-
re.X
17-
)
3+
__all__ = 'url_endswith_slash'
184

195

206
def url_endswith_slash(url):
@@ -24,22 +10,3 @@ def url_endswith_slash(url):
2410
if not path.endswith('/'):
2511
path += '/'
2612
return urllib.parse.urlunsplit((scheme, netloc, path, '', ''))
27-
28-
29-
def import_string(imp):
30-
m = IMPORT_RE.match(imp)
31-
if not m:
32-
raise ValueError(
33-
"malformed expression: {}, have to be x.y:z(...)".format(imp)
34-
)
35-
module_name = m.group('modname')
36-
import_root_mod = __import__(module_name)
37-
# it is used in `eval()`
38-
import_mod = reduce(getattr, module_name.split('.')[1:], import_root_mod) # noqa
39-
class_expression = m.group('clsexp')
40-
try:
41-
v = eval(class_expression, import_mod.__dict__, {})
42-
except AttributeError:
43-
raise ValueError("Can't import {}".format(imp))
44-
else:
45-
return v

nirum/rpc.py

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
11
""":mod:`nirum.rpc`
22
~~~~~~~~~~~~~~~~~~~
33
4+
.. deprecated:: 0.6.0
5+
This module and all it has provided are deprecated or obsolete. The most
6+
of them are now distributed as separated packages, or replaced by a newer
7+
concept.
8+
9+
It will be completely obsolete at version 0.7.0.
10+
411
"""
5-
import argparse
612
import collections
713
import json
814
import typing
15+
import warnings
916

1017
from six import integer_types, string_types
1118
from six.moves import urllib
1219
from werkzeug.exceptions import HTTPException
1320
from werkzeug.http import HTTP_STATUS_CODES
1421
from werkzeug.routing import Map, Rule
15-
from werkzeug.serving import run_simple
1622
from werkzeug.wrappers import Request as WsgiRequest, Response as WsgiResponse
1723

18-
from .constructs import NameDict
1924
from .deserialize import deserialize_meta
20-
from .exc import (InvalidNirumServiceMethodNameError,
21-
InvalidNirumServiceMethodTypeError,
22-
NirumProcedureArgumentRequiredError,
25+
from .exc import (NirumProcedureArgumentRequiredError,
2326
NirumProcedureArgumentValueError,
2427
UnexpectedNirumResponseError)
25-
from .func import import_string, url_endswith_slash
28+
from .func import url_endswith_slash
2629
from .serialize import serialize_meta
30+
from .service import Service as BaseService
2731

2832
__all__ = 'Client', 'WsgiApp', 'Service', 'client_type', 'service_type'
2933
JSONType = typing.Mapping[
3034
str, typing.Union[str, float, int, bool, object]
3135
]
3236

3337

34-
class Service(object):
35-
"""Nirum RPC service."""
38+
class Service(BaseService):
39+
"""Abstract base of Nirum services.
3640
37-
__nirum_service_methods__ = {}
38-
__nirum_method_names__ = NameDict([])
41+
.. deprecated:: 0.6.0
42+
Use :class:`nirum.service.Service` instead.
43+
It will be completely obsolete at version 0.7.0.
3944
40-
@staticmethod
41-
def __nirum_method_error_types__(k, d=None):
42-
return d
45+
"""
4346

4447
def __init__(self):
45-
for method_name in self.__nirum_service_methods__:
46-
try:
47-
method = getattr(self, method_name)
48-
except AttributeError:
49-
raise InvalidNirumServiceMethodNameError(
50-
"{0}.{1} inexist.".format(
51-
typing._type_repr(self.__class__), method_name
52-
)
53-
)
54-
if not callable(method):
55-
raise InvalidNirumServiceMethodTypeError(
56-
"{0}.{1} isn't callable".format(
57-
typing._type_repr(self.__class__), method_name
58-
)
59-
)
48+
warnings.warn(
49+
'nirum.rpc.Service is deprecated; use nirum.service.Service '
50+
'instead. It will be completely obsolete at version 0.7.0.',
51+
DeprecationWarning
52+
)
53+
super(Service, self).__init__()
6054

6155

6256
class WsgiApp:
6357
"""Create WSGI application adapt Nirum service.
6458
6559
:param service: A nirum service.
6660
61+
.. deprecated:: 0.6.0
62+
Use ``nirum_wsgi.WsgiApp`` (provided by `nirum-wsgi
63+
<https://github.com/spoqa/nirum-python-wsgi>`_ package) instead.
64+
65+
It will be completely obsolete at version 0.7.0.
66+
6767
"""
6868

6969
#: (:class:`werkzeug.routing.Map`) url map
@@ -73,6 +73,12 @@ class WsgiApp:
7373
])
7474

7575
def __init__(self, service):
76+
warnings.warn(
77+
'nirum.rpc.WsgiApp is deprecated; use nirum_wsgi.WsgiApp '
78+
'(provided by nirum-wsgi package). It will be completely '
79+
'obsolete at version 0.7.0.',
80+
DeprecationWarning
81+
)
7682
self.service = service
7783

7884
def __call__(self, environ, start_response):
@@ -323,8 +329,23 @@ def _raw_response(self, status_code, response_json, **kwargs):
323329

324330

325331
class Client(object):
332+
"""HTTP service client base class.
333+
334+
.. deprecated:: 0.6.0
335+
Use :class:`nirum.transport.Transport` and
336+
:mod:`nirum_http.HttpTransport` (provided by `nirum-http
337+
<https://github.com/spoqa/nirum-python-http>` package) instead.
338+
It will be completely obsolete at version 0.7.0.
339+
340+
"""
326341

327342
def __init__(self, url, opener=urllib.request.build_opener()):
343+
warnings.warn(
344+
'nirum.rpc.Client is deprecated; use nirum.transport.Transport '
345+
'and nirum_http.HttpTransport (provided by nirum-http package) '
346+
'instead. It will be completely obsolete at version 0.7.0.',
347+
DeprecationWarning
348+
)
328349
self.url = url_endswith_slash(url)
329350
self.opener = opener
330351

@@ -431,21 +452,3 @@ def do_request(self, request_url, payload):
431452
# with postfix named `_type`
432453
service_type = Service
433454
client_type = Client
434-
435-
436-
def main():
437-
parser = argparse.ArgumentParser(description='Nirum service runner')
438-
parser.add_argument('-H', '--host', help='the host to listen',
439-
default='0.0.0.0')
440-
parser.add_argument('-p', '--port', help='the port number to listen',
441-
type=int, default=9322)
442-
parser.add_argument('-d', '--debug', help='debug mode',
443-
action='store_true', default=False)
444-
parser.add_argument('service_impl', help='service implementation name')
445-
args = parser.parse_args()
446-
service_impl = import_string(args.service_impl)
447-
run_simple(
448-
args.host, args.port, WsgiApp(service_impl),
449-
use_reloader=args.debug, use_debugger=args.debug,
450-
use_evalex=args.debug
451-
)

nirum/service.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
""":mod:`nirum.service` --- Runtime base of Nirum services
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
"""
5+
import typing
6+
7+
from .constructs import NameDict
8+
from .exc import (InvalidNirumServiceMethodTypeError,
9+
InvalidNirumServiceMethodNameError)
10+
11+
__all__ = 'Service',
12+
13+
14+
class Service(object):
15+
"""Abstract base of Nirum services.
16+
17+
All service classes generated by Nirum compiler inherit this.
18+
19+
"""
20+
21+
__nirum_service_methods__ = {}
22+
__nirum_method_names__ = NameDict([])
23+
24+
@staticmethod
25+
def __nirum_method_error_types__(k, d=None):
26+
return d
27+
28+
def __init__(self):
29+
for method_name in self.__nirum_service_methods__:
30+
try:
31+
method = getattr(self, method_name)
32+
except AttributeError:
33+
raise InvalidNirumServiceMethodNameError(
34+
'{0}.{1}() method has to be implemented.'.format(
35+
typing._type_repr(type(self)), method_name
36+
)
37+
)
38+
if not callable(method):
39+
raise InvalidNirumServiceMethodTypeError(
40+
'{0}.{1} has to be callable so that is a method'.format(
41+
typing._type_repr(type(self)), method_name
42+
)
43+
)

0 commit comments

Comments
 (0)