Skip to content

Commit 1175fc8

Browse files
committed
Copy cli.py functionality into pushover. Now can do python3 -m pushover .... to invoke cli functionality.
1 parent 5e59325 commit 1175fc8

2 files changed

Lines changed: 105 additions & 1 deletion

File tree

cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ConfigParser import RawConfigParser, NoSectionError, NoOptionError
1+
from configparser import RawConfigParser, NoSectionError, NoOptionError
22
from argparse import ArgumentParser, RawDescriptionHelpFormatter
33
import os
44
from pushover import Pushover

pushover.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,107 @@ def read_config(config_path):
261261
def send_message(self, message, title=None):
262262
Pushover(self.params['api_token']).message(self.params['user_key'], message, title=title)
263263

264+
if __name__ == '__main__':
265+
# Copied from cli.py
266+
from argparse import ArgumentParser, RawDescriptionHelpFormatter
267+
268+
def read_config(config_path):
269+
config_path = os.path.expanduser(config_path)
270+
config = configparser.RawConfigParser()
271+
params = {"users": {}}
272+
files = config.read(config_path)
273+
if not files:
274+
return params
275+
params["token"] = config.get("main", "token")
276+
for name in config.sections():
277+
if name != "main":
278+
user = {}
279+
user["user_key"] = config.get(name, "user_key")
280+
try:
281+
user["device"] = config.get(name, "device")
282+
except NoOptionError:
283+
user["device"] = None
284+
params["users"][name] = user
285+
return params
286+
287+
288+
def main():
289+
parser = ArgumentParser(
290+
description="Send a message to pushover.",
291+
formatter_class=RawDescriptionHelpFormatter,
292+
epilog="""
293+
For more details and bug reports, see: https://github.com/Thibauth/python-pushover""",
294+
)
295+
parser.add_argument("--token", help="API token")
296+
parser.add_argument(
297+
"--user",
298+
"-u",
299+
help="User key or section name in the configuration",
300+
required=True,
301+
)
302+
parser.add_argument(
303+
"-c",
304+
"--config",
305+
help="configuration file\
306+
(default: ~/.pushoverrc)",
307+
default="~/.pushoverrc",
308+
)
309+
parser.add_argument("message", help="message to send")
310+
parser.add_argument("--url", help="additional url")
311+
parser.add_argument("--url-title", help="url title")
312+
parser.add_argument("--title", "-t", help="message title")
313+
parser.add_argument(
314+
"--priority", "-p", help="notification priority (-1, 0, 1 or 2)", type=int
315+
)
316+
parser.add_argument(
317+
"--retry",
318+
"-r",
319+
help="resend interval in seconds (required for priority 2)",
320+
type=int,
321+
)
322+
parser.add_argument(
323+
"--expire",
324+
"-e",
325+
help="expiration time in seconds (required for priority 2)",
326+
type=int,
327+
)
328+
parser.add_argument(
329+
"--version",
330+
"-v",
331+
action="version",
332+
help="output version information and exit",
333+
version="""
334+
%(prog)s 1.0
335+
Copyright (C) 2013-2018 Thibaut Horel <thibaut.horel@gmail.com>
336+
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
337+
This is free software: you are free to change and redistribute it.
338+
There is NO WARRANTY, to the extent permitted by law.""",
339+
)
340+
341+
args = parser.parse_args()
342+
params = read_config(args.config)
343+
if args.priority == 2 and (args.retry is None or args.expire is None):
344+
parser.error("priority of 2 requires expire and retry")
345+
if args.user in params["users"]:
346+
user_key = params["users"][args.user]["user_key"]
347+
device = params["users"][args.user]["device"]
348+
else:
349+
user_key = args.user
350+
device = None
351+
token = args.token or params["token"]
352+
353+
Pushover(token).message(
354+
user_key,
355+
args.message,
356+
device=device,
357+
title=args.title,
358+
priority=args.priority,
359+
url=args.url,
360+
url_title=args.url_title,
361+
timestamp=True,
362+
retry=args.retry,
363+
expire=args.expire,
364+
)
365+
366+
main()
367+

0 commit comments

Comments
 (0)