Remove URL hook (is set automatically by GTK+)
[panucci.git] / src / panucci / util.py
blobadef3261305e9d275515b7879224a7561252fd1a
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 gtk
23 import os
24 import os.path
25 import sys
26 import traceback
27 import logging
29 from panucci import platform
31 __log = logging.getLogger('panucci.util')
33 def convert_ns(time_int):
34 time_int = max( 0, int(time_int) )
35 time_int = time_int / 10**9
36 time_str = ""
37 if time_int >= 3600:
38 _hours = time_int / 3600
39 time_int = time_int - (_hours * 3600)
40 time_str = str(_hours) + ":"
41 if time_int >= 600:
42 _mins = time_int / 60
43 time_int = time_int - (_mins * 60)
44 time_str = time_str + str(_mins) + ":"
45 elif time_int >= 60:
46 _mins = time_int / 60
47 time_int = time_int - (_mins * 60)
48 time_str = time_str + "0" + str(_mins) + ":"
49 else:
50 time_str = time_str + "00:"
51 if time_int > 9:
52 time_str = time_str + str(time_int)
53 else:
54 time_str = time_str + "0" + str(time_int)
56 return time_str
58 def detect_filetype( filepath ):
59 if len(filepath.split('.')) > 1:
60 filename, extension = filepath.rsplit( '.', 1 )
61 return extension.lower()
63 def pretty_filename( filename ):
64 filename, extension = os.path.basename(filename).rsplit('.',1)
65 return filename.replace('_', ' ')
67 def build_full_path( path ):
68 if path is not None:
69 if path.startswith('/'):
70 return os.path.abspath(path)
71 else:
72 return os.path.abspath( os.path.join(os.getcwdu(), path) )
74 def find_image(filename):
75 locations = ['./icons/', '../icons/', '/usr/share/panucci/',
76 os.path.dirname(sys.argv[0])+'/../icons/']
78 for location in locations:
79 if os.path.exists(location+filename):
80 return os.path.abspath(location+filename)
82 try:
83 import pynotify
84 pynotify.init('Panucci')
85 have_pynotify = True
86 except:
87 have_pynotify = False
89 def notify( msg, title='Panucci' ):
90 """ Sends a notification using pynotify, returns msg """
91 if platform.DESKTOP and have_pynotify:
92 icon = find_image('panucci_64x64.png')
93 args = ( title, msg ) if icon is None else ( title, msg, icon )
94 notification = pynotify.Notification(*args)
95 notification.show()
96 elif platform.MAEMO:
97 # Note: This won't work if we're not in the gtk main loop
98 markup = '<b>%s</b>\n<small>%s</small>' % (title, msg)
99 import hildon
100 hildon.hildon_banner_show_information_with_markup(
101 gtk.Label(''), None, markup )
103 return msg
105 def get_logfile():
106 if platform.MAEMO:
107 f = '~/MyDocs/panucci.log'
108 else:
109 f = '~/.panucci.log'
111 return os.path.expanduser( f )