Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python3 #18

Open
Liso77 opened this issue Feb 28, 2017 · 3 comments
Open

python3 #18

Liso77 opened this issue Feb 28, 2017 · 3 comments

Comments

@Liso77
Copy link

Liso77 commented Feb 28, 2017

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
Copy link

john9631 commented Nov 20, 2017

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
Copy link

john9631 commented Nov 22, 2017

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
Copy link

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants