diff options
| -rw-r--r-- | pushover.py | 58 | ||||
| -rw-r--r-- | setup.py | 5 |
2 files changed, 54 insertions, 9 deletions
diff --git a/pushover.py b/pushover.py index 0e3f4e6..2a7c61d 100644 --- a/pushover.py +++ b/pushover.py @@ -6,15 +6,19 @@ A typical use of the module looks like this:: import pushover pushover.init("token") - client = Client("client-id") + client = pushover.Client("client-id") client.send_message("Hello!", title="Hello", priority=1) """ -import requests import time +from ConfigParser import RawConfigParser +from argparse import ArgumentParser +import os + +import requests __all__ = ["init", "get_sounds", "Client", "MessageRequest", - "InitError", "RequestError"] + "InitError", "RequestError", "get_client"] BASE_URL = "https://api.pushover.net/1/" MESSAGE_URL = BASE_URL + "messages.json" @@ -86,7 +90,7 @@ class Request: raise RequestError(self.answer["errors"]) def __str__(self): - print self.answer + return str(self.answer) class MessageRequest(Request): """Class representing a message request to the Pushover API. You do not @@ -192,7 +196,7 @@ class Client: if key not in valid_keywords: raise ValueError("{0}: invalid message parameter".format(key)) - if key == "timestamp" and value == True: + if key == "timestamp" and value is True: payload[key] = int(time.time()) elif key == "sound": if not SOUNDS: @@ -206,9 +210,46 @@ class Client: return MessageRequest(payload) +def get_client(profile=None, config_path='~/.pushover'): + """Create a :class:`Client` object from a default configuration file. -if __name__ == "__main__": - from argparse import ArgumentParser + e.g. + ``` + #This is the default profile (returned by get_client() with no arguments.) + [Default] + api_token=aaaaaa + user_key=xxxxxx + + # You can specify a device as well. + [Sam-iPhone] + api_token=bbbbbb + user_key=yyyyyy + device=iPhone + ``` + + * ``profile``: the profile to load as a client (`Default` by default.) + * ``config_path``: path of the configuration file (`~/.pushover` by default.) + """ + + + config_path = os.path.expanduser(config_path) + + if not os.path.exists(config_path): + raise IOError(2, "No such file", config_path) + + config = RawConfigParser({"device": None}) + config.read(config_path) + + section = profile or 'Default' + + init(config.get(section, 'api_token'), sound=False) + + return Client( + config.get(section, 'user_key'), + device=config.get(section, 'device') + ) + +def main(): parser = ArgumentParser(description="Send a message to pushover.") parser.add_argument("--token", help="Pushover application token", required=True) @@ -225,3 +266,6 @@ if __name__ == "__main__": Client(args.client).send_message(args.message, title=args.title, priority=args.priority, url=args.url, url_title=args.url_title, timestamp=True) + +if __name__ == "__main__": + main() @@ -1,6 +1,6 @@ #!/usr/bin/env python -from distutils.core import setup +from setuptools import setup setup(name='python-pushover', version='0.1', @@ -10,6 +10,7 @@ setup(name='python-pushover', author='Thibaut Horel', author_email='thibaut+pushover@gmail.com', py_modules=['pushover'], - requires=['requests'], + entry_points={"console_scripts": ["pushover = pushover:main"]}, + install_requires=['requests'], license='GNU GPLv3' ) |
