Skip to content

Commit

Permalink
Add envmodules option and update directory option (#27)
Browse files Browse the repository at this point in the history
* Add envmodules option and update directory option

* Make envmodules_configfile names consistent
  • Loading branch information
percyfal authored Jan 10, 2025
1 parent 5e97347 commit f1e9111
Showing 1 changed file with 76 additions and 3 deletions.
79 changes: 76 additions & 3 deletions src/nbis/snakemake.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import re
from pathlib import Path
from typing import Callable

import click
Expand Down Expand Up @@ -218,14 +219,86 @@ def test_callback(
)


def directory_option():
def directory_option(
default: str = None,
expose_value: bool = True,
) -> Callable[[FC], FC]:
"""Add snakemake directory option"""

def directory_callback(
ctx: click.core.Context, # pylint: disable=unused-argument
param: click.core.Option, # pylint: disable=unused-argument
value: str,
) -> str:
"""Directory callback"""
env = ctx.ensure_object(Environment)
if value is None:
try:
value = env.home
except AttributeError:
pass
if value is None:
return []
return ["--directory", str(value)]

return click.option(
"--directory/-d",
"-d",
"--directory",
help=(
"Specify working directory (relative paths in the snakefile will "
"use this as their origin). (default: project directory)"
"use this as their origin). (default: project directory). Will fallback"
"on project environment home directory if available."
),
default=default,
callback=directory_callback,
expose_value=expose_value,
type=click.Path(exists=True, file_okay=False, dir_okay=True),
)


def envmodules_configfile_option(
default: str = None,
envmodules_configfile: str = "envmodules.yaml",
expose_value: bool = True,
) -> Callable[[FC], FC]:
"""Add snakemake envmodules yaml config file option."""

def envmodules_configfile_callback(
ctx: click.core.Context, # pylint: disable=unused-argument
param: click.core.Option, # pylint: disable=unused-argument
value: str,
) -> str:
"""Envmodules config callback."""
env = ctx.ensure_object(Environment)
try:
directory = ctx.params.get("directory", env.home)
except AttributeError:
directory = None
if value is not None:
configfile = value
if directory is not None:
configfile = Path(directory) / value
return ["--configfile", str(configfile)]
try:
value = env.home
except AttributeError:
value = None
if value is not None:
configfile = Path(value) / envmodules_configfile
else:
configfile = Path(envmodules_configfile)
if not configfile.exists():
logger.warning("envmodules config file %s does not exist", configfile)
return []
return ["--configfile", str(configfile)]

return click.option(
"--envmodules-configfile",
help=("Specify environment modules configuration file."),
default=default,
callback=envmodules_configfile_callback,
expose_value=expose_value,
type=click.Path(exists=True, file_okay=True, dir_okay=False),
)


Expand Down

0 comments on commit f1e9111

Please sign in to comment.