Skip to content

Commit e7c5458

Browse files
committed
pylint!
1 parent 6a90cd7 commit e7c5458

7 files changed

+71
-40
lines changed

.pylintrc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ indent-after-paren=4
337337
indent-string=' '
338338

339339
# Maximum number of characters on a single line.
340-
max-line-length=100
340+
max-line-length=120
341341

342342
# Maximum number of lines in a module.
343343
max-module-lines=1000
@@ -429,7 +429,15 @@ disable=raw-checker-failed,
429429
useless-suppression,
430430
deprecated-pragma,
431431
use-symbolic-message-instead,
432-
missing-function-docstring
432+
missing-function-docstring,
433+
fixme,
434+
no-else-return,
435+
invalid-name,
436+
redefined-outer-name,
437+
too-many-locals, # TODO: remove these next 3 after refactoring match_abbrevs_to_phrases
438+
too-many-branches,
439+
too-many-nested-blocks
440+
433441

434442

435443
# Enable the message, report, category or checker with the given id(s). You can

find_suggested_phrases.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import json
1+
#!/usr/bin/env python
2+
3+
"""
4+
Script to turn a corpus of text into a list of suggested phrases
5+
and abbreviations.
6+
"""
7+
28
import os
39
from typing import List, Dict, Optional, Set, Tuple
410
from collections import Counter, namedtuple
@@ -67,14 +73,8 @@ def try_plural_of_word(word : str) -> str:
6773
return word[:-1] + "ies"
6874
if word[-1] == "f":
6975
return word[:-1] + "ves"
70-
if word[-1] == "o":
71-
return word + "es"
72-
if word[-1] == "h":
73-
return word + "es"
74-
if word[-1] == "x":
76+
if word[-1] in "ohxz":
7577
return word + "es"
76-
if word[-1] == "z":
77-
return word[:-1] + "zes"
7878
return word + "s"
7979

8080

@@ -219,7 +219,7 @@ def load_corpus() -> List[str]:
219219
all_lines = []
220220
for filename in os.listdir(corpus_path):
221221
if filename.endswith(".txt"):
222-
with open(corpus_path + filename, 'r') as f:
222+
with open(corpus_path + filename, 'r', encoding="utf8") as f:
223223
all_lines.extend(f.readlines())
224224
return all_lines
225225

@@ -267,7 +267,7 @@ def fix_grammer(text: str) -> str:
267267

268268
def save_shortcuts(shortcuts: Dict[str, str]) -> None:
269269
"""Save the shortcuts to a yaml file"""
270-
with open("output/suggested_shortcuts.yaml", 'w') as f:
270+
with open("output/suggested_shortcuts.yaml", 'w', encoding="utf8") as f:
271271
yaml.dump(shortcuts, f, default_flow_style=False)
272272

273273

generate_autokeys.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Script to turn the json file of shortcuts.yaml into config files for Autokey
5+
"""
6+
7+
18
import json
2-
import yaml
39
import os
10+
import yaml
411

512

613
def make_safe_filename_from_string(s : str) -> str:
@@ -21,10 +28,10 @@ def create_autokey_config_for_abbrev(phrase : str, abbrev : str) -> None:
2128

2229
file_extensions = ["py"] # add . as a word character for any of these abbrevs.
2330
if abbrev in file_extensions:
24-
extra_word_chars = "\."
31+
extra_word_chars = r"\."
2532
else:
2633
extra_word_chars = ""
27-
words_regex = "[\\w\\t'{}\-&\+]".format(extra_word_chars) # what should not trigger a substitution
34+
words_regex = rf"[\w\t'{extra_word_chars}\-&\+]" # what should not trigger a substitution
2835

2936
result_path = "output/autokey_phrases/"
3037

@@ -39,10 +46,10 @@ def create_autokey_config_for_abbrev(phrase : str, abbrev : str) -> None:
3946

4047
abbrevs = [a.strip() for a in abbrev.split(",")]
4148

42-
with open(result_path + f"{name}.txt", 'w') as f:
49+
with open(result_path + f"{name}.txt", 'w', encoding="utf8") as f:
4350
f.write(phrase)
4451

45-
with open(result_path + f".{name}.json", 'w') as f:
52+
with open(result_path + f".{name}.json", 'w', encoding="utf8") as f:
4653
config = {
4754
"usageCount": 0,
4855
"omitTrigger": False,
@@ -76,7 +83,9 @@ def create_autokey_config_for_abbrev(phrase : str, abbrev : str) -> None:
7683

7784

7885
if __name__ == "__main__":
79-
shortcuts = yaml.load(open("output/shortcuts.yaml", 'r'), Loader=yaml.FullLoader)
8086

81-
for phrase, abbrev in shortcuts.items():
82-
create_autokey_config_for_abbrev(phrase, abbrev)
87+
with open("output/shortcuts.yaml", 'r', encoding="utf8") as file:
88+
shortcuts = yaml.load(file, Loader=yaml.FullLoader)
89+
90+
for phrase, abbrev in shortcuts.items():
91+
create_autokey_config_for_abbrev(phrase, abbrev)

parse_slack.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
"""
24
Parses a slack export folder, cleans up the slack messages found for your user
35
and then exports them into a file in corpus.
@@ -14,12 +16,12 @@
1416
def extract_slack_msgs(export_root_path : str, username : str) -> List[str]:
1517
"""extract all text from the users messages as a list of strings"""
1618
file_strs : List[str] = []
17-
for (dirpath, dirnames, filenames) in os.walk(export_root_path):
19+
for (dirpath, _, filenames) in os.walk(export_root_path):
1820
file_strs += [os.path.join(dirpath, file) for file in filenames]
1921

2022
msgs : List[str] = []
2123
for file_str in file_strs:
22-
with open(file_str) as file:
24+
with open(file_str, encoding="utf8") as file:
2325
channel = json.load(file)
2426

2527
for msg in channel:
@@ -55,6 +57,6 @@ def clean_slack_msg(text: str) -> str:
5557
texts = [clean_slack_msg(m) for m in msgs]
5658

5759
# write each msg as a line to a file
58-
with open("data/corpus/slack_msgs.txt", 'w') as f:
60+
with open("data/corpus/slack_msgs.txt", 'w', encoding="utf8") as f:
5961
for text in texts:
6062
f.write(text + "\n")

preset_abbrevs.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Abbrevs you definitely want in your set.
5+
you can set None if you want to pick one automatically
6+
you're responsible for creating any plurals here
7+
"""
8+
19

2-
# Abbrevs you definitely want in your set.
3-
# you can set None if you want to pick one automatically
4-
# you're responsible for creating any plurals here
510
PRESET_ABBREVS = {
611
"right now": "rn",
712
"ansible": "ans",

run_tests.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# exit when any command fails
55
set -e
66

7-
# linter
8-
# pylint $(git ls-files '*.py')
9-
107
# tests
11-
pytest --cov
8+
pytest --cov
9+
10+
# linter
11+
pylint $(git ls-files '*.py')

test_compress.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
#!/usr/bin/env python
12

3+
"""
4+
Tests for the compress repo. This is best executed from `run_tests.sh`
5+
in order to keep parity with what's running in CI.
6+
"""
7+
import json
28
from typing import Counter
3-
from preset_abbrevs import BLACKLIST, PRESET_ABBREVS
4-
from parse_slack import extract_slack_msgs, clean_slack_msg
9+
import os
10+
511
from find_suggested_phrases import (
612
get_plural,
713
get_possible_abbrevs,
@@ -13,8 +19,9 @@
1319
fix_grammer,
1420
)
1521
from generate_autokeys import create_autokey_config_for_abbrev
16-
import os
17-
import json
22+
from parse_slack import extract_slack_msgs, clean_slack_msg
23+
from preset_abbrevs import BLACKLIST, PRESET_ABBREVS
24+
1825

1926

2027
def test_presets():
@@ -194,15 +201,15 @@ def test_autokey_configs():
194201
main_path = result_path + "testbecause.txt"
195202
config_path = result_path + ".testbecause.json"
196203

197-
with open(main_path, 'r') as f:
204+
with open(main_path, 'r', encoding="utf8") as f:
198205
out = f.readline()
199206
assert out == "test_because"
200207

201-
with open(config_path, 'r') as f:
208+
with open(config_path, 'r', encoding="utf8") as f:
202209
out = json.load(f)
203210
expected = {'usageCount': 0, 'omitTrigger': False,
204211
'prompt': False, 'description': 'test_because', 'abbreviation':
205-
{'wordChars': "[\\w\\t'\-&\+]", 'abbreviations': ['bc'], 'immediate': False,
212+
{'wordChars': r"[\w\t'\-&\+]", 'abbreviations': ['bc'], 'immediate': False,
206213
'ignoreCase': True, 'backspace': True, 'triggerInside': False},
207214
'hotkey': {'hotKey': None, 'modifiers': []}, 'modes': [1],
208215
'showInTrayMenu': False, 'matchCase': True, 'filter':
@@ -216,15 +223,15 @@ def test_autokey_configs():
216223
main_path = result_path + "testi.txt"
217224
config_path = result_path + ".testi.json"
218225

219-
with open(main_path, 'r') as f:
226+
with open(main_path, 'r', encoding="utf8") as f:
220227
out = f.readline()
221228
assert out == "test_i"
222229

223-
with open(config_path, 'r') as f:
230+
with open(config_path, 'r', encoding="utf8") as f:
224231
out = json.load(f)
225232
expected = {'usageCount': 0, 'omitTrigger': False,
226233
'prompt': False, 'description': 'test_i', 'abbreviation':
227-
{'wordChars': "[\\w\\t'\-&\+]", 'abbreviations': ['i'], 'immediate': False,
234+
{'wordChars': r"[\w\t'\-&\+]", 'abbreviations': ['i'], 'immediate': False,
228235
'ignoreCase': True, 'backspace': True, 'triggerInside': False},
229236
'hotkey': {'hotKey': None, 'modifiers': []}, 'modes': [1],
230237
'showInTrayMenu': False, 'matchCase': True, 'filter':

0 commit comments

Comments
 (0)