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 ################################################################################
22 def versionsToConstant(v
):
38 raise str("Invalid ID3 version: %s" % str(v
));
40 def versionToString(v
):
58 if v
== ID3_ANY_VERSION
:
60 raise str("versionToString - Invalid ID3 version constant: %s" % hex(v
));
62 def constantToVersions(v
):
79 raise str("constantToVersions - Invalid ID3 version constant: %s" % hex(v
));
81 ################################################################################
83 prefix
= "eyeD3 trace> ";
88 except UnicodeEncodeError:
94 ################################################################################
102 # MUST return R_CONT or R_HALT
103 def handleFile(self
, f
):
106 # MUST for all files processed return 0 for success and a positive int
108 def handleDone(self
):
112 def __init__(self
, handler
, root
, excludes
= []):
113 self
._handler
= handler
;
115 self
._excludes
= excludes
;
118 for (root
, dirs
, files
) in os
.walk(self
._root
):
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:
133 ################################################################################
134 # Time and memory string formatting
135 def format_track_time(curr
, total
=None):
137 if ts
is None or ts
< 0:
140 mins
= (ts
% 3600) / 60
141 secs
= (ts
% 3600) % 60
142 tstr
= '%02d:%02d' % (mins
, secs
)
144 tstr
= '%02d:%s' % (hours
, tstr
)
145 return (int(hours
), int(mins
), int(secs
), tstr
)
147 hours
, mins
, secs
, curr_str
= time_tuple(curr
)
150 hours
, mins
, secs
, total_str
= time_tuple(total
)
151 retval
+= ' / %s' % total_str
156 GB_BYTES
= 1073741824
164 sz
= float(sz
) / float(GB_BYTES
)
167 sz
= float(sz
) / float(MB_BYTES
)
170 sz
= float(sz
) / float(KB_BYTES
)
172 return "%.2f %s" % (sz
, unit
)
174 def format_time_delta(td
):
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
)
181 tstr
= "%d days %s" % (days
, tstr
)