not needed with 20Mi
[pyTivo/wgw.git] / eyeD3 / utils.py
blob8c9a682532ed310bbfb77a47d3085b9973ffd363
1 ################################################################################
2 # Copyright (C) 2003-2005 Travis Shirk <travis@pobox.com>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # $Id: utils.py,v 1.11.2.1 2005/12/02 03:18:42 travis Exp $
19 ################################################################################
20 from eyeD3 import *;
22 def versionsToConstant(v):
23 major = v[0];
24 minor = v[1];
25 rev = v[2];
26 if major == 1:
27 if minor == 0:
28 return ID3_V1_0;
29 elif minor == 1:
30 return ID3_V1_1;
31 elif major == 2:
32 if minor == 2:
33 return ID3_V2_2;
34 if minor == 3:
35 return ID3_V2_3;
36 elif minor == 4:
37 return ID3_V2_4;
38 raise str("Invalid ID3 version: %s" % str(v));
40 def versionToString(v):
41 if v & ID3_V1:
42 if v == ID3_V1_0:
43 return "v1.0";
44 elif v == ID3_V1_1:
45 return "v1.1";
46 elif v == ID3_V1:
47 return "v1.x";
48 elif v & ID3_V2:
49 if v == ID3_V2_2:
50 return "v2.2";
51 elif v == ID3_V2_3:
52 return "v2.3";
53 elif v == ID3_V2_4:
54 return "v2.4";
55 elif v == ID3_V2:
56 return "v2.x";
58 if v == ID3_ANY_VERSION:
59 return "v1.x/v2.x";
60 raise str("versionToString - Invalid ID3 version constant: %s" % hex(v));
62 def constantToVersions(v):
63 if v & ID3_V1:
64 if v == ID3_V1_0:
65 return [1, 0, 0];
66 elif v == ID3_V1_1:
67 return [1, 1, 0];
68 elif v == ID3_V1:
69 return [1, 1, 0];
70 elif v & ID3_V2:
71 if v == ID3_V2_2:
72 return [2, 2, 0];
73 elif v == ID3_V2_3:
74 return [2, 3, 0];
75 elif v == ID3_V2_4:
76 return [2, 4, 0];
77 elif v == ID3_V2:
78 return [2, 4, 0];
79 raise str("constantToVersions - Invalid ID3 version constant: %s" % hex(v));
81 ################################################################################
82 TRACE = 0;
83 prefix = "eyeD3 trace> ";
84 def TRACE_MSG(msg):
85 if TRACE:
86 try:
87 print prefix + msg;
88 except UnicodeEncodeError:
89 pass;
91 STRICT_ID3 = 0;
92 def strictID3():
93 return STRICT_ID3;
94 ################################################################################
96 import os;
98 class FileHandler:
99 R_CONT = 0;
100 R_HALT = -1;
102 # MUST return R_CONT or R_HALT
103 def handleFile(self, f):
104 pass
106 # MUST for all files processed return 0 for success and a positive int
107 # for error
108 def handleDone(self):
109 pass
111 class FileWalker:
112 def __init__(self, handler, root, excludes = []):
113 self._handler = handler;
114 self._root = root;
115 self._excludes = excludes;
117 def go(self):
118 for (root, dirs, files) in os.walk(self._root):
119 for f in files:
120 f = os.path.abspath(root + os.sep + f);
121 if not self._isExcluded(f):
122 if self._handler.handleFile(f) == FileHandler.R_HALT:
123 return FileHandler.R_HALT;
124 return self._handler.handleDone();
126 def _isExcluded(self, path):
127 for ex in self._excludes:
128 match = re.compile(exd).search(path);
129 if match and match.start() == 0:
130 return 1;
131 return 0;
133 ################################################################################
134 # Time and memory string formatting
135 def format_track_time(curr, total=None):
136 def time_tuple(ts):
137 if ts is None or ts < 0:
138 ts = 0
139 hours = ts / 3600
140 mins = (ts % 3600) / 60
141 secs = (ts % 3600) % 60
142 tstr = '%02d:%02d' % (mins, secs)
143 if int(hours):
144 tstr = '%02d:%s' % (hours, tstr)
145 return (int(hours), int(mins), int(secs), tstr)
147 hours, mins, secs, curr_str = time_tuple(curr)
148 retval = curr_str
149 if total:
150 hours, mins, secs, total_str = time_tuple(total)
151 retval += ' / %s' % total_str
152 return retval
154 KB_BYTES = 1024
155 MB_BYTES = 1048576
156 GB_BYTES = 1073741824
157 KB_UNIT = 'KB'
158 MB_UNIT = 'MB'
159 GB_UNIT = 'GB'
161 def format_size(sz):
162 unit = 'Bytes'
163 if sz >= GB_BYTES:
164 sz = float(sz) / float(GB_BYTES)
165 unit = GB_UNIT
166 elif sz >= MB_BYTES:
167 sz = float(sz) / float(MB_BYTES)
168 unit = MB_UNIT
169 elif sz >= KB_BYTES:
170 sz = float(sz) / float(KB_BYTES)
171 unit = KB_UNIT
172 return "%.2f %s" % (sz, unit)
174 def format_time_delta(td):
175 days = td.days
176 hours = td.seconds / 3600
177 mins = (td.seconds % 3600) / 60
178 secs = (td.seconds % 3600) % 60
179 tstr = "%02d:%02d:%02d" % (hours, mins, secs)
180 if days:
181 tstr = "%d days %s" % (days, tstr)
182 return tstr