def test_build_file_list_from_folders_and_files(files): """ Tests `build_file_list` deals with paths + directories Given a list of file paths and 1+ directory paths this function should return a list of paths. """ subfolder = build_path(join_path('subfolder')) def full_path(path): paths = [] for p in os.listdir(path): paths.append(os.path.join(files, p)) return paths def build_all_paths(): paths = [] for path in full_path(files) + full_path(subfolder): if os.path.isfile(path): paths.append(path) return paths all_paths = build_all_paths() file_list = build_file_list(list(random_files(files)) + [subfolder]) for path in file_list: assert os.path.join(*path) in all_paths
def test_passing_current_dir_makes_file_list_a_list(files): file_list = helpers.build_file_list([files]) assert isinstance(file_list, collections.Iterable) PY3 = sys.version_info[0] == 3 string_type = str if PY3 else basestring text_type = str if PY3 else unicode assert not isinstance(file_list, (string_type, text_type))
def test_setting_recursive_adds_all_files_below_the_folder(files): new_folders = ('herp', 'derp', 'test') os.makedirs(os.path.join(files, *new_folders)) def build_folder(folder): new_files = ('foo', 'bar', 'blah') for fn in new_files: with open(os.path.join(files, folder, fn), 'w') as f: f.write('') build_folder('herp') build_folder('herp/derp') build_folder('herp/derp/test') file_list = helpers.build_file_list([files], recursive=True) for root, dirs, files in os.walk(files): for fn in files: assert (root, fn) in file_list
def test_build_file_list_from_a_folder_path(files): file_list = build_file_list([files]) for fn in os.listdir(files): if os.path.isdir(fn): assert os.path.join(files, fn) in file_list
def test_build_file_list_from_single_file(files): fn = list(random_files(files))[0] file_list = list(build_file_list([fn])) assert file_list == [os.path.split(fn)]
def test_build_file_list_from_multiple_files(files): _files = random_files(files) file_list = build_file_list(_files) for head, tail in file_list: assert tail in os.listdir(files)
def rename(config, canonical, debug, dry_run, episode, # pylint: disable-msg=too-many-arguments ignore_filelist, log_file, log_level, name, # pylint: disable-msg=too-many-arguments no_cache, output_format, organise, partial, # pylint: disable-msg=too-many-arguments quiet, recursive, rename_dir, regex, season, # pylint: disable-msg=too-many-arguments show, show_override, specials, the, paths): # pylint: disable-msg=too-many-arguments if debug: log_level = 10 start_logging(log_file, log_level, quiet) logger = functools.partial(log.log, level=26) if dry_run or debug: start_dry_run(logger) for current_dir, filename in build_file_list(paths, recursive, ignore_filelist): try: tv = TvRenamr(current_dir, debug, dry_run, no_cache) _file = File(**tv.extract_details_from_file( filename, user_regex=regex, partial=partial, )) # TODO: Warn setting season & episode will override *all* episodes _file.user_overrides(show, season, episode) _file.safety_check() config = get_config(config) for episode in _file.episodes: canonical = config.get( 'canonical', _file.show_name, default=episode.file_.show_name, override=canonical ) # TODO: Warn setting name will override *all* episodes episode.title = tv.retrieve_episode_title( episode, canonical=canonical, override=name, ) show = config.get_output(_file.show_name, override=show_override) the = config.get('the', show=_file.show_name, override=the) _file.show_name = tv.format_show_name(show, the=the) _file.set_output_format(config.get( 'format', _file.show_name, default=_file.output_format, override=output_format )) organise = config.get( 'organise', _file.show_name, default=False, override=organise ) rename_dir = config.get( 'renamed', _file.show_name, default=current_dir, override=rename_dir ) specials_folder = config.get( 'specials_folder', _file.show_name, default='Season 0', override=specials, ) path = tv.build_path( _file, rename_dir=rename_dir, organise=organise, specials_folder=specials_folder, ) tv.rename(filename, path) except errors.NoNetworkConnectionException: if dry_run or debug: stop_dry_run(logger) sys.exit(1) except (AttributeError, errors.EmptyEpisodeTitleException, errors.EpisodeNotFoundException, errors.IncorrectCustomRegularExpressionSyntaxException, errors.InvalidXMLException, errors.MissingInformationException, errors.OutputFormatMissingSyntaxException, errors.PathExistsException, errors.ShowNotFoundException, errors.UnexpectedFormatException) as e: continue except Exception as e: if debug: # In debug mode, show the full traceback. raise for msg in e.args: log.critical('Error: %s', msg) sys.exit(1) # if we're not doing a dry run add a blank line for clarity if not (debug and dry_run): log.info('') if dry_run or debug: stop_dry_run(logger)
def rename( config, canonical, debug, dry_run, episode, # pylint: disable-msg=too-many-arguments ignore_filelist, log_file, log_level, name, # pylint: disable-msg=too-many-arguments no_cache, output_format, organise, partial, # pylint: disable-msg=too-many-arguments quiet, recursive, rename_dir, regex, season, # pylint: disable-msg=too-many-arguments show, show_override, specials, symlink, the, # pylint: disable-msg=too-many-arguments paths): # pylint: disable-msg=too-many-arguments logger = functools.partial(log.log, 26) if dry_run or debug: start_dry_run(logger) if not paths: paths = [os.getcwd()] for current_dir, filename in build_file_list(paths, recursive, ignore_filelist): try: tv = TvRenamr(current_dir, debug, dry_run, symlink, no_cache) _file = File(**tv.extract_details_from_file( filename, user_regex=regex, partial=partial, )) # TODO: Warn setting season & episode will override *all* episodes _file.user_overrides(show, season, episode) _file.safety_check() conf = get_config(config) for ep in _file.episodes: canonical = conf.get('canonical', _file.show_name, default=ep.file_.show_name, override=canonical) # TODO: Warn setting name will override *all* episodes ep.title = tv.retrieve_episode_title( ep, canonical=canonical, override=name, ) # TODO: make this a sanitisation method on ep? ep.title = ep.title.replace('/', '-') show = conf.get_output(_file.show_name, override=show_override) the = conf.get('the', show=_file.show_name, override=the) _file.show_name = tv.format_show_name(show, the=the) _file.set_output_format( conf.get('format', _file.show_name, default=_file.output_format, override=output_format)) organise = conf.get('organise', _file.show_name, default=False, override=organise) rename_dir = conf.get('renamed', _file.show_name, default=current_dir, override=rename_dir) specials_folder = conf.get( 'specials_folder', _file.show_name, default='Season 0', override=specials, ) path = tv.build_path( _file, rename_dir=rename_dir, organise=organise, specials_folder=specials_folder, ) tv.rename(filename, path) except errors.NetworkException: if dry_run or debug: stop_dry_run(logger) sys.exit(1) except (AttributeError, errors.EmptyEpisodeTitleException, errors.EpisodeNotFoundException, errors.IncorrectRegExpException, errors.InvalidXMLException, errors.MissingInformationException, errors.OutputFormatMissingSyntaxException, errors.PathExistsException, errors.ShowNotFoundException, errors.UnexpectedFormatException) as e: continue except Exception as e: if debug: # In debug mode, show the full traceback. raise for msg in e.args: log.critical('Error: %s', msg) sys.exit(1) # if we're not doing a dry run add a blank line for clarity if not (debug and dry_run): log.info('') if dry_run or debug: stop_dry_run(logger)
def rename(config, copy, canonical, debug, dry_run, # pylint: disable-msg=too-many-arguments episode, ignore_filelist, log_file, # pylint: disable-msg=too-many-arguments log_level, name, no_cache, output_format, # pylint: disable-msg=too-many-arguments organise, partial, quiet, recursive, # pylint: disable-msg=too-many-arguments rename_dir, regex, season, show, # pylint: disable-msg=too-many-arguments show_override, specials, symlink, the, # pylint: disable-msg=too-many-arguments paths): # pylint: disable-msg=too-many-arguments if debug: log_level = 10 start_logging(log_file, log_level, quiet) logger = functools.partial(log.log, 26) if dry_run or debug: start_dry_run(logger) if copy and symlink: raise click.UsageError("You can't use --copy and --symlink at the same time.") if not paths: paths = [os.getcwd()] for current_dir, filename in build_file_list(paths, recursive, ignore_filelist): try: tv = TvRenamr(current_dir, debug, dry_run, no_cache) _file = File(**tv.extract_details_from_file( filename, user_regex=regex, partial=partial, )) # TODO: Warn setting season & episode will override *all* episodes _file.user_overrides(show, season, episode) _file.safety_check() conf = get_config(config) for ep in _file.episodes: canonical = conf.get( 'canonical', _file.show_name, default=ep.file_.show_name, override=canonical ) # TODO: Warn setting name will override *all* episodes ep.title = tv.retrieve_episode_title( ep, canonical=canonical, override=name, ) # TODO: make this a sanitisation method on ep? ep.title = ep.title.replace('/', '-') show = conf.get_output(_file.show_name, override=show_override) the = conf.get('the', show=_file.show_name, override=the) _file.show_name = tv.format_show_name(show, the=the) _file.set_output_format(conf.get( 'format', _file.show_name, default=_file.output_format, override=output_format )) copy = conf.get( 'copy', _file.show_name, default=False, override=copy ) organise = conf.get( 'organise', _file.show_name, default=True, override=organise ) rename_dir = conf.get( 'renamed', _file.show_name, default=current_dir, override=rename_dir ) specials_folder = conf.get( 'specials_folder', _file.show_name, default='Season 0', override=specials, ) symlink = conf.get( 'symlink', _file.show_name, default=False, override=symlink ) path = tv.build_path( _file, rename_dir=rename_dir, organise=organise, specials_folder=specials_folder, ) tv.rename(filename, path, copy, symlink) except errors.NetworkException: if dry_run or debug: stop_dry_run(logger) sys.exit(1) except (AttributeError, errors.EmptyEpisodeTitleException, errors.EpisodeNotFoundException, errors.IncorrectRegExpException, errors.InvalidXMLException, errors.MissingInformationException, errors.OutputFormatMissingSyntaxException, errors.PathExistsException, errors.ShowNotFoundException, errors.UnexpectedFormatException) as e: continue except Exception as e: if debug: # In debug mode, show the full traceback. raise for msg in e.args: log.critical('Error: %s', msg) sys.exit(1) # if we're not doing a dry run add a blank line for clarity if not (debug and dry_run): log.info('') if dry_run or debug: stop_dry_run(logger)
def test_ignoring_files(files): ignore = random_files(files) file_list = helpers.build_file_list([files], ignore_filelist=ignore) assert all(fn not in file_list for fn in ignore)