From ba252181adecfc6c2de67ea0025a5eaae8e633df Mon Sep 17 00:00:00 2001 From: Young Joon Lee Date: Tue, 9 Jul 2024 09:13:23 +0900 Subject: [PATCH] feat(cli): add command line interface for nlp4ss --- src/nlp4ss/__cli__.py | 69 +++++++++++++++++++++++++++++++ src/nlp4ss/__init__.py | 13 ++++++ src/nlp4ss/_version.py | 1 + src/nlp4ss/conf/about/nlp4ss.yaml | 6 +++ src/nlp4ss/py.typed | 0 tests/nlp4ss/test_cli.py | 22 ++++++++++ tests/nlp4ss/test_init.py | 12 ++++++ 7 files changed, 123 insertions(+) create mode 100644 src/nlp4ss/__cli__.py create mode 100644 src/nlp4ss/__init__.py create mode 100644 src/nlp4ss/_version.py create mode 100644 src/nlp4ss/conf/about/nlp4ss.yaml create mode 100644 src/nlp4ss/py.typed create mode 100644 tests/nlp4ss/test_cli.py create mode 100644 tests/nlp4ss/test_init.py diff --git a/src/nlp4ss/__cli__.py b/src/nlp4ss/__cli__.py new file mode 100644 index 0000000..b62c0ac --- /dev/null +++ b/src/nlp4ss/__cli__.py @@ -0,0 +1,69 @@ +"""Command line interface for nlp4ss""" + +# Importing the libraries +import os +import subprocess +import webbrowser + +import click +import yaml + +from nlp4ss._version import __version__ + +__package_path__ = os.path.abspath(os.path.dirname(__file__)) +__package_name__ = os.path.basename(__package_path__) + + +def load_about() -> dict: + """ + Load the about.yml file. + """ + + about_path = os.path.join(__package_path__, f"conf/about/{__package_name__}.yaml") + if not os.path.isfile(about_path): + click.echo(f"The `{about_path}` was not found.") + raise click.Abort() + with open(about_path, "r", encoding="utf-8") as f: + return yaml.load(f, Loader=yaml.Loader) + + +__about__ = load_about() + + +@click.command() +@click.version_option(__version__) +@click.option("--build", "-b", is_flag=True, help="Build the book") +def main(build: bool = False) -> None: + """ + This is the cli function of the package. + It opens the book in the browser or builds the book. + """ + if build: + click.echo("Building the book...") + build_book() + else: + open_book() + + +def open_book() -> None: + """ + Open the book in the browser. + """ + click.echo("Opening the book...") + webbrowser.open_new_tab(__about__["homepage"]) + + +def build_book() -> None: + """ + Build the book. + """ + book_path = os.path.join(__package_path__, "book") + if not os.path.isdir(book_path): + click.echo("The book directory was not found.") + return + subprocess.run(["jupyter-book", "build", book_path], check=True) + + +# main function for the main module +if __name__ == "__main__": + main() diff --git a/src/nlp4ss/__init__.py b/src/nlp4ss/__init__.py new file mode 100644 index 0000000..27d5e3b --- /dev/null +++ b/src/nlp4ss/__init__.py @@ -0,0 +1,13 @@ +from nlp4ss._version import __version__ + + +def get_version() -> str: + """ + Get the version of the package. This is the cli function of the package. + If you want to check the version of the package you can use this function. + + + Returns: + The version of the package as a string e.g. "0.1.0" + """ + return __version__ diff --git a/src/nlp4ss/_version.py b/src/nlp4ss/_version.py new file mode 100644 index 0000000..6c8e6b9 --- /dev/null +++ b/src/nlp4ss/_version.py @@ -0,0 +1 @@ +__version__ = "0.0.0" diff --git a/src/nlp4ss/conf/about/nlp4ss.yaml b/src/nlp4ss/conf/about/nlp4ss.yaml new file mode 100644 index 0000000..f6e408d --- /dev/null +++ b/src/nlp4ss/conf/about/nlp4ss.yaml @@ -0,0 +1,6 @@ +_open_link_: True +name: "NLP for Social Science" +authors: "Young Joon Lee " +description: "A course on applying natural language processing (NLP) techniques for social science research." +homepage: "https://nlp4ss.entelecheia.ai" +license: "CC-BY-4.0" diff --git a/src/nlp4ss/py.typed b/src/nlp4ss/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/tests/nlp4ss/test_cli.py b/tests/nlp4ss/test_cli.py new file mode 100644 index 0000000..24d9be2 --- /dev/null +++ b/tests/nlp4ss/test_cli.py @@ -0,0 +1,22 @@ +""" +test cli module +""" +import subprocess +from typing import List, Tuple + + +def capture(command: List[str]) -> Tuple[bytes, bytes, int]: + proc = subprocess.Popen( + command, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + out, err = proc.communicate() + return out, err, proc.returncode + + +def test_cli() -> None: + """Test cli module""" + command = ["nlp4ss"] + out, err, exitcode = capture(command) + assert exitcode == 0 diff --git a/tests/nlp4ss/test_init.py b/tests/nlp4ss/test_init.py new file mode 100644 index 0000000..ec5cf8a --- /dev/null +++ b/tests/nlp4ss/test_init.py @@ -0,0 +1,12 @@ +from nlp4ss import get_version + + +def test_get_version() -> None: + """ + Test the get_version function. + + version format: major.minor.patch[.devN+g] + """ + version = get_version() + # check version format + assert version.count(".") in range(2, 5)