diff options
| author | Thibaut Horel <thibaut.horel@gmail.com> | 2014-07-25 00:01:08 +0200 |
|---|---|---|
| committer | Thibaut Horel <thibaut.horel@gmail.com> | 2014-07-25 00:01:08 +0200 |
| commit | 769c689db1f24525be7d2c84c1e3569f1cd10edf (patch) | |
| tree | 48c1872a87116a96835224d8f4bbf3c515176bf3 | |
| parent | 86cee389d58fc0e5377469128cbd38887f2d608f (diff) | |
| parent | 201bbafc647595b33cde31ba988d5d61a5545b9c (diff) | |
| download | python-pushover-769c689db1f24525be7d2c84c1e3569f1cd10edf.tar.gz | |
Merge pull request #5 from sbirch/patch-1
Convenience function to create a client
| -rw-r--r-- | pushover.py | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/pushover.py b/pushover.py index 282ba32..ebee0b0 100644 --- a/pushover.py +++ b/pushover.py @@ -12,9 +12,11 @@ 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"] + "InitError", "RequestError", "get_client"] BASE_URL = "https://api.pushover.net/1/" MESSAGE_URL = BASE_URL + "messages.json" @@ -206,6 +208,45 @@ class Client: return MessageRequest(payload) +def get_client(profile=None, config_path='~/.pushover'): + """Create a :class:`Client` object from a default configuration file. + + 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') + ) + if __name__ == "__main__": from argparse import ArgumentParser |
