Release preparation (fix version number resolving)
[panucci.git] / bin / panucci
blob9ba799d598c3bddeb9ad953f87921cd8ecae07a7
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 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 main
110 main.run(filename=filepath)
111 else:
112 if filepath is not None:
113 if opts.queue_filename is not None:
114 remote_object.queue_file( filepath )
115 else:
116 remote_object.play_file( filepath )
118 remote_object.show_main_window()