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

Fix #77 - Stop throwing exceptions if $WINDOWID has not been set. #132

Merged
merged 1 commit into from
Jan 11, 2017
Merged

Fix #77 - Stop throwing exceptions if $WINDOWID has not been set. #132

merged 1 commit into from
Jan 11, 2017

Conversation

pmav99
Copy link
Contributor

@pmav99 pmav99 commented Jan 11, 2017

Traceback (most recent call last):                                                                                                                                                                                 
  File "/usr/bin/ntfy", line 11, in <module>
    load_entry_point('ntfy==2.4.1', 'console_scripts', 'ntfy')()
  File "/usr/lib/python3.6/site-packages/ntfy/cli.py", line 341, in main
    message, retcode = args.func(args)
  File "/usr/lib/python3.6/site-packages/ntfy/cli.py", line 64, in run_cmd
    if args.unfocused_only and is_focused():
  File "/usr/lib/python3.6/site-packages/ntfy/terminal.py", line 61, in is_focused
    return linux_window_is_focused()
  File "/usr/lib/python3.6/site-packages/ntfy/terminal.py", line 15, in linux_window_is_focused
    return int(environ['WINDOWID']) == window_id
  File "/usr/lib/python3.6/os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'WINDOWID'

The KeyError comes from the usage of environ["WINDOWID"]. What should be used instead is the get method, e.g. environ.get("WINDOWID") which return None if the key is missing. Of course None is also a non-valid argument for int() but I think that defaulting to 0 is reasonable.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 87.5% when pulling cb0b11d on pmav99:issue-77 into 6ab15e0 on dschep:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 87.5% when pulling cb0b11d on pmav99:issue-77 into 6ab15e0 on dschep:master.

@pmav99
Copy link
Contributor Author

pmav99 commented Jan 11, 2017

Travis builds for python 3.3 and 3.4 are failing but the failures seem to be unrelated with the code changes.

window_id = int(check_output(['xprop', '-root', '\t$0',
'_NET_ACTIVE_WINDOW']).split()[1], 16)
return int(environ['WINDOWID']) == window_id
xprop_cmd = shlex.split("xprop -root _NET_ACTIVE_WINDOW")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason for using shlex and splitting a str command instead of defining it as a list to begin with?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shlex.split("xprop -root _NET_ACTIVE_WINDOW")
# vs
['xprop', '-root', '\t$0', '_NET_ACTIVE_WINDOW']

IMHO, shlex has the following advantages:

  • It is more readable: less visual clutter (quotes and commas), especially if you have a long command.
  • It is more easily testable: just copy paste the command and run it on your shell.

All in all I believe that using shlex is a better practice in general and I use it on my own code, but this is not my repo, so if you prefer a list, a list it shall be :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me 👍

Copy link
Owner

@dschep dschep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch " use to ' and I'm happy with this change (I should probably make the linter enforce that rule 😁)

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 87.5% when pulling 5c2128e on pmav99:issue-77 into 6ab15e0 on dschep:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 87.5% when pulling 5c2128e on pmav99:issue-77 into 6ab15e0 on dschep:master.

@pmav99
Copy link
Contributor Author

pmav99 commented Jan 11, 2017

I rebased.

BTW, I think that get_tty() is not being used anywhere and it should probably be removed.

@dschep dschep merged commit bab1dc8 into dschep:master Jan 11, 2017
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

Successfully merging this pull request may close these issues.

3 participants