forked from demisto/content
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspell_checker.py
More file actions
75 lines (56 loc) · 2.3 KB
/
spell_checker.py
File metadata and controls
75 lines (56 loc) · 2.3 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
import sys
import yaml
import argparse
from spellchecker import SpellChecker
from demisto_sdk.commands.common.tools import print_error
DISPLAYABLE_LINES = [
"description",
"name",
"display",
"comment"
]
SCRIPT_ARGS = 'scriptarguments'
def check_yaml(spellchecker, yml_info, unknown_words):
for key, value in yml_info.items():
if key in DISPLAYABLE_LINES and isinstance(value, str):
for word in value.split():
if word.isalpha() and spellchecker.unknown([word]):
unknown_words.add(word)
else:
if isinstance(value, dict):
if key != SCRIPT_ARGS:
check_yaml(spellchecker, value, unknown_words)
elif isinstance(value, list):
for sub_list in value:
if isinstance(sub_list, dict):
check_yaml(spellchecker, sub_list, unknown_words)
def check_md_file(spellchecker, md_data, unknown_words):
for line in md_data:
for word in line.split():
if word.isalpha() and spellchecker.unknown([word]):
unknown_words.add(word)
def spell_checker(path, is_md=False):
unknown_words = set([])
spellchecker = SpellChecker()
spellchecker.word_frequency.load_text_file('Tests/known_words.txt')
if is_md:
with open(path, 'r') as md_file:
md_data = md_file.readlines()
check_md_file(spellchecker, md_data, unknown_words)
else:
with open(path, 'r') as yaml_file:
yml_info = yaml.safe_load(yaml_file)
check_yaml(spellchecker, yml_info, unknown_words)
if unknown_words:
print_error(u"Found the problematic words:\n{}".format('\n'.join(unknown_words)))
return 1
print("No problematic words found")
return 0
if __name__ == "__main__":
description = """Run spell check on a given yml/md file. """
parser = argparse.ArgumentParser(description=description, formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-p", "--path", help="Specify path of yml/md file", required=True)
parser.add_argument("-i", "--isMD", help="Whether the path is to a yml file or an md.", action='store_true')
args = parser.parse_args()
sys.exit(spell_checker(args.path, args.isMD))