Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Feb 9, 2021
1 parent a6d3d57 commit 33394cc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
17 changes: 7 additions & 10 deletions flake8_codes/_plugins/_discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
try:
import importlib.metadata as importlib_metadata
except ImportError:
import importlib_metadata
import importlib_metadata # type: ignore


REX_CODE = re.compile(r'^[A-Z]{1,5}[0-9]{0,5}$')
Expand All @@ -33,43 +33,40 @@


class Plugin(NamedTuple):
check_type: str
name: str
codes: Tuple[str, ...]
version: str


def get_installed(app: Application) -> Iterator[Plugin]:
plugins_codes: DefaultDict[Tuple[str, str], List[str]]
plugins_codes: DefaultDict[str, List[str]]
plugins_codes = defaultdict(list)
versions = dict()
codes: Iterable[str]

entry_points = importlib_metadata.entry_points()[NAMESPACE]
for entry_point in entry_points:
plugin = Flake8Plugin(entry_point.name, entry_point, local=False)
check_type = plugin.parameter_names[0]
key = (check_type, get_plugin_name(plugin.to_dictionary()))
versions[key[-1]] = plugin.version
name = get_plugin_name(plugin.to_dictionary())
versions[name] = plugin.version

# if codes for plugin specified explicitly in ALIASES, use it
codes = ALIASES.get(plugin.plugin_name, [])
if codes:
plugins_codes[key] = list(codes)
plugins_codes[name] = list(codes)
continue

# otherwise get codes from plugin entrypoint
code = plugin.name
if not REX_CODE.match(code):
raise ValueError('Invalid code format: {}'.format(code))
plugins_codes[key].append(code)
plugins_codes[name].append(code)

if 'flake8-docstrings' in versions:
versions['flake8-docstrings'] = versions['flake8-docstrings'].split(',')[0]

for (check_type, name), codes in plugins_codes.items():
for name, codes in plugins_codes.items():
yield Plugin(
check_type=check_type,
name=name,
codes=tuple(sorted(codes)),
version=versions[name],
Expand Down
2 changes: 1 addition & 1 deletion tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ def test_smoke_prefixes(plugin_name):
codes = extract(plugin_name)
for code in codes:
print(plugin_name, code, plugin.codes)
assert code.startswith(tuple(plugin.codes))
assert code.startswith(plugin.codes)

0 comments on commit 33394cc

Please sign in to comment.