From 6eeadc88214bf3b5aff8c893ac6a338b38d26187 Mon Sep 17 00:00:00 2001 From: Serene-Arc Date: Fri, 11 Jun 2021 15:31:11 +1000 Subject: [PATCH] Add option for archiver full context --- bdfr/__main__.py | 1 + bdfr/archiver.py | 3 +++ bdfr/configuration.py | 3 ++- tests/test_integration.py | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/bdfr/__main__.py b/bdfr/__main__.py index 1103581..6312c76 100644 --- a/bdfr/__main__.py +++ b/bdfr/__main__.py @@ -50,6 +50,7 @@ _downloader_options = [ _archiver_options = [ click.option('--all-comments', is_flag=True, default=None), + click.option('--full-context', is_flag=True, default=None), click.option('-f', '--format', type=click.Choice(('xml', 'json', 'yaml')), default=None), ] diff --git a/bdfr/archiver.py b/bdfr/archiver.py index b19a042..f2870cc 100644 --- a/bdfr/archiver.py +++ b/bdfr/archiver.py @@ -61,6 +61,9 @@ class Archiver(RedditConnector): raise ArchiverError(f'Factory failed to classify item of type {type(praw_item).__name__}') def write_entry(self, praw_item: (praw.models.Submission, praw.models.Comment)): + if self.args.full_context and isinstance(praw_item, praw.models.Comment): + logger.debug(f'Converting comment {praw_item.id} to submission {praw_item.submission.id}') + praw_item = praw_item.submission archive_entry = self._pull_lever_entry_factory(praw_item) if self.args.format == 'json': self._write_entry_json(archive_entry) diff --git a/bdfr/configuration.py b/bdfr/configuration.py index 327a453..558b79f 100644 --- a/bdfr/configuration.py +++ b/bdfr/configuration.py @@ -41,8 +41,9 @@ class Configuration(Namespace): self.verbose: int = 0 # Archiver-specific options - self.format = 'json' self.all_comments = False + self.format = 'json' + self.full_context: bool = False def process_click_arguments(self, context: click.Context): for arg_key in context.params.keys(): diff --git a/tests/test_integration.py b/tests/test_integration.py index 6a9e52b..0a6de3d 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -252,6 +252,20 @@ def test_cli_archive_all_user_comments(test_args: list[str], tmp_path: Path): assert result.exit_code == 0 +@pytest.mark.online +@pytest.mark.reddit +@pytest.mark.skipif(not does_test_config_exist, reason='A test config file is required for integration tests') +@pytest.mark.parametrize('test_args', ( + ['--full-context', '--link', 'gxqapql'], +)) +def test_cli_archive_full_context(test_args: list[str], tmp_path: Path): + runner = CliRunner() + test_args = create_basic_args_for_archive_runner(test_args, tmp_path) + result = runner.invoke(cli, test_args) + assert result.exit_code == 0 + assert 'Converting comment' in result.output + + @pytest.mark.online @pytest.mark.reddit @pytest.mark.slow