Skip to content

Commit

Permalink
Merge 5d879d4 into 4a4decb
Browse files Browse the repository at this point in the history
  • Loading branch information
myslak71 committed Aug 6, 2019
2 parents 4a4decb + 5d879d4 commit bef65b9
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 194 deletions.
9 changes: 5 additions & 4 deletions README.md
Expand Up @@ -20,14 +20,15 @@ pip install flake8-koles

## Usage
```
flake8 --ignore-shorties 4 --censor-msg --lang=english,polish
flake8 --ignore-shorties 4 --censor-msg --lang=english,polish --ignore-swears=very,bad,words
```
##### Options
|OPTION | DEFAULT|DESCRIPTION |
| -------- |---|-------------|
|`--ignore-shorties`|0 |ignore bad words shorter or equal to the argument|
|`--censor-msg`|False |replace bad words not leading letters with `*` in error messages|
|`--lang`|english |use bad words from the selected languages|
|`--censor-msg`|False |replace swears not leading letters with `*` in error messages|
|`--ignore-shorties`|0 |ignore swears shorter or equal to the argument|
|`--ignore-swears`| |explicitly pass swears to ignore|
|`--lang`|english |use swears from the selected languages|

## Development notes

Expand Down
3 changes: 1 addition & 2 deletions flake8_koles/__about__.py
@@ -1,8 +1,7 @@
"""Package information module."""

__title__ = 'flake8-koles'
__description__ = 'Watch your language young lad! Swears and curses linter.'
__version__ = 'v0.1.2'
__version__ = '0.1.2'
__author__ = 'myslak71'
__author_email__ = 'myslak@protonmail.com'
__url__ = 'https://github.com/myslak71/flake8-koles'
Expand Down
62 changes: 37 additions & 25 deletions flake8_koles/checker.py
Expand Up @@ -3,7 +3,8 @@
import optparse
import os
import re
from typing import Generator, List, Set, Tuple
from itertools import chain, permutations
from typing import Generator, List, Tuple

import pkg_resources
from flake8.options.manager import OptionManager
Expand All @@ -24,7 +25,7 @@ class KolesChecker:
def __init__(self, tree: ast.Module, filename: str) -> None:
"""Initialize class values. Parameter `tree` is required by flake8."""
self.filename = filename
self._pattern = '|'.join(self._get_bad_words())
self._pattern = self._get_pattern()

def run(self) -> Generator[Tuple[int, int, str, type], None, None]:
"""Run the linter and return a generator of errors."""
Expand All @@ -36,51 +37,56 @@ def run(self) -> Generator[Tuple[int, int, str, type], None, None]:
def add_options(cls, parser: OptionManager) -> None:
"""Add koles linter options to the flake8 parser."""
parser.add_option(
'--ignore-shorties',
default=0,
type='int',
parse_from_config=True
'--censor-msg', default=0, parse_from_config=True, action='store_true'
)
parser.add_option(
'--censor-msg',
default=0,
'--ignore-shorties', default=0, type='int', parse_from_config=True
)
parser.add_option(
'--ignore-swears',
default='',
parse_from_config=True,
action='store_true'
comma_separated_list=True,
)
parser.add_option(
'--lang',
default='english',
parse_from_config=True,
comma_separated_list=True,
choices=cls._get_lang_choices()
choices=cls._get_lang_choices(),
)

@classmethod
def parse_options(cls, options: optparse.Values) -> None:
"""Get parser options from flake8."""
cls.options = options

def _get_bad_words(self) -> Set[str]:
def _get_pattern(self) -> str:
"""Get a set of bad words."""
data = self._get_swears_data()

return {
swear_set = {
word
for word in data.decode().strip().split('\n')
if len(word) > self.options.ignore_shorties
if not self._is_swear_ignored(word)
}
return '|'.join(swear_set)

def _get_swears_data(self) -> bytes:
"""Get swears data from languages present in the options."""
data = b''
for lang in self.options.lang:
file_path = f'{self.SWEAR_DATA_DIR}/{lang}.dat'
data += pkg_resources.resource_string(
__name__, file_path
)
data += pkg_resources.resource_string(__name__, file_path)

return data

def _is_swear_ignored(self, word: str) -> bool:
"""Check if word is ignored in options."""
is_short = len(word) <= self.options.ignore_shorties
is_ignored = word in self.options.ignore_swears

return is_short or is_ignored

def _get_file_content(self) -> List[str]:
"""Return file content as a list of lines."""
if self.filename in ('stdin', '-', None):
Expand Down Expand Up @@ -125,10 +131,7 @@ def _check_row(self, string: str) -> List[Tuple[int, str]]:

regex = re.compile(f'(?=({self._pattern}))', flags=re.IGNORECASE)

return [
(match.start(), match.group(1))
for match in regex.finditer(string)
]
return [(match.start(), match.group(1)) for match in regex.finditer(string)]

def _censor_word(self, word: str) -> str:
"""Replace all letters but first with `*` if censor_msg option is True."""
Expand All @@ -138,9 +141,18 @@ def _censor_word(self, word: str) -> str:

@classmethod
def _get_lang_choices(cls) -> List[str]:
"""Get language choices by removing .dat from language filenames."""
return [
"""
Get language choices.
Remove.dat from language filenames and generate all language combinations.
"""
languages = [
lang_file.replace('.dat', '')
for lang_file in
pkg_resources.resource_listdir(__name__, cls.SWEAR_DATA_DIR)
for lang_file in pkg_resources.resource_listdir(__name__, cls.SWEAR_DATA_DIR)
]
lang_permutations = [
permutations(languages, number)
for number, language in enumerate(languages, 1)
]

return [','.join(permutation) for permutation in chain(*lang_permutations)]

0 comments on commit bef65b9

Please sign in to comment.