generated from jCloud/repository-template
Add feature to get the names of all sub-namespaces of a core.python.namespaces.PythonPackageNamespace
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from __future__ import annotations
|
||||
from ...exceptions import InvalidNamespaceError, NamespaceNotFoundError
|
||||
from ...utils import assert_that_is_instance
|
||||
|
||||
@@ -31,6 +32,9 @@ class PythonNamespace:
|
||||
def __init__(self, name: str) -> None:
|
||||
assert_that_is_instance(name, str)
|
||||
|
||||
if not name.strip():
|
||||
raise InvalidNamespaceError('invalid namespace', namespace_identifier = name)
|
||||
|
||||
self.name = name
|
||||
|
||||
class PythonPackageNamespace(PythonNamespace):
|
||||
@@ -39,7 +43,7 @@ class PythonPackageNamespace(PythonNamespace):
|
||||
|
||||
:param name: The name of the namespace.
|
||||
:type name: str
|
||||
:param sub_namespaces: The sub namespaces.
|
||||
:param sub_namespaces: The sub-namespaces.
|
||||
:type sub_namespaces: list[PythonNamespace]
|
||||
'''
|
||||
|
||||
@@ -61,7 +65,7 @@ class PythonPackageNamespace(PythonNamespace):
|
||||
if not sn:
|
||||
raise InvalidNamespaceError('invalid namespace')
|
||||
|
||||
# check whether the sub namespace exists
|
||||
# check whether the sub-namespace exists
|
||||
if sub_namespaces[0] not in self.sub_namespaces:
|
||||
raise NamespaceNotFoundError('no such namespace', namespace_identifier = sub_namespaces[0])
|
||||
|
||||
@@ -75,6 +79,34 @@ class PythonPackageNamespace(PythonNamespace):
|
||||
# module namespace
|
||||
else:
|
||||
return sub_namespace
|
||||
|
||||
def _sub_namespaces_names(self, package: PythonPackageNamespace) -> list[str]:
|
||||
'''
|
||||
Returns the names of all sub-namespaces of the specified package
|
||||
namespace and keeps the package structure.
|
||||
|
||||
:param package: The package.
|
||||
:type package: PythonPackageNamespace
|
||||
|
||||
:return: The names if all sub-namespaces.
|
||||
:rtype: list[str]
|
||||
'''
|
||||
|
||||
return {
|
||||
n: self._sub_namespaces_names(sn) if isinstance(sn, PythonPackageNamespace) else sn.name
|
||||
for n, sn in package.sub_namespaces.items()
|
||||
}
|
||||
|
||||
def sub_namespace_names(self) -> list[str]:
|
||||
'''
|
||||
Returns the names of all sub-namespaces and keeps the package
|
||||
structure.
|
||||
|
||||
:return: The names if all sub-namespaces.
|
||||
:rtype: list[str]
|
||||
'''
|
||||
|
||||
return self._sub_namespaces_names(self)
|
||||
|
||||
class PythonModuleNamespace(PythonNamespace):
|
||||
'''
|
||||
|
||||
Reference in New Issue
Block a user