forked from cuttlesystems/simplegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIFileCreator.py
119 lines (90 loc) · 4.35 KB
/
APIFileCreator.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from pathlib import Path
from b_logic.data_objects import HandlerInit
from cuttle_builder.builder.additional.file_read_write.file_manager import FileManager
class APIFileCreator(FileManager):
def __init__(self, bot_direcory: str):
assert isinstance(bot_direcory, str)
self._bot_directory = bot_direcory
def create_app_file(self, code: str) -> None:
"""
Создает app файл (исполняемый файл).
Args:
code (str): Подготовленный код(содержимое файла)
"""
assert isinstance(code, str)
path_to_file = str(
Path(self._bot_directory) / 'app.py')
self._write_file_owerwrite(path_to_file, code)
def create_commands_file(self, code: str) -> None:
"""
Создает файл с кодом функции для отображения команд бота
Args:
code (str): Подготовленный код(содержимое файла)
"""
assert isinstance(code, str)
path_to_file = str(
Path(self._bot_directory) / 'on_startup_commands.py')
self._write_file_insert(path_to_file, code)
def create_utils_file(self, code: str) -> None:
"""
Создает файл с кодом функции для сохранения username и user id
Args:
code (str): Подготовленный код(содержимое файла)
"""
assert isinstance(code, str)
path_to_file = str(
Path(self._bot_directory) / 'utils' / 'save_id_and_username.py')
self._write_file_insert(path_to_file, code)
def create_config_file(self, code) -> None:
assert isinstance(code, str)
config_code_file = str(
Path(self._bot_directory) / 'data' / 'config.py')
self._write_file_insert(config_code_file, code)
def create_file_handler(self, name: str, code: str) -> None:
"""create file in specific directory, contains handler and register this handler in the package
Args:
name (str): name of handler (message_id)
code (str): generated code of handler
"""
assert isinstance(name, str)
assert isinstance(code, str)
handler_code_file = str(
Path(self._bot_directory) / 'handlers' / f'get_{name}.py')
self._write_file_insert(handler_code_file, code)
def create_file_keyboard(self, keyboard_name: str, keyboard_code: str) -> None:
"""create file in specific directory, contains keyboard and register this keyboard in the package
Args:
keyboard_name (str): name of keyboard (message_id + _kb)
keyboard_code (str): generated code of keyboard
"""
assert isinstance(keyboard_name, str)
assert isinstance(keyboard_code, str)
keyboard_code_file = str(
Path(self._bot_directory) / 'keyboards' / f'{keyboard_name}.py')
self._write_file_insert(keyboard_code_file, keyboard_code)
def create_handler_file_init(self, name: str) -> None:
assert isinstance(name, str)
import_code = f'from .get_{name} import dp\n'
handler_init_file = str(
Path(self._bot_directory) / 'handlers' / '__init__.py')
self._write_into_init(handler_init_file, import_code)
def create_keyboard_file_init(self, keyboard_name: str) -> None:
assert isinstance(keyboard_name, str)
import_code = f'\nfrom .{keyboard_name} import {keyboard_name}'
keyboard_init_file = str(
Path(self._bot_directory) / 'keyboards' / '__init__.py')
self._write_into_init(keyboard_init_file, import_code)
def create_state_file(self, code: str) -> None:
"""create file in specific directory, contains states class and register this class in the package
Args:
code (str): code of state
"""
assert isinstance(code, str)
state_code_file = str(
Path(self._bot_directory) / 'state' / 'states.py')
self._write_file_insert(state_code_file, code)
def create_state_file_init(self) -> None:
import_code = 'from .states import States'
state_init_file = str(
Path(self._bot_directory) / 'state' / '__init__.py')
self._write_into_init(state_init_file, import_code)