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
25 import logging
.handlers
28 from optparse
import OptionParser
31 # Set up gettext support
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
):
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
) ):
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
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')
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')
94 # force all exceptions to pass through the logger
95 sys
.excepthook
= lambda *args
: logger
.critical( 'Exception caught:',
99 # Attempt to contact an already-running copy of Panucci
100 session_bus
= dbus
.SessionBus()
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
:
109 if remote_object
is None:
111 init_logging( logging
.DEBUG
if opts
.debug
else logging
.ERROR
)
113 from panucci
import main
114 main
.run(filename
=filepath
)
116 if filepath
is not None:
117 if opts
.queue_filename
is not None:
118 remote_object
.queue_file( filepath
)
120 remote_object
.play_file( filepath
)
122 remote_object
.show_main_window()