summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Huuhko <kai.huuhko@gmail.com>2014-08-29 02:31:00 +0300
committerKai Huuhko <kai.huuhko@gmail.com>2014-08-29 02:31:00 +0300
commit0ecb75fcf943a2a9bd1fb6a8a9d88f1046f56a55 (patch)
tree1fd1c30eabae99fcfec2db01389743d865c0d458
parent18faef62f5ab95bc6362cb25cf50f8a9f0d36659 (diff)
Add logging, fix notify hang on start up
-rwxr-xr-xvalosoitin/Valosoitin.py92
1 files changed, 62 insertions, 30 deletions
diff --git a/valosoitin/Valosoitin.py b/valosoitin/Valosoitin.py
index 3d1ff1c..0a1e4c6 100755
--- a/valosoitin/Valosoitin.py
+++ b/valosoitin/Valosoitin.py
@@ -43,7 +43,19 @@ import mimetypes
43mimetypes.init() 43mimetypes.init()
44 44
45import logging 45import logging
46logging.basicConfig(level=logging.DEBUG) 46
47log = logging.getLogger("valosoitin")
48formatter = logging.Formatter(
49 "%(levelname)s %(filename)s:%(lineno)d - %(message)s"
50 )
51handler = logging.StreamHandler()
52handler.setFormatter(formatter)
53log.addHandler(handler)
54log.setLevel(logging.INFO)
55
56efllog = logging.getLogger("efl")
57efllog.addHandler(handler)
58efllog.setLevel(logging.WARN)
47 59
48import gettext 60import gettext
49try: 61try:
@@ -129,10 +141,10 @@ for section in ("Settings", "Last played"):
129 141
130try: 142try:
131 if not conf.get("Settings", "audio_backend") in ("gstreamer", "xine"): 143 if not conf.get("Settings", "audio_backend") in ("gstreamer", "xine"):
132 logging.error("Invalid audio backend in config file. Set it to either gstreamer or xine.") 144 log.error("Invalid audio backend in config file. Set it to either gstreamer or xine.")
133 sys.exit() 145 sys.exit()
134except: 146except:
135 logging.error("audio_backend was not found in settings file. Set it to a value or remove the settings file so that a new one will be generated at next start of this program.") 147 log.error("audio_backend was not found in settings file. Set it to a value or remove the settings file so that a new one will be generated at next start of this program.")
136 148
137class Valosoitin(object): 149class Valosoitin(object):
138 def __init__(self, *args): 150 def __init__(self, *args):
@@ -214,7 +226,6 @@ class Valosoitin(object):
214 self.dbo = ValosoitinDBus(self) 226 self.dbo = ValosoitinDBus(self)
215 227
216 window.resize(480, 480) 228 window.resize(480, 480)
217 window.show()
218 229
219 # Restore playlist 230 # Restore playlist
220 order = [] 231 order = []
@@ -227,7 +238,7 @@ class Valosoitin(object):
227 with open(os.path.expanduser(os.path.join("~", ".local", "share", "valosoitin.playlist_order")), 'rb') as pkl_file: 238 with open(os.path.expanduser(os.path.join("~", ".local", "share", "valosoitin.playlist_order")), 'rb') as pkl_file:
228 order = cPickle.load(pkl_file) 239 order = cPickle.load(pkl_file)
229 except: 240 except:
230 logging.exception("Could not restore playlist") 241 log.exception("Could not restore playlist")
231 242
232 paths = [] 243 paths = []
233 for i in order: 244 for i in order:
@@ -255,6 +266,8 @@ class Valosoitin(object):
255 if clargs.files: 266 if clargs.files:
256 self.add_files_with_idler(clargs.files) 267 self.add_files_with_idler(clargs.files)
257 268
269 window.show()
270
258 def raise_window(self): 271 def raise_window(self):
259 self.window.raise_() 272 self.window.raise_()
260 273
@@ -297,9 +310,9 @@ class Valosoitin(object):
297 pbar.span_size = self.window.size[0] - 100 310 pbar.span_size = self.window.size[0] - 100
298 pbarn.show() 311 pbarn.show()
299 312
300 logging.debug("Adding files") 313 log.debug("Adding files")
301 self.time1 = time.clock() 314 self.time1 = time.clock()
302 logging.debug(self.time1) 315 log.debug(self.time1)
303 316
304 path_generator = (f for f in paths) 317 path_generator = (f for f in paths)
305 idler = Idler(self.add_file, path_generator) 318 idler = Idler(self.add_file, path_generator)
@@ -309,8 +322,8 @@ class Valosoitin(object):
309 f = paths.next() 322 f = paths.next()
310 except StopIteration: 323 except StopIteration:
311 self.time2 = time.clock() 324 self.time2 = time.clock()
312 logging.debug(self.time2) 325 log.debug(self.time2)
313 logging.debug(self.time2 - self.time1) 326 log.debug(self.time2 - self.time1)
314 if self.progress_bar_notify: 327 if self.progress_bar_notify:
315 self.progress_bar_notify.delete() 328 self.progress_bar_notify.delete()
316 self.progress_bar_notify = None 329 self.progress_bar_notify = None
@@ -325,7 +338,7 @@ class Valosoitin(object):
325 self.pbar.value = self.pbar.value + self.pbaru 338 self.pbar.value = self.pbar.value + self.pbaru
326 339
327 if not os.path.exists(f): 340 if not os.path.exists(f):
328 logging.info("File doesn't exist") 341 log.info("File doesn't exist")
329 return True 342 return True
330 343
331 m = self.check_mime_type(f) 344 m = self.check_mime_type(f)
@@ -338,17 +351,17 @@ class Valosoitin(object):
338 351
339 self.playlist.add_item(self, m) 352 self.playlist.add_item(self, m)
340 else: 353 else:
341 logging.info("Audio already on playlist") 354 log.info("Audio already on playlist")
342 355
343 return True 356 return True
344 357
345 def check_mime_type(self, f): 358 def check_mime_type(self, f):
346 mimetype = mimetypes.guess_type(f, strict=False)[0] 359 mimetype = mimetypes.guess_type(f, strict=False)[0]
347 if not mimetype: 360 if not mimetype:
348 logging.info("Invalid type %s for file %s" % (mimetype, f)) 361 log.info("Invalid type %s for file %s" % (mimetype, f))
349 return False 362 return False
350 elif not mimetype.startswith("audio"): 363 elif not mimetype.startswith("audio"):
351 logging.info("Invalid type %s for file %s" % (mimetype, f)) 364 log.info("Invalid type %s for file %s" % (mimetype, f))
352 return False 365 return False
353 366
354 m = None 367 m = None
@@ -360,29 +373,29 @@ class Valosoitin(object):
360 try: 373 try:
361 m = mutagen.mp3.MP3(f) 374 m = mutagen.mp3.MP3(f)
362 except mutagen.mp3.HeaderNotFoundError: 375 except mutagen.mp3.HeaderNotFoundError:
363 logging.info("Mpeg audio header not found.") 376 log.info("Mpeg audio header not found.")
364 elif mimetype == "audio/flac": 377 elif mimetype == "audio/flac":
365 try: 378 try:
366 m = mutagen.flac.FLAC(f) 379 m = mutagen.flac.FLAC(f)
367 except mutagen.flac.FLACNoHeaderError: 380 except mutagen.flac.FLACNoHeaderError:
368 logging.info("Invalid header in FLAC file.") 381 log.info("Invalid header in FLAC file.")
369 except mutagen.flac.FLACVorbisError: 382 except mutagen.flac.FLACVorbisError:
370 logging.info("Invalid header in FLAC vorbis file.") 383 log.info("Invalid header in FLAC vorbis file.")
371 elif mimetype == "audio/opus": 384 elif mimetype == "audio/opus":
372 try: 385 try:
373 m = mutagen.oggopus.OggOpus(f) 386 m = mutagen.oggopus.OggOpus(f)
374 except mutagen.oggopus.OggOpusHeaderError: 387 except mutagen.oggopus.OggOpusHeaderError:
375 logging.info("Invalid header in Opus file.") 388 log.info("Invalid header in Opus file.")
376 elif mimetype == "audio/ogg": 389 elif mimetype == "audio/ogg":
377 try: 390 try:
378 m = mutagen.oggvorbis.OggVorbis(f) 391 m = mutagen.oggvorbis.OggVorbis(f)
379 except mutagen.oggvorbis.OggVorbisHeaderError: 392 except mutagen.oggvorbis.OggVorbisHeaderError:
380 logging.info("Invalid header in Ogg Vorbis file.") 393 log.info("Invalid header in Ogg Vorbis file.")
381 else: 394 else:
382 logging.info("No known mutagen type for %s." % mimetype) 395 log.info("No known mutagen type for %s." % mimetype)
383 396
384 if m is None: 397 if m is None:
385 logging.info("Trying generic type for %s" % f) 398 log.info("Trying generic type for %s" % f)
386 try: 399 try:
387 m = mutagen.File(f, easy=True) 400 m = mutagen.File(f, easy=True)
388 except Exception: 401 except Exception:
@@ -425,7 +438,7 @@ class Valosoitin(object):
425 try: 438 try:
426 new_item = self.playlist.items[f] 439 new_item = self.playlist.items[f]
427 except KeyError: 440 except KeyError:
428 logging.exception("Item for {0} not found".format(f)) 441 log.exception("Item for {0} not found".format(f))
429 442
430 self.player.file_set("file://"+urllib.quote(f)) 443 self.player.file_set("file://"+urllib.quote(f))
431 if not paused: 444 if not paused:
@@ -453,7 +466,7 @@ class Valosoitin(object):
453 try: 466 try:
454 f = self.playlist.order[0] 467 f = self.playlist.order[0]
455 except: 468 except:
456 logging.info("No files on the playlist") 469 log.info("No files on the playlist")
457 return 470 return
458 i = self.playlist.items[f] 471 i = self.playlist.items[f]
459 m = i.data_get() 472 m = i.data_get()
@@ -465,6 +478,16 @@ class Valosoitin(object):
465 self.playtimer.thaw() 478 self.playtimer.thaw()
466 self.dbo.playerprops["PlaybackStatus"] = "Playing" 479 self.dbo.playerprops["PlaybackStatus"] = "Playing"
467 480
481 fn = self.player.file
482 f = urllib.unquote(fn).split("://")[1]
483 m = self.library[f]
484
485 titles = cgi.escape(", ".join(m.get("title"))) if m.get("title") else os.path.splitext(os.path.basename(m.filename))[0]
486 artists = cgi.escape(", ".join(m.get("artist"))) if m.get("artist") else ""
487 albums = cgi.escape(", ".join(m.get("album"))) if m.get("album") else ""
488
489 notify(_("You're listening to"), "\n".join(filter(None, (artists, titles, albums))), "notification-information")
490
468 def pause(self, override=False): 491 def pause(self, override=False):
469 if not self.player.play_get() and not override: 492 if not self.player.play_get() and not override:
470 return 493 return
@@ -574,7 +597,12 @@ class Valosoitin(object):
574 self.conf.set("Settings", "random", str(mode)) 597 self.conf.set("Settings", "random", str(mode))
575 598
576 def playing(self, player): 599 def playing(self, player):
577 f = urllib.unquote(self.player.file_get()).split("://")[1] 600 fn = self.player.file
601 if not fn:
602 log.error("Got playing signal but no file set")
603 return
604
605 f = urllib.unquote(fn).split("://")[1]
578 m = self.library[f] 606 m = self.library[f]
579 607
580 titles = cgi.escape(", ".join(m.get("title"))) if m.get("title") else os.path.splitext(os.path.basename(m.filename))[0] 608 titles = cgi.escape(", ".join(m.get("title"))) if m.get("title") else os.path.splitext(os.path.basename(m.filename))[0]
@@ -591,6 +619,10 @@ class Valosoitin(object):
591 self.dbo.playerprops["Metadata"]["xesam:artist"] = artists 619 self.dbo.playerprops["Metadata"]["xesam:artist"] = artists
592 self.dbo.playerprops["Metadata"]["xesam:album"] = albums 620 self.dbo.playerprops["Metadata"]["xesam:album"] = albums
593 621
622 if not player.play:
623 log.error("Got playing signal but play state is paused")
624 return
625
594 notify(_("You're listening to"), "\n".join(filter(None, (artists, titles, albums))), "notification-information") 626 notify(_("You're listening to"), "\n".join(filter(None, (artists, titles, albums))), "notification-information")
595 627
596 def finished(self, player): 628 def finished(self, player):
@@ -610,9 +642,9 @@ class Valosoitin(object):
610 self.randomorder = o 642 self.randomorder = o
611 643
612 def search(self, string, cb): 644 def search(self, string, cb):
613 logging.debug("Searching") 645 log.debug("Searching")
614 time1 = time.clock() 646 time1 = time.clock()
615 logging.debug(time1) 647 log.debug(time1)
616 648
617 result = {} 649 result = {}
618 for k, m in self.library.iteritems(): 650 for k, m in self.library.iteritems():
@@ -631,8 +663,8 @@ class Valosoitin(object):
631 ordered_result.append(result[k]) 663 ordered_result.append(result[k])
632 664
633 time2 = time.clock() 665 time2 = time.clock()
634 logging.debug(time2) 666 log.debug(time2)
635 logging.debug(time2 - time1) 667 log.debug(time2 - time1)
636 668
637 e = "" 669 e = ""
638 670
@@ -653,20 +685,20 @@ class Valosoitin(object):
653 with open(os.path.expanduser(os.path.join("~", ".local", "share", "valosoitin.library")), 'wb') as output: 685 with open(os.path.expanduser(os.path.join("~", ".local", "share", "valosoitin.library")), 'wb') as output:
654 cPickle.dump(self.library, output, 2) 686 cPickle.dump(self.library, output, 2)
655 except: 687 except:
656 logging.exception("Could not save library") 688 log.exception("Could not save library")
657 689
658 try: 690 try:
659 with open(os.path.expanduser(os.path.join("~", ".local", "share", "valosoitin.playlist_order")), 'wb') as output: 691 with open(os.path.expanduser(os.path.join("~", ".local", "share", "valosoitin.playlist_order")), 'wb') as output:
660 cPickle.dump(self.playlist.order, output, 2) 692 cPickle.dump(self.playlist.order, output, 2)
661 except: 693 except:
662 logging.exception("Could not save playlist order") 694 log.exception("Could not save playlist order")
663 695
664 try: 696 try:
665 conf.set("Last played", "filename", urllib.unquote(self.player.file_get()).split("://")[1] if self.player.file_get() else "") 697 conf.set("Last played", "filename", urllib.unquote(self.player.file_get()).split("://")[1] if self.player.file_get() else "")
666 with open(conf_path, 'wb') as configfile: 698 with open(conf_path, 'wb') as configfile:
667 conf.write(configfile) 699 conf.write(configfile)
668 except: 700 except:
669 logging.exception("Could not save conf") 701 log.exception("Could not save conf")
670 702
671 elm.exit() 703 elm.exit()
672 704