-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved command handling (Now commands are created in commands folder) Moved Logger from server.py to an independent handler
- Loading branch information
1 parent
4163cbf
commit 4f9073d
Showing
5 changed files
with
84 additions
and
59 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from piemc.handlers.command import Command | ||
|
||
@Command | ||
def ping(self): | ||
self.logger.info("Pong!") | ||
|
||
@Command | ||
def stop(self): | ||
self.logger.info("Stopping the server...") | ||
self.stop() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,21 @@ | ||
from piemc.handlers.lang import LangHandler | ||
import inspect | ||
|
||
registered_commands = {} | ||
|
||
class CommandHandler: | ||
def __init__(self, logger): | ||
self.logger = logger | ||
def Command(func): | ||
cmd_name = func.__name__.lower() | ||
registered_commands[cmd_name] = func | ||
return func | ||
|
||
def handle(self, cmd, server): | ||
self.lang = LangHandler.initialize_language() | ||
if cmd == 'stop': | ||
self.handle_stop_cmd(server) | ||
elif cmd == 'restart': | ||
self.handle_restart_cmd(server) | ||
elif cmd == '': | ||
print(self.lang['EMPTY_COMMAND']) | ||
def handle_command(server, cmd): | ||
cmd_args = cmd.strip().split() | ||
cmd_name = cmd_args[0].lower() | ||
|
||
func = registered_commands.get(cmd_name) | ||
if func is not None: | ||
if inspect.getfullargspec(func).args: | ||
func(server, *cmd_args[1:]) | ||
else: | ||
print(self.lang['NOT_A_COMMAND'].format(cmd)) | ||
|
||
def handle_stop_cmd(self, server): | ||
self.logger.info(self.lang['STOPPING']) | ||
server.stop() | ||
|
||
def handle_restart_cmd(self, server): | ||
self.logger.info(self.lang['RESTARTING']) | ||
self.logger.error('This command in development now') | ||
# server.stop() | ||
# server.start() | ||
func(server) | ||
else: | ||
print(f"Command '{cmd_name}' not found.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import logging | ||
import os | ||
import piemc.config # Assuming you have a config module with LOG_LEVEL defined | ||
|
||
def create_logger(name): | ||
log_level_mapping = { | ||
'DEBUG': logging.DEBUG, | ||
'INFO': logging.INFO, | ||
'WARNING': logging.WARNING, | ||
'ERROR': logging.ERROR, | ||
'CRITICAL': logging.CRITICAL | ||
} | ||
log_level = log_level_mapping.get(piemc.config.LOG_LEVEL.upper(), logging.INFO) | ||
logger = logging.getLogger(name) | ||
logger.setLevel(log_level) | ||
|
||
log_dir = './log' | ||
os.makedirs(log_dir, exist_ok=True) # Create the directory if it doesn't exist | ||
|
||
log_file = os.path.join(log_dir, name + '.log') | ||
fhandler = logging.FileHandler(log_file, 'w', 'utf-8') | ||
shandler = logging.StreamHandler() | ||
|
||
formatter = logging.Formatter( | ||
"[%(name)s]" + str(' ' * (11 - len(name))) + "[%(asctime)s] [%(levelname)s] : %(message)s") | ||
fhandler.setFormatter(formatter) | ||
shandler.setFormatter(formatter) | ||
|
||
logger.addHandler(fhandler) | ||
logger.addHandler(shandler) | ||
|
||
return logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters