Back to index

eyed3  0.6.18
utils.py
Go to the documentation of this file.
00001 ################################################################################
00002 #  Copyright (C) 2003-2005  Travis Shirk <travis@pobox.com>
00003 #
00004 #  This program is free software; you can redistribute it and/or modify
00005 #  it under the terms of the GNU General Public License as published by
00006 #  the Free Software Foundation; either version 2 of the License, or
00007 #  (at your option) any later version.
00008 #
00009 #  This program is distributed in the hope that it will be useful,
00010 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 #  GNU General Public License for more details.
00013 #
00014 #  You should have received a copy of the GNU General Public License
00015 #  along with this program; if not, write to the Free Software
00016 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 #
00018 #  $Id: utils.py,v 1.11.2.1 2005/12/02 03:18:42 travis Exp $
00019 ################################################################################
00020 from eyeD3 import *;
00021 
00022 def versionsToConstant(v):
00023    major = v[0];
00024    minor = v[1];
00025    rev = v[2];
00026    if major == 1:
00027       if minor == 0:
00028          return ID3_V1_0;
00029       elif minor == 1:
00030          return ID3_V1_1;
00031    elif major == 2:
00032       if minor == 2:
00033          return ID3_V2_2;
00034       if minor == 3:
00035          return ID3_V2_3;
00036       elif minor == 4:
00037          return ID3_V2_4;
00038    raise str("Invalid ID3 version: %s" % str(v));
00039 
00040 def versionToString(v):
00041    if v & ID3_V1:
00042       if v == ID3_V1_0:
00043          return "v1.0";
00044       elif v == ID3_V1_1:
00045          return "v1.1";
00046       elif v == ID3_V1:
00047          return "v1.x";
00048    elif v & ID3_V2:
00049       if v == ID3_V2_2:
00050          return "v2.2";
00051       elif v == ID3_V2_3:
00052          return "v2.3";
00053       elif v == ID3_V2_4:
00054          return "v2.4";
00055       elif v == ID3_V2:
00056          return "v2.x";
00057 
00058    if v == ID3_ANY_VERSION:
00059       return "v1.x/v2.x";
00060    raise str("versionToString - Invalid ID3 version constant: %s" % hex(v));
00061 
00062 def constantToVersions(v):
00063    if v & ID3_V1:
00064       if v == ID3_V1_0:
00065          return [1, 0, 0];
00066       elif v == ID3_V1_1:
00067          return [1, 1, 0];
00068       elif v == ID3_V1:
00069          return [1, 1, 0];
00070    elif v & ID3_V2:
00071       if v == ID3_V2_2:
00072          return [2, 2, 0];
00073       elif v == ID3_V2_3:
00074          return [2, 3, 0];
00075       elif v == ID3_V2_4:
00076          return [2, 4, 0];
00077       elif v == ID3_V2:
00078          return [2, 4, 0];
00079    raise str("constantToVersions - Invalid ID3 version constant: %s" % hex(v));
00080 
00081 ################################################################################
00082 TRACE = 0;
00083 prefix = "eyeD3 trace> ";
00084 def TRACE_MSG(msg):
00085    if TRACE:
00086        try:
00087            print prefix + msg;
00088        except UnicodeEncodeError:
00089            pass;
00090 
00091 STRICT_ID3 = 0
00092 def strictID3():
00093    return STRICT_ID3
00094 
00095 ITUNES_COMPAT = 0
00096 def itunesCompat():
00097    return ITUNES_COMPAT
00098 ################################################################################
00099 
00100 import os;
00101 
00102 class FileHandler:
00103     R_CONT = 0;
00104     R_HALT = -1;
00105 
00106     # MUST return R_CONT or R_HALT
00107     def handleFile(self, f):
00108         pass
00109 
00110     # MUST for all files processed return 0 for success and a positive int
00111     # for error
00112     def handleDone(self):
00113         pass
00114 
00115 class FileWalker:
00116     def __init__(self, handler, root, excludes = []):
00117         self._handler = handler;
00118         self._root = root;
00119         self._excludes = excludes;
00120 
00121     def go(self):
00122         for (root, dirs, files) in os.walk(self._root):
00123             for f in files:
00124                 f = os.path.abspath(root + os.sep + f);
00125                 if not self._isExcluded(f):
00126                     if self._handler.handleFile(f) == FileHandler.R_HALT:
00127                         return FileHandler.R_HALT;
00128         return self._handler.handleDone();
00129 
00130     def _isExcluded(self, path):
00131         for ex in self._excludes:
00132             match = re.compile(exd).search(path);
00133             if match and match.start() == 0:
00134                 return 1;
00135         return 0;
00136 
00137 ################################################################################
00138 # Time and memory string formatting
00139 def format_track_time(curr, total=None):
00140     def time_tuple(ts):
00141         if ts is None or ts < 0:
00142             ts = 0
00143         hours = ts / 3600
00144         mins = (ts % 3600) / 60
00145         secs = (ts % 3600) % 60
00146         tstr = '%02d:%02d' % (mins, secs)
00147         if int(hours):
00148             tstr = '%02d:%s' % (hours, tstr)
00149         return (int(hours), int(mins), int(secs), tstr)
00150 
00151     hours, mins, secs, curr_str = time_tuple(curr)
00152     retval = curr_str
00153     if total:
00154         hours, mins, secs, total_str = time_tuple(total)
00155         retval += ' / %s' % total_str
00156     return retval
00157 
00158 KB_BYTES = 1024
00159 MB_BYTES = 1048576
00160 GB_BYTES = 1073741824
00161 KB_UNIT = 'KB'
00162 MB_UNIT = 'MB'
00163 GB_UNIT = 'GB'
00164 
00165 def format_size(sz):
00166     unit = 'Bytes'
00167     if sz >= GB_BYTES:
00168         sz = float(sz) / float(GB_BYTES)
00169         unit = GB_UNIT
00170     elif sz >= MB_BYTES:
00171         sz = float(sz) / float(MB_BYTES)
00172         unit = MB_UNIT
00173     elif sz >= KB_BYTES:
00174         sz = float(sz) / float(KB_BYTES)
00175         unit = KB_UNIT
00176     return "%.2f %s" % (sz, unit)
00177 
00178 def format_time_delta(td):
00179     days = td.days
00180     hours = td.seconds / 3600
00181     mins = (td.seconds % 3600) / 60
00182     secs = (td.seconds % 3600) % 60
00183     tstr = "%02d:%02d:%02d" % (hours, mins, secs)
00184     if days:
00185         tstr = "%d days %s" % (days, tstr)
00186     return tstr
00187 
00188 ## MIME type guessing
00189 import mimetypes
00190 try:
00191     import magic
00192     _magic = magic.open(magic.MAGIC_SYMLINK | magic.MAGIC_MIME)
00193     _magic.load()
00194 except:
00195     _magic = None
00196 
00197 def guess_mime_type(filename):
00198     mime = mimetypes.guess_type(filename)[0]
00199     if not mime and _magic and os.path.isfile(filename):
00200         mime = _magic.file(filename)
00201         if mime:
00202             mime = mime.split(";")[0]
00203     return mime
00204