Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a few issues with generate_config #10893

Merged
merged 10 commits into from
Oct 22, 2020
Merged
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ Bug Fixes
astropy.config
^^^^^^^^^^^^^^

- Fix a few issues with ``generate_config`` when used with other packages.
[#10893]

astropy.constants
^^^^^^^^^^^^^^^^^

Expand Down
27 changes: 22 additions & 5 deletions astropy/config/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from contextlib import contextmanager

from astropy.extern.configobj import configobj, validate
from astropy.utils import find_current_module
from astropy.utils import find_current_module, silence
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these years, I didn't know this existed. 😱

@contextlib.contextmanager
def silence():
"""A context manager that silences sys.stdout and sys.stderr."""

from astropy.utils.exceptions import AstropyDeprecationWarning, AstropyWarning
from astropy.utils.introspection import resolve_name

Expand Down Expand Up @@ -602,15 +602,15 @@ def generate_config(pkgname='astropy', filename=None):

Parameters
----------
packageormod : str or None
pkgname : str or None
The package for which to retrieve the configuration object.
filename : str or file object or None
If None, the default configuration path is taken from `get_config`.

"""
package = importlib.import_module(pkgname)
with warnings.catch_warnings():
warnings.simplefilter('ignore', AstropyDeprecationWarning)
with silence(), warnings.catch_warnings():
saimn marked this conversation as resolved.
Show resolved Hide resolved
warnings.simplefilter('ignore')
saimn marked this conversation as resolved.
Show resolved Hide resolved
for mod in pkgutil.walk_packages(path=package.__path__,
prefix=package.__name__ + '.'):

Expand All @@ -628,7 +628,7 @@ def generate_config(pkgname='astropy', filename=None):
width=78)

if filename is None:
filename = get_config(package).filename
filename = get_config(pkgname).filename
saimn marked this conversation as resolved.
Show resolved Hide resolved

with contextlib.ExitStack() as stack:
if isinstance(filename, (str, pathlib.Path)):
Expand All @@ -639,7 +639,24 @@ def generate_config(pkgname='astropy', filename=None):

# Parse the subclasses, ordered by their module name
subclasses = ConfigNamespace.__subclasses__()
processed = set()

for conf in sorted(subclasses, key=lambda x: x.__module__):
mod = conf.__module__

# Skip modules for other packages, e.g. astropy modules that
# would be imported when running the function for astroquery.
if mod.split('.')[0] != pkgname:
continue

# Check that modules are not processed twice, which can happen
# when they are imported in another module.
if mod in processed:
print(mod, 'already processed')
saimn marked this conversation as resolved.
Show resolved Hide resolved
saimn marked this conversation as resolved.
Show resolved Hide resolved
continue
else:
processed.add(mod)

print_module = True
for item in conf().values():
if print_module:
Expand Down