Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
is_package_or_sage_namespace_package_dir: Add option distribution_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Koeppe committed Oct 3, 2022
1 parent dde4bdb commit ffe67e8
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/sage/misc/package_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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::
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit ffe67e8

Please sign in to comment.