-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
180 additions
and
34 deletions.
There are no files selected for viewing
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 +1,5 @@ | ||
from pygeoweaver.commands.pgw_interface import * | ||
from pygeoweaver.log_config import setup_logging | ||
|
||
|
||
setup_logging() |
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
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
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,14 @@ | ||
from pydantic import BaseModel | ||
from datetime import datetime | ||
from typing import Optional | ||
|
||
class History(BaseModel): | ||
history_id: str | ||
history_input: Optional[str] | ||
history_output: Optional[str] | ||
history_begin_time: Optional[datetime] | ||
history_end_time: Optional[datetime] | ||
history_notes: Optional[str] | ||
history_process: Optional[str] | ||
host_id: Optional[str] | ||
indicator: Optional[str] |
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,34 @@ | ||
import logging | ||
import os | ||
|
||
|
||
def setup_logging(): | ||
log_file = '~/geoweaver.log' | ||
log_file = os.path.expanduser(log_file) | ||
|
||
# Ensure the directory for the log file exists, create if not | ||
log_dir = os.path.dirname(log_file) | ||
os.makedirs(log_dir, exist_ok=True) | ||
with open('logging.ini', 'rt') as f: | ||
config_str = f.read() | ||
config_str = config_str.replace('%(log_file)s', os.path.expanduser(log_file)) | ||
|
||
config_file = 'logging_temp.ini' | ||
with open(config_file, 'wt') as f: | ||
f.write(config_str) | ||
|
||
logging.config.fileConfig(config_file) | ||
os.remove(config_file) | ||
|
||
|
||
def get_logger(class_name): | ||
""" | ||
Get a logger with the specified class name. | ||
""" | ||
logger = logging.getLogger(class_name) | ||
logger.setLevel(logging.INFO) | ||
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") | ||
console_handler = logging.StreamHandler() | ||
console_handler.setFormatter(formatter) | ||
logger.addHandler(console_handler) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import inspect | ||
import io | ||
import logging | ||
from functools import wraps | ||
import os | ||
import sys | ||
|
||
from pygeoweaver.commands.pgw_history import save_history | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def geoweaver_process(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
func_name = func.__name__ | ||
try: | ||
# Capture the source code of the wrapped function | ||
func_code = inspect.getsource(func) | ||
except OSError: | ||
func_code = f"# Source code not available for {func_name}" | ||
|
||
# Capture logging output | ||
logger.info(f"Starting {func_name}") | ||
|
||
# Capture console output | ||
old_stdout = sys.stdout | ||
old_stderr = sys.stderr | ||
sys.stdout = io.StringIO() | ||
sys.stderr = io.StringIO() | ||
|
||
try: | ||
result = func(*args, **kwargs) | ||
finally: | ||
# Restore standard output and error | ||
stdout_content = sys.stdout.getvalue() | ||
stderr_content = sys.stderr.getvalue() | ||
sys.stdout = old_stdout | ||
sys.stderr = old_stderr | ||
|
||
# Log the captured output | ||
if stdout_content: | ||
logger.info(f"Standard Output:\n{stdout_content}") | ||
if stderr_content: | ||
logger.error(f"Standard Error:\n{stderr_content}") | ||
|
||
save_history(code=func_code, log_output=stdout_content, status=None) | ||
|
||
logger.info(f"Finished {func_name}") | ||
|
||
return result | ||
|
||
return wrapper |
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,65 @@ | ||
import inspect | ||
import io | ||
import logging | ||
from functools import wraps | ||
import os | ||
import sys | ||
|
||
# Ensure the log directory exists | ||
LOG_DIR = os.path.expanduser("~/geoweaver_logs") | ||
os.makedirs(LOG_DIR, exist_ok=True) | ||
|
||
def get_log_file_path(func_name): | ||
return os.path.join(LOG_DIR, f"{func_name}_logs.log") | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def geoweaver_workflow(func): | ||
|
||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
workflow_name = func.__name__ | ||
try: | ||
# Capture the source code of the wrapped function | ||
workflow_code = inspect.getsource(func) | ||
except OSError: | ||
workflow_code = f"# Source code not available for {workflow_name}" | ||
|
||
with open(f"{LOG_DIR}/{workflow_name}_code.py", "w") as code_file: | ||
code_file.write(workflow_code) | ||
|
||
# Capture logging output | ||
logger.info(f"Starting workflow {workflow_name}") | ||
|
||
# Capture console output | ||
captured_stdout = io.StringIO() | ||
captured_stderr = io.StringIO() | ||
old_stdout = sys.stdout | ||
old_stderr = sys.stderr | ||
sys.stdout = captured_stdout | ||
sys.stderr = captured_stderr | ||
|
||
try: | ||
result = func(*args, **kwargs) | ||
finally: | ||
# Restore standard output and error | ||
sys.stdout = old_stdout | ||
sys.stderr = old_stderr | ||
|
||
# Log the captured output | ||
stdout_content = captured_stdout.getvalue() | ||
stderr_content = captured_stderr.getvalue() | ||
if stdout_content: | ||
logger.info(f"Standard Output:\n{stdout_content}") | ||
print(stdout_content) | ||
if stderr_content: | ||
logger.error(f"Standard Error:\n{stderr_content}") | ||
print(stderr_content) | ||
|
||
logger.info(f"Finished workflow {workflow_name}") | ||
|
||
return result | ||
|
||
return wrapper | ||
|
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
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
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 |
---|---|---|
|
@@ -22,6 +22,7 @@ dependencies = [ | |
"ipython", | ||
"pandas", | ||
"halo", | ||
"pydantic", | ||
] | ||
|
||
[project.urls] | ||
|
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
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