Open Liso77 opened 7 years ago
Just discovered this library and will have a more detailed look at it later.
I'm using Linux Mint 18.3 (based on Ubuntu 16.04) with Anaconda Python 3.6.3 and so far it's been great. I defined a couple of functions to handle the byte string issue and support multiple keys as a bonus:
#!/home/john/anaconda3/bin/python3.6
import sys
from xdo import Xdo
from time import sleep
def sendkeys(*keys):
for k in keys: xdo.send_keysequence_window(0, k.encode())
def type(text):
xdo.enter_text_window(0, text.encode())
sleep(0.5)
xdo = Xdo()
if 'Trades' in xdo.get_window_name(xdo.get_active_window()).decode():
An alternative would be to wrap the class instance so that uuencode was converted to bytes & vice versa. Here's one that works for me:
class Wrapper():
# wrap xdo instance for python 3 to convert between bytes & uuencode
def __init__(self, _xdo):
self._xdo = _xdo
def __getattr__(self, name):
func = getattr(self.__dict__['_xdo'], name)
if callable(func):
def my_wrapper(*args, **kwargs):
args = [a.encode() if isinstance(a, str) else a for a in args]
kwargs = {k: a.encode() if isinstance(a, str) else a for k, a in kwargs.items()}
ret = func(*args, **kwargs)
return ret.decode() if isinstance(ret, bytes) else ret
return my_wrapper
else:
return func.decode() if isinstance(func, bytes) else func
xdo = Wrapper(Xdo())
Possibly preferable is:
class Xdo3(Xdo):
def __getattribute__(self, attr):
try:
func = getattr(Xdo, attr)
except AttributeError:
return Xdo.__getattribute__(self, attr)
if callable(func):
def my_wrapper(*args, **kwargs):
args = [a.encode() if isinstance(a, str) else a for a in args]
kwargs = {k: a.encode() if isinstance(a, str) else a
for k, a in kwargs.items()}
ret = func(self, *args, **kwargs)
return ret.decode() if isinstance(ret, bytes) else ret
return my_wrapper
else:
return func.decode() if isinstance(func, bytes) else func
xdo = Xdo3()
xdo.send_keysequence_window(ff_win_id, "alt+F4")
doesn`t work. But no problem with alt and F4 separated.
Example in readme.rst is based on python2
b'Python rocks!' is better than 'Python rocks!' .
Couldn't we also translate str to bytes where it is appropriate?