Skip to content

Commit

Permalink
fix(advanced-config): fix removal of custom rebulk rules (#692)
Browse files Browse the repository at this point in the history
Close #692
  • Loading branch information
Toilal committed Apr 4, 2021
1 parent 777d2b5 commit c2bc1ea
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
30 changes: 24 additions & 6 deletions guessit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
API functions that can be used by external software
"""

from collections import OrderedDict

from pathlib import Path
import os
import traceback
from collections import OrderedDict
from copy import deepcopy
from pathlib import Path

from rebulk.introspector import introspect

Expand Down Expand Up @@ -40,7 +40,7 @@ def __init__(self, string, options):
self.options = options


def configure(options=None, rules_builder=rebulk_builder, force=False):
def configure(options=None, rules_builder=None, force=False):
"""
Load configuration files and initialize rebulk rules if required.
Expand All @@ -55,6 +55,13 @@ def configure(options=None, rules_builder=rebulk_builder, force=False):
default_api.configure(options, rules_builder=rules_builder, force=force)


def reset():
"""
Reset api internal state.
"""
default_api.reset()


def guessit(string, options=None):
"""
Retrieves all matches from string as a dict
Expand Down Expand Up @@ -104,6 +111,12 @@ def __init__(self):
self.load_config_options = None
self.advanced_config = None

def reset(self):
"""
Reset api internal state.
"""
self.__init__()

@classmethod
def _fix_encoding(cls, value):
if isinstance(value, list):
Expand All @@ -121,7 +134,7 @@ def _has_same_properties(cls, dic1, dic2, values):
return False
return True

def configure(self, options=None, rules_builder=rebulk_builder, force=False, sanitize_options=True):
def configure(self, options=None, rules_builder=None, force=False, sanitize_options=True):
"""
Load configuration files and initialize rebulk rules if required.
Expand All @@ -131,9 +144,14 @@ def configure(self, options=None, rules_builder=rebulk_builder, force=False, san
:type rules_builder:
:param force:
:type force: bool
:param sanitize_options:
:type force: bool
:return:
:rtype: dict
"""
if not rules_builder:
rules_builder = rebulk_builder

if sanitize_options:
options = parse_options(options, True)
options = self._fix_encoding(options)
Expand All @@ -154,7 +172,7 @@ def configure(self, options=None, rules_builder=rebulk_builder, force=False, san
self.advanced_config != advanced_config

if should_build_rebulk:
self.advanced_config = advanced_config
self.advanced_config = deepcopy(advanced_config)
self.rebulk = rules_builder(advanced_config)

self.config = config
Expand Down
50 changes: 49 additions & 1 deletion guessit/test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
from pathlib import Path

import pytest
from pytest_mock import MockerFixture

from ..api import guessit, properties, suggested_expected, GuessitException
from .. import api
from ..api import guessit, properties, suggested_expected, GuessitException, default_api

__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))

Expand Down Expand Up @@ -66,3 +68,49 @@ def test_suggested_expected():
content = json.load(f)
actual = suggested_expected(content['titles'])
assert actual == content['suggested']


def test_should_rebuild_rebulk_on_advanced_config_change(mocker: MockerFixture):
api.reset()
rebulk_builder_spy = mocker.spy(api, 'rebulk_builder')

string = "some.movie.trfr.mkv"

result1 = default_api.guessit(string)

assert result1.get('title') == 'some movie trfr'
assert 'subtitle_language' not in result1

rebulk_builder_spy.assert_called_once_with(mocker.ANY)
rebulk_builder_spy.reset_mock()

result2 = default_api.guessit(string, {'advanced_config': {'language': {'subtitle_prefixes': ['tr']}}})

assert result2.get('title') == 'some movie'
assert str(result2.get('subtitle_language')) == 'fr'

rebulk_builder_spy.assert_called_once_with(mocker.ANY)
rebulk_builder_spy.reset_mock()


def test_should_not_rebuild_rebulk_on_same_advanced_config(mocker: MockerFixture):
api.reset()
rebulk_builder_spy = mocker.spy(api, 'rebulk_builder')

string = "some.movie.subfr.mkv"

result1 = default_api.guessit(string)

assert result1.get('title') == 'some movie'
assert str(result1.get('subtitle_language')) == 'fr'

rebulk_builder_spy.assert_called_once_with(mocker.ANY)
rebulk_builder_spy.reset_mock()

result2 = default_api.guessit(string)

assert result2.get('title') == 'some movie'
assert str(result2.get('subtitle_language')) == 'fr'

assert rebulk_builder_spy.call_count == 0
rebulk_builder_spy.reset_mock()
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from setuptools import setup, find_packages

import io
import os
import re

from setuptools import setup, find_packages

here = os.path.abspath(os.path.dirname(__file__))

with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
Expand All @@ -21,7 +21,7 @@

dev_require = ['tox', 'mkdocs', 'mkdocs-material', 'pyinstaller', 'python-semantic-release']

tests_require = ['pytest', 'pytest-benchmark', 'pytest-cov', 'pylint', 'PyYAML']
tests_require = ['pytest', 'pytest-mock', 'pytest-benchmark', 'pytest-cov', 'pylint', 'PyYAML']

package_data = ['config/*', 'data/*']

Expand Down

0 comments on commit c2bc1ea

Please sign in to comment.