Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'Programming Language :: Python :: 3.5',
'Topic :: Communications'
]
INSTALL_REQUIRES = ['requests']
INSTALL_REQUIRES = ['requests', 'click']
EXTRAS_REQUIRE = {}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

We might want to make the CLI an "extra"? Not completely sure how I feel about that, what do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  1. nah I think it's worth including
  2. ehh maybe
  3. I don't care either way

TESTS_REQUIRE = ['tox']

Expand Down Expand Up @@ -69,5 +69,9 @@ def get_long_description():
package_dir={'': 'src'},
install_requires=INSTALL_REQUIRES,
extras_require=EXTRAS_REQUIRE,
tests_require=TESTS_REQUIRE
tests_require=TESTS_REQUIRE,
entry_points={
'console_scripts':
['pushover=pushover_complete.pushover_cli:cli']
}
)
3 changes: 3 additions & 0 deletions src/pushover_complete/pushover_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class PushoverAPI(object):
def __init__(self, token):
self.token = token

def __repr__(self):
return "PushoverAPI(token={})".format(self.token)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I knew I forgot something!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

gotta add some tests for this. Are these standard __repr__ functions suppsoed to get the class name from self.__class__ instead of being hard-coded?


def _generic_get(self, endpoint, url_parameter=None, payload=None, session=None):
"""A method for abstracting GET requests to the Pushover API.

Expand Down
138 changes: 138 additions & 0 deletions src/pushover_complete/pushover_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
from .pushover_api import PushoverAPI
import click
import os
import configparser

PUSHOVER_CONFIG = os.path.expanduser("~/.config/pushover_complete/config.ini")


@click.group()
@click.option('--config', '-c')
@click.option('--config-preset', '-p')
@click.option('--token', '-t')
@click.pass_context
def cli(ctx, config, config_preset, token):
cf = dict()
has_global_config = os.path.exists(PUSHOVER_CONFIG)

if has_global_config or config:
cfp = configparser.ConfigParser()
cfp.read(PUSHOVER_CONFIG if has_global_config and config is None else config)
cf = dict(cfp['DEFAULT'])
if config_preset is not None:
cf.update(cfp[config_preset])

token = cf.pop('token')
if token is None:
ctx.fail("missing TOKEN")

pushover = PushoverAPI(token)

ctx.obj = pushover, cf


@cli.command()
@click.argument('message')
@click.option('--user')
@click.option('--device')
@click.option('--title')
@click.option('--url')
@click.option('--url-title')
@click.option('--priority')
@click.option('--retry', type=int)
@click.option('--expire', type=int)
@click.option('--callback-url')
@click.option('--timestamp', type=int)
@click.option('--sound')
@click.option('--html', type=int)
@click.pass_context
def send(ctx, message, **kwargs):
pushover, cf = ctx.obj
cf.update({k: v for k, v in kwargs.items() if v is not None})

if 'user' not in cf:
ctx.fail('missing USER')
user = cf.pop('user')

response = pushover.send_message(user, message, **cf)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Using the ** expansion is great, but what happens if a key is defined in the configuration that doesn't correspond with this particular command (like a group key or something)?

click.echo(response)


@cli.command()
@click.argument('user')
@click.argument('device')
@click.pass_obj
def validate(obj, user, device):
pushover, cf = obj
response = pushover.validate(user, device)
click.echo(response)


@cli.command()
@click.argument('receipt')
@click.pass_obj
def check_receipt(obj, receipt):
pushover, cf = obj
response = pushover.check_receipt(receipt)
click.echo(response)


@cli.command()
@click.argument('receipt')
@click.pass_obj
def cancel_receipt(obj, receipt):
pushover, cf = obj
response = pushover.cancel_receipt(receipt)
click.echo(response)


@cli.command()
def migrate():
pass


@cli.command()
@click.argument('user')
def assign(user):
pass


@cli.group()
@click.option('group-key')
def group(group_key):
pass


@group.command()
@click.argument('group')
@click.argument('user')
def add(group, user):
pass


@group.command()
@click.argument('group')
@click.argument('user')
def delete(group, user):
pass


@group.command()
@click.argument('group')
@click.argument('user')
def enable(group, user):
pass


@group.command()
@click.argument('group')
@click.argument('user')
def disable(group, user):
pass


@group.command()
@click.argument('group')
@click.argument('name')
def rename(group, name):
pass