generated from entelecheia/course-template-i18n
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add command line interface for nlp4ss
- Loading branch information
1 parent
7396651
commit ba25218
Showing
7 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__ = "0.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |