diff options
Diffstat (limited to 'beinc_poller.py')
| -rwxr-xr-x | beinc_poller.py | 163 |
1 files changed, 130 insertions, 33 deletions
diff --git a/beinc_poller.py b/beinc_poller.py index 16cacd8..4fbe0e0 100755 --- a/beinc_poller.py +++ b/beinc_poller.py | |||
| @@ -35,9 +35,18 @@ import urllib2 | |||
| 35 | try: | 35 | try: |
| 36 | import pynotify | 36 | import pynotify |
| 37 | except ImportError as e: | 37 | except ImportError as e: |
| 38 | sys.stderr.write( | 38 | pynotify = None |
| 39 | 'A working pynotify library is required by BEINC-poller\n') | 39 | |
| 40 | sys.exit(1) | 40 | try: |
| 41 | import pyosd | ||
| 42 | pyosd_positions = {'top': pyosd.POS_TOP, | ||
| 43 | 'middle': pyosd.POS_MID, | ||
| 44 | 'bottom': pyosd.POS_BOT} | ||
| 45 | pyosd_alignments = {'left': pyosd.ALIGN_LEFT, | ||
| 46 | 'center': pyosd.ALIGN_CENTER, | ||
| 47 | 'right': pyosd.ALIGN_RIGHT} | ||
| 48 | except ImportError as e: | ||
| 49 | pyosd = None | ||
| 41 | 50 | ||
| 42 | 51 | ||
| 43 | __author__ = 'Simeon Simeonov' | 52 | __author__ = 'Simeon Simeonov' |
| @@ -97,9 +106,16 @@ def poll_notifications(scheduler, args, notification_obj): | |||
| 97 | response.close() | 106 | response.close() |
| 98 | res_list = json.loads(res_str) | 107 | res_list = json.loads(res_str) |
| 99 | for entry in res_list: | 108 | for entry in res_list: |
| 100 | notification_obj.set_properties(summary=entry.get('title', ''), | 109 | title = entry.get('title', '') |
| 101 | body=entry.get('message', '')) | 110 | message = entry.get('message', '') |
| 102 | notification_obj.show() | 111 | if args.osd_sys == 'pyosd': |
| 112 | notification_obj.display(title, line=0) | ||
| 113 | notification_obj.display(message, line=1) | ||
| 114 | else: | ||
| 115 | notification_obj.set_properties( | ||
| 116 | summary=title, | ||
| 117 | body=message) | ||
| 118 | notification_obj.show() | ||
| 103 | scheduler.enter(args.frequency, | 119 | scheduler.enter(args.frequency, |
| 104 | 1, | 120 | 1, |
| 105 | poll_notifications, | 121 | poll_notifications, |
| @@ -121,6 +137,13 @@ def main(): | |||
| 121 | type=str, | 137 | type=str, |
| 122 | help='BEINC destination URL') | 138 | help='BEINC destination URL') |
| 123 | parser.add_argument( | 139 | parser.add_argument( |
| 140 | '-a', '--align', | ||
| 141 | metavar='ALIGNMENT', | ||
| 142 | type=str, | ||
| 143 | dest='alignment', | ||
| 144 | default='left', | ||
| 145 | help='Alignment for "pyosd" (default: "left")') | ||
| 146 | parser.add_argument( | ||
| 124 | '-c', '--cert-file', | 147 | '-c', '--cert-file', |
| 125 | metavar='FILE', | 148 | metavar='FILE', |
| 126 | type=str, | 149 | type=str, |
| @@ -128,17 +151,18 @@ def main(): | |||
| 128 | default='', | 151 | default='', |
| 129 | help='BEINC CA-cert to check the server-cert against (default: None)') | 152 | help='BEINC CA-cert to check the server-cert against (default: None)') |
| 130 | parser.add_argument( | 153 | parser.add_argument( |
| 131 | '-d', | 154 | '-C', '--color', |
| 132 | action='store_true', | 155 | metavar='COLOR', |
| 133 | dest='daemonize', | 156 | type=str, |
| 134 | default=False, | 157 | dest='color', |
| 135 | help='Run the poller-process in the background') | 158 | default='blue', |
| 159 | help='Color for "pyosd" (default: "blue")') | ||
| 136 | parser.add_argument( | 160 | parser.add_argument( |
| 137 | '-D', '--debug', | 161 | '-d', '--debug', |
| 138 | action='store_true', | 162 | action='store_true', |
| 139 | dest='debug', | 163 | dest='debug', |
| 140 | default=False, | 164 | default=False, |
| 141 | help='Run the poller-process in debug-mode (disables daemonize)') | 165 | help='Run the poller-process in debug-mode') |
| 142 | parser.add_argument( | 166 | parser.add_argument( |
| 143 | '-f', '--frequency', | 167 | '-f', '--frequency', |
| 144 | metavar='SECONDS', | 168 | metavar='SECONDS', |
| @@ -147,47 +171,120 @@ def main(): | |||
| 147 | default=10, | 171 | default=10, |
| 148 | help='Polling frequency in seconds (default: 10)') | 172 | help='Polling frequency in seconds (default: 10)') |
| 149 | parser.add_argument( | 173 | parser.add_argument( |
| 150 | '-p', '--password', | 174 | '--font', |
| 175 | metavar='FONT', | ||
| 176 | type=str, | ||
| 177 | dest='font', | ||
| 178 | default=None, | ||
| 179 | help='Custom font for "pyosd" (default: Default font)') | ||
| 180 | parser.add_argument( | ||
| 181 | '--h-offset', | ||
| 182 | metavar='OFFSET', | ||
| 183 | type=int, | ||
| 184 | dest='hoffset', | ||
| 185 | default=30, | ||
| 186 | help='Horizontal offset for "pyosd" (default: 30)') | ||
| 187 | parser.add_argument( | ||
| 188 | '-o', '--osd-system', | ||
| 189 | metavar='SYSTEM', | ||
| 190 | type=str, | ||
| 191 | dest='osd_sys', | ||
| 192 | default='pynotify', | ||
| 193 | help='BEINC osd-system ("pynotify" or "pyosd") (default: pynotify)') | ||
| 194 | parser.add_argument( | ||
| 195 | '-P', '--password', | ||
| 151 | metavar='PASSWORD', | 196 | metavar='PASSWORD', |
| 152 | type=str, | 197 | type=str, |
| 153 | dest='password', | 198 | dest='password', |
| 154 | default='', | 199 | default='', |
| 155 | help='BEINC taget-password (default & recommended: prompt for passwd)') | 200 | help='BEINC taget-password (default & recommended: prompt for passwd)') |
| 156 | parser.add_argument( | 201 | parser.add_argument( |
| 202 | '-p', '--position', | ||
| 203 | metavar='POSITION', | ||
| 204 | type=str, | ||
| 205 | dest='position', | ||
| 206 | default='bottom', | ||
| 207 | help='Position for "pyosd" (default: "bottom")') | ||
| 208 | parser.add_argument( | ||
| 157 | '-t', '--osd-timeout', | 209 | '-t', '--osd-timeout', |
| 158 | metavar='MILLISECONDS', | 210 | metavar='SECONDS', |
| 159 | type=int, | 211 | type=int, |
| 160 | dest='osd_timeout', | 212 | dest='osd_timeout', |
| 161 | default=5000, | 213 | default=5, |
| 162 | help='OSD timeout (default: 5000)') | 214 | help='OSD timeout (default: 5)') |
| 163 | parser.add_argument( | 215 | parser.add_argument( |
| 164 | '-v', '--version', | 216 | '-v', '--version', |
| 165 | action='version', | 217 | action='version', |
| 166 | version='%(prog)s {0}'.format(__version__), | 218 | version='%(prog)s {0}'.format(__version__), |
| 167 | help='display program-version and exit') | 219 | help='display program-version and exit') |
| 220 | parser.add_argument( | ||
| 221 | '--v-offset', | ||
| 222 | metavar='OFFSET', | ||
| 223 | type=int, | ||
| 224 | dest='voffset', | ||
| 225 | default=120, | ||
| 226 | help='Vertical offset for "pyosd" (default: 120)') | ||
| 168 | args = parser.parse_args() | 227 | args = parser.parse_args() |
| 228 | if args.osd_sys == 'pyosd': | ||
| 229 | if not pyosd: | ||
| 230 | sys.stderr.write( | ||
| 231 | 'Could not load "pyosd".\n' | ||
| 232 | 'Please install "pyosd" or use a different osd-system!\n' | ||
| 233 | 'Terminating...\n') | ||
| 234 | sys.exit(1) | ||
| 235 | try: | ||
| 236 | notification_obj = pyosd.osd() | ||
| 237 | notification_obj.set_timeout(args.osd_timeout) | ||
| 238 | if args.font: | ||
| 239 | notification_obj.set_font(args.font) | ||
| 240 | notification_obj.set_vertical_offset(args.voffset) | ||
| 241 | notification_obj.set_horizontal_offset(args.hoffset) | ||
| 242 | notification_obj.set_align( | ||
| 243 | pyosd_alignments.get(args.alignment, pyosd.ALIGN_LEFT)) | ||
| 244 | notification_obj.set_pos( | ||
| 245 | pyosd_positions.get(args.position, pyosd.POS_BOT)) | ||
| 246 | notification_obj.set_colour(args.color) | ||
| 247 | except Exception as e: | ||
| 248 | sys.stderr.write( | ||
| 249 | 'Unable to create pyosd osd-object: {0}\n'.format( | ||
| 250 | e)) | ||
| 251 | sys.exit(1) | ||
| 252 | else: | ||
| 253 | if not pynotify: | ||
| 254 | sys.stderr.write( | ||
| 255 | 'Could not load "pynotify".\n' | ||
| 256 | 'Please install "pynotify" or use a different osd-system!\n' | ||
| 257 | 'Terminating...\n') | ||
| 258 | sys.exit(1) | ||
| 259 | if not pynotify.init('BEINC Notify'): | ||
| 260 | sys.stderr.write('There was a problem with libnotify\n') | ||
| 261 | sys.exit(1) | ||
| 262 | try: | ||
| 263 | notification_obj = pynotify.Notification(' ') | ||
| 264 | notification_obj.set_timeout(1000 * args.osd_timeout) | ||
| 265 | notification_obj.set_property( | ||
| 266 | 'app_name', | ||
| 267 | '{0} {1}'.format(sys.argv[0], __version__)) | ||
| 268 | except Exception as e: | ||
| 269 | sys.stderr.write( | ||
| 270 | 'Unable to create pynotify Notification-object: {0}\n'.format( | ||
| 271 | e)) | ||
| 272 | sys.exit(1) | ||
| 169 | if not args.password: | 273 | if not args.password: |
| 170 | try: | 274 | try: |
| 171 | args.password = getpass.getpass() | 275 | args.password = getpass.getpass() |
| 172 | except Exception as e: | 276 | except Exception as e: |
| 173 | sys.stderr.write('Prompt terminated\n') | 277 | sys.stderr.write('Prompt terminated\n') |
| 174 | sys.exit(errno.EACCES) | 278 | sys.exit(errno.EACCES) |
| 175 | 279 | elif os.path.isfile(args.password): | |
| 176 | if not pynotify.init('BEINC Notify'): | 280 | try: |
| 177 | sys.stderr.write('There was a problem with libnotify\n') | 281 | with open(args.password, 'r') as fp: |
| 178 | sys.exit(1) | 282 | passwd = fp.readline() |
| 179 | 283 | if passwd.strip(): | |
| 180 | try: | 284 | args.password = passwd.strip() |
| 181 | notification_obj = pynotify.Notification(' ') | 285 | except Exception as e: |
| 182 | notification_obj.set_timeout(args.osd_timeout) | 286 | sys.stderr.write('Unable to open password file: {0}'.format(e)) |
| 183 | notification_obj.set_property( | 287 | sys.exit(1) |
| 184 | 'app_name', | ||
| 185 | '{0} {1}'.format(sys.argv[0], __version__)) | ||
| 186 | except Exception as e: | ||
| 187 | sys.stderr.write( | ||
| 188 | 'Unable to create pynotify Notification-object: {0}\n'.format(e)) | ||
| 189 | sys.exit(1) | ||
| 190 | |||
| 191 | sc = sched.scheduler(time.time, time.sleep) | 288 | sc = sched.scheduler(time.time, time.sleep) |
| 192 | sc.enter(args.frequency, | 289 | sc.enter(args.frequency, |
| 193 | 1, | 290 | 1, |
