Fix a bug in the Maemo code
[panucci.git] / bin / panucci
blobd9896a8cd38ba4af1cbdc9c259fc1d95ab3e7e5f
1 #!/usr/bin/env python
3 # This file is part of Panucci.
4 # Copyright (c) 2008-2010 The Panucci Audiobook and Podcast Player Project
6 # Panucci is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # Panucci is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Panucci. If not, see <http://www.gnu.org/licenses/>.
20 from __future__ import absolute_import
22 import dbus
23 import dbus.glib
24 import logging
25 import logging.handlers
26 import os.path
27 import sys
28 from optparse import OptionParser
31 # Set up gettext support
32 import locale
33 import gettext
35 prefix = os.path.join( os.path.dirname(sys.argv[0]), '..' )
37 for basedir in 'share', 'data':
38 locale_dir = os.path.abspath(os.path.join( prefix, basedir, 'locale' ))
39 if os.path.exists( locale_dir ):
40 break
42 locale_dir = os.environ.get('LOCALE_DIR', locale_dir)
43 gettext.install( 'panucci', locale_dir )
45 # Set up the command line option parser
46 usage = 'usage: %prog [options] FILE'
47 parser = OptionParser(usage=usage)
48 parser.add_option('-q', '--queue', action='store', type='string',
49 dest='queue_filename', help='Add FILE to the queue', metavar='FILE')
50 parser.add_option('-d', '--debug', action='store_true', default=False,
51 dest='debug', help='Enable verbose logging')
52 opts, args = parser.parse_args()
54 if len(args) > 1 or ( opts.queue_filename and len(args) ):
55 parser.print_help()
56 sys.exit(1)
58 # add src/ to the PYTHONPATH
59 local_module_dir = os.path.join(os.path.dirname(sys.argv[0]), '..', 'src')
60 if os.path.isdir(local_module_dir):
61 sys.path.append(local_module_dir)
63 optified_install_path = '/opt/panucci/lib/'
64 if os.path.isdir(optified_install_path):
65 sys.path.insert(0, optified_install_path)
67 import panucci
69 # Detect the platform we're running on
70 from panucci import platform
71 platform.detect()
73 if platform.FREMANTLE:
74 # Workaround Maemo bug 6694 (Playback in Silent mode)
75 import gobject
76 gobject.set_application_name('FMRadio')
78 if args:
79 if '://' in args[0]:
80 # Assume a URL (HTTP, HTTPs, etc...)
81 filepath = args[0]
82 else:
83 filepath = os.path.abspath(args[0])
84 elif opts.queue_filename:
85 filepath = os.path.abspath(opts.queue_filename)
86 else:
87 filepath = None
89 def init_logging( log_level ):
90 """ Configure the logging module for panucci """
91 logger = logging.getLogger('panucci')
92 logger.setLevel( logging.DEBUG )
94 # the stream handler (logging to the console)
95 sh = logging.StreamHandler()
96 sh.setLevel( log_level )
97 fmt = logging.Formatter('%(levelname)s:%(name)s %(message)s')
98 sh.setFormatter(fmt)
99 logger.addHandler(sh)
101 # the file handler (logging to a file)
102 fh = logging.handlers.RotatingFileHandler(panucci.LOGFILE, \
103 backupCount=0, maxBytes=25*1024)
105 fh.doRollover() # reset the logfile at startup
106 fh.setLevel( logging.DEBUG ) # log everything to the file
107 fmt = logging.Formatter('%(asctime)s %(levelname)s:%(name)s %(message)s')
108 fh.setFormatter(fmt)
109 logger.addHandler(fh)
111 # force all exceptions to pass through the logger
112 sys.excepthook = lambda *args: logger.critical( 'Exception caught:',
113 exc_info=args )
116 # Attempt to contact an already-running copy of Panucci
117 session_bus = dbus.SessionBus()
118 try:
119 if session_bus.name_has_owner('org.panucci.panucciInterface'):
120 remote_object = session_bus.get_object(
121 'org.panucci.panucciInterface', '/panucciInterface' )
122 print 'Found panucci instance already running, will try to use it...'
123 else:
124 remote_object = None
125 except dbus.exceptions.DBusException:
126 remote_object = None
129 if remote_object is None:
130 # configure logging
131 init_logging( logging.DEBUG if opts.debug else logging.ERROR )
133 from panucci import main
134 main.run(filename=filepath)
135 else:
136 if filepath is not None:
137 if opts.queue_filename is not None:
138 remote_object.queue_file( filepath )
139 else:
140 remote_object.play_file( filepath )
142 remote_object.show_main_window()