def test_passing_in_a_show_name_renames_a_file_using_that_name(self): show_name = 'Doctor Who (2005)' _file = File('doctor who', '5', ['10'], 'mp4') _file.show_name = show_name _file.episodes[0].title = '' path = self.tv.build_path(_file, rename_dir=self.files, organise=False) assert os.path.split(path.split('-')[0].strip())[1] == show_name
def test_passing_in_a_show_name_renames_a_file_using_that_name(self): show_name = 'Doctor Who (2005)' _file = File('doctor who', '5', ['10'], 'mp4') _file.show_name = show_name _file.episodes[0].title = '' path = self.tv.build_path(_file, organise=False) assert_equal(os.path.split(path.split('-')[0].strip())[1], show_name)
def test_passing_in_a_show_name_renames_a_file_using_that_name(files, tv): show_name = "Doctor Who (2005)" _file = File("doctor who", "5", ["10"], "mp4") _file.show_name = show_name _file.episodes[0].title = "" path = tv.build_path(_file, rename_dir=files, organise=False) assert os.path.split(path.split("-")[0].strip())[1] == show_name
def test_passing_in_a_season_number_to_retrieve_episode_name_returns_the_correct_episode_name_from_that_season( self): _file = File('chuck', '1', ['08'], 'mp4') _file.season = '2' _file.episodes[0].title = '' title = self.tv.retrieve_episode_title(_file.episodes[0]) assert title == 'Chuck Versus the Gravitron'
def test_passing_in_a_season_number_to_retrieve_episode_name_returns_the_correct_episode_name_from_that_season( tv ): # noqa _file = File("chuck", "1", ["08"], "mp4") _file.season = "2" _file.episodes[0].title = "" title = tv.retrieve_episode_title(_file.episodes[0]) assert title == "Chuck Versus the Gravitron"
def setup(self): # absolute path to the file is pretty useful self.path = os.path.abspath(os.path.dirname(__file__)) def build_path(path): if not os.path.exists(path): os.mkdir(path) return path def join_path(path): return os.path.join(self.path, path) self.files = build_path(join_path('files')) self.subfolder = build_path(join_path('subfolder')) self.organised = build_path(join_path('organised')) self.renamed = join_path('renamed') for path in (self.files, self.subfolder): self.build_files(path) # instantiate tvr self.config = Config() self.config.config['defaults']['renamed'] = self.files self.tv = TvRenamr(self.files, self.config, cache=False) self._file = File('The Big Bang Theory', '3', ['01'], '.mp4') self._file.episodes[0].title = 'The Electric Can Opener Fluctuation'
def test_searching_with_an_ambiguous_name_returns_the_correct_show(self): _file = File('The O.C.', '3', ['04'], 'mp4') for library in self.libs: self.tv.retrieve_episode_title(_file.episodes[0], library=library) method = partial(self.tv.format_show_name, _file.show_name) assert method(the=False) == 'The O.C.' assert method(the=True) == 'O.C., The'
def test_searching_with_an_ambiguous_name_returns_the_correct_show(tv): _file = File('The O.C.', '3', ['04'], 'mp4') with mock.patch('requests.get', side_effect=mock_get): tv.retrieve_episode_title(_file.episodes[0]) method = partial(tv.format_show_name, _file.show_name) assert method(the=False) == 'The O.C.' assert method(the=True) == 'O.C., The'
def test_the_return_of_a_formatted_show_name(tv): _file = File('The Big Bang Theory', '3', ['01'], '.mp4') with mock.patch('requests.get', side_effect=mock_get): tv.retrieve_episode_title(_file.episodes[0]) method = partial(tv.format_show_name, _file.show_name) assert method(the=False) == 'The Big Bang Theory' assert method(the=True) == 'Big Bang Theory, The'
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 test_missing_episode_raises_missing_information_exception(self): with raises(MissingInformationException): File( show_name='foo', season=1, ).safety_check()
def test_missing_show_name_raises_missing_information_exception(self): with raises(MissingInformationException): File(season=1, episodes=[1]).safety_check()
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 test_searching_for_a_specific_episode_returns_the_correct_episode(tv): _file = File('The Big Bang Theory', '3', ['01'], '.mp4') with mock.patch('requests.get', side_effect=mock_get): title = tv.retrieve_episode_title(episode=_file.episodes[0]) assert title == 'The Electric Can Opener Fluctuation'
class TestWithBigBang: def setup(self): self._file = File("The Big Bang Theory", "3", ["01"], ".mp4") self._file.episodes[0].title = "The Electric Can Opener Fluctuation" def test_passing_in_a_season_number_renames_a_file_using_that_season_number(self, files, tv): self._file.season = "2" path = tv.build_path(self._file, rename_dir=files, organise=False) season_number = path.split("-")[1].strip()[0] assert season_number == "2" def test_passing_in_an_episode_number_returns_the_correct_episode_title(self, tv): self._file.episodes[0].number = "3" title = tv.retrieve_episode_title(self._file.episodes[0]) assert title == "The Gothowitz Deviation" def test_passing_in_an_episode_number_renames_a_file_using_that_episode_number(self, files, tv): self._file.episodes[0].number = "3" path = tv.build_path(self._file, rename_dir=files, organise=False) assert path.split("-")[1].strip()[-2:] == "03" def test_setting_the_position_of_a_shows_leading_the_to_the_end_of_the_file_name(self, files, tv): with mock.patch("requests.get", mock_get): tv.retrieve_episode_title(self._file.episodes[0]) self._file.show_name = tv.format_show_name(self._file.show_name, the=True) assert self._file.show_name == "Big Bang Theory, The" def test_setting_an_episodes_format_as_name_season_episode_title(self, files, tv): self._file.output_format = "%n - %s%e - %t%x" path = tv.build_path(self._file, rename_dir=files, organise=False) filename = "The Big Bang Theory - 301 - The Electric Can Opener Fluctuation.mp4" assert os.path.split(path)[1] == filename def test_setting_an_episodes_format_as_season_episode_title_name(self, files, tv): self._file.set_output_format("%s - %e - %t - %n%x") path = tv.build_path(self._file, rename_dir=files, organise=False) filename = "3 - 01 - The Electric Can Opener Fluctuation - The Big Bang Theory.mp4" assert os.path.split(path)[1] == filename def test_setting_an_episodes_format_as_title_episode_season_name(self, files, tv): self._file.output_format = "%t - %e - %s - %n%x" path = tv.build_path(self._file, rename_dir=files, organise=False) filename = "The Electric Can Opener Fluctuation - 01 - 3 - The Big Bang Theory.mp4" assert os.path.split(path)[1] == filename def test_setting_season_number_digit_length(self, files, tv): self._file.output_format = "%n S%s{2}E%e %t%x" path = tv.build_path(self._file, rename_dir=files, organise=False) filename = "The Big Bang Theory S03E01 The Electric Can Opener Fluctuation.mp4" assert os.path.split(path)[1] == filename def test_setting_episode_number_digit_length(self, files, tv): self._file.output_format = "%n S%sE%e{2} %t%x" path = tv.build_path(self._file, rename_dir=files, organise=False) filename = "The Big Bang Theory S3E01 The Electric Can Opener Fluctuation.mp4" assert os.path.split(path)[1] == filename def test_setting_season_and_episode_number_digit_length(self, files, tv): self._file.output_format = "%n S%s{3}E%e{4} %t%x" path = tv.build_path(self._file, rename_dir=files, organise=False) filename = "The Big Bang Theory S003E0001 The Electric Can Opener Fluctuation.mp4" assert os.path.split(path)[1] == filename def test_no_organise_in_config(self, files, tv): filename = "The Big Bang Theory - 301 - The Electric Can Opener Fluctuation.mp4" path = tv.build_path(self._file, rename_dir=files) expected = os.path.join(files, filename) assert path == expected
def test_searching_for_an_incorrect_name_returns_an_exception(tv): _file = File('west, wing', '4', ['01'], '.mp4') with mock.patch('requests.get', side_effect=mock_get), raises(ShowNotFoundException): tv.retrieve_episode_title(_file.episodes[0])
def test_searching_for_an_incorrect_name_returns_an_exception(self): _file = File('west, wing', '4', ['01'], '.mp4') for library in self.libs: with raises(NoMoreLibrariesException): self.tv.retrieve_episode_title(_file.episodes[0], library)
class TestWithBigBang: def setup(self): self._file = File('The Big Bang Theory', '3', ['01'], '.mp4') self._file.episodes[0].title = 'The Electric Can Opener Fluctuation' def test_passing_in_a_season_number_renames_a_file_using_that_season_number(self, files, tv): self._file.season = '2' path = tv.build_path(self._file, rename_dir=files, organise=False) season_number = path.split('-')[1].strip()[0] assert season_number == '2' def test_passing_in_an_episode_number_returns_the_correct_episode_title(self, tv): self._file.episodes[0].number = '3' title = tv.retrieve_episode_title(self._file.episodes[0]) assert title == 'The Gothowitz Deviation' def test_passing_in_an_episode_number_renames_a_file_using_that_episode_number(self, files, tv): self._file.episodes[0].number = '3' path = tv.build_path(self._file, rename_dir=files, organise=False) assert path.split('-')[1].strip()[-2:] == '03' def test_setting_the_position_of_a_shows_leading_the_to_the_end_of_the_file_name(self, files, tv): with mock.patch('requests.get', mock_get): tv.retrieve_episode_title(self._file.episodes[0]) self._file.show_name = tv.format_show_name(self._file.show_name, the=True) assert self._file.show_name == 'Big Bang Theory, The' def test_setting_an_episodes_format_as_name_season_episode_title(self, files, tv): self._file.output_format = '%n - %s%e - %t%x' path = tv.build_path(self._file, rename_dir=files, organise=False) filename = 'The Big Bang Theory - 301 - The Electric Can Opener Fluctuation.mp4' assert os.path.split(path)[1] == filename def test_setting_an_episodes_format_as_season_episode_title_name(self, files, tv): self._file.set_output_format('%s - %e - %t - %n%x') path = tv.build_path(self._file, rename_dir=files, organise=False) filename = '3 - 01 - The Electric Can Opener Fluctuation - The Big Bang Theory.mp4' assert os.path.split(path)[1] == filename def test_setting_an_episodes_format_as_title_episode_season_name(self, files, tv): self._file.output_format = '%t - %e - %s - %n%x' path = tv.build_path(self._file, rename_dir=files, organise=False) filename = 'The Electric Can Opener Fluctuation - 01 - 3 - The Big Bang Theory.mp4' assert os.path.split(path)[1] == filename def test_setting_season_number_digit_length(self, files, tv): self._file.output_format = '%n S%s{2}E%e %t%x' path = tv.build_path(self._file, rename_dir=files, organise=False) filename = 'The Big Bang Theory S03E01 The Electric Can Opener Fluctuation.mp4' assert os.path.split(path)[1] == filename def test_setting_episode_number_digit_length(self, files, tv): self._file.output_format = '%n S%sE%e{2} %t%x' path = tv.build_path(self._file, rename_dir=files, organise=False) filename = 'The Big Bang Theory S3E01 The Electric Can Opener Fluctuation.mp4' assert os.path.split(path)[1] == filename def test_setting_season_and_episode_number_digit_length(self, files, tv): self._file.output_format = '%n S%s{3}E%e{4} %t%x' path = tv.build_path(self._file, rename_dir=files, organise=False) filename = 'The Big Bang Theory S003E0001 The Electric Can Opener Fluctuation.mp4' assert os.path.split(path)[1] == filename def test_no_organise_in_config(self, files, tv): filename = 'The Big Bang Theory - 301 - The Electric Can Opener Fluctuation.mp4' path = tv.build_path(self._file, rename_dir=files) expected = os.path.join(files, filename) assert path == expected
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 setup(self): self._file = File("The Big Bang Theory", "3", ["01"], ".mp4") self._file.episodes[0].title = "The Electric Can Opener Fluctuation"
def test_searching_for_an_episode_that_does_not_exist_returns_an_exception( self): _file = File('chuck', '1', ['99'], '.mp4') for library in self.libs: with raises(NoMoreLibrariesException): self.tv.retrieve_episode_title(_file.episodes[0], library)
def test_searching_for_an_episode_that_does_not_exist_returns_an_exception(tv): _file = File('chuck', '1', ['99'], '.mp4') with mock.patch('requests.get', side_effect=mock_get), raises(EpisodeNotFoundException): tv.retrieve_episode_title(_file.episodes[0])
def test_passing_in_a_season_number_to_retrieve_episode_name_returns_the_correct_episode_name_from_that_season(self): _file = File('chuck', '1', ['08'], 'mp4') _file.season = '2' _file.episodes[0].title = '' title = self.tv.retrieve_episode_title(_file.episodes[0]) assert_equal(title, 'Chuck Versus the Gravitron')
def setup(self): self._file = File('The Big Bang Theory', '3', ['01'], '.mp4') self._file.episodes[0].title = 'The Electric Can Opener Fluctuation'