From cb41d4749ad8cf3e9036919c8ea6a26c03eff0e5 Mon Sep 17 00:00:00 2001 From: Serene-Arc Date: Tue, 27 Apr 2021 12:29:37 +1000 Subject: [PATCH] Add option to specify logfile location --- bdfr/__main__.py | 1 + bdfr/configuration.py | 1 + bdfr/downloader.py | 7 +++++- tests/test_integration.py | 51 +++++++++++++++++++++++++-------------- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/bdfr/__main__.py b/bdfr/__main__.py index 26759a1..4d78149 100644 --- a/bdfr/__main__.py +++ b/bdfr/__main__.py @@ -20,6 +20,7 @@ _common_options = [ click.option('-m', '--multireddit', multiple=True, default=None, type=str), click.option('-L', '--limit', default=None, type=int), click.option('--authenticate', is_flag=True, default=None), + click.option('--log', type=str, default=None), click.option('--submitted', is_flag=True, default=None), click.option('--upvoted', is_flag=True, default=None), click.option('--saved', is_flag=True, default=None), diff --git a/bdfr/configuration.py b/bdfr/configuration.py index 1d9610c..c5c7142 100644 --- a/bdfr/configuration.py +++ b/bdfr/configuration.py @@ -17,6 +17,7 @@ class Configuration(Namespace): self.exclude_id_file = [] self.limit: Optional[int] = None self.link: list[str] = [] + self.log: Optional[str] = None self.max_wait_time = None self.multireddit: list[str] = [] self.no_dupes: bool = False diff --git a/bdfr/downloader.py b/bdfr/downloader.py index fce0631..48c4234 100644 --- a/bdfr/downloader.py +++ b/bdfr/downloader.py @@ -190,7 +190,12 @@ class RedditDownloader: def _create_file_logger(self): main_logger = logging.getLogger() - log_path = Path(self.config_directory, 'log_output.txt') + if self.args.log is None: + log_path = Path(self.config_directory, 'log_output.txt') + else: + log_path = Path(self.args.log).resolve().expanduser() + if not log_path.parent.exists(): + raise errors.BulkDownloaderException(f'Designated location for logfile does not exist') backup_count = self.cfg_parser.getint('DEFAULT', 'backup_log_count', fallback=3) file_handler = logging.handlers.RotatingFileHandler( log_path, diff --git a/tests/test_integration.py b/tests/test_integration.py index a384af6..327acc4 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -12,6 +12,21 @@ from bdfr.__main__ import cli does_test_config_exist = Path('test_config.cfg').exists() +def create_basic_args_for_download_runner(test_args: list[str], tmp_path: Path): + out = [ + 'download', str(tmp_path), + '-v', + '--config', 'test_config.cfg', + '--log', str(Path(tmp_path, 'test_log.txt')), + ] + test_args + return out + + +def create_basic_args_for_archive_runner(test_args: list[str], tmp_path: Path): + out = ['archive', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + return out + + @pytest.mark.online @pytest.mark.reddit @pytest.mark.skipif(not does_test_config_exist, reason='A test config file is required for integration tests') @@ -36,7 +51,7 @@ does_test_config_exist = Path('test_config.cfg').exists() )) def test_cli_download_subreddits(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 assert 'Added submissions from subreddit ' in result.output @@ -54,7 +69,7 @@ def test_cli_download_subreddits(test_args: list[str], tmp_path: Path): )) def test_cli_download_links(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 @@ -70,7 +85,7 @@ def test_cli_download_links(test_args: list[str], tmp_path: Path): )) def test_cli_download_multireddit(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 assert 'Added submissions from multireddit ' in result.output @@ -84,7 +99,7 @@ def test_cli_download_multireddit(test_args: list[str], tmp_path: Path): )) def test_cli_download_multireddit_nonexistent(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 assert 'Failed to get submissions for multireddit' in result.output @@ -105,7 +120,7 @@ def test_cli_download_multireddit_nonexistent(test_args: list[str], tmp_path: Pa )) def test_cli_download_user_data_good(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 assert 'Downloaded submission ' in result.output @@ -120,7 +135,7 @@ def test_cli_download_user_data_good(test_args: list[str], tmp_path: Path): )) def test_cli_download_user_data_bad_me_unauthenticated(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 assert 'To use "me" as a user, an authenticated Reddit instance must be used' in result.output @@ -135,7 +150,7 @@ def test_cli_download_user_data_bad_me_unauthenticated(test_args: list[str], tmp def test_cli_download_search_existing(test_args: list[str], tmp_path: Path): Path(tmp_path, 'test.txt').touch() runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 assert 'Calculating hashes for' in result.output @@ -149,7 +164,7 @@ def test_cli_download_search_existing(test_args: list[str], tmp_path: Path): )) def test_cli_download_download_filters(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 assert 'Download filter removed submission' in result.output @@ -164,7 +179,7 @@ def test_cli_download_download_filters(test_args: list[str], tmp_path: Path): )) def test_cli_download_long(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 @@ -178,7 +193,7 @@ def test_cli_download_long(test_args: list[str], tmp_path: Path): )) def test_cli_archive_single(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['archive', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_archive_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 assert re.search(r'Writing entry .*? to file in .*? format', result.output) @@ -197,7 +212,7 @@ def test_cli_archive_single(test_args: list[str], tmp_path: Path): )) def test_cli_archive_subreddit(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['archive', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_archive_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 assert re.search(r'Writing entry .*? to file in .*? format', result.output) @@ -211,7 +226,7 @@ def test_cli_archive_subreddit(test_args: list[str], tmp_path: Path): )) def test_cli_archive_all_user_comments(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['archive', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_archive_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 @@ -226,7 +241,7 @@ def test_cli_archive_all_user_comments(test_args: list[str], tmp_path: Path): )) def test_cli_archive_long(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['archive', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_archive_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 assert re.search(r'Writing entry .*? to file in .*? format', result.output) @@ -243,7 +258,7 @@ def test_cli_archive_long(test_args: list[str], tmp_path: Path): )) def test_cli_download_soft_fail(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 @@ -258,7 +273,7 @@ def test_cli_download_soft_fail(test_args: list[str], tmp_path: Path): )) def test_cli_download_hard_fail(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code != 0 @@ -278,7 +293,7 @@ def test_cli_download_use_default_config(tmp_path: Path): )) def test_cli_download_links_exclusion(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 assert 'in exclusion list' in result.output @@ -294,7 +309,7 @@ def test_cli_download_links_exclusion(test_args: list[str], tmp_path: Path): )) def test_cli_download_subreddit_exclusion(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 assert 'in skip list' in result.output @@ -310,7 +325,7 @@ def test_cli_download_subreddit_exclusion(test_args: list[str], tmp_path: Path): )) def test_cli_file_scheme_warning(test_args: list[str], tmp_path: Path): runner = CliRunner() - test_args = ['download', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args + test_args = create_basic_args_for_download_runner(test_args, tmp_path) result = runner.invoke(cli, test_args) assert result.exit_code == 0 assert 'Some files might not be downloaded due to name conflicts' in result.output