Skip to content

Commit

Permalink
Env var CMDICT_HISTORY with any value will avoid history recording …
Browse files Browse the repository at this point in the history
…in YAML (#173)
  • Loading branch information
jiedxu authored Apr 27, 2024
1 parent 5e2671b commit d49575c
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 194 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
python-version: "3.8"
- uses: snok/[email protected]
with:
version: 1.5.1
version: "1.5.1"
virtualenvs-create: true
virtualenvs-in-project: true
- name: Build wheels
Expand Down Expand Up @@ -56,7 +56,7 @@ jobs:
python-version: "3.8"
- uses: snok/[email protected]
with:
version: 1.5.1
version: "1.5.1"
virtualenvs-create: true
virtualenvs-in-project: true
- name: Increase version by one patch
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ jobs:
pytest --version
- 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
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,4 @@ cython_debug/
.idea/
db/stardict.csv
**/.DS_Store
.vscode/

.vscode/settings.json
54 changes: 28 additions & 26 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.1.0
hooks:
- id: check-toml
- id: check-yaml
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
language_version: python3

- repo: https://github.com/pycqa/flake8
rev: 3.8.2
hooks:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: check-toml
- id: check-yaml
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: "22.6.0"
hooks:
- id: black
language_version: python3
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.12.0
hooks:
- id: reorder-python-imports
args:
- --application-directories=src
- repo: https://github.com/pycqa/flake8
rev: "4.0.1"
hooks:
- id: flake8
additional_dependencies:
- 'flake8-builtins'
- 'flake8-docstrings'
- 'flake8-import-order'
- 'pycodestyle'
- 'pydocstyle'

- repo: https://github.com/terrencepreilly/darglint
rev: v1.8.1
hooks:
- id: darglint
- 'flake8-builtins'
- 'flake8-docstrings'
- 'pycodestyle'
- 'pydocstyle'
- repo: https://github.com/terrencepreilly/darglint
rev: v1.8.1
hooks:
- id: darglint
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Attach",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
}
}
]
}
281 changes: 151 additions & 130 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cmdict"
version = "0.1.9"
version = "0.1.10"
description = "A command line dictionary toolset."
authors = ["zequnyu <[email protected]>", "edxu96 <[email protected]>"]
license = "GPL-3.0"
Expand Down Expand Up @@ -35,6 +35,7 @@ optional = true
pytest = ">=7,<9"
pytest-cov = ">=2.9,<6.0"
pytest-xdist = ">=1.32,<4.0"
debugpy = ">=1.8.1"

[tool.poetry.group.check]
optional = true
Expand Down
44 changes: 29 additions & 15 deletions src/cmdict/ecdict_connector.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
"""Database Connector."""
import os
import pathlib
import sqlite3
from os import getenv
from pathlib import Path
from sqlite3 import connect
from sqlite3 import Error
from typing import Optional
from typing import Union

from loguru import logger

from cmdict.history import record

_path = os.path.join(str(pathlib.Path(__file__).parent), "data", "stardict.db")
_key_names = (
_HISTORY = True if getenv("CMDICT_HISTORY", None) is None else False
"""Whether to record queried words in a YAML file.
`True` if no env var is called `CMDICT_HISTORY`.
"""
_PATH = Path(__file__).parent / "data" / "stardict.db"
_KEY_NAMES = (
"id",
"word",
"sw",
Expand All @@ -32,20 +40,23 @@ class ECDICTConnector:
"""

def __init__(self, path=_path):
def __init__(self, path: Optional[Union[str, Path]] = _PATH):
"""Initialize database Connector.
Args:
path (str, optional): Path to the database file.
Defaults to ``_path``.
path: Path to the database file. Defaults to be ``stardict``.
Raises:
ValueError: When the database file is missing or invalid.
"""
if pathlib.Path(path).is_file() and path.endswith(".db"):
_path = Path(path) if isinstance(path, str) else path

if _path.is_file() and (_path.suffix == ".db"):
self._conn = ECDICTConnector._init_conn(path)
else:
raise ValueError("Database file is missing or invalid.")
raise ValueError(
f'Database file at "{_path}" is missing or invalid.'
)

@staticmethod
def _init_conn(path):
Expand All @@ -58,8 +69,8 @@ def _init_conn(path):
sqlite3.Connection: Connection object to the database.
"""
try:
return sqlite3.connect(path)
except sqlite3.Error:
return connect(path)
except Error:
logger.exception("SQLite DB connection failed.")

def query(self, word):
Expand Down Expand Up @@ -90,14 +101,17 @@ def query(self, word):
query = "select * from stardict where word = ?"
cursor = self._conn.cursor()
cursor.execute(query, (word,))
record(word)

if _HISTORY:
record(word)

res = cursor.fetchone()

return (
dict([(x, y) for x, y in zip(_key_names, res)])
dict([(x, y) for x, y in zip(_KEY_NAMES, res)])
if res
else None
)

except sqlite3.Error:
except Error:
logger.exception("SQLite DB search failed.")
6 changes: 3 additions & 3 deletions src/cmdict/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import pathlib
from typing import Optional

from loguru import logger
import yaml
from loguru import logger

_path = os.path.join(
_PATH = os.path.join(
str(pathlib.Path(__file__).parent), "data", ".history.yaml"
)


def record(word: str, path: Optional[str] = _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
Expand Down
12 changes: 7 additions & 5 deletions src/cmdict/run_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import zipfile

import click
from colorama import Fore, Style
from colorama import init as _init_colorama
import requests
import yaml
from colorama import Fore
from colorama import init as _init_colorama
from colorama import Style
from tqdm import tqdm
from trogon import tui
import yaml

from cmdict.ecdict_connector import ECDICTConnector
from cmdict.pdf_tools import extract_words, PDF_FEATURES
from cmdict.pdf_tools import extract_words
from cmdict.pdf_tools import PDF_FEATURES
from cmdict.txt_tools import scan_words

DB_URL = "https://github.com/skywind3000/ECDICT/releases/download/1.0.28/ecdict-sqlite-28.zip" # noqa: E501
Expand Down Expand Up @@ -115,7 +117,7 @@ def search(words):
"""
if _valid_db_exists():
db_engine = ECDICTConnector()
for i, word in enumerate(words):
for word in words:
_echo_item(word, db_engine.query(word))
else:
_echo_warn_download()
Expand Down
8 changes: 8 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

# Development Guide

Connect to Python debugger in VS Code:

```python
poetry run python -m debugpy --listen 5678 --wait-for-client src/cmdict/__main__.py search banana
```
10 changes: 4 additions & 6 deletions tests/cmdict/test_pdf_tools.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""Test functions for extracting highlights in PDF files."""
from cmdict.pdf_tools import (
_check_contain,
_fix_hyphen_broken,
_get_color_name,
extract_words,
)
from cmdict.pdf_tools import _check_contain
from cmdict.pdf_tools import _fix_hyphen_broken
from cmdict.pdf_tools import _get_color_name
from cmdict.pdf_tools import extract_words


def test_get_color_name_func():
Expand Down
7 changes: 5 additions & 2 deletions tests/cmdict/test_run_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
import os
import pathlib

from click.testing import CliRunner
import yaml
from click.testing import CliRunner

from cmdict.run_script import cli, extract, scan, search
from cmdict.run_script import cli
from cmdict.run_script import extract
from cmdict.run_script import scan
from cmdict.run_script import search

_path_yaml = os.path.join(
str(pathlib.Path(__file__).parents[2]), "src/cmdict/data/.extraction.yaml"
Expand Down

0 comments on commit d49575c

Please sign in to comment.