Skip to content

Commit

Permalink
add the database classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ZihengSun committed Jun 29, 2024
1 parent 19d9249 commit 91a33af
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 34 deletions.
4 changes: 4 additions & 0 deletions pygeoweaver/__init__.py
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()
19 changes: 1 addition & 18 deletions pygeoweaver/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,12 @@
from pygeoweaver.commands.pgw_history import get_process_history, get_workflow_history
from pygeoweaver.commands.pgw_list import list_processes_in_workflow
from pygeoweaver.commands.pgw_sync import sync, sync_workflow
from pygeoweaver.log_config import setup_logging
from pygeoweaver.server import check_geoweaver_status, show
from halo import Halo
from pygeoweaver.utils import get_spinner


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)

setup_logging()


Expand Down
5 changes: 5 additions & 0 deletions pygeoweaver/commands/pgw_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ def get_workflow_history(workflow_id):
shell=True,
cwd=f"{get_root_dir()}/",
)


def save_history(code: str = None, status: str = None, log_output: str = None, ):

pass
14 changes: 14 additions & 0 deletions pygeoweaver/database_management/pgw_history.py
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]
34 changes: 34 additions & 0 deletions pygeoweaver/log_config.py
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
53 changes: 53 additions & 0 deletions pygeoweaver/runtime_tags/pgw_process.py
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
65 changes: 65 additions & 0 deletions pygeoweaver/runtime_tags/pgw_workflow.py
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

2 changes: 1 addition & 1 deletion pygeoweaver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

from pygeoweaver.constants import GEOWEAVER_DEFAULT_ENDPOINT_URL
from pygeoweaver.jdk_utils import check_java
from pygeoweaver.log_config import get_logger
from pygeoweaver.utils import (
check_ipython,
check_os,
download_geoweaver_jar,
get_logger,
get_module_absolute_path,
get_root_dir,
get_spinner,
Expand Down
13 changes: 0 additions & 13 deletions pygeoweaver/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,6 @@ def check_ipython():
return False


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


def copy_files(source_folder, destination_folder):
"""
Copy files from the source folder to the destination folder.
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies = [
"ipython",
"pandas",
"halo",
"pydantic",
]

[project.urls]
Expand Down
2 changes: 1 addition & 1 deletion test/test_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import unittest
from pygeoweaver.commands.pgw_detail import detail_host, detail_process, detail_workflow
from pygeoweaver.utils import get_logger
from pygeoweaver.log_config import get_logger


logger = get_logger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion test/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import unittest

from pygeoweaver.constants import GEOWEAVER_DEFAULT_ENDPOINT_URL
from pygeoweaver.log_config import get_logger
from pygeoweaver.server import show
from pygeoweaver.utils import get_logger


logger = get_logger(__name__)
Expand Down

0 comments on commit 91a33af

Please sign in to comment.