Cleanup and document panucci.util
[panucci.git] / src / panucci / util.py
blob8b5a239009bdeae801d4291110c1cb707b9b4316
1 # -*- coding: utf-8 -*-
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 os.path
25 def convert_ns(time_int):
26 """Convert nanosecond values into strings
28 This function should be used to generate
29 a string suitable for display in the UI.
30 """
31 time_int = max( 0, int(time_int) )
32 time_int = time_int / 10**9
33 time_str = ""
34 if time_int >= 3600:
35 _hours = time_int / 3600
36 time_int = time_int - (_hours * 3600)
37 time_str = str(_hours) + ":"
38 if time_int >= 600:
39 _mins = time_int / 60
40 time_int = time_int - (_mins * 60)
41 time_str = time_str + str(_mins) + ":"
42 elif time_int >= 60:
43 _mins = time_int / 60
44 time_int = time_int - (_mins * 60)
45 time_str = time_str + "0" + str(_mins) + ":"
46 else:
47 time_str = time_str + "00:"
48 if time_int > 9:
49 time_str = time_str + str(time_int)
50 else:
51 time_str = time_str + "0" + str(time_int)
53 return time_str
56 def detect_filetype(filepath):
57 """Return the file type (extension) of a file path
59 This returns something like "ogg", "mp3", etc..
60 """
61 filename, extension = os.path.splitext(filepath)
62 if extension.startswith('.'):
63 extension = extension[1:]
64 return extension.lower()
67 def pretty_filename(filename):
68 """Return a prettified version of a filename
70 Currently, this removes the extension and
71 replaces underscores with spaces.
72 """
73 filename, extension = os.path.splitext(filename)
74 basename = os.path.basename(filename)
75 return filename.replace('_', ' ')