Skip to content

Commit 9e63085

Browse files
committed
test
1 parent 02800f0 commit 9e63085

6 files changed

Lines changed: 29 additions & 88 deletions

File tree

loader.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from telegram.ext import Application, CommandHandler, CallbackQueryHandler
66

7-
87
def load_modules(app: Application):
98
for py_file in Path("modules").glob("*.py"):
109
module_name = py_file.stem
@@ -15,36 +14,41 @@ def load_modules(app: Application):
1514
module = importlib.util.module_from_spec(spec)
1615
spec.loader.exec_module(module)
1716

18-
functions = [name for name, obj in inspect.getmembers(module) if inspect.isfunction(obj) and not name.startswith("_")]
17+
functions = []
18+
for name, obj in inspect.getmembers(module):
19+
if inspect.isfunction(obj) and not name.startswith("_"):
20+
functions.append(name)
1921

2022
decorator_groups = {"command": [], "callback": []}
2123

2224
for func_name in functions:
2325
func = getattr(module, func_name)
2426

25-
if hasattr(func, "__wrapped__"):
26-
decorator_name = func.__name__ if hasattr(func, "__name__") else "unknown"
27-
original_func = func
28-
while hasattr(original_func, "__wrapped__"):
29-
original_func = original_func.__wrapped__
30-
decorator_name = original_func.__name__
27+
if hasattr(func, "decorator"):
28+
decorator_name = func.decorator
3129
else:
32-
raise ValueError(f"No decorator found for function {func_name} in module {module_name}")
30+
print(f"Function {func_name} in module {module_name} does not have a recognized decorator.")
31+
continue
3332

3433
if decorator_name not in decorator_groups:
35-
raise ValueError(f"Decorator {decorator_name} not recognized in module {module_name}")
34+
print(f"Decorator {decorator_name} not recognized in module {module_name}")
35+
continue
3636

3737
decorator_groups[decorator_name].append(func_name)
38+
39+
print(decorator_groups)
3840

3941
for decorator_name, func_names in decorator_groups.items():
4042
if decorator_name == "command":
4143
for func_name in func_names:
4244
func = getattr(module, func_name)
4345

4446
app.add_handler(CommandHandler(func.command, func))
47+
print(f"Command handler for {func.command} added from module {module_name}")
4548

4649
elif decorator_name == "callback":
4750
for func_name in func_names:
4851
func = getattr(module, func_name)
4952

50-
app.add_handler(CallbackQueryHandler(func))
53+
app.add_handler(CallbackQueryHandler(func))
54+
print(f"Callback handler for {func.callback_data} added from module {module_name}")

modules/__example__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from modules.__apis__ import *
2-
from modules.__decorators__ import command, callback
1+
from modules.apis import *
2+
from modules.decorators import command, callback
33

44
from telegram import Update
55

File renamed without changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ async def wrapper(update: Update, context: ContextTypes.DEFAULT_TYPE):
88
return result
99

1010
wrapper.command = cmd
11+
wrapper.decorator = "command"
1112
return wrapper
1213
return decorator
1314

@@ -24,5 +25,6 @@ async def wrapper(update: Update, context: ContextTypes.DEFAULT_TYPE):
2425
else:
2526
await query.edit_message_text("‼️잘못된 요청입니다‼️")
2627

28+
wrapper.decorator = "callback"
2729
return wrapper
2830
return decorator

modules/test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from modules.__apis__ import *
2-
from modules.__decorators__ import command, callback
1+
from modules.apis import *
2+
from modules.decorators import command, callback
33

44
from telegram import Update
55

66
@command("test")
77
async def test_command(update: Update):
8+
print("Test command executed")
89
await update.message.reply_text("This is a test command.")

test.py

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,9 @@
1-
import os
2-
import importlib.util
3-
import inspect
4-
from pathlib import Path
1+
from modules.apis import *
2+
from modules.decorators import command, callback
53

6-
from telegram.ext import Application
4+
from telegram import Update
75

8-
def load_modules(app: Application):
9-
for py_file in Path("modules").glob("*.py"):
10-
module_name = py_file.stem
11-
if module_name.startswith('__'):
12-
continue
13-
14-
spec = importlib.util.spec_from_file_location(module_name, py_file)
15-
module = importlib.util.module_from_spec(spec)
16-
spec.loader.exec_module(module)
17-
18-
functions = [name for name, obj in inspect.getmembers(module)
19-
if inspect.isfunction(obj) and not name.startswith('_')]
20-
21-
print(f"Module: {module_name}")
22-
23-
decorator_groups = {}
24-
25-
for func_name in functions:
26-
func = getattr(module, func_name)
27-
28-
# 데코레이터 이름 찾기
29-
if hasattr(func, '__wrapped__'):
30-
# 데코레이터가 적용된 함수
31-
decorator_name = func.__name__ if hasattr(func, '__name__') else 'unknown'
32-
# 실제 데코레이터 이름을 찾기 위해 원본 함수까지 추적
33-
original_func = func
34-
while hasattr(original_func, '__wrapped__'):
35-
original_func = original_func.__wrapped__
36-
decorator_name = f"decorated_{original_func.__name__}"
37-
else:
38-
decorator_name = "no_decorator"
39-
40-
if decorator_name not in decorator_groups:
41-
decorator_groups[decorator_name] = []
42-
decorator_groups[decorator_name].append(func_name)
43-
44-
# 데코레이터별로 함수 실행
45-
for decorator_name, func_names in decorator_groups.items():
46-
print(f"\n[{decorator_name}]")
47-
print(f"Functions: {func_names}")
48-
49-
for func_name in func_names:
50-
func = getattr(module, func_name)
51-
sig = inspect.signature(func)
52-
53-
if len(sig.parameters) == 0:
54-
if inspect.iscoroutinefunction(func):
55-
import asyncio
56-
result = asyncio.run(func())
57-
print(f"{func_name}() -> {result}")
58-
else:
59-
result = func()
60-
print(f"{func_name}() -> {result}")
61-
else:
62-
try:
63-
if inspect.iscoroutinefunction(func):
64-
import asyncio
65-
result = asyncio.run(func())
66-
print(f"{func_name}() -> {result}")
67-
else:
68-
result = func()
69-
print(f"{func_name}() -> {result}")
70-
except:
71-
print(f"{func_name}() -> needs parameters")
72-
73-
print("="*50)
74-
75-
load_and_execute_modules()
6+
@command("test")
7+
async def test_command(update: Update):
8+
print("Test command executed")
9+
await update.message.reply_text("This is a test command.")

0 commit comments

Comments
 (0)