|
4 | 4 | from typing_extensions import Self |
5 | 5 | from abc import ABC, abstractmethod |
6 | 6 | from codeboxapi.schema import ( |
7 | | - CodeBoxStatus, |
| 7 | + CodeBoxStatus, |
8 | 8 | CodeBoxOutput, |
9 | 9 | CodeBoxFile, |
10 | 10 | ) |
11 | 11 |
|
12 | 12 |
|
13 | 13 | class BaseBox(ABC): |
14 | | - """ |
| 14 | + """ |
15 | 15 | ABC for Isolated Execution Environments |
16 | 16 | """ |
17 | | - |
| 17 | + |
18 | 18 | def __init__(self) -> None: |
19 | 19 | self.id: Optional[int] = None |
20 | 20 | self.last_interaction = datetime.now() |
21 | 21 |
|
22 | 22 | def _update(self) -> None: |
23 | 23 | self.last_interaction = datetime.now() |
24 | | - |
| 24 | + |
25 | 25 | @abstractmethod |
26 | | - def start(self) -> CodeBoxStatus: ... |
27 | | - |
| 26 | + def start(self) -> CodeBoxStatus: |
| 27 | + ... |
| 28 | + |
| 29 | + @abstractmethod |
| 30 | + async def astart(self) -> CodeBoxStatus: |
| 31 | + ... |
| 32 | + |
28 | 33 | @abstractmethod |
29 | | - async def astart(self) -> CodeBoxStatus: ... |
30 | | - |
31 | | - @abstractmethod |
32 | | - def status(self) -> CodeBoxStatus: ... |
| 34 | + def status(self) -> CodeBoxStatus: |
| 35 | + ... |
33 | 36 |
|
34 | 37 | @abstractmethod |
35 | | - async def astatus(self) -> CodeBoxStatus: ... |
36 | | - |
| 38 | + async def astatus(self) -> CodeBoxStatus: |
| 39 | + ... |
| 40 | + |
37 | 41 | @abstractmethod |
38 | | - def run(self, code: Optional[str] = None, file_path: Optional[os.PathLike] = None) -> CodeBoxOutput: ... |
39 | | - |
| 42 | + def run( |
| 43 | + self, code: Optional[str] = None, file_path: Optional[os.PathLike] = None |
| 44 | + ) -> CodeBoxOutput: |
| 45 | + ... |
| 46 | + |
40 | 47 | @abstractmethod |
41 | | - async def arun(self, code: Optional[str] = None, file_path: Optional[os.PathLike] = None) -> CodeBoxOutput: ... |
42 | | - |
| 48 | + async def arun( |
| 49 | + self, code: Optional[str] = None, file_path: Optional[os.PathLike] = None |
| 50 | + ) -> CodeBoxOutput: |
| 51 | + ... |
| 52 | + |
43 | 53 | @abstractmethod |
44 | | - def upload(self, file_name: str, content: bytes) -> CodeBoxStatus: ... |
45 | | - |
| 54 | + def upload(self, file_name: str, content: bytes) -> CodeBoxStatus: |
| 55 | + ... |
| 56 | + |
46 | 57 | @abstractmethod |
47 | | - async def aupload(self, file_name: str, content: bytes) -> CodeBoxStatus: ... |
| 58 | + async def aupload(self, file_name: str, content: bytes) -> CodeBoxStatus: |
| 59 | + ... |
48 | 60 |
|
49 | 61 | @abstractmethod |
50 | | - def download(self, file_name: str) -> CodeBoxFile: ... |
51 | | - |
| 62 | + def download(self, file_name: str) -> CodeBoxFile: |
| 63 | + ... |
| 64 | + |
52 | 65 | @abstractmethod |
53 | | - async def adownload(self, file_name: str) -> CodeBoxFile: ... |
54 | | - |
| 66 | + async def adownload(self, file_name: str) -> CodeBoxFile: |
| 67 | + ... |
| 68 | + |
55 | 69 | @abstractmethod |
56 | | - def install(self, package_name: str) -> CodeBoxStatus: ... |
57 | | - |
| 70 | + def install(self, package_name: str) -> CodeBoxStatus: |
| 71 | + ... |
| 72 | + |
58 | 73 | @abstractmethod |
59 | | - async def ainstall(self, package_name: str) -> CodeBoxStatus: ... |
60 | | - |
| 74 | + async def ainstall(self, package_name: str) -> CodeBoxStatus: |
| 75 | + ... |
| 76 | + |
61 | 77 | @abstractmethod |
62 | | - def list_files(self) -> list[CodeBoxFile]: ... |
63 | | - |
| 78 | + def list_files(self) -> list[CodeBoxFile]: |
| 79 | + ... |
| 80 | + |
64 | 81 | @abstractmethod |
65 | | - async def alist_files(self) -> list[CodeBoxFile]: ... |
66 | | - |
| 82 | + async def alist_files(self) -> list[CodeBoxFile]: |
| 83 | + ... |
| 84 | + |
67 | 85 | # @abstractmethod # TODO: implement |
68 | 86 | # def restart(self) -> CodeBoxStatus: ... |
69 | | - |
| 87 | + |
70 | 88 | # @abstractmethod # TODO: implement |
71 | 89 | # async def arestart(self) -> CodeBoxStatus: ... |
72 | | - |
| 90 | + |
73 | 91 | @abstractmethod |
74 | | - def stop(self) -> CodeBoxStatus: ... |
75 | | - |
| 92 | + def stop(self) -> CodeBoxStatus: |
| 93 | + ... |
| 94 | + |
76 | 95 | @abstractmethod |
77 | | - async def astop(self) -> CodeBoxStatus: ... |
78 | | - |
| 96 | + async def astop(self) -> CodeBoxStatus: |
| 97 | + ... |
| 98 | + |
79 | 99 | def __enter__(self) -> Self: |
80 | 100 | self.start() |
81 | 101 | return self |
82 | | - |
| 102 | + |
83 | 103 | async def __aenter__(self) -> Self: |
84 | 104 | await self.astart() |
85 | 105 | return self |
86 | | - |
| 106 | + |
87 | 107 | def __exit__(self, exc_type, exc_value, traceback) -> None: |
88 | 108 | self.stop() |
89 | | - |
| 109 | + |
90 | 110 | async def __aexit__(self, exc_type, exc_value, traceback) -> None: |
91 | 111 | await self.astop() |
92 | | - |
|
0 commit comments