Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimal fixes to get logging working #184

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions snakebids/_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import logging
from typing import Union


def setup_logging(level: Union[str, int] = logging.DEBUG):
logger = logging.getLogger("snakebids")
stream_handler = logging.StreamHandler()
formatter = logging.Formatter("%(levelname)-8s %(message)s")
stream_handler.setFormatter(formatter)

logger.addHandler(stream_handler)
logger.setLevel(level)
8 changes: 6 additions & 2 deletions snakebids/admin.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
"""Script to generate a Snakebids project."""

import argparse
import logging
import os
from pathlib import Path

from cookiecutter.main import cookiecutter

import snakebids
from snakebids._logging import setup_logging
from snakebids.app import SnakeBidsApp
from snakebids.cli import add_dynamic_args

logger = logging.getLogger(__name__)


def create_app(_):
cookiecutter(os.path.join(snakebids.__path__[0], "project_template"))
Expand All @@ -20,7 +24,7 @@ def create_descriptor(args):
app = SnakeBidsApp(args.app_dir.resolve())
add_dynamic_args(app.parser, app.config["parse_args"], app.config["pybids_inputs"])
app.create_descriptor(args.out_path)
print(f"Boutiques descriptor created at {args.out_path}")
logger.info("Boutiques descriptor created at %s", args.out_path)


def gen_parser():
Expand Down Expand Up @@ -53,7 +57,7 @@ def gen_parser():

def main():
"""Invoke Cookiecutter on the Snakebids project template."""

setup_logging()
parser = gen_parser()
args = parser.parse_args()
args.func(args)
20 changes: 15 additions & 5 deletions snakebids/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import snakemake
from snakemake.io import load_configfile

from snakebids._logging import setup_logging
from snakebids.cli import (
SnakebidsArgs,
add_dynamic_args,
Expand All @@ -24,7 +25,7 @@
write_output_mode,
)

logger = logging.Logger(__name__)
_logger = logging.getLogger(__name__)


SNAKEFILE_CHOICES = [
Expand Down Expand Up @@ -61,7 +62,8 @@ def wrapper(self: "SnakeBidsApp"):
return Path(self.snakemake_dir, path)

raise ConfigError(
f"Error: no {file_name} file found, tried {', '.join(choices)}."
f"Error: no {file_name} file found under {self.snakemake_dir}, tried "
f"{', '.join(choices)}."
)

return wrapper
Expand Down Expand Up @@ -112,6 +114,14 @@ class SnakeBidsApp:
)
args: Optional[SnakebidsArgs] = None

# pylint: disable=no-self-use
def __attrs_post_init__(self):
setup_logging()

@property
def logger(self):
return _logger

def run_snakemake(self):
"""Run snakemake with that config.

Expand Down Expand Up @@ -165,10 +175,10 @@ def run_snakemake(self):
self.config["output_dir"] /= "results"
self.config["root"] = "results"
# Print a friendly warning that the output directory will change
logger.info(
_logger.info(
"You specified your output to be in the snakebids directory, so "
"we're automatically putting your outputs in the results "
"subdirectory.\nYou'll find your results in `%s`",
"subdirectory. You'll find your results in `%s`",
(self.snakemake_dir / "results").resolve(),
)
else:
Expand All @@ -181,7 +191,7 @@ def run_snakemake(self):
try:
prepare_bidsapp_output(args.outputdir, args.force)
except RunError as err:
print(err.msg)
_logger.error(err.msg)
sys.exit(1)
cwd = args.outputdir
new_config_file = args.outputdir / self.configfile_path
Expand Down
2 changes: 1 addition & 1 deletion snakebids/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# either Path or pathlib.Path
Path = pathlib.Path

logger = logging.Logger(__name__)
logger = logging.getLogger(__name__)


# pylint: disable=too-few-public-methods,
Expand Down