Don't crash without a neteork
[pyTivo.git] / eyeD3 / utils.py
blob7116a660f73c6ecd81f3419f27a65344372d4332
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;