Refactor platform detection into its own module
[panucci.git] / bin / panucci
blob78b49a8cb5e644a9f7ddad025d7a063cec6f9ecd
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 # Detect the platform we're running on
64 from panucci import platform
65 platform.detect()
67 from panucci import util
68 filepath = util.build_full_path(args[0] if len(args) else opts.queue_filename)
71 def init_logging( log_level ):
72 """ Configure the logging module for panucci """
73 logger = logging.getLogger('panucci')
74 logger.setLevel( logging.DEBUG )
76 # the stream handler (logging to the console)
77 sh = logging.StreamHandler()
78 sh.setLevel( log_level )
79 fmt = logging.Formatter('%(levelname)s:%(name)s %(message)s')
80 sh.setFormatter(fmt)
81 logger.addHandler(sh)
83 # the file handler (logging to a file)
84 from panucci import util
85 f = util.get_logfile()
86 fh = logging.handlers.RotatingFileHandler( f, backupCount=0, # 25kb should
87 maxBytes=25*1024 ) # be enough.
88 fh.doRollover() # reset the logfile at startup
89 fh.setLevel( logging.DEBUG ) # log everything to the file
90 fmt = logging.Formatter('%(asctime)s %(levelname)s:%(name)s %(message)s')
91 fh.setFormatter(fmt)
92 logger.addHandler(fh)
94 # force all exceptions to pass through the logger
95 sys.excepthook = lambda *args: logger.critical( 'Exception caught:',
96 exc_info=args )
99 # Attempt to contact an already-running copy of Panucci
100 session_bus = dbus.SessionBus()
101 try:
102 remote_object = session_bus.get_object(
103 'org.panucci.panucciInterface', '/panucciInterface' )
104 print 'Found panucci instance already running, will try to use it...'
105 except dbus.exceptions.DBusException:
106 remote_object = None
109 if remote_object is None:
110 # configure logging
111 init_logging( logging.DEBUG if opts.debug else logging.ERROR )
113 from panucci import main
114 main.run(filename=filepath)
115 else:
116 if filepath is not None:
117 if opts.queue_filename is not None:
118 remote_object.queue_file( filepath )
119 else:
120 remote_object.play_file( filepath )
122 remote_object.show_main_window()