From 8eb75ed9da699bc41d91c5debecbeae21109c5eb Mon Sep 17 00:00:00 2001 From: ZaharChernenko <124883289+ZaharChernenko@users.noreply.github.com> Date: Tue, 10 Mar 2026 15:45:43 +0300 Subject: [PATCH] refactor: extract base class for project info extractor --- .../vim_coderunner_factory.py | 6 ++--- python_coderunner/src/config/__init__.py | 2 +- python_coderunner/src/config/basic.py | 4 +-- python_coderunner/src/config/config_field.py | 4 +-- python_coderunner/src/config/exceptions.py | 2 +- .../src/project_info_extractor/base.py | 27 +++++++++++++++++++ .../src/project_info_extractor/interface.py | 18 +++---------- .../vim_project_info_extractor.py | 4 +-- python_coderunner/tests/unit/conftest.py | 6 ++--- 9 files changed, 45 insertions(+), 28 deletions(-) create mode 100644 python_coderunner/src/project_info_extractor/base.py diff --git a/python_coderunner/src/coderunner_factory/vim_coderunner_factory.py b/python_coderunner/src/coderunner_factory/vim_coderunner_factory.py index 80349a8..c5e0104 100644 --- a/python_coderunner/src/coderunner_factory/vim_coderunner_factory.py +++ b/python_coderunner/src/coderunner_factory/vim_coderunner_factory.py @@ -79,19 +79,19 @@ def _create_config(self) -> TVimConfig: name="g:coderunner_by_file_ext", getter=TVimByFileExtConfigValueGetter(), validator=TDispatchersValidator(), - allowed_values_description="Dict[str, str] value", + allowed_values_description="dict[str, str]", ), by_file_type_field=TConfigField( name="g:coderunner_by_file_type", getter=TVimByFileTypeConfigValueGetter(), validator=TDispatchersValidator(), - allowed_values_description="Dict[str, str] value", + allowed_values_description="dict[str, str]", ), by_glob_field=TConfigField( name="g:coderunner_by_glob", getter=TVimByGlobConfigValueGetter(), validator=TDispatchersValidator(), - allowed_values_description="Dict[str, str] value", + allowed_values_description="dict[str, str]", ), dispatchers_order_field=TConfigField( name="g:coderunner_runners_order", diff --git a/python_coderunner/src/config/__init__.py b/python_coderunner/src/config/__init__.py index a7c16fd..f608de2 100644 --- a/python_coderunner/src/config/__init__.py +++ b/python_coderunner/src/config/__init__.py @@ -1,5 +1,5 @@ from .basic import TBasicConfig from .config_field import TConfigField -from .exceptions import ConfigFieldNotFoundError, ConfigFieldValidationError +from .exceptions import ConfigFieldUndefinedValueError, ConfigFieldValidationError from .interface import EDispatchersTypes, IConfig from .vim_config import TVimConfig diff --git a/python_coderunner/src/config/basic.py b/python_coderunner/src/config/basic.py index b76cfa4..30dd04c 100644 --- a/python_coderunner/src/config/basic.py +++ b/python_coderunner/src/config/basic.py @@ -1,7 +1,7 @@ from typing import TypeVar from .config_field import TConfigField -from .exceptions import ConfigFieldNotFoundError, ConfigFieldValidationError +from .exceptions import ConfigFieldUndefinedValueError, ConfigFieldValidationError from .interface import EDispatchersTypes, IConfig ValueType = TypeVar("ValueType") @@ -41,7 +41,7 @@ def _get_field_value(self, field: TConfigField[ValueType]) -> ValueType: """ try: return field.get() - except (ConfigFieldNotFoundError, ConfigFieldValidationError) as e: + except (ConfigFieldUndefinedValueError, ConfigFieldValidationError) as e: raise ValueError(str(e)) from e def get_by_file_ext(self) -> dict[str, str]: diff --git a/python_coderunner/src/config/config_field.py b/python_coderunner/src/config/config_field.py index 5508a6a8..2aa73d0 100644 --- a/python_coderunner/src/config/config_field.py +++ b/python_coderunner/src/config/config_field.py @@ -1,6 +1,6 @@ from typing import Any, Generic, TypeVar -from .exceptions import ConfigFieldNotFoundError, ConfigFieldValidationError +from .exceptions import ConfigFieldUndefinedValueError, ConfigFieldValidationError from .getter import IConfigValueGetter, UndefinedValueError from .validator import IValidator, ValidationError @@ -32,7 +32,7 @@ def get(self) -> ValueType: try: raw_value: Any = self._getter() except UndefinedValueError as e: - raise ConfigFieldNotFoundError.from_undefined_value_error(e, self._allowed_values_description) + raise ConfigFieldUndefinedValueError.from_undefined_value_error(e, self._allowed_values_description) try: return self._validator(raw_value) diff --git a/python_coderunner/src/config/exceptions.py b/python_coderunner/src/config/exceptions.py index fe16ac8..77b0984 100644 --- a/python_coderunner/src/config/exceptions.py +++ b/python_coderunner/src/config/exceptions.py @@ -4,7 +4,7 @@ from .validator import ValidationError -class ConfigFieldNotFoundError(UndefinedValueError): +class ConfigFieldUndefinedValueError(UndefinedValueError): @classmethod def from_undefined_value_error(cls, e: UndefinedValueError, allowed_values: str) -> Self: return cls(f"{e} Allowed values: {allowed_values}.") diff --git a/python_coderunner/src/project_info_extractor/base.py b/python_coderunner/src/project_info_extractor/base.py new file mode 100644 index 0000000..b0d3194 --- /dev/null +++ b/python_coderunner/src/project_info_extractor/base.py @@ -0,0 +1,27 @@ +import os +from abc import abstractmethod +from typing import Iterable + +from ..file_info_extractor import IFileInfoExtractor +from .interface import IProjectInfoExtractor + + +class TBaseProjectInfoExtractor(IProjectInfoExtractor): + def __init__(self, file_info_extractor: IFileInfoExtractor): + self._file_info_extractor: IFileInfoExtractor = file_info_extractor + + @abstractmethod + def get_workspace_root(self) -> str: + raise NotImplementedError + + def get_all_files_filter_by_exts(self, exts: set[str]) -> Iterable[str]: + for root, _, files in os.walk(self.get_workspace_root()): + for file in files: + if self._file_info_extractor.get_file_ext(file) in exts: + yield os.path.join(root, file) + + def get_all_files_filter_by_file_type(self, file_types: set[str]) -> Iterable[str]: + for root, _, files in os.walk(self.get_workspace_root()): + for file in files: + if self._file_info_extractor.get_file_type(file) in file_types: + yield os.path.join(root, file) diff --git a/python_coderunner/src/project_info_extractor/interface.py b/python_coderunner/src/project_info_extractor/interface.py index ea06b67..1c20060 100644 --- a/python_coderunner/src/project_info_extractor/interface.py +++ b/python_coderunner/src/project_info_extractor/interface.py @@ -1,26 +1,16 @@ -import os from abc import ABC, abstractmethod from typing import Iterable -from ..file_info_extractor import IFileInfoExtractor - class IProjectInfoExtractor(ABC): - def __init__(self, file_info_extractor: IFileInfoExtractor): - self._file_info_extractor: IFileInfoExtractor = file_info_extractor - @abstractmethod def get_workspace_root(self) -> str: raise NotImplementedError + @abstractmethod def get_all_files_filter_by_exts(self, exts: set[str]) -> Iterable[str]: - for root, _, files in os.walk(self.get_workspace_root()): - for file in files: - if self._file_info_extractor.get_file_ext(file) in exts: - yield os.path.join(root, file) + raise NotImplementedError + @abstractmethod def get_all_files_filter_by_file_type(self, file_types: set[str]) -> Iterable[str]: - for root, _, files in os.walk(self.get_workspace_root()): - for file in files: - if self._file_info_extractor.get_file_type(file) in file_types: - yield os.path.join(root, file) + raise NotImplementedError diff --git a/python_coderunner/src/project_info_extractor/vim_project_info_extractor.py b/python_coderunner/src/project_info_extractor/vim_project_info_extractor.py index 02b099d..5c69058 100644 --- a/python_coderunner/src/project_info_extractor/vim_project_info_extractor.py +++ b/python_coderunner/src/project_info_extractor/vim_project_info_extractor.py @@ -1,8 +1,8 @@ import vim -from .interface import IProjectInfoExtractor +from .base import TBaseProjectInfoExtractor -class TVimProjectInfoExtractor(IProjectInfoExtractor): +class TVimProjectInfoExtractor(TBaseProjectInfoExtractor): def get_workspace_root(self) -> str: return vim.eval("getcwd()") diff --git a/python_coderunner/tests/unit/conftest.py b/python_coderunner/tests/unit/conftest.py index 93cc2b0..be497d9 100644 --- a/python_coderunner/tests/unit/conftest.py +++ b/python_coderunner/tests/unit/conftest.py @@ -61,19 +61,19 @@ def fixture_vim_config() -> IConfig: name="g:coderunner_by_file_ext", getter=TVimByFileExtConfigValueGetter(), validator=TDispatchersValidator(), - allowed_values_description="Dict[str, str] value", + allowed_values_description="dict[str, str]", ), by_file_type_field=TConfigField( name="g:coderunner_by_file_type", getter=TVimByFileTypeConfigValueGetter(), validator=TDispatchersValidator(), - allowed_values_description="Dict[str, str] value", + allowed_values_description="dict[str, str]", ), by_glob_field=TConfigField( name="g:coderunner_by_glob", getter=TVimByGlobConfigValueGetter(), validator=TDispatchersValidator(), - allowed_values_description="Dict[str, str] value", + allowed_values_description="dict[str, str]", ), dispatchers_order_field=TConfigField( name="g:coderunner_runners_order",