Bump version to 0.4
[panucci.git] / bin / panucci
blob017593d985f1f3f98bd93f6e77a2acb70a00f904
1 #!/usr/bin/env python
3 # This file is part of Panucci.
4 # Copyright (c) 2008-2009 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 app_version = '0.4'
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, version='%prog ' + app_version)
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 from panucci import util
64 filepath = util.build_full_path(args[0] if len(args) else opts.queue_filename)
67 def init_logging( log_level ):
68 """ Configure the logging module for panucci """
69 logger = logging.getLogger('panucci')
70 logger.setLevel( logging.DEBUG )
72 # the stream handler (logging to the console)
73 sh = logging.StreamHandler()
74 sh.setLevel( log_level )
75 fmt = logging.Formatter('%(levelname)s:%(name)s %(message)s')
76 sh.setFormatter(fmt)
77 logger.addHandler(sh)
79 # the file handler (logging to a file)
80 from panucci import util
81 f = util.get_logfile()
82 fh = logging.handlers.RotatingFileHandler( f, backupCount=0, # 25kb should
83 maxBytes=25*1024 ) # be enough.
84 fh.doRollover() # reset the logfile at startup
85 fh.setLevel( logging.DEBUG ) # log everything to the file
86 fmt = logging.Formatter('%(asctime)s %(levelname)s:%(name)s %(message)s')
87 fh.setFormatter(fmt)
88 logger.addHandler(fh)
90 # force all exceptions to pass through the logger
91 sys.excepthook = lambda *args: logger.critical( 'Exception caught:',
92 exc_info=args )
95 # Attempt to contact an already-running copy of Panucci
96 session_bus = dbus.SessionBus()
97 try:
98 remote_object = session_bus.get_object(
99 'org.panucci.panucciInterface', '/panucciInterface' )
100 print _('Found panucci instance already running, will try to use it...')
101 except dbus.exceptions.DBusException:
102 remote_object = None
105 if remote_object is None:
106 # configure logging
107 init_logging( logging.DEBUG if opts.debug else logging.ERROR )
109 from panucci import panucci
110 panucci.app_version = app_version
111 panucci.run( filename=filepath )
112 else:
113 if filepath is not None:
114 if opts.queue_filename is not None:
115 remote_object.queue_file( filepath )
116 else:
117 remote_object.play_file( filepath )
119 remote_object.show_main_window()