Skip to content

Commit

Permalink
allow method renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
mosquito committed Jan 10, 2022
1 parent 496a3cb commit 5617649
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion aiohttp_xmlrpc/handler.py
Expand Up @@ -43,7 +43,11 @@ def __new__(cls, clsname, superclasses, attributedict):
continue

value = getattr(instance, key)
method_name = key.replace(instance.METHOD_PREFIX, "", 1)

method_name = getattr(value, "__xmlrpc_name__", None)
if method_name is None:
method_name = key.replace(instance.METHOD_PREFIX, "", 1)

allowed_methods[method_name] = key
argmapping[method_name] = inspect.getfullargspec(value)

Expand Down Expand Up @@ -185,3 +189,10 @@ def _build_xml(cls, tree):
encoding="utf-8",
pretty_print=cls.DEBUG,
)


def rename(new_name):
def decorator(func):
func.__xmlrpc_name__ = new_name
return func
return decorator
10 changes: 10 additions & 0 deletions tests/test_handler.py
Expand Up @@ -8,6 +8,7 @@
from lxml import etree
from lxml.builder import E

from aiohttp_xmlrpc.handler import rename

pytest_plugins = (
"aiohttp.pytest_plugin",
Expand Down Expand Up @@ -52,6 +53,10 @@ def rpc_dict_kwargs(self, d, **kw):
def rpc_dict_kw_only_args(self, d, *, foo, **kw):
return (d, foo, kw)

@rename("method_with.new_name")
def rpc_ranamed(self):
return "renamed_function"


class XMLRPCChild(XMLRPCMain):
def rpc_child(self):
Expand Down Expand Up @@ -214,3 +219,8 @@ async def test_13_kw_only_args(client):
{"foo": "bar"}, foo=32, spam="egg"
)
assert result == [{"foo": "bar"}, 32, {"spam": "egg"}]


async def test_14_method_renaming(client):
result = await client.method_with.new_name()
assert result == "renamed_function"

0 comments on commit 5617649

Please sign in to comment.