aboutsummaryrefslogtreecommitdiff
path: root/files/.task/hooks/on-add-spell-check.py
blob: 8da631ab326ff66a014022423429102b2e37e8c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
import os
import re
import sys
from glob import glob
from os.path import isfile, splitext

try:
    from hunspell import HunSpell
    from tasklib import Task
except ImportError as e:
    print(e)
    sys.exit(0)

DICT_PATH = "/usr/share/hunspell/"
LANGS = ["en_US", "ru_RU"]
ENV_SKIP = "TW_IGNORE_SPELL"


def spellcheck(text):
    spells = []
    for dic in glob(DICT_PATH + "*.dic"):
        aff = splitext(dic)[0] + ".aff"
        if isfile(dic) and isfile(aff):
            spells.append(HunSpell(dic, aff))

    if len(spells) == 0:
        return

    errors = []

    for word in re.findall(r"\w+", text):
        ok = False
        for spell in spells:
            if spell.spell(word):
                ok = True
                break
        if not ok:
            errors.append(word)

    return errors


def should_run_spellcheck(task):
    if len(sys.argv) > 1:
        opts = dict(arg.split(":", 1) for arg in sys.argv[1:])
        command = opts["command"]
        return command in ["add", "append", "log", "modify", "prepend"]
    return False


task = Task.from_input()
if should_run_spellcheck(task):
    errors = spellcheck(task["description"])
    if len(errors) > 0:
        print("Spell errors:", ", ".join(errors))
        if ENV_SKIP not in os.environ:
            sys.exit(1)
print(task.export_data())