Skip to content

Commit

Permalink
Merge pull request #53 from AndPuQing/feature-cli
Browse files Browse the repository at this point in the history
Use click as the command line interfaces
  • Loading branch information
visualDust authored Oct 30, 2023
2 parents f82a2e5 + 359d447 commit 3dab0c0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 91 deletions.
2 changes: 1 addition & 1 deletion neetbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def init(path=None, load=False, **kwargs) -> bool:
logger.err(f"Failed to create {config_file_path}: {e}")
return False
else: # config file exist:
logger.err((f"Config file already exists."))
logger.err((f"Config file already exists.")) # noqa
return False
else: # if load only
if not os.path.isfile(config_file_path): # but config file not exist
Expand Down
113 changes: 37 additions & 76 deletions neetbox/cli/parse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import argparse
import json
import os

import click

import neetbox
from neetbox.daemon._apis import get_status_of
Expand All @@ -12,17 +12,35 @@
logger = Logger("NEETBOX", style=_log_style) # builtin standalone logger


def handle_status(args):
@click.group()
@click.option(
"--verbose",
"-v",
help="set verbose flag to True",
default=False,
)
@click.pass_context
def main(ctx, verbose: bool):
ctx.ensure_object(dict)
"""This is NEETBOX cli"""
ctx.obj["verbose"] = verbose


@main.command()
def status():
"""Show the working tree status"""
_stat_json = None
try:
_stat_json = get_status_of()
print(json.dumps(_stat_json))
except Exception as e:
click.echo(json.dumps(_stat_json))
except Exception as e: # noqa
logger.log("Could not fetch data. Is there any project with NEETBOX running?")


def handle_init(args):
name = vars(args)["name"]
@main.command()
@click.option("--name", "-n", help="set project name", metavar="name", required=False)
def init(name: str):
"""initialize current folder as workspace and generate the config file from defaults"""
try:
if neetbox.init(name=name):
logger.skip_lines(2)
Expand All @@ -32,88 +50,31 @@ def handle_init(args):
logger.err(f"Failed to init here: {e}")


def handle_archive(args):
logger.err(
"This feature is not availiable. CLI Features are still under construction."
)
pass


def handle_list(args):
logger.err(
"This feature is not availiable. CLI Features are still under construction."
)
pass


def handle_dataset(args):
logger.err(
"This feature is not availiable. CLI Features are still under construction."
)
pass


parser = argparse.ArgumentParser(
prog="NEETBOX", description="This is NEETBOX cli", epilog="NEETBOX would help?"
)
parser.add_argument(
"-v", "--verbose", help="set verbose flag to True", action="store_true"
) # on/off flag

subparsers = parser.add_subparsers(
title="These are common NEEBOX commands used in various situations",
metavar="command",
)

status_parser = subparsers.add_parser("status", help="Show the working tree status")
status_parser.set_defaults(handle=handle_status)

init_parser = subparsers.add_parser(
"init",
help="initialize current folder as workspace and generate the config file from defaults",
)
init_parser.add_argument(
"--name",
"-n",
help="set project name",
metavar="name",
required=False,
default=None,
)
init_parser.set_defaults(handle=handle_init)

commit_parser = subparsers.add_parser(
"archive", help="Record changes to the repository"
)
commit_parser.add_argument(
@main.command()
@click.option(
"--message",
"-m",
help="Use the given <msg> as the commit message",
metavar="msg",
metavar="message",
required=True,
)
commit_parser.add_argument(
@click.option(
"--delete",
"-d",
help="Delete files after archive done.",
default=False,
required=False,
)
commit_parser.set_defaults(handle=handle_archive)

list_parser = subparsers.add_parser(
"list", help="List all the resources connected to NEETBOX"
)
list_parser.set_defaults(handle=handle_list)
def archive(message: str, delete: bool):
"""Record changes to the repository"""
pass


def parse():
args = parser.parse_args()
if hasattr(args, "handle"):
args.handle(args)
else:
parser.print_help()
@main.command()
def list():
"""List all the resources connected to NEETBOX"""
pass


if __name__ == "__main__":
parse()
main()
8 changes: 4 additions & 4 deletions neetbox/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
from enum import Enum
from inspect import isclass, iscoroutinefunction, isgeneratorfunction
from random import randint
from typing import *
from typing import Callable, Iterable, Optional, Union

from rich import print as rprint
from rich.panel import Panel

from neetbox.logging.formatting import *
from neetbox.logging.formatting import LogStyle, colored_text, styled_text
from neetbox.utils import format
from neetbox.utils.framing import *
from neetbox.utils.framing import get_caller_identity_traceback


class LogLevel(Enum):
Expand Down Expand Up @@ -346,7 +346,7 @@ def ok(self, *message, flag="OK"):
self.log(*message, prefix=flag, into_stdout=False, traceback=3)
return self

def debug(self, *message, flag=f"DEBUG"):
def debug(self, *message, flag="DEBUG"):
if _global_log_level >= LogLevel.DEBUG:
self.log(
*message,
Expand Down
15 changes: 5 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["VisualDust <[email protected]>"]
maintainers = [
"VisualDust <[email protected]>",
"PommesPeter <[email protected]>",
"PaperCube <[email protected]>"
"PaperCube <[email protected]>",
]


Expand Down Expand Up @@ -40,6 +40,7 @@ rich = ">=13"
numpy = "^1.26.0"
pillow = "^10.0.1"
pre-commit = ">=3.4.0"
click = "^8.1.7"

[tool.poetry.group.dev.dependencies]
pytest = "^6.0.0"
Expand All @@ -49,19 +50,13 @@ black = "^23.9.1"
isort = "^5.11.5"

[tool.poetry.extras]
torch = [
"torch",
"torchvision",
"torchaudio"
]
torch = ["torch", "torchvision", "torchaudio"]

transformer = [
"transformers",
]
transformer = ["transformers"]

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

[tool.poetry.scripts]
neet = 'neetbox.cli.parse:parse'
neet = 'neetbox.cli.parse:main'

1 comment on commit 3dab0c0

@vercel
Copy link

@vercel vercel bot commented on 3dab0c0 Oct 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.