Permalink
Browse files

convert: PEP8, changelog note and license

  • Loading branch information...
yagebu committed Oct 11, 2012
1 parent 115c0e7 commit e316d0ea3062a67aab0171dfba93de5f6002b89d
Showing with 31 additions and 15 deletions.
  1. +29 −15 beetsplug/convert.py
  2. +2 −0 docs/changelog.rst
View
@@ -1,8 +1,21 @@
+# This file is part of beets.
+# Copyright 2012, Jakob Schnitzer.
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+
"""Converts tracks or albums to external directory
"""
import logging
import os
-import threading
import shutil
from subprocess import Popen, PIPE
@@ -15,6 +28,7 @@
DEVNULL = open(os.devnull, 'wb')
conf = {}
+
def _embed(path, items):
"""Embed an image file, located at `path`, into each item.
"""
@@ -35,21 +49,22 @@ def _embed(path, items):
f.art = data
f.save()
+
def encode(source, dest):
- log.info('Started encoding '+ source)
+ log.info('Started encoding ' + source)
temp_dest = dest + '~'
source_ext = os.path.splitext(source)[1].lower()
if source_ext == '.flac':
decode = Popen([conf['flac'], '-c', '-d', '-s', source],
stdout=PIPE)
encode = Popen([conf['lame']] + conf['opts'] + ['-', temp_dest],
- stdin=decode.stdout, stderr=DEVNULL)
+ stdin=decode.stdout, stderr=DEVNULL)
decode.stdout.close()
encode.communicate()
elif source_ext == '.mp3':
encode = Popen([conf['lame']] + conf['opts'] + ['--mp3input'] +
- [source, temp_dest], close_fds=True, stderr=DEVNULL)
+ [source, temp_dest], close_fds=True, stderr=DEVNULL)
encode.communicate()
else:
log.error('Only converting from FLAC or MP3 implemented')
@@ -61,7 +76,7 @@ def encode(source, dest):
util.prune_dirs(os.path.dirname(temp_dest))
return
shutil.move(temp_dest, dest)
- log.info('Finished encoding '+ source)
+ log.info('Finished encoding ' + source)
def convert_item(lib, dest_dir):
@@ -71,15 +86,15 @@ def convert_item(lib, dest_dir):
log.info('Skipping {0} : not supported format'.format(item.path))
continue
- dest = os.path.join(dest_dir,lib.destination(item, fragment = True))
+ dest = os.path.join(dest_dir, lib.destination(item, fragment=True))
dest = os.path.splitext(dest)[0] + '.mp3'
if os.path.exists(dest):
log.info('Skipping {0} : target file exists'.format(item.path))
continue
util.mkdirall(dest)
- if item.format == 'MP3' and item.bitrate < 1000*conf['max_bitrate']:
+ if item.format == 'MP3' and item.bitrate < 1000 * conf['max_bitrate']:
log.info('Copying {0}'.format(item.path))
shutil.copy(item.path, dest)
dest_item = library.Item.from_path(dest)
@@ -91,7 +106,7 @@ def convert_item(lib, dest_dir):
artpath = lib.get_album(item).artpath
if artpath and conf['embed']:
- _embed(artpath,[dest_item])
+ _embed(artpath, [dest_item])
def convert_func(lib, config, opts, args):
@@ -102,7 +117,7 @@ def convert_func(lib, config, opts, args):
threads = opts.threads if opts.threads is not None else conf['threads']
fmt = '$albumartist - $album' if opts.album \
- else '$artist - $album - $title'
+ else '$artist - $album - $title'
ui.commands.list_items(lib, ui.decargs(args), opts.album, False, fmt)
if not ui.input_yn("Convert? (Y/n)"):
@@ -126,18 +141,17 @@ def configure(self, config):
conf['opts'] = ui.config_val(config, 'convert',
'opts', '-V2').split(' ')
conf['max_bitrate'] = int(ui.config_val(config, 'convert',
- 'max_bitrate','500'))
+ 'max_bitrate', '500'))
conf['embed'] = ui.config_val(config, 'convert', 'embed', True,
- vtype = bool)
-
+ vtype=bool)
def commands(self):
cmd = ui.Subcommand('convert', help='convert to external location')
cmd.parser.add_option('-a', '--album', action='store_true',
- help='choose albums instead of tracks')
+ help='choose albums instead of tracks')
cmd.parser.add_option('-t', '--threads', action='store', type='int',
- help='change the number of threads (default 2)')
+ help='change the number of threads (default 2)')
cmd.parser.add_option('-d', '--dest', action='store',
- help='set the destination directory')
+ help='set the destination directory')
cmd.func = convert_func
return [cmd]
View
@@ -4,6 +4,8 @@ Changelog
1.0b16 (in development)
-----------------------
+* New plugin: :doc:`/plugins/convert` lets you convert parts of your collection
+ to an external directory using flac and lame.
* New plugin: :doc:`/plugins/fuzzy_search` lets you find albums and tracks using
fuzzy string matching so you don't have to type (or even remember) their exact
names. Thanks to Philippe Mongeau.

2 comments on commit e316d0e

@sampsyo

This comment has been minimized.

Show comment Hide comment
@sampsyo

sampsyo Oct 11, 2012

Owner

Looks great. Thanks for PEP8-ifying. But you forgot to thank yourself in the changelog. :)

(Joking aside, let me know if you'd prefer not to be credited; otherwise I'll add "Thanks to..." on there. You deserve fame.)

Owner

sampsyo replied Oct 11, 2012

Looks great. Thanks for PEP8-ifying. But you forgot to thank yourself in the changelog. :)

(Joking aside, let me know if you'd prefer not to be credited; otherwise I'll add "Thanks to..." on there. You deserve fame.)

@yagebu

This comment has been minimized.

Show comment Hide comment
@yagebu

yagebu Oct 11, 2012

Collaborator

Feel free to add a "Thanks to.." ;)

Collaborator

yagebu replied Oct 11, 2012

Feel free to add a "Thanks to.." ;)

Please sign in to comment.