diff --git a/beets/importer.py b/beets/importer.py index 120cf903b..9436df424 100644 --- a/beets/importer.py +++ b/beets/importer.py @@ -20,8 +20,6 @@ import os import logging import pickle -from beets import ui -from beets.ui import print_ from beets import autotag from beets import library import beets.autotag.art @@ -34,6 +32,7 @@ CHOICE_MANUAL = 'CHOICE_MANUAL' CHOICE_ALBUM = 'CHOICE_ALBUM' QUEUE_SIZE = 128 +STATE_FILE = os.path.expanduser('~/.beetsstate') # Global logger. log = logging.getLogger('beets') @@ -88,7 +87,7 @@ def progress_set(toppath, path): that the tagging completed). """ try: - with open(ui.STATE_FILE) as f: + with open(STATE_FILE) as f: state = pickle.load(f) except IOError: state = {PROGRESS_KEY: {}} @@ -100,14 +99,14 @@ def progress_set(toppath, path): else: state[PROGRESS_KEY][toppath] = path - with open(ui.STATE_FILE, 'w') as f: + with open(STATE_FILE, 'w') as f: pickle.dump(state, f) def progress_get(toppath): """Get the last successfully tagged subpath of toppath. If toppath has no progress information, returns None. """ try: - with open(ui.STATE_FILE) as f: + with open(STATE_FILE) as f: state = pickle.load(f) except IOError: return None @@ -123,7 +122,7 @@ class ImportConfig(object): """ __slots__ = ['lib', 'paths', 'resume', 'logfile', 'color', 'quiet', 'quiet_fallback', 'copy', 'write', 'art', 'delete', - 'choose_match_func'] + 'choose_match_func', 'should_resume_func'] def __init__(self, **kwargs): for slot in self.__slots__: setattr(self, slot, kwargs[slot]) @@ -239,11 +238,6 @@ def read_albums(config): # Use absolute paths. paths = [library._normpath(path) for path in config.paths] - # Check the user-specified directories. - for path in paths: - if not os.path.isdir(library._syspath(path)): - raise ui.UserError('not a directory: ' + path) - # Look for saved progress. progress = config.resume is not False if progress: @@ -255,12 +249,9 @@ def read_albums(config): # Either accept immediately or prompt for input to decide. if config.resume: do_resume = True - ui.print_('Resuming interrupted import of %s' % path) + log.warn('Resuming interrupted import of %s' % path) else: - do_resume = ui.input_yn("Import of the directory:\n%s" - "\nwas interrupted. Resume (Y/n)?" % - path) - ui.print_() + do_resume = config.should_resume_func(config, path) if do_resume: resume_dirs[path] = resume_dir @@ -312,20 +303,12 @@ def user_query(config): accepts and yields ImportTask objects. """ lib = _reopen_lib(config.lib) - first = True task = None while True: task = yield task if task.sentinel: continue - # Empty lines between albums. - if not first: - print_() - first = False - # Show current album path. - print_(task.path) - # Ask the user for a choice. choice = config.choose_match_func(task, config) task.set_choice(choice) @@ -346,7 +329,7 @@ def user_query(config): album = task.info['album'] if _duplicate_check(lib, artist, album): tag_log(config.logfile, 'duplicate', task.path) - print_("This album is already in the library!") + log.warn("This album is already in the library!") task.set_choice(CHOICE_SKIP) def apply_choices(config): diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index bcd41f620..4fa5c897e 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -32,7 +32,6 @@ from beets import plugins # Constants. CONFIG_PATH_VAR = 'BEETSCONFIG' DEFAULT_CONFIG_FILE = os.path.expanduser('~/.beetsconfig') -STATE_FILE = os.path.expanduser('~/.beetsstate') DEFAULT_LIBRARY = '~/.beetsmusic.blb' DEFAULT_DIRECTORY = '~/Music' DEFAULT_PATH_FORMATS = { diff --git a/beets/ui/commands.py b/beets/ui/commands.py index e0af1d28b..e3143715b 100755 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -18,6 +18,7 @@ interface. from __future__ import with_statement # Python 2.5 import logging import sys +import os from beets import ui from beets.ui import print_ @@ -26,6 +27,7 @@ import beets.autotag.art from beets.ui import pipeline from beets import plugins from beets import importer +from beets import library # Global logger. log = logging.getLogger('beets') @@ -123,6 +125,10 @@ def show_change(cur_artist, cur_album, items, info, dist, color=True): elif cur_track != new_track: print_(" * %s (%s -> %s)" % (item.title, cur_track, new_track)) +def should_resume(config, path): + return ui.input_yn("Import of the directory:\n%s" + "\nwas interrupted. Resume (Y/n)?" % path) + def choose_candidate(cur_artist, cur_album, candidates, rec, color=True): """Given current metadata and a sorted list of (distance, candidate) pairs, ask the user for a selection @@ -232,6 +238,10 @@ def choose_match(task, config): dance with the user to ask for a choice of metadata. Returns an (info, items) pair, CHOICE_ASIS, or CHOICE_SKIP. """ + # Show what we're tagging. + print_() + print_(task.path) + if config.quiet: # No input; just make a decision. if task.rec == autotag.RECOMMEND_STRONG: @@ -293,6 +303,11 @@ def import_files(lib, paths, copy, write, autot, logpath, art, threaded, indicates what should happen in quiet mode when the recommendation is not strong. """ + # Check the user-specified directories. + for path in paths: + if not os.path.isdir(library._syspath(path)): + raise ui.UserError('not a directory: ' + path) + # Open the log. if logpath: logfile = open(logpath, 'w') @@ -317,6 +332,7 @@ def import_files(lib, paths, copy, write, autot, logpath, art, threaded, art = art, delete = delete, choose_match_func = choose_match, + should_resume_func = should_resume, ) # Perform the import. diff --git a/test/_common.py b/test/_common.py index b04d98a27..8fc2dfed4 100644 --- a/test/_common.py +++ b/test/_common.py @@ -54,6 +54,7 @@ def iconfig(lib, **kwargs): art = False, delete = False, choose_match_func = lambda x, y: importer.CHOICE_SKIP, + should_resume_func = lambda _: False, ) for k, v in kwargs.items(): setattr(config, k, v)