diff options
| author | Sam Birch <sam.m.birch@gmail.com> | 2014-07-22 01:45:28 -0400 |
|---|---|---|
| committer | Sam Birch <sam.m.birch@gmail.com> | 2014-07-22 01:45:28 -0400 |
| commit | 52b379fee609a40b54af4cc70c8b49daf71148a6 (patch) | |
| tree | ebfbf30ce5400907696bcbd0f14bb91bc11860ad /pushover.py | |
| parent | e057972cf477d2846535b9514d964dc00aafcf4a (diff) | |
| download | python-pushover-52b379fee609a40b54af4cc70c8b49daf71148a6.tar.gz | |
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)
Diffstat (limited to 'pushover.py')
| -rw-r--r-- | pushover.py | 36 |
1 files changed, 35 insertions, 1 deletions
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 |
