Skip to content

Commit 2332b34

Browse files
phlogistonjohnbehrmann
authored andcommitted
varlink/server.py: add argument to service for customizing json encoder
Add a new argument to the Service class so that code using that class can specify a custom json encoder class, ideally one that subclasses from VarlinkEncoder. Signed-off-by: John Mulligan <jmulligan@redhat.com>
1 parent 2861678 commit 2332b34

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

varlink/server.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,21 @@ class Service:
5252
5353
"""
5454

55-
def __init__(self, vendor="", product="", version="", url="", interface_dir=".", namespaced=False):
55+
def __init__(
56+
self,
57+
vendor="",
58+
product="",
59+
version="",
60+
url="",
61+
interface_dir=".",
62+
namespaced=False,
63+
*,
64+
json_encoder_cls=None,
65+
):
5666
"""Initialize the service with the data org.varlink.service.GetInfo() returns
5767
5868
:param interface_dir: the directory with the \\*.varlink files for the interfaces
69+
:param json_encoder_cls: Optional; supply a json.JSONEncoder subclass to use instead of the default
5970
6071
"""
6172
self.vendor = vendor
@@ -64,6 +75,7 @@ def __init__(self, vendor="", product="", version="", url="", interface_dir=".",
6475
self.url = url
6576
self.interface_dir = interface_dir
6677
self._namespaced = namespaced
78+
self.json_encoder_cls = json_encoder_cls or VarlinkEncoder
6779

6880
self.interfaces = {}
6981
self.interfaces_handlers = {}
@@ -248,7 +260,7 @@ def handle(self, message, _server=None, _request=None):
248260
if out is None:
249261
return
250262
try:
251-
yield json.dumps(out, cls=VarlinkEncoder).encode("utf-8")
263+
yield json.dumps(out, cls=self.json_encoder_cls).encode("utf-8")
252264
except ConnectionError as e:
253265
try:
254266
handle.throw(e)

0 commit comments

Comments
 (0)