From ffe67e8c6ad0efd6481aabec24dd0a4a1d603426 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Sun, 2 Oct 2022 22:20:11 -0700 Subject: [PATCH] is_package_or_sage_namespace_package_dir: Add option distribution_filter --- src/sage/misc/package_dir.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/sage/misc/package_dir.py b/src/sage/misc/package_dir.py index d9b0914ace3..abc64fe7f91 100644 --- a/src/sage/misc/package_dir.py +++ b/src/sage/misc/package_dir.py @@ -129,7 +129,7 @@ def read_distribution(src_file): return '' -def is_package_or_sage_namespace_package_dir(path): +def is_package_or_sage_namespace_package_dir(path, *, distribution_filter=None): r""" Return whether ``path`` is a directory that contains a Python package. @@ -140,6 +140,15 @@ def is_package_or_sage_namespace_package_dir(path): a file ``all.py`` or a file matching the pattern ``all__*.py`` such as ``all__sagemath_categories.py``. + INPUT: + + - ``path`` -- a directory name. + + - ``distribution_filter`` -- (optional, default: ``None``) + only consider ``all*.py`` files whose distribution (from a + ``# sage_setup: distribution = PACKAGE`` directive in the source file) + is an element of ``distribution_filter``. + EXAMPLES: :mod:`sage.cpython` is an ordinary package:: @@ -172,14 +181,17 @@ def is_package_or_sage_namespace_package_dir(path): sage: is_package_or_sage_namespace_package_dir(directory) False """ - if os.path.exists(os.path.join(path, '__init__.py')): # ordinary package + if os.path.exists(os.path.join(path, '__init__.py')): # ordinary package return True - if os.path.exists(os.path.join(path, '__init__.pxd')): # for consistency with Cython + if os.path.exists(os.path.join(path, '__init__.pxd')): # for consistency with Cython return True - if os.path.exists(os.path.join(path, 'all.py')): # complete namespace package - return True - for _ in glob.iglob(os.path.join(path, 'all__*.py')): - return True # partial namespace package + fname = os.path.join(path, 'all.py') + if os.path.exists(fname): + if distribution_filter is None or fname in distribution_filter: # complete namespace package + return True + for fname in glob.iglob(os.path.join(path, 'all__*.py')): + if distribution_filter is None or fname in distribution_filter: # partial namespace package + return True return False