-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessor.py
More file actions
56 lines (46 loc) · 1.92 KB
/
Copy pathpreprocessor.py
File metadata and controls
56 lines (46 loc) · 1.92 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
import importlib.abc
import importlib.util
from translate import RussianTransformer
replacements = RussianTransformer.replacements
time_replacements= { # Пример добавления своих команд
'время': "time",
'спать': "sleep",
}
replacements.update(time_replacements)
class RussianLoader(importlib.abc.SourceLoader):
"""
Пользовательский загрузчик для чтения и преобразования файлов с русским
кодом.
"""
def get_data(self, path):
"""
Считывает файл и преобразует его содержимое с русского на английский.
"""
try:
with open(path, 'r', encoding='utf-8') as file:
code = file.read()
# Transform the code
code = RussianTransformer.transform(code)
return code.encode('utf-8')
except FileNotFoundError:
raise Exception(f"File not found: {path}")
except Exception as e:
raise Exception(f"Error reading file {path}: {e}")
def get_filename(self, fullname):
"""
Возвращает имя файла.
"""
return fullname
def import_and_run(file_path):
"""
Импортирует и запускает Python-файл с русскими ключевыми словами.
"""
spec = importlib.util.spec_from_file_location("code.py", file_path,
loader=RussianLoader())
module = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(module)
except Exception as e:
raise Exception(f"Error executing the module: {e}")
# Запуск файла, расширение у файла может быть любым, хоть .txt
import_and_run('code.py')