|
2 | 2 | import json |
3 | 3 | import pathlib |
4 | 4 | from typing import Dict, List, Union |
| 5 | +from abc import ABC, abstractmethod |
5 | 6 |
|
6 | 7 | import aiofiles |
7 | 8 |
|
8 | | -HERE = pathlib.Path(__file__) |
9 | | -DATA = HERE.joinpath("..", "data").resolve() |
10 | | - |
11 | | - |
12 | | -def save( |
13 | | - name: str, |
14 | | - content: Union[str, Dict, List], |
15 | | - write_mode: str = "w", |
16 | | - indent: int = 2, |
17 | | - **json_dumps_kwargs, |
18 | | -) -> pathlib.Path: |
19 | | - """Save content to a file. If content is a dictionary, use json.dumps().""" |
20 | | - path = DATA / name |
21 | | - if isinstance(content, (dict, list)): |
22 | | - content = json.dumps(content, indent=indent, **json_dumps_kwargs) |
23 | | - with open(DATA / name, mode=write_mode) as f_out: |
24 | | - f_out.write(content) |
25 | | - return path |
26 | | - |
27 | | - |
28 | | -def load(name: str, **json_kwargs) -> Union[str, Dict, List]: |
29 | | - """Loads content from a file. If file ends with '.json', call json.load() and return a Dictionary.""" |
30 | | - path = DATA / name |
31 | | - with open(path) as f_in: |
32 | | - if path.suffix == ".json": |
33 | | - return json.load(f_in, **json_kwargs) |
34 | | - return f_in.read() |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +class Context: |
| 14 | + |
| 15 | + _state = None |
| 16 | + |
| 17 | + |
| 18 | + def __init__(self, state): |
| 19 | + self.transition_to(state) |
| 20 | + |
| 21 | + def transition_to(self, state: State): |
| 22 | + |
| 23 | + self._state = state |
| 24 | + self._state.context = self |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + def request_save(self): |
| 29 | + self._state.handle_save() |
| 30 | + |
| 31 | + def request_load(self): |
| 32 | + self._state.handle_load() |
| 33 | + |
| 34 | + |
| 35 | +class State(ABC): |
| 36 | + HERE = pathlib.Path(__file__) |
| 37 | + DATA = HERE.joinpath("..", "data").resolve() |
| 38 | + @property |
| 39 | + def context(self): |
| 40 | + return self._context |
| 41 | + |
| 42 | + @context.setter |
| 43 | + def context(self, context: Context): |
| 44 | + self._context = context |
| 45 | + |
| 46 | + @abstractmethod |
| 47 | + def handle_save(self) -> None: |
| 48 | + pass |
| 49 | + |
| 50 | + @abstractmethod |
| 51 | + def handle_load(self) -> None: |
| 52 | + pass |
| 53 | + |
| 54 | + |
| 55 | +class SaveState(State): |
| 56 | + def handle_save(self, name: str, |
| 57 | + content: Union[str, Dict, List], |
| 58 | + write_mode: str = "w", |
| 59 | + indent: int = 2, |
| 60 | + **json_dumps_kwargs,): |
| 61 | + """Save content to a file. If content is a dictionary, use json.dumps().""" |
| 62 | + path = self.DATA / name |
| 63 | + if isinstance(content, (dict, list)): |
| 64 | + content = json.dumps(content, indent=indent, **json_dumps_kwargs) |
| 65 | + with open(self.DATA / name, mode=write_mode) as f_out: |
| 66 | + f_out.write(content) |
| 67 | + return path |
| 68 | + |
| 69 | +class LoadState(State): |
| 70 | + def handle_load(name: str, **json_kwargs): |
| 71 | + path = DATA / name |
| 72 | + with open(path) as f_in: |
| 73 | + if path.suffix == ".json": |
| 74 | + return json.load(f_in, **json_kwargs) |
| 75 | + return f_in.read() |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +# def save( |
| 80 | +# name: str, |
| 81 | +# content: Union[str, Dict, List], |
| 82 | +# write_mode: str = "w", |
| 83 | +# indent: int = 2, |
| 84 | +# **json_dumps_kwargs, |
| 85 | +# ) -> pathlib.Path: |
| 86 | +# """Save content to a file. If content is a dictionary, use json.dumps().""" |
| 87 | +# path = DATA / name |
| 88 | +# if isinstance(content, (dict, list)): |
| 89 | +# content = json.dumps(content, indent=indent, **json_dumps_kwargs) |
| 90 | +# with open(DATA / name, mode=write_mode) as f_out: |
| 91 | +# f_out.write(content) |
| 92 | +# return path |
| 93 | + |
| 94 | + |
| 95 | +# def load(name: str, **json_kwargs) -> Union[str, Dict, List]: |
| 96 | +# """Loads content from a file. If file ends with '.json', call json.load() and return a Dictionary.""" |
| 97 | +# path = DATA / name |
| 98 | +# with open(path) as f_in: |
| 99 | +# if path.suffix == ".json": |
| 100 | +# return json.load(f_in, **json_kwargs) |
| 101 | +# return f_in.read() |
35 | 102 |
|
36 | 103 |
|
37 | 104 | class AIO: |
|
0 commit comments