upload pyapp
This commit is contained in:
3
pyapp/utils/__init__.py
Normal file
3
pyapp/utils/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Utility modules for PyApp
|
||||
"""
|
||||
BIN
pyapp/utils/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
pyapp/utils/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
BIN
pyapp/utils/__pycache__/log.cpython-39.pyc
Normal file
BIN
pyapp/utils/__pycache__/log.cpython-39.pyc
Normal file
Binary file not shown.
BIN
pyapp/utils/__pycache__/project.cpython-39.pyc
Normal file
BIN
pyapp/utils/__pycache__/project.cpython-39.pyc
Normal file
Binary file not shown.
BIN
pyapp/utils/__pycache__/timer.cpython-39.pyc
Normal file
BIN
pyapp/utils/__pycache__/timer.cpython-39.pyc
Normal file
Binary file not shown.
99
pyapp/utils/log.py
Normal file
99
pyapp/utils/log.py
Normal file
@@ -0,0 +1,99 @@
|
||||
import time
|
||||
import json
|
||||
from pyapp.status import status_manager
|
||||
from pyapp.namespace import LogType
|
||||
class Log:
|
||||
MAX_TITLE_LENGTH:int = 7
|
||||
TYPE_COLOR_MAP = {
|
||||
LogType.INFO: "\033[94m",
|
||||
LogType.ERROR: "\033[91m",
|
||||
LogType.WARNING: "\033[93m",
|
||||
LogType.SUCCESS: "\033[92m",
|
||||
LogType.DEBUG: "\033[95m",
|
||||
LogType.TERMINATE: "\033[96m"
|
||||
}
|
||||
|
||||
def get_time():
|
||||
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
||||
|
||||
def blue(message):
|
||||
# blue
|
||||
print(f"\033[94m{message}\033[0m")
|
||||
def red(message):
|
||||
# red
|
||||
print(f"\033[91m{message}\033[0m")
|
||||
def yellow(message):
|
||||
# yellow
|
||||
print(f"\033[93m{message}\033[0m")
|
||||
def green(message):
|
||||
# green
|
||||
print(f"\033[92m{message}\033[0m")
|
||||
|
||||
def log(message, log_type: str):
|
||||
time_str = Log.get_time()
|
||||
space = ""
|
||||
if len(log_type) < Log.MAX_TITLE_LENGTH:
|
||||
space = " " * (Log.MAX_TITLE_LENGTH - len(log_type))
|
||||
if isinstance(message, dict):
|
||||
formatted_message = json.dumps(message, indent=2, ensure_ascii=False)
|
||||
else:
|
||||
formatted_message = message
|
||||
|
||||
print (f"\033[1m\033[4m({time_str})\033[0m \033[1m{Log.TYPE_COLOR_MAP[log_type]}[{log_type.capitalize()}]\033[0m{space} {Log.TYPE_COLOR_MAP[log_type]}{formatted_message}\033[0m")
|
||||
status_manager.add_log(time_str, log_type, formatted_message)
|
||||
|
||||
def bold(message):
|
||||
print(f"\033[1m{message}\033[0m")
|
||||
def underline(message):
|
||||
print(f"\033[4m{message}\033[0m")
|
||||
|
||||
def info(message):
|
||||
Log.log(message, LogType.INFO)
|
||||
|
||||
def error(message, terminate=False):
|
||||
Log.log(message, LogType.ERROR)
|
||||
if terminate:
|
||||
Log.terminate("Application Terminated.")
|
||||
|
||||
|
||||
def warning(message):
|
||||
Log.log(message, LogType.WARNING)
|
||||
def success(message):
|
||||
Log.log(message, LogType.SUCCESS)
|
||||
|
||||
def debug(message):
|
||||
Log.log(message, LogType.DEBUG)
|
||||
|
||||
def terminate(message):
|
||||
Log.log(message, LogType.TERMINATE)
|
||||
exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
Log.info("This is a info message")
|
||||
Log.error("This is a error message")
|
||||
Log.warning("This is a warning message")
|
||||
Log.success("This is a success message")
|
||||
Log.debug("This is a debug message")
|
||||
Log.blue("This is a blue message")
|
||||
Log.red("This is a red message")
|
||||
Log.yellow("This is a yellow message")
|
||||
Log.green("This is a green message")
|
||||
|
||||
Log.bold("This is a bold message")
|
||||
Log.underline("This is a underline message")
|
||||
|
||||
test_dict = {
|
||||
"name": "John Doe",
|
||||
"age": 25,
|
||||
"city": "New York",
|
||||
"hobbies": ["reading", "swimming", "programming"],
|
||||
"address": {
|
||||
"street": "Main Street",
|
||||
"number": 123
|
||||
}
|
||||
}
|
||||
Log.info("Dictionary test:")
|
||||
Log.info(test_dict)
|
||||
|
||||
|
||||
|
||||
50
pyapp/utils/project.py
Normal file
50
pyapp/utils/project.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import os
|
||||
import sys
|
||||
import yaml
|
||||
import importlib
|
||||
|
||||
class Project:
|
||||
@staticmethod
|
||||
def scan_project(root_path):
|
||||
sys.path.append(root_path)
|
||||
if not os.path.exists(root_path) or not os.path.isdir(root_path):
|
||||
raise ValueError(f"The provided root_path '{root_path}' is not a valid directory.")
|
||||
|
||||
parent_dir = os.path.dirname(root_path)
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
def import_all_modules(path, package_name):
|
||||
for root, dirs, files in os.walk(path):
|
||||
relative_path = os.path.relpath(root, root_path)
|
||||
if relative_path == '.':
|
||||
module_package = package_name
|
||||
else:
|
||||
module_package = f"{package_name}.{relative_path.replace(os.sep, '.')}"
|
||||
for file in files:
|
||||
if file.endswith(".py") and file != "__init__.py":
|
||||
module_name = file[:-3]
|
||||
full_module_name = f"{module_package}.{module_name}"
|
||||
if full_module_name not in sys.modules:
|
||||
importlib.import_module(full_module_name)
|
||||
dirs[:] = [d for d in dirs if not d.startswith('.')]
|
||||
|
||||
package_name = os.path.basename(root_path)
|
||||
import_all_modules(root_path, package_name)
|
||||
|
||||
@staticmethod
|
||||
def scan_configs(root_path):
|
||||
configs = {}
|
||||
for root, dirs, files in os.walk(root_path):
|
||||
for file in files:
|
||||
if file.endswith(('.yaml', '.yml')):
|
||||
if file.startswith('__'):
|
||||
continue
|
||||
file_path = os.path.join(root, file)
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
try:
|
||||
content = yaml.safe_load(f)
|
||||
configs[os.path.splitext(file)[0]] = content
|
||||
except yaml.YAMLError as e:
|
||||
print(f"Error reading {file_path}: {e}")
|
||||
|
||||
return configs
|
||||
32
pyapp/utils/timer.py
Normal file
32
pyapp/utils/timer.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import time
|
||||
|
||||
class Timer:
|
||||
MILLI_SECONDS = "milliseconds"
|
||||
SECONDS = "seconds"
|
||||
MINUTES = "minutes"
|
||||
HOURS = "hours"
|
||||
def __init__(self, name=None):
|
||||
self.start_time = None
|
||||
self.end_time = None
|
||||
self.name = name
|
||||
|
||||
def start(self):
|
||||
self.start_time = time.time()
|
||||
|
||||
def stop(self):
|
||||
self.end_time = time.time()
|
||||
|
||||
def elapsed_time(self):
|
||||
return int(self.end_time - self.start_time)
|
||||
|
||||
def get_elasped_time_str(self, format):
|
||||
if format == Timer.SECONDS:
|
||||
return f"Elapsed time in <{self.name}>: {self.elapsed_time()} seconds"
|
||||
elif format == Timer.MINUTES:
|
||||
return f"Elapsed time in <{self.name}>: {self.elapsed_time() // 60} minutes, {self.elapsed_time() % 60} seconds"
|
||||
elif format == Timer.HOURS:
|
||||
return f"Elapsed time in <{self.name}>: {self.elapsed_time() // 3600} hours, {(self.elapsed_time() % 3600)//60} minutes, {self.elapsed_time() % 60} seconds"
|
||||
elif format == Timer.MILLI_SECONDS:
|
||||
return f"Elapsed time in <{self.name}>: {(self.end_time - self.start_time) * 1000} milliseconds"
|
||||
else:
|
||||
return f"Invalid format: {format}"
|
||||
Reference in New Issue
Block a user