Skip to content

Commit

Permalink
handle "None" aka "Null" values
Browse files Browse the repository at this point in the history
  • Loading branch information
haraldh committed Mar 19, 2018
1 parent 558945a commit a63c2de
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
6 changes: 3 additions & 3 deletions examples/client-simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
client.open('org.example.more', namespaced=True) as con1, \
client.open('org.example.more', namespaced=True) as con2:
for m in con1.TestMore(10, _more=True):
if hasattr(m.state, 'start'):
if hasattr(m.state, 'start') and m.state.start != None:
if m.state.start:
print("--- Start ---", file=sys.stderr)

if hasattr(m.state, 'end'):
if hasattr(m.state, 'end') and m.state.end != None:
if m.state.end:
print("--- End ---", file=sys.stderr)

if hasattr(m.state, 'progress'):
if hasattr(m.state, 'progress') and m.state.progress != None:
print("Progress:", m.state.progress, file=sys.stderr)
if m.state.progress > 50:
ret = con2.Ping("Test")
Expand Down
10 changes: 5 additions & 5 deletions examples/client-twisted.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ def replyMore(self):
message = yield self._next_message()
if self._namespaced:
message = json.loads(message, object_hook=lambda d: SimpleNamespace(**d))
if hasattr(message, "error"):
if hasattr(message, "error") and message.error != None:
raise varlink.VarlinkError(message, self._namespaced)
else:
return message.parameters, hasattr(message, "continues") and message.continues
else:
message = json.loads(message)
if 'error' in message:
if 'error' in message and message['error'] != None:
raise varlink.VarlinkError(message, self._namespaced)
else:
return message['parameters'], ('continues' in message) and message['continues']
Expand Down Expand Up @@ -186,14 +186,14 @@ def main(reactor, address):
(m, more) = yield con1.replyMore()

if hasattr(m.state, 'start'):
if m.state.start:
if m.state.start and m.state.start != None:
print("--- Start ---", file=sys.stderr)

if hasattr(m.state, 'end'):
if hasattr(m.state, 'end') and m.state.end != None:
if m.state.end:
print("--- End ---", file=sys.stderr)

if hasattr(m.state, 'progress'):
if hasattr(m.state, 'progress') and m.state.progress != None:
print("Progress:", m.state.progress, file=sys.stderr)
if m.state.progress > 50:
yield con2.Ping("Test")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name = "varlink",
packages = ["varlink"],
version = "13",
version = "14",
description = "Varlink",
long_description = "Python implementation of the varlink protocol http://varlink.org",
author = "Lars Karlitski<lars@karlitski.net>, Harald Hoyer<harald@redhat.com>",
Expand Down
15 changes: 13 additions & 2 deletions varlink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@


class VarlinkEncoder(json.JSONEncoder):

def default(self, o):
if isinstance(o, SimpleNamespace):
return o.__dict__
Expand Down Expand Up @@ -129,6 +130,7 @@ def _next_message(self):
raise NotImplementedError

def _add_method(self, method):

def _wrapped(*args, **kwargs):
if "_more" in kwargs and kwargs.pop("_more"):
return self._call_more(method.name, *args, **kwargs)
Expand All @@ -145,13 +147,13 @@ def _next_varlink_message(self):

if self._namespaced:
message = json.loads(message, object_hook=lambda d: SimpleNamespace(**d))
if hasattr(message, "error"):
if hasattr(message, "error") and message.error != None:
raise VarlinkError(message, self._namespaced)
else:
return message.parameters, hasattr(message, "continues") and message.continues
else:
message = json.loads(message)
if 'error' in message:
if 'error' in message and message["error"] != None:
raise VarlinkError(message, self._namespaced)
else:
return message['parameters'], ('continues' in message) and message['continues']
Expand Down Expand Up @@ -628,6 +630,7 @@ def _add_interface(self, filename, handler):
self.interfaces_handlers[interface.name] = handler

def interface(self, filename):

def decorator(interface_class):
self._add_interface(filename, interface_class())
return interface_class
Expand Down Expand Up @@ -852,32 +855,38 @@ def read_member(self):


class _Struct:

def __init__(self, fields):
self.fields = collections.OrderedDict(fields)


class _Union:

def __init__(self, fields):
self.fields = fields


class _Array:

def __init__(self, element_type):
self.element_type = element_type


class _CustomType:

def __init__(self, name):
self.name = name


class _Alias:

def __init__(self, name, varlink_type):
self.name = name
self.type = varlink_type


class _Method:

def __init__(self, name, in_type, out_type, _signature):
self.name = name
self.in_type = in_type
Expand All @@ -886,13 +895,15 @@ def __init__(self, name, in_type, out_type, _signature):


class _Error:

def __init__(self, name, varlink_type):
self.name = name
self.type = varlink_type


# Used by the SimpleServer
class _Connection:

def __init__(self, _socket):
self._socket = _socket
self._in_buffer = b''
Expand Down

0 comments on commit a63c2de

Please sign in to comment.