Skip to content

Commit

Permalink
Fix tool import issues
Browse files Browse the repository at this point in the history
  • Loading branch information
percyfal committed Mar 8, 2024
1 parent 2e4c3af commit c4c9b01
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ commands =
[testenv:project]
allowlist_externals =
git
sed
touch
changedir = {envtmpdir}/project_foo
setenv =
Expand All @@ -109,7 +108,6 @@ deps =
wheel
commands:
nbis-admin init .
sed -i -e "s/disable=fixme/disable=fixme,import-error,too-few-public-methods/g" .pre-commit-config.yaml
git init -b main .
git add -f .
{envpython} -m pip install -e . --no-deps
Expand Down
15 changes: 9 additions & 6 deletions src/nbis/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@
class NbisCLI(click.MultiCommand):
"""NBIS CLI multicommand"""

module = "nbis.commands"
cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), "commands"))

def list_commands(self, ctx):
rv = []
for filename in os.listdir(cmd_folder):
data = []
for filename in os.listdir(self.cmd_folder):
if filename.endswith(".py") and not filename.startswith("__"):
rv.append(filename[:-3])
rv.sort()
return rv
data.append(filename[:-3])
data.sort()
return data

def get_command(self, ctx, cmd_name):
mod = __import__(f"nbis.commands.{cmd_name}", None, None, ["main"])
mod = __import__(f"{self.module}.{cmd_name}", None, None, ["main"])
return mod.main


Expand Down
4 changes: 1 addition & 3 deletions src/nbis/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,6 @@ def tool(env, tool_name, show, **kw):
kw["name"] = pathlib.Path("src") / env.config.project_name / "tools"
kw["tool_name"] = tool_name
kw["project_name"] = env.config.project_name
kw["project_us"] = tool_name.replace("-", "_")
kw["project_cc"] = _snake_to_caml(kw["project_us"])
outdir = pathlib.Path(kw["name"])
tpl = "src/python_module/tools/tool.py.j2"
tools_tpl = "src/python_module/commands/tools.py.j2"
Expand All @@ -220,7 +218,7 @@ def tool(env, tool_name, show, **kw):
templates.add_template(cmd_path, tools_tpl, **kw)
click.echo(
f"Add {env.config.project_name}-{tool_name} = "
'"{env.config.project_name}.tools.{tool_name}:cli" to '
f'"{env.config.project_name}.tools.{tool_name}:cli" to '
"pyproject.toml and reinstall"
)

Expand Down
10 changes: 5 additions & 5 deletions src/nbis/templates/src/python_module/cli.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), "commands")
class {{ ProjectName }}Cli(click.MultiCommand):
"""{{ project_name }} CLI class"""

module = "{{ project_name }}.commands"
cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), "commands"))

def list_commands(self, ctx):
data = []
for filename in os.listdir(cmd_folder):
for filename in os.listdir(self.cmd_folder):
if filename.endswith(".py") and not filename.startswith("__"):
data.append(filename[:-3])
data.sort()
return data

def get_command(self, ctx, cmd_name):
try:
mod = __import__(f"{{ python_module }}.commands.{cmd_name}", None, None, ["main"])
except ImportError:
return None
mod = __import__(f"{self.module}.{cmd_name}", None, None, ["main"])
return mod.main


Expand Down
23 changes: 14 additions & 9 deletions src/nbis/templates/src/python_module/commands/tools.py.j2
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
"""{{ project_name }} tools and scripts.

Tools that also can be called as standalone scripts by adding prefix
'{{ project_name }}-' to name in list below.
'{{ project_name.replace("_", "-") }}-' to name in list below.

"""

import logging
import os

import click
from {{ project_us }}.cli import CONTEXT_SETTINGS
from {{ project_us }}.cli import {{ project_cc }}Cli

from {{ project_name }}.cli import CONTEXT_SETTINGS
{% set ProjectName = project_name.replace("_", " ").title().replace(" ", "") -%}
from {{ project_name }}.cli import {{ ProjectName }}Cli

__shortname__ = __name__.rsplit(".", maxsplit=1)[-1]


logger = logging.getLogger(__name__)


class {{ project_cc }}ToolsCli({{ project_cc }}Cli):
"""{{ project_cc }} tools command line interface"""
class {{ ProjectName }}ToolsCli({{ ProjectName }}Cli):
"""{{ project_name }} tools command line interface"""

cmd_folder = os.path.abspath(
os.path.join(os.path.dirname(__file__), os.pardir, "tools")
)
module = "{{ project_us }}.tools"
module = "{{ project_name }}.tools"


@click.command(
cls={{ project_cc }}ToolsCli, context_settings=CONTEXT_SETTINGS, help=__doc__, name="tools"
cls={{ ProjectName }}ToolsCli,
context_settings=CONTEXT_SETTINGS,
help=__doc__,
name="tools",
)
def cli():
"""{{ project_cc }} tools and scripts"""
def main():
"""{{ project_name }} tools and scripts"""
logger.info(__shortname__)
2 changes: 1 addition & 1 deletion src/nbis/templates/src/python_module/tools/tool.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ logger = logging.getLogger(__name__)
@click.command(help=__doc__, name=__shortname__)
@click.version_option(version=__version__)
@click.pass_context
def cli(ctx):
def main(ctx):
"""{{ tool_name }} docstring"""
ctx.ensure_object(dict)
ctx.obj["VERSION"] = __version__
Expand Down

0 comments on commit c4c9b01

Please sign in to comment.