core: add 'core' config section, add disabled_modules/enabled_modules configs, use them for hpi modules and hpi doctor

This commit is contained in:
Dima Gerasimov 2020-09-28 18:53:58 +02:00 committed by karlicoss
parent f939daac99
commit 70c801f692
6 changed files with 121 additions and 35 deletions

View file

@ -3,27 +3,24 @@ from itertools import chain
import os
import re
import pkgutil
from typing import List
from typing import List, Iterable
# TODO reuse in readme/blog post
# borrowed from https://github.com/sanitizers/octomachinery/blob/24288774d6dcf977c5033ae11311dbff89394c89/tests/circular_imports_test.py#L22-L55
def _find_all_importables(pkg):
"""Find all importables in the project.
Return them in order.
"""
return sorted(
set(
chain.from_iterable(
_discover_path_importables(Path(p), pkg.__name__)
for p in pkg.__path__
),
),
def _iter_all_importables(pkg):
yield from chain.from_iterable(
_discover_path_importables(Path(p), pkg.__name__)
for p in pkg.__path__
)
def _discover_path_importables(pkg_pth, pkg_name):
"""Yield all importables under a given path and package."""
for dir_path, _d, file_names in os.walk(pkg_pth):
for dir_path, dirs, file_names in os.walk(pkg_pth):
file_names.sort()
# NOTE: sorting dirs in place is intended, it's the way you're supposed to do it with os.walk
dirs.sort()
pkg_dir_path = Path(dir_path)
if pkg_dir_path.parts[-1] == '__pycache__':
@ -36,6 +33,7 @@ def _discover_path_importables(pkg_pth, pkg_name):
pkg_pref = '.'.join((pkg_name, ) + rel_pt.parts)
# TODO might need to make it defensive and yield Exception (otherwise hpi doctor might fail for no good reason)
yield from (
pkg_path
for _, pkg_path, _ in pkgutil.walk_packages(
@ -44,7 +42,7 @@ def _discover_path_importables(pkg_pth, pkg_name):
)
# todo need a better way to mark module as 'interface'
# TODO marking hpi modules or unmarking non-modules? not sure what's worse
def ignored(m: str):
excluded = [
'kython.*',
@ -75,8 +73,12 @@ def ignored(m: str):
return re.match(f'^my.({exs})$', m)
def get_modules() -> List[str]:
def modules() -> Iterable[str]:
import my as pkg # todo not sure?
importables = _find_all_importables(pkg)
public = [x for x in importables if not ignored(x)]
return public
for x in _iter_all_importables(pkg):
if not ignored(x):
yield x
def get_modules() -> List[str]:
return list(modules())