From cdc5973cdea27e2a3a60fd7a9a07f2de5b67da6d Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sat, 13 Feb 2021 16:28:17 -0600 Subject: [PATCH] Option to treat negative scores as ignored The `--strict-negative-scores` option will take any negative preferred term scores and move those terms to the "Must Not Contains" (ignored) field of the release profile. --- README.md | 2 +- src/app/cmd.py | 2 ++ src/app/guide/anime.py | 7 +++++-- src/tests/guide/test_anime.py | 33 ++++++++++++++++++++++++++++++++- src/trash.py | 2 +- 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e1e71507..84b38338 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Features list will continue to grow. See the limitations & roadmap section for m * Profiles get created if they do not exist, or updated if they already exist. Profiles get a unique name based on the guide and this name is used to find them in subsequent runs. * Tags can be added to any updated or created profiles. + * Ability to convert preferred with negative scores to "Must not contain" terms. * Sonarr Quality Definitions * Anime and Non-Anime quality definitions are now synced to Sonarr @@ -135,7 +136,6 @@ the following limitations: In addition to the above limitations, the following items are planned for the future. * Better and more polished error handling (it's pretty minimal right now) -* Add a way to convert preferred with negative scores to "Must not contain" terms. * Implement some sort of guide versioning (e.g. to avoid updating a release profile if the guide did not change). * Unit Testing diff --git a/src/app/cmd.py b/src/app/cmd.py index 78831b28..21f1b3fa 100644 --- a/src/app/cmd.py +++ b/src/app/cmd.py @@ -39,6 +39,8 @@ def setup_and_parse_args(args_override=None): }) profile_p.add_argument('--tags', help='Tags to assign to the profiles that are created or updated. These tags will replace any existing tags when updating profiles.', nargs='+') + profile_p.add_argument('--strict-negative-scores', help='Any negative scores get added to the list of "Must Not Contain" items', + action='store_true') # Subcommands for 'quality' quality_p = subparsers.add_parser('quality', help='Pages in the guide that provide quality definitions', diff --git a/src/app/guide/anime.py b/src/app/guide/anime.py index cafdeaa3..81bd5243 100644 --- a/src/app/guide/anime.py +++ b/src/app/guide/anime.py @@ -32,7 +32,7 @@ def parse_category(line): return None # -------------------------------------------------------------------------------------------------- -def parse_markdown(logger, markdown_content): +def parse_markdown(args, logger, markdown_content): results = defaultdict(ProfileData) profile_name = None score = None @@ -72,7 +72,10 @@ def parse_markdown(logger, markdown_content): elif bracket_depth: if score is not None: logger.debug(f' [Preferred] Score: {score}, Term: {line}') - profile.preferred[score].append(line) + if args.strict_negative_scores and score < 0: + profile.ignored.append(line) + else: + profile.preferred[score].append(line) elif category == TermCategory.Ignored and bracket_depth: # Sometimes a comma is present at the end of these regexes, because when it's # pasted into Sonarr it acts as a delimiter. However, when using them with the diff --git a/src/tests/guide/test_anime.py b/src/tests/guide/test_anime.py index ab15255b..0f02a008 100644 --- a/src/tests/guide/test_anime.py +++ b/src/tests/guide/test_anime.py @@ -9,7 +9,10 @@ def test_parse_markdown_complete_doc(): with open(md_file) as file: test_markdown = file.read() - results = anime.parse_markdown(MockLogger(), test_markdown) + class args: + strict_negative_scores = False + + results = anime.parse_markdown(args, MockLogger(), test_markdown) assert len(results) == 1 profile = next(iter(results.values())) @@ -22,3 +25,31 @@ def test_parse_markdown_complete_doc(): assert len(profile.preferred) == 1 assert profile.preferred.get(100) == ['term1'] + + +def test_parse_markdown_strict_negative_scores(): + test_markdown = ''' +# Test Release Profile + +This score is negative [-1] + +``` +abc +``` + +This score is positive [0] + +``` +xyz +``` +''' + + class args: + strict_negative_scores = True + + results = anime.parse_markdown(args, MockLogger(), test_markdown) + assert len(results['Test Release Profile'].required) == 0 + assert len(results['Test Release Profile'].ignored) == 1 + assert results['Test Release Profile'].ignored[0] == 'abc' + assert len(results['Test Release Profile'].preferred) == 1 + assert results['Test Release Profile'].preferred[0] == ['xyz'] diff --git a/src/trash.py b/src/trash.py index 213d53d4..065544a8 100644 --- a/src/trash.py +++ b/src/trash.py @@ -10,7 +10,7 @@ from app.cmd import setup_and_parse_args from app.logger import Logger def process_sonarr_profile(args, logger): - profiles = anime.parse_markdown(logger, anime.get_trash_anime_markdown()) + profiles = anime.parse_markdown(args, logger, anime.get_trash_anime_markdown()) # A few false-positive profiles are added sometimes. We filter these out by checking if they # actually have meaningful data attached to them, such as preferred terms. If they are mostly empty,