-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoderunner.py
More file actions
57 lines (47 loc) · 2.34 KB
/
coderunner.py
File metadata and controls
57 lines (47 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from typing import Callable, Optional
from ..command_builder import ICommandBuilder
from ..command_dispatcher_strategy_selector import (
TBasicCommandDispatcherStrategySelector,
)
from ..commands_executor import ICommandsExecutor
from ..config_manager import TBasicConfigManager
from ..editor_service_for_coderunner import TBasicEditorServiceForCodeRunner
from ..message_printer import IMessagePrinter
class TCodeRunner:
def __init__(
self,
*,
config_manager: TBasicConfigManager,
editor_service: TBasicEditorServiceForCodeRunner,
command_dispatcher_strategy_selector: TBasicCommandDispatcherStrategySelector,
commands_executor: ICommandsExecutor,
message_printer: IMessagePrinter,
):
self._config_manager: TBasicConfigManager = config_manager
self._editor_service: TBasicEditorServiceForCodeRunner = editor_service
self._command_dispatcher_strategy_selector: TBasicCommandDispatcherStrategySelector = (
command_dispatcher_strategy_selector
)
self._commands_executor: ICommandsExecutor = commands_executor
self._message_printer: IMessagePrinter = message_printer
def run(self) -> None:
self._run(self._command_dispatcher_strategy_selector.dispatch)
def run_by_glob(self) -> None:
self._run(self._command_dispatcher_strategy_selector.dispatch_by_glob)
def run_by_file_ext(self) -> None:
self._run(self._command_dispatcher_strategy_selector.dispatch_by_file_ext)
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:
try:
with self._editor_service.get_file_for_run() as file_path_abs:
if (command_builder := strategy(file_path_abs)) is not None:
self._editor_service.prepare_for_run()
self._commands_executor.execute(command_builder.build(file_path_abs))
except ValueError as e:
self._message_printer.error(str(e))
def remove_coderunner_tempfiles(self) -> None:
self._editor_service.remove_coderunner_tempfiles()
def on_exit(self) -> None:
if self._config_manager.get_remove_coderunner_tempfiles_on_exit():
self.remove_coderunner_tempfiles()