Skip to content

Commit

Permalink
#144 Move logging settings to cli functions
Browse files Browse the repository at this point in the history
  • Loading branch information
astropenguin committed Jul 14, 2024
1 parent b8159f1 commit b92c2ff
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 33 deletions.
32 changes: 22 additions & 10 deletions demerge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


# standard library
from contextlib import contextmanager
from logging import DEBUG, basicConfig, getLogger
from pathlib import Path
from typing import Any
Expand All @@ -19,6 +20,19 @@
LOGGER = getLogger(__name__)


@contextmanager
def set_logger(debug: bool):
level = LOGGER.level

if debug:
LOGGER.setLevel(DEBUG)

try:
yield
finally:
LOGGER.setLevel(level)


def demerge(
obsid: str,
/,
Expand Down Expand Up @@ -51,16 +65,9 @@ def demerge(
Path of the merged DEMS file.
"""
if debug:
LOGGER.setLevel(DEBUG)

basicConfig(
datefmt="%Y-%m-%d %H:%M:%S",
format="[%(asctime)s %(name)s %(levelname)s] %(message)s",
)

for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")
with set_logger(debug):
for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")

data_dir_ = Path(data_dir).resolve() / f"cosmos_{obsid}"
reduced_dir_ = Path(reduced_dir).resolve() / f"reduced_{obsid}"
Expand Down Expand Up @@ -127,4 +134,9 @@ def demerge(

def cli() -> None:
"""Command line interface of the demerge function."""
basicConfig(
datefmt="%Y-%m-%d %H:%M:%S",
format="[%(asctime)s %(name)s %(funcName)s %(levelname)s] %(message)s",
)

Fire(demerge)
35 changes: 22 additions & 13 deletions demerge/merge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


# standard library
from contextlib import contextmanager
from logging import DEBUG, basicConfig, getLogger
from pathlib import Path

Expand All @@ -15,6 +16,19 @@
LOGGER = getLogger(__name__)


@contextmanager
def set_logger(debug: bool):
level = LOGGER.level

if debug:
LOGGER.setLevel(DEBUG)

try:
yield
finally:
LOGGER.setLevel(level)


def merge(
dems: Path,
/,
Expand Down Expand Up @@ -60,20 +74,10 @@ def merge(
FileExistsError: Raised if ``dems`` exists.
"""
# ロガーの設定
if debug:
LOGGER.setLevel(DEBUG)
with set_logger(debug):
for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")

basicConfig(
datefmt="%Y-%m-%d %H:%M:%S",
format="[%(asctime)s %(name)s %(levelname)s] %(message)s",
)

# 引数と値をロガーに記録
for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")

# マージの実行
da = create_dems(
ddbfits_path=ddb,
corresp_path=corresp,
Expand Down Expand Up @@ -102,4 +106,9 @@ def merge(

def cli() -> None:
"""Command line interface of the merge function."""
basicConfig(
datefmt="%Y-%m-%d %H:%M:%S",
format="[%(asctime)s %(name)s %(funcName)s %(levelname)s] %(message)s",
)

Fire(merge)
32 changes: 22 additions & 10 deletions demerge/reduce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


# standard library
from contextlib import contextmanager
from logging import DEBUG, basicConfig, getLogger
from pathlib import Path
from shutil import rmtree
Expand All @@ -18,6 +19,19 @@
SCRIPTS = Path(__file__).parent / "utils" / "scripts" / "aste"


@contextmanager
def set_logger(debug: bool):
level = LOGGER.level

if debug:
LOGGER.setLevel(DEBUG)

try:
yield
finally:
LOGGER.setLevel(level)


def reduce(
data_dir: Path,
reduced_dir: Path,
Expand All @@ -42,16 +56,9 @@ def reduce(
FileExistsError: Raised if ``reduced_dir`` exists.
"""
if debug:
LOGGER.setLevel(DEBUG)

basicConfig(
datefmt="%Y-%m-%d %H:%M:%S",
format="[%(asctime)s %(name)s %(levelname)s] %(message)s",
)

for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")
with set_logger(debug):
for key, val in locals().items():
LOGGER.debug(f"{key}: {val!r}")

# Resolve paths (must be done before changing working directory)
if not (data_dir := Path(data_dir).resolve()).exists():
Expand Down Expand Up @@ -92,4 +99,9 @@ def reduce(

def cli() -> None:
"""Command line interface of the reduce function."""
basicConfig(
datefmt="%Y-%m-%d %H:%M:%S",
format="[%(asctime)s %(name)s %(funcName)s %(levelname)s] %(message)s",
)

Fire(reduce)

0 comments on commit b92c2ff

Please sign in to comment.