root/branches/0.3/src/mesk/utils.py

Revision 807, 4.3 kB (checked in by nicfit, 2 years ago)

Fixed logging (#311)

Line 
1 ################################################################################
2 #  Copyright (C) 2006  Travis Shirk <travis@pobox.com>
3 #  Copyright (C) 2005  Nikos Kouremenos <nkour@jabber.org>
4 #  Copyright (C) 2005  Dimitur Kirov <dkirov@gmail.com>
5 #
6 #  This program 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 2 of the License, or
9 #  (at your option) any later version.
10 #
11 #  This program 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.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program; if not, write to the Free Software
18 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 #
20 ################################################################################
21 import os, sys, traceback
22
23 from i18n import _
24
25 class MeskException(Exception):
26     def __init__(self, primary_msg, secondary_msg=''):
27         Exception.__init__(self, primary_msg)
28         self.primary_msg = primary_msg
29         self.secondary_msg = secondary_msg
30     def __str__(self):
31         return '%s: %s' % (self.primary_msg, self.secondary_msg)
32
33 # Time and memory string formatting
34 def format_track_time(curr, total = None):
35     def time_tuple(ts):
36         if ts is None or ts < 0:
37             ts = 0
38         hours = ts / 3600
39         mins = (ts % 3600) / 60
40         secs = (ts % 3600) % 60
41         tstr = '%02d:%02d' % (mins, secs)
42         if int(hours):
43             tstr = '%02d:%s' % (hours, tstr)
44         return (int(hours), int(mins), int(secs), tstr)
45
46     hours, mins, secs, curr_str = time_tuple(curr)
47     retval = curr_str
48     if total:
49         hours, mins, secs, total_str = time_tuple(total)
50         retval += ' / %s' % total_str
51     return retval
52
53 KB_BYTES = 1024
54 MB_BYTES = 1048576
55 GB_BYTES = 1073741824
56 KB_UNIT = _('KB')
57 MB_UNIT = _('MB')
58 GB_UNIT = _('GB')
59
60 def format_size(sz):
61     unit = _('Bytes')
62     if sz >= GB_BYTES:
63         sz = float(sz) / float(GB_BYTES)
64         unit = GB_UNIT
65     elif sz >= MB_BYTES:
66         sz = float(sz) / float(MB_BYTES)
67         unit = MB_UNIT
68     elif sz >= KB_BYTES:
69         sz = float(sz) / float(KB_BYTES)
70         unit = KB_UNIT
71     return "%.2f %s" % (sz, unit)
72
73 def format_time_delta(td):
74     days = td.days
75     hours = td.seconds / 3600
76     mins = (td.seconds % 3600) / 60
77     secs = (td.seconds % 3600) % 60
78     tstr = "%02d:%02d:%02d" % (hours, mins, secs)
79     if days:
80         tstr = "%d days %s" % (days, tstr)
81     return tstr
82
83 def pad_string(label, width):
84     side = -1
85     while len(label) < width:
86         if side < 0:
87             label = ' %s' % label
88         else:
89             label = '%s ' % label
90         side *= -1
91     return label
92
93 # Version compares
94 def version_cmp(v1, v2):
95     lhs = []
96     rhs = []
97     for i in range(max(len(v1), len(v2))):
98         try:
99             lhs.append(int(v1[i]))
100         except IndexError:
101             lhs.append(0)
102         try:
103             rhs.append(int(v2[i]))
104         except IndexError:
105             rhs.append(0)
106
107     for i in range(len(lhs)):
108         if lhs[i] > rhs[i]:
109             return 1
110         elif lhs[i] < rhs[i]:
111             return -1
112     return 0
113
114 def remove_credentials(uri):
115     '''Removes any username and/or password from a uri.'''
116     if uri.user_name or uri.password:
117         # Remove username and password if they exist
118         uri_copy = uri.copy()
119         uri_copy.user_name = ''
120         uri_copy.password = ''
121         # Unfortunately, this leaves the delimiters
122         uri_copy = uri.make_uri(str(uri_copy).replace(':@', ''))
123         return uri_copy
124     else:
125         return uri
126
127 def load_web_page(url):
128     import webbrowser
129     url = str(url)
130     try:
131         webbrowser.open_new_tab(url)
132     except AttributeError:
133         # Python < 2.5
134         webbrowser.open(url)
135
136
137 import threading
138 class Thread(threading.Thread):
139     def __init__(self):
140         threading.Thread.__init__(self)
141         self._lock = threading.Lock()
142         self._stopped = False
143
144     def stop(self):
145         '''Cooperative thread shutdown.  Subclasses MUST react accordingly
146         when self._stopped is True.'''
147         self._lock.acquire()
148         self._stopped = True
149         self._lock.release()
Note: See TracBrowser for help on using the browser.