diff options
author | Chris Billington <chrisjbillington@gmail.com> | 2018-05-29 09:31:10 +1000 |
---|---|---|
committer | Chris Billington <chrisjbillington@gmail.com> | 2018-05-29 09:31:33 +1000 |
commit | e62dac857a503464b2f2ae12094199f667940ee4 (patch) | |
tree | 18f011e67c3816b32662ec8534a8c169f2db1614 /tools/EventClients/lib | |
parent | b946a56cec65c27f05783cbd52666d2c639643db (diff) |
Port xbmcclient.py to Python 3, retaining Python 2 compatibility
Bump minor version to 0.1 and remove blanket exception catching
in favour of raising unexpected exceptions.
Diffstat (limited to 'tools/EventClients/lib')
-rw-r--r-- | tools/EventClients/lib/python/xbmcclient.py | 75 |
1 files changed, 39 insertions, 36 deletions
diff --git a/tools/EventClients/lib/python/xbmcclient.py b/tools/EventClients/lib/python/xbmcclient.py index fd29f5b4c4..424f53c64e 100644 --- a/tools/EventClients/lib/python/xbmcclient.py +++ b/tools/EventClients/lib/python/xbmcclient.py @@ -37,11 +37,16 @@ at least once every 60 seconds. To "ping" XBMC with an empty packet use PacketPING or XBMCClient.ping(). See the documentation for details. """ +from __future__ import unicode_literals, print_function, absolute_import, division + __author__ = "d4rk@xbmc.org" -__version__ = "0.0.3" +__version__ = "0.1.0" +import sys +if sys.version_info.major == 2: + str = unicode from struct import pack -from socket import * +from socket import socket, AF_INET, SOCK_DGRAM, SOL_SOCKET, SO_BROADCAST import time MAX_PACKET_SIZE = 1024 @@ -96,7 +101,7 @@ ACTION_BUTTON = 0x02 def format_string(msg): """ """ - return msg + "\0" + return msg.encode() + b"\0" def format_uint32(num): """ """ @@ -139,17 +144,15 @@ class Packet: ----------------------------- """ def __init__(self): - self.sig = "XBMC" + self.sig = b"XBMC" self.minver = 0 self.majver = 2 self.seq = 1 self.maxseq = 1 self.payloadsize = 0 self.uid = UNIQUE_IDENTIFICATION - self.reserved = "\0" * 10 - self.payload = "" - return - + self.reserved = b"\0" * 10 + self.payload = b"" def append_payload(self, blob): """Append to existing payload @@ -157,6 +160,8 @@ class Packet: Arguments: blob -- binary data to append to the current payload """ + if isinstance(blob, str): + blob = blob.encode() self.set_payload(self.payload + blob) @@ -166,6 +171,8 @@ class Packet: Arguments: payload -- binary data that contains the payload """ + if isinstance(payload, str): + payload = payload.encode() self.payload = payload self.payloadsize = len(self.payload) self.maxseq = int((self.payloadsize + (MAX_PAYLOAD_SIZE - 1)) / MAX_PAYLOAD_SIZE) @@ -191,8 +198,8 @@ class Packet: if packettype < 0: packettype = self.packettype header = self.sig - header += chr(self.majver) - header += chr(self.minver) + header += chr(self.majver).encode() + header += chr(self.minver).encode() header += format_uint16(packettype) header += format_uint32(seq) header += format_uint32(maxseq) @@ -225,8 +232,8 @@ class Packet: (default 1) """ if packetnum > self.num_packets() or packetnum < 1: - return "" - header = "" + return b"" + header = b"" if packetnum==1: header = self.get_header(self.packettype, packetnum, self.maxseq, self.get_payload_size(packetnum)) @@ -249,11 +256,7 @@ class Packet: """ self.uid = uid for a in range ( 0, self.num_packets() ): - try: - sock.sendto(self.get_udp_message(a+1), addr) - except: - return False - return True + sock.sendto(self.get_udp_message(a+1), addr) class PacketHELO (Packet): @@ -279,7 +282,9 @@ class PacketHELO (Packet): self.append_payload( format_uint32 (0) ) # reserved1 self.append_payload( format_uint32 (0) ) # reserved2 if icon_type != ICON_NONE and icon_file: - self.append_payload( file(icon_file).read() ) + with open(icon_file, 'rb') as f: + self.append_payload(f.read()) + class PacketNOTIFICATION (Packet): """A NOTIFICATION packet @@ -305,7 +310,8 @@ class PacketNOTIFICATION (Packet): self.append_payload( chr (icon_type) ) self.append_payload( format_uint32 (0) ) # reserved if icon_type != ICON_NONE and icon_file: - self.append_payload( file(icon_file).read() ) + with open(icon_file, 'rb') as f: + self.append_payload(f.read()) class PacketBUTTON (Packet): """A BUTTON packet @@ -438,7 +444,7 @@ class PacketLOG (Packet): self.append_payload( chr (loglevel) ) self.append_payload( format_string(logmessage) ) if (autoprint): - print(logmessage) + print(logmessage) class PacketACTION (Packet): """An ACTION packet @@ -495,19 +501,19 @@ class XBMCClient: self.port = int(port) self.addr = (self.ip, self.port) packet = PacketHELO(self.name, self.icon_type, self.icon_file) - return packet.send(self.sock, self.addr, self.uid) + packet.send(self.sock, self.addr, self.uid) def close(self): """Close the current connection""" packet = PacketBYE() - return packet.send(self.sock, self.addr, self.uid) + packet.send(self.sock, self.addr, self.uid) def ping(self): """Send a PING packet""" packet = PacketPING() - return packet.send(self.sock, self.addr, self.uid) + packet.send(self.sock, self.addr, self.uid) def send_notification(self, title="", message="", icon_file=None): @@ -521,7 +527,7 @@ class XBMCClient: packet = PacketNOTIFICATION(title, message, self._get_icon_type(icon_file), icon_file) - return packet.send(self.sock, self.addr, self.uid) + packet.send(self.sock, self.addr, self.uid) def send_keyboard_button(self, button=None): @@ -531,7 +537,7 @@ class XBMCClient: """ if not button: return - return self.send_button(map="KB", button=button) + self.send_button(map="KB", button=button) def send_remote_button(self, button=None): @@ -541,13 +547,13 @@ class XBMCClient: """ if not button: return - return self.send_button(map="R1", button=button) + self.send_button(map="R1", button=button) def release_button(self): """Release all buttons""" packet = PacketBUTTON(code=0x01, down=0) - return packet.send(self.sock, self.addr, self.uid) + packet.send(self.sock, self.addr, self.uid) def send_button(self, map="", button="", amount=0): @@ -569,7 +575,7 @@ class XBMCClient: "printscreen", "minus", "x", etc. """ packet = PacketBUTTON(map_name=str(map), button_name=str(button), amount=amount) - return packet.send(self.sock, self.addr, self.uid) + packet.send(self.sock, self.addr, self.uid) def send_button_state(self, map="", button="", amount=0, down=0, axis=0): """Send a button event to XBMC @@ -590,13 +596,10 @@ class XBMCClient: "printscreen", "minus", "x", etc. """ if axis: - if amount == 0: - down = 0 - else: - down = 1 + down = int(amount != 0) packet = PacketBUTTON(map_name=str(map), button_name=str(button), amount=amount, down=down, queue=1, axis=axis) - return packet.send(self.sock, self.addr, self.uid) + packet.send(self.sock, self.addr, self.uid) def send_mouse_position(self, x=0, y=0): """Send a mouse event to XBMC @@ -606,7 +609,7 @@ class XBMCClient: y -- same a 'x' but relates to the screen height """ packet = PacketMOUSE(int(x), int(y)) - return packet.send(self.sock, self.addr, self.uid) + packet.send(self.sock, self.addr, self.uid) def send_log(self, loglevel=0, logmessage="", autoprint=True): """ @@ -616,7 +619,7 @@ class XBMCClient: autoprint -- if the logmessage should automatically be printed to stdout """ packet = PacketLOG(loglevel, logmessage) - return packet.send(self.sock, self.addr, self.uid) + packet.send(self.sock, self.addr, self.uid) def send_action(self, actionmessage="", actiontype=ACTION_EXECBUILTIN): """ @@ -625,7 +628,7 @@ class XBMCClient: actiontype -- The ActionType the ActionString should be sent to. """ packet = PacketACTION(actionmessage, actiontype) - return packet.send(self.sock, self.addr, self.uid) + packet.send(self.sock, self.addr, self.uid) def _get_icon_type(self, icon_file): if icon_file: |