|
| 1 | +# Copyright: 2013 Paul Traylor |
| 2 | +# These sources are released under the terms of the MIT license: see LICENSE |
| 3 | + |
| 4 | +import logging |
| 5 | +import os |
| 6 | +import sys |
| 7 | +from optparse import OptionParser, OptionGroup |
| 8 | + |
| 9 | +from gntp.notifier import GrowlNotifier |
| 10 | +from gntp.shim import RawConfigParser |
| 11 | +from gntp.version import __version__ |
| 12 | + |
| 13 | +DEFAULT_CONFIG = os.path.expanduser('~/.gntp') |
| 14 | + |
| 15 | +config = RawConfigParser({ |
| 16 | + 'hostname': 'localhost', |
| 17 | + 'password': None, |
| 18 | + 'port': 23053, |
| 19 | +}) |
| 20 | +config.read([DEFAULT_CONFIG]) |
| 21 | +if not config.has_section('gntp'): |
| 22 | + config.add_section('gntp') |
| 23 | + |
| 24 | + |
| 25 | +class ClientParser(OptionParser): |
| 26 | + def __init__(self): |
| 27 | + OptionParser.__init__(self, version="%%prog %s" % __version__) |
| 28 | + |
| 29 | + group = OptionGroup(self, "Network Options") |
| 30 | + group.add_option("-H", "--host", |
| 31 | + dest="host", default=config.get('gntp', 'hostname'), |
| 32 | + help="Specify a hostname to which to send a remote notification. [%default]") |
| 33 | + group.add_option("--port", |
| 34 | + dest="port", default=config.getint('gntp', 'port'), type="int", |
| 35 | + help="port to listen on [%default]") |
| 36 | + group.add_option("-P", "--password", |
| 37 | + dest='password', default=config.get('gntp', 'password'), |
| 38 | + help="Network password") |
| 39 | + self.add_option_group(group) |
| 40 | + |
| 41 | + group = OptionGroup(self, "Notification Options") |
| 42 | + group.add_option("-n", "--name", |
| 43 | + dest="app", default='Python GNTP Test Client', |
| 44 | + help="Set the name of the application [%default]") |
| 45 | + group.add_option("-s", "--sticky", |
| 46 | + dest='sticky', default=False, action="store_true", |
| 47 | + help="Make the notification sticky [%default]") |
| 48 | + group.add_option("--image", |
| 49 | + dest="icon", default=None, |
| 50 | + help="Icon for notification (URL or /path/to/file)") |
| 51 | + group.add_option("-m", "--message", |
| 52 | + dest="message", default=None, |
| 53 | + help="Sets the message instead of using stdin") |
| 54 | + group.add_option("-p", "--priority", |
| 55 | + dest="priority", default=0, type="int", |
| 56 | + help="-2 to 2 [%default]") |
| 57 | + group.add_option("-d", "--identifier", |
| 58 | + dest="identifier", |
| 59 | + help="Identifier for coalescing") |
| 60 | + group.add_option("-t", "--title", |
| 61 | + dest="title", default=None, |
| 62 | + help="Set the title of the notification [%default]") |
| 63 | + group.add_option("-N", "--notification", |
| 64 | + dest="name", default='Notification', |
| 65 | + help="Set the notification name [%default]") |
| 66 | + group.add_option("--callback", |
| 67 | + dest="callback", |
| 68 | + help="URL callback") |
| 69 | + self.add_option_group(group) |
| 70 | + |
| 71 | + # Extra Options |
| 72 | + self.add_option('-v', '--verbose', |
| 73 | + dest='verbose', default=0, action='count', |
| 74 | + help="Verbosity levels") |
| 75 | + |
| 76 | + def parse_args(self, args=None, values=None): |
| 77 | + values, args = OptionParser.parse_args(self, args, values) |
| 78 | + |
| 79 | + if values.message is None: |
| 80 | + print('Enter a message followed by Ctrl-D') |
| 81 | + try: |
| 82 | + message = sys.stdin.read() |
| 83 | + except KeyboardInterrupt: |
| 84 | + exit() |
| 85 | + else: |
| 86 | + message = values.message |
| 87 | + |
| 88 | + if values.title is None: |
| 89 | + values.title = ' '.join(args) |
| 90 | + |
| 91 | + # If we still have an empty title, use the |
| 92 | + # first bit of the message as the title |
| 93 | + if values.title == '': |
| 94 | + values.title = message[:20] |
| 95 | + |
| 96 | + values.verbose = logging.WARNING - values.verbose * 10 |
| 97 | + |
| 98 | + return values, message |
| 99 | + |
| 100 | + |
| 101 | +def main(): |
| 102 | + (options, message) = ClientParser().parse_args() |
| 103 | + logging.basicConfig(level=options.verbose) |
| 104 | + if not os.path.exists(DEFAULT_CONFIG): |
| 105 | + logging.info('No config read found at %s', DEFAULT_CONFIG) |
| 106 | + |
| 107 | + growl = GrowlNotifier( |
| 108 | + applicationName=options.app, |
| 109 | + notifications=[options.name], |
| 110 | + defaultNotifications=[options.name], |
| 111 | + hostname=options.host, |
| 112 | + password=options.password, |
| 113 | + port=options.port, |
| 114 | + ) |
| 115 | + result = growl.register() |
| 116 | + if result is not True: |
| 117 | + exit(result) |
| 118 | + |
| 119 | + # This would likely be better placed within the growl notifier |
| 120 | + # class but until I make _checkIcon smarter this is "easier" |
| 121 | + if options.icon is not None and not options.icon.startswith('http'): |
| 122 | + logging.info('Loading image %s', options.icon) |
| 123 | + f = open(options.icon) |
| 124 | + options.icon = f.read() |
| 125 | + f.close() |
| 126 | + |
| 127 | + result = growl.notify( |
| 128 | + noteType=options.name, |
| 129 | + title=options.title, |
| 130 | + description=message, |
| 131 | + icon=options.icon, |
| 132 | + sticky=options.sticky, |
| 133 | + priority=options.priority, |
| 134 | + callback=options.callback, |
| 135 | + identifier=options.identifier, |
| 136 | + ) |
| 137 | + if result is not True: |
| 138 | + exit(result) |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + main() |
0 commit comments