diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3bd420e..2922acb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,35 +4,49 @@ on: push: branches: [ master ] tags: - - "*" + - "*" paths-ignore: - - 'README.md' - - 'LICENSE' - - '.gitignore' - - 'codecov.yml' - - '.python-version' - - 'poetry.toml' + - 'README.md' + - 'LICENSE' + - '.gitignore' + - 'codecov.yml' + - '.python-version' + - 'poetry.toml' pull_request: branches: [ master ] paths-ignore: - - 'README.md' - - 'LICENSE' - - '.gitignore' - - 'codecov.yml' - - '.python-version' - - 'poetry.toml' + - 'README.md' + - 'LICENSE' + - '.gitignore' + - 'codecov.yml' + - '.python-version' + - 'poetry.toml' workflow_dispatch: schedule: - - cron: '30 5 * * 1' + - cron: '30 5 * * 1' concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true jobs: - test: - name: pre-commit and test + pre-commit: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@master + - uses: actions/setup-python@v2 + with: + python-version: 3.7 + - name: Install pre-commit + run: | + pip install pre-commit==2.9.2 + - name: Run pre-commit + run: | + pre-commit install + pre-commit run --all-files + unit-test: runs-on: ${{ matrix.os }} + needs: pre-commit strategy: matrix: python-version: [3.7, 3.8] @@ -42,12 +56,10 @@ jobs: shell: bash steps: - uses: actions/checkout@master - - name: Install Python - uses: actions/setup-python@v2 + - uses: actions/setup-python@v2 with: python-version: ${{ matrix.python-version }} - - name: Install latest Poetry - uses: snok/install-poetry@v1.3.3 + - uses: snok/install-poetry@v1.3.3 with: virtualenvs-create: true virtualenvs-in-project: true @@ -58,20 +70,65 @@ jobs: path: ~/.cache key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }} - name: Install dependencies - run: poetry install --no-interaction --no-root --with dev,pdf + run: | + poetry install --no-interaction --no-root --with dev,pdf - name: Install library run: poetry install --no-interaction - run: | source $VENV pytest --version - - name: Run pre-commit - run: | - poetry run pre-commit install - poetry run pre-commit run --all-files - - name: Run tests + - name: Run unit tests and Codecov run: poetry run pytest -n auto --cov=./ --cov-report=xml - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 + build: + runs-on: ubuntu-22.04 + needs: unit-test + steps: + - uses: actions/checkout@master + - uses: actions/setup-python@v2 + with: + python-version: 3.7 + - uses: snok/install-poetry@v1.3.3 + with: + virtualenvs-create: true + virtualenvs-in-project: true + - name: Build wheels + run: | + poetry build + - name: Upload wheel and tar ball + uses: actions/upload-artifact@v3 + with: + name: dist + path: ./dist/* + integration-test: + runs-on: ${{ matrix.os }} + needs: build + strategy: + matrix: + python-version: [3.7, 3.8] + os: [ubuntu-22.04, windows-2022, macos-11] + defaults: + run: + shell: bash + steps: + - uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Download wheel and tar ball built previously + uses: actions/download-artifact@master + with: + name: dist + path: ./dist/ + - name: Install wheel + run: | + python -m pip install dist/cmdict-*py3-none-any.whl + - name: Run module as script + run: | + python -m cmdict --help + - name: Run module as executable + run: | + cmdict --help publish: if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags') && github.repository == 'pastydev/cmdict' environment: @@ -80,23 +137,12 @@ jobs: permissions: id-token: write runs-on: ubuntu-22.04 - needs: test + needs: integration-test steps: - - uses: actions/checkout@master - - name: Install dependecies - uses: actions/setup-python@v2 - with: - python-version: 3.7 - cache: pip - - name: Install latest Poetry - uses: snok/install-poetry@v1.3.3 - - name: Build wheels - run: | - poetry build - - name: Upload wheel and tar ball as artifacts - uses: actions/upload-artifact@v3 - with: - name: dist - path: ./dist/* - - name: Publish to PyPI - uses: pypa/gh-action-pypi-publish@release/v1 + - name: Download wheel and tar ball built previously + uses: actions/download-artifact@master + with: + name: dist + path: ./dist/ + - name: Publish to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 diff --git a/poetry.lock b/poetry.lock index 2b41ec0..d288dd6 100644 --- a/poetry.lock +++ b/poetry.lock @@ -759,4 +759,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = ">=3.7.0" -content-hash = "1efd408e6d25c44ccd717de937822542d039ee151be662f3fa00e8f35b806806" +content-hash = "636d78a789329b36e8afe194394590fb59e291271a4703433c1b4d10911af2a0" diff --git a/pyproject.toml b/pyproject.toml index 56a9065..0566bc5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,6 +20,7 @@ colorama = ">=0.4.3" requests = ">=2.24.0" tqdm = ">=4.48.0" pyyaml = ">=5.3.1" +virtualenv = ">=20.10.0" [tool.poetry.group.pdf] optional = true @@ -31,11 +32,16 @@ PyMuPDF = "1.22.3" optional = true [tool.poetry.group.dev.dependencies] -pre-commit = "^2.9.2" pytest = "^7.0.0" pytest-cov = "^2.9.0" pytest-xdist = "^1.32.0" +[tool.poetry.group.check] +optional = true + +[tool.poetry.group.check.dependencies] +pre-commit = "2.21.0" + [tool.black] line-length = 79 include = '\.pyi?$' diff --git a/src/cmdict/__init__.py b/src/cmdict/__init__.py index a7b6052..c2902e0 100644 --- a/src/cmdict/__init__.py +++ b/src/cmdict/__init__.py @@ -1,4 +1,4 @@ -"""Main cmdict module.""" +"""Init, when module is imported.""" from cmdict import run_script __all__ = ["run_script"] diff --git a/src/cmdict/__main__.py b/src/cmdict/__main__.py new file mode 100644 index 0000000..240451b --- /dev/null +++ b/src/cmdict/__main__.py @@ -0,0 +1,5 @@ +"""Execute command line interface, when module is run as a script.""" +from cmdict.run_script import cli + + +cli() diff --git a/src/cmdict/history.py b/src/cmdict/history.py index dc553cc..20de5e4 100644 --- a/src/cmdict/history.py +++ b/src/cmdict/history.py @@ -1,6 +1,7 @@ -"""Functions for recording and reading user history.""" +"""Function to record and read user history.""" import os import pathlib +from typing import Optional from loguru import logger import yaml @@ -10,7 +11,7 @@ ) -def record(word, path=_path): +def record(word: str, path: Optional[str] = _path): """Append queried word in a yaml file in lowercase format. If the path is directed to yaml file, there will be three @@ -19,28 +20,29 @@ def record(word, path=_path): file does exist, we read its content first. Args: - word (str): to be appended at the end of the yaml file. - path (str): to the yaml file. + word: to be appended at the end of the yaml file. + path: to the yaml file. Raises: ValueError: when the path is not to a yaml file. """ if not path.endswith(".yaml"): - raise ValueError(f"{path} is not a path to yaml file.") + raise ValueError(f'"{path}" is not a yaml file.') if not pathlib.Path(path).is_file(): with open(path, "w") as f: yaml.safe_dump([word], f) else: + history: list[str] = None with open(path, "r") as f: try: - hist = yaml.safe_load(f) + history = yaml.safe_load(f) except yaml.YAMLError as exc: logger.exception(exc) - if hist is None: + if history is None: with open(path, "w") as f: yaml.safe_dump([word], f) - elif word not in hist: + elif word not in history: with open(path, "a") as f: yaml.safe_dump([word], f)