rshk / python-libxdo

Python bindings for libxdo
BSD 3-Clause "New" or "Revised" License
77 stars 19 forks source link

python3 #18

Open Liso77 opened 7 years ago

Liso77 commented 7 years ago

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?

    if winname is not None:
            if isinstance(winname, str):  # checking python version is probably not necessary
                    winname = winname.encode(self.encoding)  # default encoding could be utf8 or maybe latin1 ... 
            search.winname = winname
            search.searchmask |= SEARCH_NAME
john9631 commented 6 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():
john9631 commented 6 years ago

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()
steemandlinux commented 6 years ago

xdo.send_keysequence_window(ff_win_id, "alt+F4")

doesn`t work. But no problem with alt and F4 separated.