Packages
plateforme.core.packages
This module provides functionality for managing packages within the Plateforme framework. It includes classes and functions for importing modules, retrieving package configurations, and initializing the package catalog.
The Package
class lifecycle is managed by the framework runtime environment
that enforces a singleton behavior for package instances. It is initialized
with a Python module name and provides access to the package's configuration,
metadata, and resources.
The PackageImpl
class is a proxy object that provides access to the
implementation of a package within a given application context. It includes
additional configuration settings such as the namespace, slug, and file
system path.
Package
Package(
name: str,
module: ModuleType,
*,
info: dict[Any, Any] | None = None,
settings: PackageSettings | None = None,
)
Bases: Representation
A package within the Plateforme framework.
It is initialized with a python module name. It checks for a package configuration to ensure the given module name corresponds to an importable package within the Plateforme framework. Each package is uniquely represented as a singleton object, identified by its module name. This process guarantees the consistent and correct identification of Plateforme packages for further operations.
The package class exposes a catalog
attribute that maps the alias and
slug names within the package namespace to their respective objects, either
a resource type, an association (i.e. a tuple of one or two linked fields),
or a service method. It also provides access to the package resources,
dependencies, and dependents, as well as the package implementations within
the current application context.
Attributes:
Name | Type | Description |
---|---|---|
_impls |
dict[Plateforme | None, PackageImpl]
|
The registered package implementations as a dictionary mapping the application context to the package implementation. |
module |
ModuleType
|
The package module object. |
name |
str
|
The package module name that is unique within the entire runtime environment. |
info |
dict[Any, Any] | None
|
Additional information about the package. |
catalog |
Catalog
|
A dictionary mapping the alias and slug names within the package namespace to their respective objects, either a resource type, an association, or a service method. |
metadata |
MetaData
|
The package resource metadata. |
registry |
Registry
|
The package resource registry. |
Initialize a package.
Note
The import_package
method should be used to retrieve a package
instance based on the provided module name.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/packages.py
impl
property
impl: PackageImpl
The current package implementation based on the app context.
A read-only property that returns the package implementation based on the application context. If no context is available, it returns the package default implementation. Otherwise, it raises an error if the package implementation is not available in the current application context.
PackageManager
PackageManager(managed: Managed | type[Managed])
Bases: Manager['PackageImpl']
A package implementation manager.
It provides a common interface to access and manage the service methods associated with a package implementation.
Initialize a new manager instance.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/managers.py
PackageImpl
PackageImpl(
package: Package,
context: Plateforme | None,
*,
settings: PackageSettings | None = None,
auto_generated: bool = False,
auto_import_namespace: bool = True,
)
Bases: Proxy[Package]
, Representation
A package implementation proxy class.
It defines the implementation details of a package within the Plateforme
framework. It extends the Proxy
class and adds additional information
about the package.
Attributes:
Name | Type | Description |
---|---|---|
context |
Plateforme | None
|
The application context associated with the package implementation. |
namespace |
NamespaceImpl
|
The package namespace used to load the package and its resources within an application and to group resources to avoid alias collisions. |
objects |
PackageManager
|
The package manager to access and manage the service methods associated with the package implementation. |
services |
tuple[BaseService, ...]
|
The service instances associated with the package implementation. |
settings |
PackageSettings
|
The package settings to use for the implementation. |
auto_generated |
bool
|
Whether the package implementation is auto-generated and should not be persisted when removing dependent packages. |
file_path |
str
|
A string that defines the filesystem path of the package module. It is used to load package related assets from the filesystem. When not provided, it is resolved from the package module. |
Initialize a package implementation proxy instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
package
|
Package
|
The package instance to proxy. |
required |
context
|
Plateforme | None
|
The application context to associate with the package implementation. |
required |
settings
|
PackageSettings | None
|
The package settings to use. Defaults to an empty
|
None
|
auto_generated
|
bool
|
Whether the package implementation is
auto-generated and should not be persisted when removing
dependent packages. Defaults to |
False
|
auto_import_namespace
|
bool
|
Whether to automatically import and create
the namespace for the package implementation.
Defaults to |
True
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/packages.py
collect_api_resources
collect_api_resources(
package: PackageImpl,
*,
include: Iterable[str] | None = None,
exclude: Iterable[str] | None = None,
) -> set[ResourceType]
Collect API resources from a package.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
package
|
PackageImpl
|
The package to collect API resources from. |
required |
include
|
Iterable[str] | None
|
An iterable of module or resource names that should be
included in the collection. If provided, only the resources with
the specified names are included. Defaults to |
None
|
exclude
|
Iterable[str] | None
|
An iterable of module or resource names that should be
excluded from the collection. If provided, the resources with the
specified names are excluded. Defaults to |
None
|
Returns:
Type | Description |
---|---|
set[ResourceType]
|
A set containing the matched resources based on the filter criteria. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/packages.py
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 |
|
collect_api_services
collect_api_services(
package: PackageImpl,
*,
include: Iterable[str] | None = None,
exclude: Iterable[str] | None = None,
) -> set[BaseService]
Collect API services from a package.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
package
|
PackageImpl
|
The package to collect API services from. |
required |
include
|
Iterable[str] | None
|
An iterable of module or service names that should be included
in the collection. If provided, only the services with the
specified names are included. Defaults to |
None
|
exclude
|
Iterable[str] | None
|
An iterable of module or service names that should be excluded
from the collection. If provided, the services with the specified
names are excluded. Defaults to |
None
|
Returns:
Type | Description |
---|---|
set[BaseService]
|
A set containing the matched services based on the filter criteria. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/packages.py
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 |
|
plateforme.core.modules
This module provides utilities for managing modules within the Plateforme framework.
ROOT_MODULE
module-attribute
The root module name used as a fallback for top-level modules.
get_root_module_name
Get the root module name used as a fallback for top-level modules.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix
|
str | None
|
An optional prefix to prepend to the root module name.
Defaults to |
None
|
Returns:
Type | Description |
---|---|
str
|
The root module name. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/modules.py
import_module
import_module(
name: str,
package: str | None = None,
*,
force_resolution: bool = False,
) -> ModuleType
Import a module.
The package
argument is required when performing a relative import. It
specifies the package to use as the anchor point from which to resolve the
relative import to an absolute import.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the module to import. |
required |
package
|
str | None
|
The package to use as the anchor point for relative imports. |
None
|
force_resolution
|
bool
|
When set to |
False
|
Returns:
Type | Description |
---|---|
ModuleType
|
The imported module. |
Note
This function is a wrapper around the importlib.import_module
built-in function. It adds the ability to attempt the import of the
modules __main__
when the given module name is an empty string.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/modules.py
import_object
Import an object.
The package
argument is required when performing a relative import. It
specifies the package to use as the anchor point from which to resolve the
relative import to an absolute import.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the object to import. |
required |
package
|
str | None
|
The package to use as the anchor point for relative imports. |
None
|
Returns:
Type | Description |
---|---|
Any
|
The imported object. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/modules.py
is_namespace
is_namespace(module: ModuleType) -> bool
is_package
is_package(
module: ModuleType, *, allow_root: bool = False
) -> bool
Whether a module is a valid package.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/modules.py
is_root_module
is_root_module(module: ModuleType | str) -> bool
Whether a module is the root module.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/modules.py
resolve_file_paths
resolve_file_paths(module: ModuleType) -> list[str]
Resolve the filesystem paths of a module.
It first tries to resolve the filesystem paths from the module's __path__
attribute. If the attribute is not available or contains multiple paths, it
attempts to resolve the filesystem path from the module's __file__
attribute. If neither is available, it returns an empty list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module
|
ModuleType
|
The module to resolve the filesystem paths from. |
required |
Returns:
Type | Description |
---|---|
list[str]
|
The resolved filesystem paths of the module. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/modules.py
resolve_forwardref_fullname
resolve_forwardref_fullname(
module_name: str,
forwardref: str | ForwardRef,
_guard: frozenset[str] = frozenset(),
) -> str | None
Resolves a forward reference to its fully qualified name.
It resolves a forward reference to its fully qualified name. This function is necessary because the Python interpreter does not resolve forward references module origin in annotations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_name
|
str
|
The module name where the forward reference occurs. |
required |
forwardref
|
str | ForwardRef
|
The forward reference to resolve. |
required |
_guard
|
frozenset[str]
|
A set of module names to prevent infinite recursion. |
frozenset()
|
Returns:
Type | Description |
---|---|
str | None
|
The fully qualified forward reference name. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/modules.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|
resolve_import_statement
resolve_import_statement(
name: str, import_statement: ImportFrom
) -> str
Resolve an import statement to a fully qualified module name.
It resolves the fully qualified module name from an import statement relative to a specified module name from which the import statement originates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The module name against which to resolve the import statement. |
required |
import_statement
|
ImportFrom
|
The import statement to resolve. |
required |
Returns:
Type | Description |
---|---|
str
|
The import statement fully qualified module name. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/modules.py
resolve_path_from_module_name
Resolve the file path of a Python module without loading it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the module. |
required |
Returns:
Type | Description |
---|---|
str
|
The file path of the module. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/modules.py
resolve_relative_import_name
Resolve relative module import name from one module to another.