@@ -121,6 +121,16 @@ Methods for analysing the module tree
121121 :raises: ``ValueError `` if the module is a squashed module, as by definition it represents both itself and all
122122 of its descendants.
123123
124+ .. py :function :: ImportGraph.find_matching_modules(expression)
125+
126+ Find all modules matching the passed expression (see :ref: `module_expressions `).
127+
128+ :param str expression: A module expression used for matching.
129+ :return: A set of module names matching the expression.
130+ :rtype: A set of strings.
131+ :raises: ``grimp.exceptions.InvalidModuleExpression `` if the module expression is invalid.
132+
133+
124134Methods for analysing direct imports
125135------------------------------------
126136
@@ -184,6 +194,28 @@ Methods for analysing direct imports
184194 So if a module is imported twice from the same module, it will only be counted once.
185195 :rtype: Integer.
186196
197+ .. py :function :: ImportGraph.find_matching_direct_imports(import_expression)
198+
199+ Find all direct imports matching the passed import expression.
200+
201+ The imports are returned are in the following form::
202+
203+ [
204+ {
205+ 'importer': 'mypackage.importer',
206+ 'imported': 'mypackage.imported',
207+ },
208+ # (additional imports here)
209+ ]
210+
211+ :param str import_expression: An expression in the form ``"importer_expression -> imported_expression" ``,
212+ where each expression is a module expression (see :ref: `module_expressions `).
213+ Example: ``"mypackage.*.blue -> mypackage.*.green" ``.
214+ :return: An ordered list of direct imports matching the expressions (ordered alphabetically).
215+ :rtype: List of dictionaries with the structure shown above. If you want to use type annotations, you may use the
216+ ``grimp.Import `` TypedDict for each dictionary.
217+ :raises: ``grimp.exceptions.InvalidImportExpression `` if the expression is not well-formed.
218+
187219Methods for analysing import chains
188220-----------------------------------
189221
@@ -517,5 +549,25 @@ Methods for manipulating the graph
517549 :param str module: The name of a module, for example ``'mypackage.foo' ``.
518550 :return: bool
519551
552+ .. _module_expressions :
553+
554+ Module expressions
555+ ------------------
556+
557+ A module expression is used to refer to sets of modules.
558+
559+ - ``* `` stands in for a module name, without including subpackages.
560+ - ``** `` includes subpackages too.
561+
562+ Examples:
563+
564+ - ``mypackage.foo ``: matches ``mypackage.foo `` exactly.
565+ - ``mypackage.* ``: matches ``mypackage.foo `` but not ``mypackage.foo.bar ``.
566+ - ``mypackage.*.baz ``: matches ``mypackage.foo.baz `` but not ``mypackage.foo.bar.baz ``.
567+ - ``mypackage.*.* ``: matches ``mypackage.foo.bar `` and ``mypackage.foobar.baz ``.
568+ - ``mypackage.** ``: matches ``mypackage.foo.bar `` and ``mypackage.foo.bar.baz ``.
569+ - ``mypackage.**.qux ``: matches ``mypackage.foo.bar.qux `` and ``mypackage.foo.bar.baz.qux ``.
570+ - ``mypackage.foo* ``: is not a valid expression. (The wildcard must replace a whole module name.)
571+
520572.. _namespace packages : https://docs.python.org/3/glossary.html#term-namespace-package
521573.. _namespace portion : https://docs.python.org/3/glossary.html#term-portion
0 commit comments