From 52b379fee609a40b54af4cc70c8b49daf71148a6 Mon Sep 17 00:00:00 2001 From: Sam Birch Date: Tue, 22 Jul 2014 01:45:28 -0400 Subject: Convenience function to create a client Loads a config file at and creates a client according to various options. Handy IMO so you don't have to constantly look up your user key etc. Has some precedent in a similar system used by boto for AWS credentials (http://boto.readthedocs.org/en/latest/boto_config_tut.html) --- pushover.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/pushover.py b/pushover.py index f6f8e98..8c5bb6a 100644 --- a/pushover.py +++ b/pushover.py @@ -14,7 +14,7 @@ import requests import time __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" @@ -206,6 +206,40 @@ class Client: return MessageRequest(payload) +def get_client(user=None, config_path='~/.pushover'): + """Create a :class:`Client` object from a default configuration file. + + e.g. + ``` + #This is the default user (returned by get_client() with no arguments.) + [Pushover] + user_key=xxxxxx + + # You can specify a device as well. + [Sam-iPhone] + user_key=yyyyyy + device=iPhone + ``` + + * ``user``: the profile to load as a client (`Pushover` by default.) + * ``config_path``: path of the configuration file (`~/.pushover` by default.) + """ + import ConfigParser + import os + + config_path = os.path.expanduser(config_path) + + if not os.path.exists(config_path): + raise Exception("Configuration file not found ({0})".format(config_path)) + + config = ConfigParser.RawConfigParser() + config.read(config_path) + + section = user if user is not None else 'Pushover' + return Client( + config.get(section, 'user_key'), + device=config.get(section, 'device') if config.has_option(section, 'device') else None + ) if __name__ == "__main__": from argparse import ArgumentParser -- cgit v1.2.3-70-g09d2 From 23d81cc523315aa7159a9dd10f843ba412004614 Mon Sep 17 00:00:00 2001 From: Sam Birch Date: Tue, 22 Jul 2014 12:04:54 -0400 Subject: Clarified some of the terms --- pushover.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pushover.py b/pushover.py index 8c5bb6a..73ed59d 100644 --- a/pushover.py +++ b/pushover.py @@ -206,13 +206,13 @@ class Client: return MessageRequest(payload) -def get_client(user=None, config_path='~/.pushover'): +def get_client(profile=None, config_path='~/.pushover'): """Create a :class:`Client` object from a default configuration file. e.g. ``` - #This is the default user (returned by get_client() with no arguments.) - [Pushover] + #This is the default profile (returned by get_client() with no arguments.) + [Default] user_key=xxxxxx # You can specify a device as well. @@ -221,7 +221,7 @@ def get_client(user=None, config_path='~/.pushover'): device=iPhone ``` - * ``user``: the profile to load as a client (`Pushover` by default.) + * ``profile``: the profile to load as a client (`Default` by default.) * ``config_path``: path of the configuration file (`~/.pushover` by default.) """ import ConfigParser @@ -235,7 +235,7 @@ def get_client(user=None, config_path='~/.pushover'): config = ConfigParser.RawConfigParser() config.read(config_path) - section = user if user is not None else 'Pushover' + section = profile if profile is not None else 'Default' return Client( config.get(section, 'user_key'), device=config.get(section, 'device') if config.has_option(section, 'device') else None -- cgit v1.2.3-70-g09d2 From 989327291c6a53e457f034c2c1d03848eb32ed46 Mon Sep 17 00:00:00 2001 From: Sam Birch Date: Tue, 22 Jul 2014 23:47:12 -0400 Subject: init() in get_client() I think it makes sense to init() in here too, and allow the user to keep the api_tokens in the config file. --- pushover.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pushover.py b/pushover.py index 73ed59d..2284c7a 100644 --- a/pushover.py +++ b/pushover.py @@ -213,10 +213,12 @@ def get_client(profile=None, config_path='~/.pushover'): ``` #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 ``` @@ -236,11 +238,15 @@ def get_client(profile=None, config_path='~/.pushover'): config.read(config_path) section = profile if profile is not None else 'Default' + + init(config.get(section, 'api_token'), sound=False) + return Client( config.get(section, 'user_key'), device=config.get(section, 'device') if config.has_option(section, 'device') else None ) + if __name__ == "__main__": from argparse import ArgumentParser parser = ArgumentParser(description="Send a message to pushover.") -- cgit v1.2.3-70-g09d2 From 201bbafc647595b33cde31ba988d5d61a5545b9c Mon Sep 17 00:00:00 2001 From: Sam Birch Date: Wed, 23 Jul 2014 23:34:32 -0400 Subject: Addressed changes --- pushover.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pushover.py b/pushover.py index 2284c7a..8b840df 100644 --- a/pushover.py +++ b/pushover.py @@ -12,6 +12,8 @@ A typical use of the module looks like this:: import requests import time +from ConfigParser import RawConfigParser +import os __all__ = ["init", "get_sounds", "Client", "MessageRequest", "InitError", "RequestError", "get_client"] @@ -226,24 +228,23 @@ def get_client(profile=None, config_path='~/.pushover'): * ``profile``: the profile to load as a client (`Default` by default.) * ``config_path``: path of the configuration file (`~/.pushover` by default.) """ - import ConfigParser - import os + config_path = os.path.expanduser(config_path) if not os.path.exists(config_path): - raise Exception("Configuration file not found ({0})".format(config_path)) + raise IOError(2, "No such file", config_path) - config = ConfigParser.RawConfigParser() + config = RawConfigParser({"device": None}) config.read(config_path) - section = profile if profile is not None else 'Default' + section = profile or 'Default' init(config.get(section, 'api_token'), sound=False) return Client( config.get(section, 'user_key'), - device=config.get(section, 'device') if config.has_option(section, 'device') else None + device=config.get(section, 'device') ) -- cgit v1.2.3-70-g09d2