Skip to content

Commit

Permalink
feat(cli): add command line interface for nlp4ss
Browse files Browse the repository at this point in the history
  • Loading branch information
entelecheia committed Jul 9, 2024
1 parent 7396651 commit ba25218
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/nlp4ss/__cli__.py
Original file line number Diff line number Diff line change
@@ -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()
13 changes: 13 additions & 0 deletions src/nlp4ss/__init__.py
Original file line number Diff line number Diff line change
@@ -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__
1 change: 1 addition & 0 deletions src/nlp4ss/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.0.0"
6 changes: 6 additions & 0 deletions src/nlp4ss/conf/about/nlp4ss.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_open_link_: True
name: "NLP for Social Science"
authors: "Young Joon Lee <entelecheia@hotmail.com>"
description: "A course on applying natural language processing (NLP) techniques for social science research."
homepage: "https://nlp4ss.entelecheia.ai"
license: "CC-BY-4.0"
Empty file added src/nlp4ss/py.typed
Empty file.
22 changes: 22 additions & 0 deletions tests/nlp4ss/test_cli.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions tests/nlp4ss/test_init.py
Original file line number Diff line number Diff line change
@@ -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<git hash>]
"""
version = get_version()
# check version format
assert version.count(".") in range(2, 5)

0 comments on commit ba25218

Please sign in to comment.