Tweaked file size units parsing.
mp3togo/conf.py
1 # - conf.py -
2 # This file is part of mp3togo
3
4 # Convert audio files to play on a mp3 player
5 # Manage program options
6 #
7 # (c) Simeon Veldstra 2004, 2006 <reallifesim@gmail.com>
8 #
9 # This software is free.
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 may redistribute this program under the terms of the
17 # GNU General Public Licence version 2
18 # Available in this package or at http://www.fsf.org
19
20 # requires python-pyvorbis and python-id3 and lame
21 # and mpg321
22
23 import sys
24 import os
25 import time
26
27 import mp3togo
28 import mp3togo.options as options
29
30 from mp3togo.helpers import helpers
31
32
33 class Fail(options.Fail):
34 pass
35
36 class Error(Exception):
37 pass
38
39 class ErrorUnknownFileType(Error):
40 pass
41
42 class ErrorNoFile(Error):
43 pass
44
45 class ErrorNoCache(Error):
46 pass
47
48 class ErrorBadFormat(Error):
49 pass
50
51 class ErrorNoXMMSControl(Error):
52 pass
53
54 class ErrorXMMSNotRunning(Error):
55 pass
56
57 class ErrorNoDCOP(Error):
58 pass
59
60 class ErrorAmarokNotRunning(Error):
61 pass
62
63 class ErrorUnlocked(Error):
64 pass
65
66 class TaskNotReadyError(Error):
67 pass
68
69 class ErrorClusterProtocol(Error):
70 pass
71
72
73
74 class Options(options.Options):
75 """Subclass options.Options and fill in application specific
76 information."""
77 def __init__(self, argv=None, conffile=None, readconf=True):
78 options.Options.__init__(self, '~/.mp3togo')
79
80 # Options to add:
81 # ((name, short option, long option, default value, hook, help text), ...)
82 self._set += [
83 ('brwarning', 't', 'file-tag', '.2go', None,
84 '''A string appended to the name of the file to indicate it has been recoded.'''),
85 ('tempdir', 'w', 'work-dir', '/tmp', self._absfile,
86 '''The path to store temporary files. This could need several hundred megabytes free.'''),
87 ('treedepth', 'd', 'tree-depth', 1, None,
88 '''The number of directory levels to preserve in the output tree. Use 0 to put all output files directly into the target directory. Use 2 to create a directory for the Artist and then the Album (Assuming your music collection is organized in directories by artist then album).'''),
89 ('treestructure', '', 'format', '', None,
90 '''A string specifying the format for the output files. The format string can contain the following escapes:<li>%a - Artist<li>%l - Album<li>%t - Title<li>%y - Year<li>%g - Genre<li>%% - Literal '%'<br>Overrides --tree-depth.'''),
91 ('notags', '', 'no-tags', False, None,
92 '''Do not try to write tags to the output files.'''),
93 ('maxunits', 'm', 'max-size', '0', self._units,
94 '''The disk space available to use in bytes. Append 'M' for megabytes, 'G' for gigabytes or 'K' for kilobytes. Use 0 to use all available space on the filesystem.'''),
95 ('maxsize', '', '', 0L, None, ''),
96 ('maxtempunits', '', 'max-temp-size', '0', self._units,
97 '''The disk space available to use for temporary files in bytes. Append 'M' for megabytes, 'G' for gigabytes or 'K' for kilobytes. Use 0 to use all available space on the filesystem.'''),
98 ('maxtempsize', '', '', 0L, None, ''),
99 ('force', 'F', 'force', False, None,
100 '''Overwrite files if they already exist.'''),
101 ('update', 'u', 'update', False, None,
102 '''Overwrite files if they already exist only if they are older than the source file.'''),
103 ('encoder', 'C', 'encoder', 'lame', None,
104 '''The encoder to use to create the output files. Options are wav, lame or oggenc.'''),
105 ('encopts', 'E', 'encoder-options', '', None,
106 '''Compression options for the encoder.'''),
107 ('compfactor', 'z', 'compression-factor', 18.0, None,
108 '''If you change the lame options, you must change this factor to match the compression rate of the new options. This is used to estimate completed file size.'''),
109 ('makecache', '', 'make-cache', '', self._absfile, '''Make an output file cache at the given path.'''),
110 ('cachesize', '', '', 0L, None, ''),
111 ('cacheunits', '', 'cache-size', '0', self._units, '''The size for the new cache.'''),
112 ('usecache', '', 'use-cache', '', self._absfile, '''Use the cache stored at the given path.'''),
113 ('help', 'h', 'help', False, None, '''Print this message.'''),
114 ('playlist', 'p', 'playlist', '', None,
115 '''The playlist to convert. Playlists can be simple lists of file paths, one per line, or .m3u files or the native XMMS playlist file format. Playlists can be also listed on the command line as free arguments after all of the options if they have a recognizable extension. Currently .m3u and .pls are recognized.'''),
116 ('readxmms', 'x', 'import-xmms', False, None,
117 '''Get playlist from running instance of XMMS. (You must have the python-xmms module installed)'''),
118 ('readamarok', '', 'import-amarok', False, None,
119 '''Get playlist from running instance of Amarok.'''),
120 ('playerdir', 'o', 'output-dir', os.curdir, self._absfile,
121 '''Where to write the output files.'''),
122 ('index', '', 'index', False, None,
123 '''Create an index file when in wav output mode.'''),
124 ('nonormal', '', 'no-normalize', False, None,
125 '''Don't normalize the wav file before encoding.'''),
126 ('cluster', '', 'cluster', '', None,
127 '''Manage a cluster of worker machines. Cluster mode distributes tracks between multiple computers connected to a LAN. Provide a comma separated list of IP addresses or hostnames. The master machine must be able to log in to the slaves without a password and the slaves must have shared access to the filesystem. See the website for an example of how to set it up.<li>http://puddle.ca/mp3togo/'''),
128 ('clusternolocal', '', 'cluster-no-local-node', False, None,
129 '''Only use the remote nodes for processing files.'''),
130 ('clusterslave', '', 'cluster-slave', False, None,
131 '''Start a worker process for cluster mode. This option is used by the cluster master to start slave processes. Do not use.''')]
132
133 # Override hook defined in Base class
134 self._conffile = self._absfile
135
136 # Options to not save to the config file:
137 self._nosave_options += []
138
139 # Binaries to check for:
140 self.bin['wav'] = True
141 self.bin['lame'] = False
142 self.bin['oggenc'] = False
143 self.bin['mpg321'] = False
144 self.bin['ogg123'] = False
145 self.bin['flac'] = False
146 self.bin['faad'] = False
147 self.bin['metaflac'] = False
148 self.bin['normalize-audio'] = False
149 self.bin['dcop'] = False
150
151 # What input types are supported on this system?
152 self.types = []
153 for helper in helpers.values():
154 if helper['action'] == 'decode':
155 bin = helper['cmd'].split()[0]
156 for path in map(lambda x: os.path.join(x, bin),
157 os.environ['PATH'].split(':')):
158 if os.path.exists(path):
159 if isinstance(helper['type'], (tuple, list)):
160 self.types.extend(helper['type'])
161 else:
162 self.types.append(helper['type'])
163 break
164
165 self.version = mp3togo.version
166 self._help_preamble = """
167 Synopsis:
168 mp3togo [-p playlist] [-o output-dir] [options] [input files]
169
170 Description:
171 mp3togo is a program for converting audio files of various
172 formats into a common format suitable for use on a portable
173 mp3 player or an mp3 CD. The files to convert can be
174 specified in a playlist file (m3u, pls and plain text are
175 supported) or given as arguments to the program. mp3togo
176 will go through the list and run mpg321, ogg123, flac, faad and
177 lame to decode and reencode the files. The newly encoded
178 files will be written to the destination directory. The
179 software can retain as much of the subdirectory structure
180 as desired.
181 """
182
183 # Check for Third party modules:
184 self.mod['ogg.vorbis'] = False
185 self.mod['ID3'] = False
186 self.mod['eyeD3'] = False
187 self.mod['xmms'] = False
188
189 # Go ahead and read in the data:
190 self.getconf(argv, conffile, readconf)
191
192
193
194 # All hooks are called with the lock held
195 # and must reference the ._d dict directly
196 def _post_hook(self):
197 #Set up default encoder options if not specified:
198 if not self._d['encopts']:
199 self.reset_encoder_options()
200
201 def _absfile(self, name, value):
202 self._d[name] = absfile(value)
203
204 def _units(self, name, value):
205 #Calculate raw bytes and set [max|temp|cache]size
206 try:
207 if value[-1] in ('b', 'B'):
208 value = value[:-1]
209 if value[-1] in ('m', 'M'):
210 max = long(value[:-1]) * 1048576L
211 elif value[-1] in ('g', 'G'):
212 max = long(value[:-1]) * 1073741824L
213 elif value[-1] in ('k', 'K'):
214 max = long(value[:-1]) * 1024L
215 else:
216 max = long(value)
217 except ValueError:
218 raise Fail, "Bad %s option: %s " % (name, value)
219 sizename = name.replace('units', 'size')
220 self._d[sizename] = max # 'maxsize'
221 self._d[name] = value # 'maxunits'
222
223 def _arg_files(self, name, value):
224 l = []
225 for f in value:
226 f = absfile(f)
227 if os.path.exists(f):
228 l.append(f)
229 self._d[name] = l
230
231 def reset_encoder_options(self):
232 """Reset encoder options to defaults."""
233 if self._d['encoder'] == 'lame':
234 self._d['encopts'] = '--abr 96'
235 elif self._d['encoder'] == 'oggenc':
236 self._d['encopts'] = '-m 96 -M 225 -b 100'
237 elif self._d['encoder'] == 'wav':
238 self._d['encopts'] = ''
239
240 def cleanfilename(self, name):
241 """Remove nasty characters from a filename"""
242 name = name.replace('"', "'")
243 name = name.replace(':', '.')
244 name = name.replace("?", "")
245 name = name.replace('*', '')
246 name = name.replace('`', "'")
247 name = name.replace('&', '+')
248 name = name.replace(';', '.')
249 name = name.replace('#', '-')
250 name = name.replace('|', '-')
251 if name[0] == '.':
252 name = '_' + name
253 return name
254
255 def getfiletype(self, filename):
256 """Return the format of an audio file"""
257 # Could use some more magic here
258 ft = os.path.splitext(filename)[1][1:]
259 #if ft not in ('mp3', 'ogg', 'flac', 'wav', 'm4a'):
260 if ft not in self.types:
261 raise ErrorUnknownFileType
262 return ft
263
264 def checkfile(self, name):
265 """Verify the existence of an audio file"""
266 name = name.replace('\n', '')
267 if not os.path.exists(name):
268 raise ErrorNoFile
269 name = absfile(name)
270 self.getfiletype(name) # Throws exception if unknown
271 return name
272
273 def est_decoded_size(self, filename):
274 """Estimate size of decoded wav file."""
275 tp = self.getfiletype(filename)
276 helper = find_helper(tp, 'decode')
277 return os.stat(filename).st_size * helpers[helper]['factor']
278
279
280
281
282 def try_print(msg):
283 """Try to print a message to a nonblocking stdout"""
284 for i in range(1, 6):
285 try:
286 sys.stdout.write(msg)
287 return
288 except:
289 time.sleep(0.01 ** (1.0 / i))
290
291 def absfile(name):
292 name = name.strip()
293 name = os.path.expanduser(name)
294 if name:
295 name = os.path.abspath(name)
296 return name
297
298 def find_helper(type, action):
299 """Return the name of the best helper that performs 'action' on 'type'
300
301 Raises KeyError if type or action is unknown."""
302 for helper in helpers.keys():
303 if helpers[helper]['action'] == action and \
304 type in helpers[helper]['type']:
305 return helper
306 raise KeyError
307
308 def make_args(helper, input, output, args=''):
309 """Substitute for command line args"""
310 esc = {'z': '%', 'i': "###input###", 'o': "###output###", 'a': args}
311 out = ""
312 fmt = helpers[helper]['cmd']
313 fmt = fmt.replace('%%', '%z')
314 fmt = fmt.split('%')
315 while fmt:
316 out += fmt[0]
317 if len(fmt) <= 1:
318 break
319 fmt = fmt[1:]
320 code = fmt[0] and fmt[0][0]
321 if code in esc.keys():
322 fmt[0] = esc[code] + fmt[0][1:]
323 else:
324 raise ErrorBadFormat
325 out = out.split()
326 # This foolishness is to prevent splitting
327 # filenames with whitespace in them.
328 if "###input###" in out:
329 out[out.index("###input###")] = input
330 if "###output###" in out:
331 out[out.index("###output###")] = output
332 return out
333
334