9d0f3fca711685955f801157cea091e25324a8d6
[trex.git] /
1 Metadata-Version: 1.1\r
2 Name: jsonrpclib-pelix\r
3 Version: 0.2.5\r
4 Summary: This project is an implementation of the JSON-RPC v2.0 specification (backwards-compatible) as a client library, for Python 2.6+ and Python 3.This version is a fork of jsonrpclib by Josh Marshall, usable with Pelix remote services.\r
5 Home-page: http://github.com/tcalmant/jsonrpclib/\r
6 Author: Thomas Calmant\r
7 Author-email: [email protected]\r
8 License: Apache License 2.0\r
9 Description: JSONRPClib (patched for Pelix)\r
10         ##############################\r
11         \r
12         .. image:: https://pypip.in/license/jsonrpclib-pelix/badge.svg\r
13             :target: https://pypi.python.org/pypi/jsonrpclib-pelix/\r
14         \r
15         .. image:: https://travis-ci.org/tcalmant/jsonrpclib.svg?branch=master\r
16             :target: https://travis-ci.org/tcalmant/jsonrpclib\r
17         \r
18         .. image:: https://coveralls.io/repos/tcalmant/jsonrpclib/badge.svg?branch=master\r
19             :target: https://coveralls.io/r/tcalmant/jsonrpclib?branch=master\r
20         \r
21         \r
22         This library is an implementation of the JSON-RPC specification.\r
23         It supports both the original 1.0 specification, as well as the\r
24         new (proposed) 2.0 specification, which includes batch submission, keyword\r
25         arguments, etc.\r
26         \r
27         It is licensed under the Apache License, Version 2.0\r
28         (http://www.apache.org/licenses/LICENSE-2.0.html).\r
29         \r
30         \r
31         About this version\r
32         ******************\r
33         \r
34         This is a patched version of the original ``jsonrpclib`` project by\r
35         Josh Marshall, available at https://github.com/joshmarshall/jsonrpclib.\r
36         \r
37         The suffix *-pelix* only indicates that this version works with Pelix Remote\r
38         Services, but it is **not** a Pelix specific implementation.\r
39         \r
40         * This version adds support for Python 3, staying compatible with Python 2.\r
41         * It is now possible to use the dispatch_method argument while extending\r
42           the SimpleJSONRPCDispatcher, to use a custom dispatcher.\r
43           This allows to use this package by Pelix Remote Services.\r
44         * It can use thread pools to control the number of threads spawned to handle\r
45           notification requests and clients connections.\r
46         * The modifications added in other forks of this project have been added:\r
47         \r
48           * From https://github.com/drdaeman/jsonrpclib:\r
49         \r
50             * Improved JSON-RPC 1.0 support\r
51             * Less strict error response handling\r
52         \r
53           * From https://github.com/tuomassalo/jsonrpclib:\r
54         \r
55             * In case of a non-pre-defined error, raise an AppError and give access to\r
56               *error.data*\r
57         \r
58           * From https://github.com/dejw/jsonrpclib:\r
59         \r
60             * Custom headers can be sent with request and associated tests\r
61         \r
62         * The support for Unix sockets has been removed, as it is not trivial to convert\r
63           to Python 3 (and I don't use them)\r
64         * This version cannot be installed with the original ``jsonrpclib``, as it uses\r
65           the same package name.\r
66         \r
67         \r
68         Summary\r
69         *******\r
70         \r
71         This library implements the JSON-RPC 2.0 proposed specification in pure Python.\r
72         It is designed to be as compatible with the syntax of ``xmlrpclib`` as possible\r
73         (it extends where possible), so that projects using ``xmlrpclib`` could easily\r
74         be modified to use JSON and experiment with the differences.\r
75         \r
76         It is backwards-compatible with the 1.0 specification, and supports all of the\r
77         new proposed features of 2.0, including:\r
78         \r
79         * Batch submission (via MultiCall)\r
80         * Keyword arguments\r
81         * Notifications (both in a batch and 'normal')\r
82         * Class translation using the ``__jsonclass__`` key.\r
83         \r
84         I've added a "SimpleJSONRPCServer", which is intended to emulate the\r
85         "SimpleXMLRPCServer" from the default Python distribution.\r
86         \r
87         \r
88         Requirements\r
89         ************\r
90         \r
91         It supports ``cjson`` and ``simplejson``, and looks for the parsers in that\r
92         order (searching first for ``cjson``, then for the *built-in* ``json`` in 2.6+,\r
93         and then the ``simplejson`` external library).\r
94         One of these must be installed to use this library, although if you have a\r
95         standard distribution of 2.6+, you should already have one.\r
96         Keep in mind that ``cjson`` is supposed to be the quickest, I believe, so if\r
97         you are going for full-on optimization you may want to pick it up.\r
98         \r
99         Since library uses ``contextlib`` module, you should have at least Python 2.5\r
100         installed.\r
101         \r
102         \r
103         Installation\r
104         ************\r
105         \r
106         You can install this from PyPI with one of the following commands (sudo\r
107         may be required):\r
108         \r
109         .. code-block:: console\r
110         \r
111            easy_install jsonrpclib-pelix\r
112            pip install jsonrpclib-pelix\r
113         \r
114         Alternatively, you can download the source from the GitHub repository\r
115         at http://github.com/tcalmant/jsonrpclib and manually install it\r
116         with the following commands:\r
117         \r
118         .. code-block:: console\r
119         \r
120            git clone git://github.com/tcalmant/jsonrpclib.git\r
121            cd jsonrpclib\r
122            python setup.py install\r
123         \r
124         \r
125         SimpleJSONRPCServer\r
126         *******************\r
127         \r
128         This is identical in usage (or should be) to the SimpleXMLRPCServer in the\r
129         Python standard library. Some of the differences in features are that it\r
130         obviously supports notification, batch calls, class translation (if left on),\r
131         etc.\r
132         Note: The import line is slightly different from the regular SimpleXMLRPCServer,\r
133         since the SimpleJSONRPCServer is distributed within the ``jsonrpclib`` library.\r
134         \r
135         .. code-block:: python\r
136         \r
137            from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer\r
138         \r
139            server = SimpleJSONRPCServer(('localhost', 8080))\r
140            server.register_function(pow)\r
141            server.register_function(lambda x,y: x+y, 'add')\r
142            server.register_function(lambda x: x, 'ping')\r
143            server.serve_forever()\r
144         \r
145         To start protect the server with SSL, use the following snippet:\r
146         \r
147         .. code-block:: python\r
148         \r
149            from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer\r
150         \r
151            # Setup the SSL socket\r
152            server = SimpleJSONRPCServer(('localhost', 8080), bind_and_activate=False)\r
153            server.socket = ssl.wrap_socket(server.socket, certfile='server.pem',\r
154                                            server_side=True)\r
155            server.server_bind()\r
156            server.server_activate()\r
157         \r
158            # ... register functions\r
159            # Start the server\r
160            server.serve_forever()\r
161         \r
162         \r
163         Notification Thread Pool\r
164         ========================\r
165         \r
166         By default, notification calls are handled in the request handling thread.\r
167         It is possible to use a thread pool to handle them, by giving it to the server\r
168         using the ``set_notification_pool()`` method:\r
169         \r
170         .. code-block:: python\r
171         \r
172            from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer\r
173            from jsonrpclib.threadpool import ThreadPool\r
174         \r
175            # Setup the thread pool: between 0 and 10 threads\r
176            pool = ThreadPool(max_threads=10, min_threads=0)\r
177         \r
178            # Don't forget to start it\r
179            pool.start()\r
180         \r
181            # Setup the server\r
182            server = SimpleJSONRPCServer(('localhost', 8080), config)\r
183            server.set_notification_pool(pool)\r
184         \r
185            # Register methods\r
186            server.register_function(pow)\r
187            server.register_function(lambda x,y: x+y, 'add')\r
188            server.register_function(lambda x: x, 'ping')\r
189         \r
190            try:\r
191                server.serve_forever()\r
192            finally:\r
193                # Stop the thread pool (let threads finish their current task)\r
194                pool.stop()\r
195                server.set_notification_pool(None)\r
196         \r
197         \r
198         Threaded server\r
199         ===============\r
200         \r
201         It is also possible to use a thread pool to handle clients requests, using the\r
202         ``PooledJSONRPCServer`` class.\r
203         By default, this class uses pool of 0 to 30 threads. A custom pool can be given\r
204         with the ``thread_pool`` parameter of the class constructor.\r
205         \r
206         The notification pool and the request pool are different: by default, a server\r
207         with a request pool doesn't have a notification pool.\r
208         \r
209         .. code-block:: python\r
210         \r
211            from jsonrpclib.SimpleJSONRPCServer import PooledJSONRPCServer\r
212            from jsonrpclib.threadpool import ThreadPool\r
213         \r
214            # Setup the notification and request pools\r
215            nofif_pool = ThreadPool(max_threads=10, min_threads=0)\r
216            request_pool = ThreadPool(max_threads=50, min_threads=10)\r
217         \r
218            # Don't forget to start them\r
219            nofif_pool.start()\r
220            request_pool.start()\r
221         \r
222            # Setup the server\r
223            server = PooledJSONRPCServer(('localhost', 8080), config,\r
224                                         thread_pool=request_pool)\r
225            server.set_notification_pool(nofif_pool)\r
226         \r
227            # Register methods\r
228            server.register_function(pow)\r
229            server.register_function(lambda x,y: x+y, 'add')\r
230            server.register_function(lambda x: x, 'ping')\r
231         \r
232            try:\r
233                server.serve_forever()\r
234            finally:\r
235                # Stop the thread pools (let threads finish their current task)\r
236                request_pool.stop()\r
237                nofif_pool.stop()\r
238                server.set_notification_pool(None)\r
239         \r
240         Client Usage\r
241         ************\r
242         \r
243         This is (obviously) taken from a console session.\r
244         \r
245         .. code-block:: python\r
246         \r
247            >>> import jsonrpclib\r
248            >>> server = jsonrpclib.ServerProxy('http://localhost:8080')\r
249            >>> server.add(5,6)\r
250            11\r
251            >>> server.add(x=5, y=10)\r
252            15\r
253            >>> server._notify.add(5,6)\r
254            # No result returned...\r
255            >>> batch = jsonrpclib.MultiCall(server)\r
256            >>> batch.add(5, 6)\r
257            >>> batch.ping({'key':'value'})\r
258            >>> batch._notify.add(4, 30)\r
259            >>> results = batch()\r
260            >>> for result in results:\r
261            >>> ... print(result)\r
262            11\r
263            {'key': 'value'}\r
264            # Note that there are only two responses -- this is according to spec.\r
265         \r
266            # Clean up\r
267            >>> server('close')()\r
268         \r
269            # Using client history\r
270            >>> history = jsonrpclib.history.History()\r
271            >>> server = jsonrpclib.ServerProxy('http://localhost:8080', history=history)\r
272            >>> server.add(5,6)\r
273            11\r
274            >>> print(history.request)\r
275            {"id": "f682b956-c8e1-4506-9db4-29fe8bc9fcaa", "jsonrpc": "2.0",\r
276             "method": "add", "params": [5, 6]}\r
277            >>> print(history.response)\r
278            {"id": "f682b956-c8e1-4506-9db4-29fe8bc9fcaa", "jsonrpc": "2.0",\r
279             "result": 11}\r
280         \r
281            # Clean up\r
282            >>> server('close')()\r
283         \r
284         If you need 1.0 functionality, there are a bunch of places you can pass that in,\r
285         although the best is just to give a specific configuration to\r
286         ``jsonrpclib.ServerProxy``:\r
287         \r
288         .. code-block:: python\r
289         \r
290            >>> import jsonrpclib\r
291            >>> jsonrpclib.config.DEFAULT.version\r
292            2.0\r
293            >>> config = jsonrpclib.config.Config(version=1.0)\r
294            >>> history = jsonrpclib.history.History()\r
295            >>> server = jsonrpclib.ServerProxy('http://localhost:8080', config=config,\r
296                                                history=history)\r
297            >>> server.add(7, 10)\r
298            17\r
299            >>> print(history.request)\r
300            {"id": "827b2923-5b37-49a5-8b36-e73920a16d32",\r
301             "method": "add", "params": [7, 10]}\r
302            >>> print(history.response)\r
303            {"id": "827b2923-5b37-49a5-8b36-e73920a16d32", "error": null, "result": 17}\r
304            >>> server('close')()\r
305         \r
306         The equivalent ``loads`` and ``dumps`` functions also exist, although with minor\r
307         modifications. The ``dumps`` arguments are almost identical, but it adds three\r
308         arguments: ``rpcid`` for the 'id' key, ``version`` to specify the JSON-RPC\r
309         compatibility, and ``notify`` if it's a request that you want to be a\r
310         notification.\r
311         \r
312         Additionally, the ``loads`` method does not return the params and method like\r
313         ``xmlrpclib``, but instead a.) parses for errors, raising ProtocolErrors, and\r
314         b.) returns the entire structure of the request / response for manual parsing.\r
315         \r
316         \r
317         Additional headers\r
318         ******************\r
319         \r
320         If your remote service requires custom headers in request, you can pass them\r
321         as as a ``headers`` keyword argument, when creating the ``ServerProxy``:\r
322         \r
323         .. code-block:: python\r
324         \r
325            >>> import jsonrpclib\r
326            >>> server = jsonrpclib.ServerProxy("http://localhost:8080",\r
327                                                headers={'X-Test' : 'Test'})\r
328         \r
329         You can also put additional request headers only for certain method invocation:\r
330         \r
331         .. code-block:: python\r
332         \r
333            >>> import jsonrpclib\r
334            >>> server = jsonrpclib.Server("http://localhost:8080")\r
335            >>> with server._additional_headers({'X-Test' : 'Test'}) as test_server:\r
336            ...     test_server.ping(42)\r
337            ...\r
338            >>> # X-Test header will be no longer sent in requests\r
339         \r
340         Of course ``_additional_headers`` contexts can be nested as well.\r
341         \r
342         \r
343         Class Translation\r
344         *****************\r
345         \r
346         I've recently added "automatic" class translation support, although it is\r
347         turned off by default. This can be devastatingly slow if improperly used, so\r
348         the following is just a short list of things to keep in mind when using it.\r
349         \r
350         * Keep It (the object) Simple Stupid. (for exceptions, keep reading.)\r
351         * Do not require init params (for exceptions, keep reading)\r
352         * Getter properties without setters could be dangerous (read: not tested)\r
353         \r
354         If any of the above are issues, use the _serialize method. (see usage below)\r
355         The server and client must BOTH have use_jsonclass configuration item on and\r
356         they must both have access to the same libraries used by the objects for\r
357         this to work.\r
358         \r
359         If you have excessively nested arguments, it would be better to turn off the\r
360         translation and manually invoke it on specific objects using\r
361         ``jsonrpclib.jsonclass.dump`` / ``jsonrpclib.jsonclass.load`` (since the default\r
362         behavior recursively goes through attributes and lists / dicts / tuples).\r
363         \r
364          Sample file: *test_obj.py*\r
365         \r
366         .. code-block:: python\r
367         \r
368            # This object is /very/ simple, and the system will look through the\r
369            # attributes and serialize what it can.\r
370            class TestObj(object):\r
371                foo = 'bar'\r
372         \r
373            # This object requires __init__ params, so it uses the _serialize method\r
374            # and returns a tuple of init params and attribute values (the init params\r
375            # can be a dict or a list, but the attribute values must be a dict.)\r
376            class TestSerial(object):\r
377                foo = 'bar'\r
378                def __init__(self, *args):\r
379                    self.args = args\r
380                def _serialize(self):\r
381                    return (self.args, {'foo':self.foo,})\r
382         \r
383         * Sample usage\r
384         \r
385         .. code-block:: python\r
386         \r
387            >>> import jsonrpclib\r
388            >>> import test_obj\r
389         \r
390            # History is used only to print the serialized form of beans\r
391            >>> history = jsonrpclib.history.History()\r
392            >>> testobj1 = test_obj.TestObj()\r
393            >>> testobj2 = test_obj.TestSerial()\r
394            >>> server = jsonrpclib.Server('http://localhost:8080', history=history)\r
395         \r
396            # The 'ping' just returns whatever is sent\r
397            >>> ping1 = server.ping(testobj1)\r
398            >>> ping2 = server.ping(testobj2)\r
399         \r
400            >>> print(history.request)\r
401            {"id": "7805f1f9-9abd-49c6-81dc-dbd47229fe13", "jsonrpc": "2.0",\r
402             "method": "ping", "params": [{"__jsonclass__":\r
403                                           ["test_obj.TestSerial", []], "foo": "bar"}\r
404                                         ]}\r
405            >>> print(history.response)\r
406            {"id": "7805f1f9-9abd-49c6-81dc-dbd47229fe13", "jsonrpc": "2.0",\r
407             "result": {"__jsonclass__": ["test_obj.TestSerial", []], "foo": "bar"}}\r
408         \r
409         This behavior is turned by default. To deactivate it, just set the\r
410         ``use_jsonclass`` member of a server ``Config`` to False.\r
411         If you want to use a per-class serialization method, set its name in the\r
412         ``serialize_method`` member of a server ``Config``.\r
413         Finally, if you are using classes that you have defined in the implementation\r
414         (as in, not a separate library), you'll need to add those (on BOTH the server\r
415         and the client) using the ``config.classes.add()`` method.\r
416         \r
417         Feedback on this "feature" is very, VERY much appreciated.\r
418         \r
419         Why JSON-RPC?\r
420         *************\r
421         \r
422         In my opinion, there are several reasons to choose JSON over XML for RPC:\r
423         \r
424         * Much simpler to read (I suppose this is opinion, but I know I'm right. :)\r
425         * Size / Bandwidth - Main reason, a JSON object representation is just much smaller.\r
426         * Parsing - JSON should be much quicker to parse than XML.\r
427         * Easy class passing with ``jsonclass`` (when enabled)\r
428         \r
429         In the interest of being fair, there are also a few reasons to choose XML\r
430         over JSON:\r
431         \r
432         * Your server doesn't do JSON (rather obvious)\r
433         * Wider XML-RPC support across APIs (can we change this? :))\r
434         * Libraries are more established, i.e. more stable (Let's change this too.)\r
435         \r
436         Tests\r
437         *****\r
438         \r
439         Tests are an almost-verbatim drop from the JSON-RPC specification 2.0 page.\r
440         They can be run using *unittest* or *nosetest*:\r
441         \r
442         .. code-block:: console\r
443         \r
444            python -m unittest discover tests\r
445            python3 -m unittest discover tests\r
446            nosetests tests\r
447         \r
448 Platform: UNKNOWN\r
449 Classifier: Development Status :: 5 - Production/Stable\r
450 Classifier: Intended Audience :: Developers\r
451 Classifier: License :: OSI Approved :: Apache Software License\r
452 Classifier: Operating System :: OS Independent\r
453 Classifier: Programming Language :: Python :: 2.6\r
454 Classifier: Programming Language :: Python :: 2.7\r
455 Classifier: Programming Language :: Python :: 3\r
456 Classifier: Programming Language :: Python :: 3.0\r
457 Classifier: Programming Language :: Python :: 3.1\r
458 Classifier: Programming Language :: Python :: 3.2\r
459 Classifier: Programming Language :: Python :: 3.3\r
460 Classifier: Programming Language :: Python :: 3.4\r