importfeeds: add new 'echo' type that writes to stdout

This commit is contained in:
robotanarchy 2014-09-13 17:11:00 +02:00
parent 395be1a0f6
commit 5b0b3eee45
2 changed files with 18 additions and 6 deletions

View file

@ -13,18 +13,20 @@
# included in all copies or substantial portions of the Software.
"""Write paths of imported files in various formats to ease later import in a
music player.
music player. Also allow printing the new file locations to stdout in case
one wants to manually add music to a player by its path.
"""
import datetime
import os
import re
import logging
from beets.plugins import BeetsPlugin
from beets.util import normpath, syspath, bytestring_path
from beets import config
M3U_DEFAULT_NAME = 'imported.m3u'
log = logging.getLogger('beets')
class ImportFeedsPlugin(BeetsPlugin):
def __init__(self):
@ -80,11 +82,16 @@ def _build_m3u_filename(basename):
def _write_m3u(m3u_path, items_paths):
"""Append relative paths to items into m3u file.
"""Append relative paths to items into m3u file, or write to stdout.
"""
with open(syspath(m3u_path), 'a') as f:
if m3u_path == "stdout":
log.info("Location of imported music:")
for path in items_paths:
f.write(path + '\n')
log.info(" " + path)
else:
with open(syspath(m3u_path), 'a') as f:
for path in items_paths:
f.write(path + '\n')
def _record_items(lib, basename, items):
@ -126,6 +133,10 @@ def _record_items(lib, basename, items):
if not os.path.exists(syspath(dest)):
os.symlink(syspath(path), syspath(dest))
if 'echo' in formats:
m3u_path = _build_m3u_filename(basename)
_write_m3u("stdout", paths)
@ImportFeedsPlugin.listen('library_opened')
def library_opened(lib):

View file

@ -18,12 +18,13 @@ root of your music library.
The ``absolute_path`` configuration option can be set to use absolute paths
instead of relative paths. Some applications may need this to work properly.
Three different types of outputs coexist, specify the ones you want to use by
Four different types of outputs coexist, specify the ones you want to use by
setting the ``formats`` parameter:
- ``m3u``: catalog the imports in a centralized playlist. By default, the playlist is named ``imported.m3u``. To use a different file, just set the ``m3u_name`` parameter inside the ``importfeeds`` config section.
- ``m3u_multi``: create a new playlist for each import (uniquely named by appending the date and track/album name).
- ``link``: create a symlink for each imported item. This is the recommended setting to propagate beets imports to your iTunes library: just drag and drop the ``dir`` folder on the iTunes dock icon.
- ``echo``: do not write a playlist file at all, but echo a list of new file paths to the terminal.
Here's an example configuration for this plugin::