Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python_coderunner/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "vim-code-runner"
authors = [
{name = "Zahar Chernenko", email = "zaharchernenko35@gmail.com"},
]
version = "1.0.1"
version = "1.0.2"
requires-python = ">=3.10"

[dependency-groups]
Expand Down
4 changes: 2 additions & 2 deletions python_coderunner/src/coderunner/coderunner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, Optional
from typing import Callable

from ..command_builder import ICommandBuilder
from ..command_dispatcher_strategy_selector import (
Expand Down Expand Up @@ -40,7 +40,7 @@ def run_by_file_ext(self) -> None:
def run_by_file_type(self) -> None:
self._run(self._command_dispatcher_strategy_selector.dispatch_by_file_type)

def _run(self, strategy: Callable[[str], Optional[ICommandBuilder]]) -> None:
def _run(self, strategy: Callable[[str], ICommandBuilder | None]) -> None:
try:
with self._editor_service.get_file_for_run() as file_path_abs:
if (command_builder := strategy(file_path_abs)) is not None:
Expand Down
3 changes: 1 addition & 2 deletions python_coderunner/src/coderunner_factory/interface.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from abc import ABC, abstractmethod
from typing import Optional

from ..coderunner import TCodeRunner


class ICodeRunnerFactory(ABC):
@abstractmethod
def create(self) -> Optional[TCodeRunner]:
def create(self) -> TCodeRunner | None:
raise NotImplementedError
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import glob
import re
from typing import Dict, Optional

from ..coderunner import TCodeRunner
from ..command_builder import TInterpolatorCommandBuilder
Expand Down Expand Up @@ -43,7 +42,7 @@


class TVimCodeRunnerFactory(ICodeRunnerFactory):
def create(self) -> Optional[TCodeRunner]:
def create(self) -> TCodeRunner | None:
config: TVimConfig = self._create_config()
message_printer: TVimMessagePrinter = TVimMessagePrinter()

Expand Down Expand Up @@ -205,7 +204,7 @@ def _create_glob_command_builders_dispatcher(
file_info_extractor: TVimFileInfoExtractor,
project_info_extractor: TVimProjectInfoExtractor,
) -> TGlobCommandBuildersDispatcher:
dict_with_commands: Dict[str, str] = config.get_by_glob()
dict_with_commands: dict[str, str] = config.get_by_glob()
return TGlobCommandBuildersDispatcher(
tuple(
(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from typing import Dict, Final, Optional
from typing import Final

from ..command_builder import ICommandBuilder
from ..file_info_extractor import IFileInfoExtractor
from .interface import ICommandBuildersDispatcher


class TFileExtCommandBuildersDispatcher(ICommandBuildersDispatcher):
def __init__(self, file_ext_to_builder: Dict[str, ICommandBuilder], file_info_extractor: IFileInfoExtractor):
self._file_ext_to_builder: Final[Dict[str, ICommandBuilder]] = file_ext_to_builder
def __init__(self, file_ext_to_builder: dict[str, ICommandBuilder], file_info_extractor: IFileInfoExtractor):
self._file_ext_to_builder: Final[dict[str, ICommandBuilder]] = file_ext_to_builder
self._file_info_extractor: IFileInfoExtractor = file_info_extractor

def dispatch(self, file_path_abs: str) -> Optional[ICommandBuilder]:
def dispatch(self, file_path_abs: str) -> ICommandBuilder | None:
file_ext: str = self._file_info_extractor.get_file_ext(file_path_abs)
return self._file_ext_to_builder.get(file_ext)
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from typing import Dict, Final, Optional
from typing import Final

from ..command_builder import ICommandBuilder
from ..file_info_extractor import IFileInfoExtractor
from .interface import ICommandBuildersDispatcher


class TFileTypeCommandBuildersDispatcher(ICommandBuildersDispatcher):
def __init__(self, file_type_to_builder: Dict[str, ICommandBuilder], file_info_extractor: IFileInfoExtractor):
self._file_type_to_builder: Final[Dict[str, ICommandBuilder]] = file_type_to_builder
def __init__(self, file_type_to_builder: dict[str, ICommandBuilder], file_info_extractor: IFileInfoExtractor):
self._file_type_to_builder: Final[dict[str, ICommandBuilder]] = file_type_to_builder
self._file_info_extractor: IFileInfoExtractor = file_info_extractor

def dispatch(self, file_path_abs: str) -> Optional[ICommandBuilder]:
file_type: Optional[str] = self._file_info_extractor.get_file_type(file_path_abs)
def dispatch(self, file_path_abs: str) -> ICommandBuilder | None:
file_type: str | None = self._file_info_extractor.get_file_type(file_path_abs)
if file_type is None:
return None
return self._file_type_to_builder.get(file_type)
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import re
from typing import Final, Optional, Tuple
from typing import Final

from ..command_builder import ICommandBuilder
from .interface import ICommandBuildersDispatcher


class TGlobCommandBuildersDispatcher(ICommandBuildersDispatcher):
def __init__(self, glob_to_builder: Tuple[Tuple[re.Pattern[str], ICommandBuilder], ...]):
self._glob_to_builder: Final[Tuple[Tuple[re.Pattern[str], ICommandBuilder], ...]] = glob_to_builder
def __init__(self, glob_to_builder: tuple[tuple[re.Pattern[str], ICommandBuilder], ...]):
self._glob_to_builder: Final[tuple[tuple[re.Pattern[str], ICommandBuilder], ...]] = glob_to_builder

def dispatch(self, file_path_abs: str) -> Optional[ICommandBuilder]:
def dispatch(self, file_path_abs: str) -> ICommandBuilder | None:
for pattern, builder in self._glob_to_builder:
if pattern.match(file_path_abs):
return builder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from abc import ABC, abstractmethod
from typing import Optional

from ..command_builder import ICommandBuilder


class ICommandBuildersDispatcher(ABC):
@abstractmethod
def dispatch(self, file_path_abs: str) -> Optional[ICommandBuilder]:
def dispatch(self, file_path_abs: str) -> ICommandBuilder | None:
"""
Dispatch is optional for fallback strategy.
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

from ..command_builder import ICommandBuilder, TConcatenatorCommandBuilder
from ..file_info_extractor import IFileInfoExtractor
from .interface import ICommandBuildersDispatcher
Expand All @@ -9,7 +7,7 @@ class TShebangCommandBuildersDispatcher(ICommandBuildersDispatcher):
def __init__(self, file_info_extractor: IFileInfoExtractor):
self._file_info_extractor: IFileInfoExtractor = file_info_extractor

def dispatch(self, file_path_abs: str) -> Optional[ICommandBuilder]:
def dispatch(self, file_path_abs: str) -> ICommandBuilder | None:
if (shebang := self._file_info_extractor.get_shebang(file_path_abs)) is not None:
return TConcatenatorCommandBuilder(shebang)
return None
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

from ..command_builder import ICommandBuilder
from ..command_builders_dispatcher import (
TFileExtCommandBuildersDispatcher,
Expand Down Expand Up @@ -32,20 +30,20 @@ def __init__(
file_type_command_builders_dispatcher
)

def dispatch_by_shebang(self, file_path_abs: str) -> Optional[ICommandBuilder]:
def dispatch_by_shebang(self, file_path_abs: str) -> ICommandBuilder | None:
return self._shebang_command_builders_dispatcher.dispatch(file_path_abs)

def dispatch_by_glob(self, file_path_abs: str) -> Optional[ICommandBuilder]:
def dispatch_by_glob(self, file_path_abs: str) -> ICommandBuilder | None:
return self._glob_command_builders_dispatcher.dispatch(file_path_abs)

def dispatch_by_file_ext(self, file_path_abs: str) -> Optional[ICommandBuilder]:
def dispatch_by_file_ext(self, file_path_abs: str) -> ICommandBuilder | None:
return self._file_ext_command_builders_dispatcher.dispatch(file_path_abs)

def dispatch_by_file_type(self, file_path_abs: str) -> Optional[ICommandBuilder]:
def dispatch_by_file_type(self, file_path_abs: str) -> ICommandBuilder | None:
return self._file_type_command_builders_dispatcher.dispatch(file_path_abs)

def dispatch(self, file_path_abs: str) -> Optional[ICommandBuilder]:
command_builder: Optional[ICommandBuilder] = None
def dispatch(self, file_path_abs: str) -> ICommandBuilder | None:
command_builder: ICommandBuilder | None = None

if (
self._config.get_respect_shebang()
Expand Down
2 changes: 1 addition & 1 deletion python_coderunner/src/commands_executor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .inteface import ICommandsExecutor
from .interface import ICommandsExecutor
from .vim_commands_executor import TVimCommandsExecutor
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import vim

from ..config import IConfig
from .inteface import ICommandsExecutor
from .interface import ICommandsExecutor


class TVimCommandsExecutor(ICommandsExecutor):
Expand Down
26 changes: 13 additions & 13 deletions python_coderunner/src/config/basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List, TypeVar
from typing import TypeVar

from .config_field import TConfigField
from .exceptions import ConfigFieldNotFoundError, ConfigFieldValidationError
Expand All @@ -10,10 +10,10 @@
class TBasicConfig(IConfig):
def __init__(
self,
by_file_ext_field: TConfigField[Dict[str, str]],
by_file_type_field: TConfigField[Dict[str, str]],
by_glob_field: TConfigField[Dict[str, str]],
dispatchers_order_field: TConfigField[List[EDispatchersTypes]],
by_file_ext_field: TConfigField[dict[str, str]],
by_file_type_field: TConfigField[dict[str, str]],
by_glob_field: TConfigField[dict[str, str]],
dispatchers_order_field: TConfigField[list[EDispatchersTypes]],
coderunner_tempfile_prefix_field: TConfigField[str],
executor_field: TConfigField[str],
ignore_selection_field: TConfigField[bool],
Expand All @@ -22,10 +22,10 @@ def __init__(
save_all_files_before_run_field: TConfigField[bool],
save_file_before_run_field: TConfigField[bool],
):
self._by_file_ext_field: TConfigField[Dict[str, str]] = by_file_ext_field
self._by_file_type_field: TConfigField[Dict[str, str]] = by_file_type_field
self._by_glob_field: TConfigField[Dict[str, str]] = by_glob_field
self._dispatchers_order_field: TConfigField[List[EDispatchersTypes]] = dispatchers_order_field
self._by_file_ext_field: TConfigField[dict[str, str]] = by_file_ext_field
self._by_file_type_field: TConfigField[dict[str, str]] = by_file_type_field
self._by_glob_field: TConfigField[dict[str, str]] = by_glob_field
self._dispatchers_order_field: TConfigField[list[EDispatchersTypes]] = dispatchers_order_field
self._coderunner_tempfile_prefix_field: TConfigField[str] = coderunner_tempfile_prefix_field
self._executor_field: TConfigField[str] = executor_field
self._ignore_selection_field: TConfigField[bool] = ignore_selection_field
Expand All @@ -44,16 +44,16 @@ def _get_field_value(self, field: TConfigField[ValueType]) -> ValueType:
except (ConfigFieldNotFoundError, ConfigFieldValidationError) as e:
raise ValueError(str(e)) from e

def get_by_file_ext(self) -> Dict[str, str]:
def get_by_file_ext(self) -> dict[str, str]:
return self._get_field_value(self._by_file_ext_field)

def get_by_file_type(self) -> Dict[str, str]:
def get_by_file_type(self) -> dict[str, str]:
return self._get_field_value(self._by_file_type_field)

def get_by_glob(self) -> Dict[str, str]:
def get_by_glob(self) -> dict[str, str]:
return self._get_field_value(self._by_glob_field)

def get_dispatchers_order(self) -> List[EDispatchersTypes]:
def get_dispatchers_order(self) -> list[EDispatchersTypes]:
return self._get_field_value(self._dispatchers_order_field)

def get_coderunner_tempfile_prefix(self) -> str:
Expand Down
9 changes: 4 additions & 5 deletions python_coderunner/src/config/interface.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from abc import ABC, abstractmethod
from enum import StrEnum
from typing import Dict, List


class EDispatchersTypes(StrEnum):
Expand All @@ -13,22 +12,22 @@ class IConfig(ABC):
"""Configuration interface"""

@abstractmethod
def get_by_file_ext(self) -> Dict[str, str]:
def get_by_file_ext(self) -> dict[str, str]:
"""Gets config for file extension-based dispatching"""
raise NotImplementedError

@abstractmethod
def get_by_file_type(self) -> Dict[str, str]:
def get_by_file_type(self) -> dict[str, str]:
"""Gets config for file type-based dispatching"""
raise NotImplementedError

@abstractmethod
def get_by_glob(self) -> Dict[str, str]:
def get_by_glob(self) -> dict[str, str]:
"""Gets config for glob pattern-based dispatching"""
raise NotImplementedError

@abstractmethod
def get_dispatchers_order(self) -> List[EDispatchersTypes]:
def get_dispatchers_order(self) -> list[EDispatchersTypes]:
"""Gets the priority order of dispatchers"""
raise NotImplementedError

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from typing import Any, List, Set
from typing import Any

from ..interface import EDispatchersTypes
from .exceptions import ValidationError
from .interface import IValidator


class TDispatchersOrderValidator(IValidator[List[EDispatchersTypes]]):
class TDispatchersOrderValidator(IValidator[list[EDispatchersTypes]]):
def __init__(self) -> None:
self.allowed_dispatcher_types: Set[EDispatchersTypes] = set(EDispatchersTypes)
self.allowed_dispatcher_types: set[EDispatchersTypes] = set(EDispatchersTypes)

def __call__(self, value: Any) -> List[EDispatchersTypes]:
def __call__(self, value: Any) -> list[EDispatchersTypes]:
if not isinstance(value, list):
raise ValidationError(f"Invalid dispatcher order container type: {type(value)}.")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Any, Dict
from typing import Any

from .exceptions import ValidationError
from .interface import IValidator


class TDispatchersValidator(IValidator[Dict[str, str]]):
def __call__(self, value: Any) -> Dict[str, str]:
class TDispatchersValidator(IValidator[dict[str, str]]):
def __call__(self, value: Any) -> dict[str, str]:
if not isinstance(value, dict):
raise ValidationError(f"Invalid dispatcher container type: {type(value)}.")
for key, val in value.items():
Expand Down
3 changes: 1 addition & 2 deletions python_coderunner/src/editor/interface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from abc import ABC, abstractmethod
from typing import Optional


class IEditor(ABC):
Expand All @@ -8,7 +7,7 @@ def get_current_file_name(self) -> str:
raise NotImplementedError

@abstractmethod
def get_selected_text(self) -> Optional[str]:
def get_selected_text(self) -> str | None:
raise NotImplementedError

@abstractmethod
Expand Down
4 changes: 1 addition & 3 deletions python_coderunner/src/editor/vim_editor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

import vim

from .interface import IEditor
Expand All @@ -9,7 +7,7 @@ class TVimEditor(IEditor):
def get_current_file_name(self) -> str:
return vim.current.buffer.name

def get_selected_text(self) -> Optional[str]:
def get_selected_text(self) -> str | None:
return vim.eval("coderunner#GetSelectedText(a:visualmode, a:range, a:first_line, a:last_line)")

def save_all_files(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions python_coderunner/src/editor_service_for_coderunner/basic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from contextlib import contextmanager
from tempfile import NamedTemporaryFile
from typing import Generator, List
from typing import Generator

from ..config import IConfig
from ..editor import IEditor
Expand All @@ -13,7 +13,7 @@ def __init__(self, config: IConfig, editor: IEditor, file_info_extractor: IFileI
self._config: IConfig = config
self._editor: IEditor = editor
self._file_info_extractor: IFileInfoExtractor = file_info_extractor
self._temp_files: List[str] = []
self._temp_files: list[str] = []

@contextmanager
def get_file_for_run(self) -> Generator[str, None, None]:
Expand Down
5 changes: 2 additions & 3 deletions python_coderunner/src/file_info_extractor/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
from abc import abstractmethod
from typing import Optional

from .interface import IFileInfoExtractor

Expand Down Expand Up @@ -33,10 +32,10 @@ def get_file_name_without_ext(self, file_path_abs: str) -> str:
return base[:dot_pos] if dot_pos != -1 else base

@abstractmethod
def get_file_type(self, file_path_abs: str) -> Optional[str]:
def get_file_type(self, file_path_abs: str) -> str | None:
raise NotImplementedError

def get_shebang(self, file_path_abs: str) -> Optional[str]:
def get_shebang(self, file_path_abs: str) -> str | None:
if not os.path.exists(file_path_abs):
return None

Expand Down
Loading