Skip to content

Commit

Permalink
Add automatic conversion of received data
Browse files Browse the repository at this point in the history
allow query of raw data by specifying raw=True
  • Loading branch information
cygnusb committed Feb 11, 2019
1 parent a189ae2 commit b6b9f3b
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pypureomapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,8 @@ def __lookup(self, ltype, **kwargs):
assert ltype_utf in [b"host", b"lease"]
msg = OmapiMessage.open(ltype_utf)
for k in kwargs:
if k == "raw":
continue
_k = k.replace("_", "-")
if _k in ["ip", "ip-address"]:
msg.obj.append((b"ip-address", pack_ip(kwargs[k])))
Expand All @@ -1214,13 +1216,22 @@ def __lookup(self, ltype, **kwargs):
response = self.query_server(msg)
if response.opcode != OMAPI_OP_UPDATE:
raise OmapiErrorNotFound()
if "raw" in kwargs and kwargs["raw"]:
return dict(response.obj)
res = dict()
for k, v in dict(response.obj).items():
_k = k.decode('utf-8')
if _k == "ip-address":
v = unpack_ip(v)
elif _k in ["hardware-address"]:
v = unpack_mac(v)
try:
if _k == "ip-address":
v = unpack_ip(v)
elif _k in ["hardware-address"]:
v = unpack_mac(v)
elif _k in ["starts", "ends", "tstp", "tsfp", "atsfp", "cltt", "subnet", "pool", "state", "hardware-type"]:
v = struct.unpack(">I", v)[0]
elif _k in ["flags"]:
v = struct.unpack(">I", v)[0]
except struct.error:
pass
res[_k] = v
return res

Expand Down

0 comments on commit b6b9f3b

Please sign in to comment.