graphcore-kernel 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- graphcore/__init__.py +5 -0
- graphcore/agent_frontend/__init__.py +8 -0
- graphcore/agent_frontend/api.py +69 -0
- graphcore/agent_frontend/base/__init__.py +11 -0
- graphcore/agent_frontend/base/views.py +167 -0
- graphcore/agent_frontend/client.py +31 -0
- graphcore/agent_frontend/context.py +157 -0
- graphcore/agent_frontend/errors.py +12 -0
- graphcore/agent_frontend/loading.py +54 -0
- graphcore/agent_frontend/registry.py +42 -0
- graphcore/agent_frontend/render.py +207 -0
- graphcore/agent_frontend/server.py +103 -0
- graphcore/agent_frontend/signatures.py +35 -0
- graphcore/agent_frontend/testing.py +45 -0
- graphcore/agent_frontend/tests/__init__.py +0 -0
- graphcore/agent_frontend/tests/fakes.py +114 -0
- graphcore/agent_frontend/tests/test_frontend.py +447 -0
- graphcore/agent_frontend/tests/test_loading.py +83 -0
- graphcore/agent_frontend/tests/test_server.py +109 -0
- graphcore/agent_frontend/tools.py +137 -0
- graphcore/api/__init__.py +4 -0
- graphcore/api/graph_api.py +378 -0
- graphcore/api/predicate_codec.py +81 -0
- graphcore/api/tests/__init__.py +0 -0
- graphcore/api/tests/test_changed_entities.py +109 -0
- graphcore/api/tests/test_graph_api.py +290 -0
- graphcore/api/tests/test_graph_api_access.py +94 -0
- graphcore/api/tests/test_graph_api_grants.py +90 -0
- graphcore/api/tests/test_predicate_codec.py +83 -0
- graphcore/api/tests/test_subtree_query.py +79 -0
- graphcore/apps.py +71 -0
- graphcore/auth/__init__.py +1 -0
- graphcore/auth/authn/README.md +178 -0
- graphcore/auth/authn/__init__.py +3 -0
- graphcore/auth/authn/authenticator.py +120 -0
- graphcore/auth/authn/onboarding.py +105 -0
- graphcore/auth/authn/principals.py +20 -0
- graphcore/auth/authn/resource_metadata.py +43 -0
- graphcore/auth/authn/service_accounts.py +96 -0
- graphcore/auth/authn/testing.py +96 -0
- graphcore/auth/authn/tests/__init__.py +0 -0
- graphcore/auth/authn/tests/test_authenticator.py +145 -0
- graphcore/auth/authn/tests/test_onboarding.py +145 -0
- graphcore/auth/authn/tests/test_service_accounts.py +180 -0
- graphcore/auth/authn/users.py +59 -0
- graphcore/auth/authz/__init__.py +0 -0
- graphcore/auth/authz/access_port.py +120 -0
- graphcore/auth/authz/adapter/__init__.py +0 -0
- graphcore/auth/authz/adapter/default_access_adapter.py +556 -0
- graphcore/auth/authz/adapter/tests/__init__.py +0 -0
- graphcore/auth/authz/adapter/tests/test_default_adapter.py +543 -0
- graphcore/auth/authz/adapter/tests/test_entity_write_hooks.py +207 -0
- graphcore/auth/authz/adapter/tests/test_group_types.py +70 -0
- graphcore/auth/authz/adapter/tests/test_sync_support.py +209 -0
- graphcore/auth/authz/defaults.py +26 -0
- graphcore/auth/authz/kernel_policy.py +191 -0
- graphcore/auth/authz/kernel_writes.py +31 -0
- graphcore/auth/authz/lookups.py +25 -0
- graphcore/auth/authz/policy.py +403 -0
- graphcore/auth/authz/tests/__init__.py +0 -0
- graphcore/auth/authz/tests/test_can_contain.py +150 -0
- graphcore/auth/authz/tests/test_contained_by.py +167 -0
- graphcore/auth/authz/tests/test_creation.py +391 -0
- graphcore/auth/authz/tests/test_delete_permission.py +82 -0
- graphcore/auth/authz/tests/test_granted_to_me.py +160 -0
- graphcore/auth/authz/tests/test_integration.py +637 -0
- graphcore/auth/authz/tests/test_invites.py +184 -0
- graphcore/auth/authz/tests/test_kernel_policy.py +140 -0
- graphcore/auth/authz/tests/test_lockout.py +132 -0
- graphcore/auth/authz/tests/test_policy.py +329 -0
- graphcore/auth/authz/tests/test_primitives.py +73 -0
- graphcore/auth/authz/tests/test_principal_creation.py +62 -0
- graphcore/bootstrap/__init__.py +0 -0
- graphcore/bootstrap/deployment.py +67 -0
- graphcore/bootstrap/signals.py +16 -0
- graphcore/bootstrap/tests/__init__.py +0 -0
- graphcore/bootstrap/tests/test_deployment_seed.py +154 -0
- graphcore/broadcast/__init__.py +4 -0
- graphcore/broadcast/broadcaster.py +201 -0
- graphcore/broadcast/channels.py +56 -0
- graphcore/broadcast/tests/__init__.py +0 -0
- graphcore/broadcast/tests/test_broadcaster.py +444 -0
- graphcore/celery.py +14 -0
- graphcore/docs/AGENT_FRONTEND_TDD.md +389 -0
- graphcore/docs/ARCHITECTURE_PRINCIPLES.md +152 -0
- graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md +1379 -0
- graphcore/docs/ENTITY_REFERENCE_TDD.md +561 -0
- graphcore/docs/GRAPHAPI_FRONT_DOOR.md +173 -0
- graphcore/docs/RUNTIME.md +90 -0
- graphcore/docs/SCHEDULING_TDD.md +244 -0
- graphcore/docs/STRATEGY_EXECUTION_TDD.md +1043 -0
- graphcore/docs/VIEW_ENTITY_TDD.md +522 -0
- graphcore/docs/VIEW_SYSTEM_TDD.md +307 -0
- graphcore/docs/__init__.py +0 -0
- graphcore/docs/doc_topics.py +97 -0
- graphcore/docs/docs_discovery.py +64 -0
- graphcore/docs/public_api.py +56 -0
- graphcore/domain/__init__.py +0 -0
- graphcore/domain/entity/Entity.py +204 -0
- graphcore/domain/entity/__init__.py +0 -0
- graphcore/domain/entity/deployment/Deployment.py +25 -0
- graphcore/domain/entity/deployment/__init__.py +0 -0
- graphcore/domain/entity/grant/Grant.py +31 -0
- graphcore/domain/entity/grant/__init__.py +0 -0
- graphcore/domain/entity/group/Group.py +48 -0
- graphcore/domain/entity/group/README.md +88 -0
- graphcore/domain/entity/group/__init__.py +0 -0
- graphcore/domain/entity/invite/Invite.py +31 -0
- graphcore/domain/entity/invite/__init__.py +0 -0
- graphcore/domain/entity/request_template/RequestTemplate.py +48 -0
- graphcore/domain/entity/request_template/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/test_request_template.py +52 -0
- graphcore/domain/entity/service_account/ServiceAccount.py +41 -0
- graphcore/domain/entity/service_account/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/StrategyRequest.py +61 -0
- graphcore/domain/entity/strategy_request/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/test_strategy_request_entity.py +35 -0
- graphcore/domain/entity/tests/__init__.py +0 -0
- graphcore/domain/entity/tests/test_entity.py +227 -0
- graphcore/domain/entity/tests/test_grant_group_entities.py +36 -0
- graphcore/domain/entity/tests/test_write_once_fields.py +98 -0
- graphcore/domain/entity/user/User.py +39 -0
- graphcore/domain/entity/user/__init__.py +0 -0
- graphcore/domain/entity/user/tests/__init__.py +0 -0
- graphcore/domain/entity/user/tests/test_user_entity.py +33 -0
- graphcore/domain/entity/view/View.py +37 -0
- graphcore/domain/entity/view/__init__.py +0 -0
- graphcore/domain/entity/view/position.py +78 -0
- graphcore/domain/entity/view/tests/__init__.py +0 -0
- graphcore/domain/entity/view/tests/test_position.py +71 -0
- graphcore/domain/entity/view/tests/test_view_entity.py +134 -0
- graphcore/domain/strategy/BaseStrategy.py +759 -0
- graphcore/domain/strategy/DeploymentStrategies.py +55 -0
- graphcore/domain/strategy/GrantStrategies.py +189 -0
- graphcore/domain/strategy/GroupStrategies.py +193 -0
- graphcore/domain/strategy/InviteStrategies.py +84 -0
- graphcore/domain/strategy/RequestTemplateStrategies.py +126 -0
- graphcore/domain/strategy/ViewStrategies.py +219 -0
- graphcore/domain/strategy/__init__.py +0 -0
- graphcore/domain/strategy/runner.py +84 -0
- graphcore/domain/strategy/session.py +56 -0
- graphcore/domain/strategy/tests/BaseStrategyTest.py +167 -0
- graphcore/domain/strategy/tests/__init__.py +0 -0
- graphcore/domain/strategy/tests/test_BaseStrategies.py +870 -0
- graphcore/domain/strategy/tests/test_grant_strategies.py +245 -0
- graphcore/domain/strategy/tests/test_group_strategies.py +99 -0
- graphcore/domain/strategy/tests/test_runner.py +220 -0
- graphcore/domain/strategy/tests/test_strategy_locks.py +446 -0
- graphcore/domain/strategy/tests/test_utils.py +95 -0
- graphcore/domain/strategy/tests/test_view_strategies.py +436 -0
- graphcore/errors.py +145 -0
- graphcore/execution/README.md +97 -0
- graphcore/execution/StrategyExecutorService.py +333 -0
- graphcore/execution/__init__.py +0 -0
- graphcore/execution/dispatcher.py +19 -0
- graphcore/execution/environments/__init__.py +0 -0
- graphcore/execution/environments/celery.py +59 -0
- graphcore/execution/environments/in_process.py +55 -0
- graphcore/execution/local_session.py +117 -0
- graphcore/execution/redis_run_events.py +74 -0
- graphcore/execution/requests.py +22 -0
- graphcore/execution/run_events.py +85 -0
- graphcore/execution/strategy_context.py +54 -0
- graphcore/execution/tasks.py +34 -0
- graphcore/execution/tests/__init__.py +5 -0
- graphcore/execution/tests/contract.py +171 -0
- graphcore/execution/tests/support.py +310 -0
- graphcore/execution/tests/test_dispatchers.py +76 -0
- graphcore/execution/tests/test_executor.py +368 -0
- graphcore/execution/tests/test_local_session.py +95 -0
- graphcore/execution/tests/test_reconcile_runs.py +134 -0
- graphcore/execution/tests/test_run_events.py +106 -0
- graphcore/execution/tests/test_strategy_context.py +62 -0
- graphcore/gateway/__init__.py +1 -0
- graphcore/gateway/remote.py +35 -0
- graphcore/gateway/tests/__init__.py +0 -0
- graphcore/gateway/tests/test_remote.py +30 -0
- graphcore/graph/EntityService.py +710 -0
- graphcore/graph/__init__.py +0 -0
- graphcore/graph/entity_cache.py +152 -0
- graphcore/graph/tests/__init__.py +4 -0
- graphcore/graph/tests/test_EntityService.py +908 -0
- graphcore/graph/tests/test_EntityService_list.py +170 -0
- graphcore/graph/tests/test_EntityService_model_resolution.py +339 -0
- graphcore/graph/tests/test_EntityService_referrers.py +236 -0
- graphcore/graph/tests/test_EntityService_routing.py +119 -0
- graphcore/graph/tests/test_dependent_on.py +141 -0
- graphcore/graph/tests/test_engine_is_type_agnostic.py +25 -0
- graphcore/graph/tests/test_engine_resolves_read_permission.py +93 -0
- graphcore/graph/tests/test_entity_cache.py +159 -0
- graphcore/graph/tests/test_null_entity_cache.py +12 -0
- graphcore/management/__init__.py +0 -0
- graphcore/management/commands/__init__.py +0 -0
- graphcore/management/commands/dev_reset.py +18 -0
- graphcore/management/commands/graphcore_createadmin.py +65 -0
- graphcore/management/commands/graphcore_policy.py +59 -0
- graphcore/management/commands/graphcore_status.py +98 -0
- graphcore/management/tests/__init__.py +0 -0
- graphcore/management/tests/test_createadmin.py +78 -0
- graphcore/management/tests/test_policy_command.py +46 -0
- graphcore/management/tests/test_status_command.py +85 -0
- graphcore/migrations/0001_initial.py +186 -0
- graphcore/migrations/0002_invite.py +26 -0
- graphcore/migrations/__init__.py +0 -0
- graphcore/models.py +11 -0
- graphcore/persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md +396 -0
- graphcore/persistence/__init__.py +6 -0
- graphcore/persistence/django/__init__.py +0 -0
- graphcore/persistence/django/backends.py +43 -0
- graphcore/persistence/django/bindings.py +28 -0
- graphcore/persistence/django/cache.py +38 -0
- graphcore/persistence/django/compiler.py +214 -0
- graphcore/persistence/django/identity.py +18 -0
- graphcore/persistence/django/models.py +252 -0
- graphcore/persistence/django/repository.py +525 -0
- graphcore/persistence/django/tests/__init__.py +0 -0
- graphcore/persistence/django/tests/test_backends.py +68 -0
- graphcore/persistence/django/tests/test_compiler.py +269 -0
- graphcore/persistence/django/tests/test_repository.py +472 -0
- graphcore/persistence/django/tests/test_shared_column.py +120 -0
- graphcore/persistence/django/tests/test_threads.py +50 -0
- graphcore/persistence/django/tests/test_update_shape.py +175 -0
- graphcore/persistence/django/threads.py +38 -0
- graphcore/persistence/repository.py +172 -0
- graphcore/persistence/tests/__init__.py +0 -0
- graphcore/persistence/tests/contract.py +688 -0
- graphcore/persistence/tests/evaluate.py +51 -0
- graphcore/persistence/tests/fixtures.py +32 -0
- graphcore/persistence/tests/memory.py +263 -0
- graphcore/persistence/tests/test_memory_repository.py +10 -0
- graphcore/persistence/tests/test_query_types.py +127 -0
- graphcore/public/__init__.py +9 -0
- graphcore/public/api.py +9 -0
- graphcore/public/apps.py +10 -0
- graphcore/public/auth/__init__.py +1 -0
- graphcore/public/auth/grants.py +7 -0
- graphcore/public/auth/onboarding.py +8 -0
- graphcore/public/auth/perms.py +7 -0
- graphcore/public/auth/policy.py +10 -0
- graphcore/public/auth/roles.py +8 -0
- graphcore/public/auth/subjects.py +7 -0
- graphcore/public/entities/__init__.py +32 -0
- graphcore/public/errors.py +19 -0
- graphcore/public/persistence/__init__.py +44 -0
- graphcore/public/persistence/django.py +24 -0
- graphcore/public/scheduling.py +23 -0
- graphcore/public/strategies/__init__.py +85 -0
- graphcore/public/test_settings.py +16 -0
- graphcore/public/testing.py +47 -0
- graphcore/registry/EntityDirectory.py +246 -0
- graphcore/registry/README.md +147 -0
- graphcore/registry/StrategyDirectory.py +308 -0
- graphcore/registry/TYPE_METADATA_PLACEMENT.md +66 -0
- graphcore/registry/__init__.py +0 -0
- graphcore/registry/app_discovery.py +108 -0
- graphcore/registry/decorators.py +77 -0
- graphcore/registry/discovery.py +111 -0
- graphcore/registry/registry.py +79 -0
- graphcore/registry/tests/__init__.py +0 -0
- graphcore/registry/tests/test_agent_frontend_discovery.py +56 -0
- graphcore/registry/tests/test_app_config.py +36 -0
- graphcore/registry/tests/test_discovery.py +147 -0
- graphcore/registry/tests/test_entity_directory_storage.py +73 -0
- graphcore/registry/tests/test_import_package.py +87 -0
- graphcore/registry/tests/test_principal_flag.py +67 -0
- graphcore/registry/tests/test_strategy_directory.py +29 -0
- graphcore/routes/__init__.py +116 -0
- graphcore/routes/tests/__init__.py +0 -0
- graphcore/routes/tests/test_routes.py +90 -0
- graphcore/runtime.py +243 -0
- graphcore/scheduling/__init__.py +0 -0
- graphcore/scheduling/beat_scheduler.py +164 -0
- graphcore/scheduling/beat_tasks.py +63 -0
- graphcore/scheduling/scheduler.py +37 -0
- graphcore/scheduling/tests/__init__.py +0 -0
- graphcore/scheduling/tests/contract.py +136 -0
- graphcore/scheduling/tests/memory.py +42 -0
- graphcore/scheduling/tests/test_beat_scheduler.py +87 -0
- graphcore/scheduling/tests/test_memory_scheduler.py +15 -0
- graphcore/scheduling/tests/test_ticks.py +143 -0
- graphcore/tasks.py +9 -0
- graphcore/test_settings.py +78 -0
- graphcore/testing/__init__.py +0 -0
- graphcore/testing/case.py +112 -0
- graphcore/testing/runner.py +32 -0
- graphcore/testing/settings.py +26 -0
- graphcore/testing/tests/__init__.py +0 -0
- graphcore/testing/tests/agent_view_fixture.py +19 -0
- graphcore/testing/tests/test_case_and_runner.py +219 -0
- graphcore/testing/tests/test_settings_for_apps.py +50 -0
- graphcore/tests/__init__.py +0 -0
- graphcore/tests/ids.py +14 -0
- graphcore/tests/public_api_manifest.py +102 -0
- graphcore/tests/runs.py +7 -0
- graphcore/tests/test_app_discovery.py +90 -0
- graphcore/tests/test_composition_boundaries.py +231 -0
- graphcore/tests/test_docs_discovery.py +108 -0
- graphcore/tests/test_no_print.py +70 -0
- graphcore/tests/test_packaging.py +64 -0
- graphcore/tests/test_public_api_boundaries.py +229 -0
- graphcore/tests/test_public_api_contract.py +62 -0
- graphcore/tests/test_public_api_docs.py +28 -0
- graphcore/tests/test_runtime.py +42 -0
- graphcore/tests/test_shared_type_boundaries.py +128 -0
- graphcore/tests/test_shared_types.py +144 -0
- graphcore/tests/test_test_settings.py +45 -0
- graphcore/transport/__init__.py +0 -0
- graphcore/transport/agent_asgi.py +68 -0
- graphcore/transport/agent_client.py +85 -0
- graphcore/transport/channel_layer.py +68 -0
- graphcore/transport/consumers.py +507 -0
- graphcore/transport/routing.py +6 -0
- graphcore/transport/tests/__init__.py +0 -0
- graphcore/transport/tests/test_agent_client.py +31 -0
- graphcore/transport/tests/test_api_views.py +733 -0
- graphcore/transport/tests/test_channel_layer.py +96 -0
- graphcore/transport/tests/test_consumers.py +963 -0
- graphcore/transport/tests/test_resource_metadata.py +67 -0
- graphcore/transport/urls.py +29 -0
- graphcore/transport/views.py +358 -0
- graphcore/types/__init__.py +4 -0
- graphcore/types/access.py +114 -0
- graphcore/types/api.py +76 -0
- graphcore/types/entities.py +100 -0
- graphcore/types/entity_id.py +85 -0
- graphcore/types/execution.py +41 -0
- graphcore/types/identity.py +20 -0
- graphcore/types/onboarding.py +20 -0
- graphcore/types/query.py +116 -0
- graphcore/types/schedules.py +91 -0
- graphcore/types/strategy.py +73 -0
- graphcore/types/tests/__init__.py +0 -0
- graphcore/types/tests/test_entity_id.py +50 -0
- graphcore/urls.py +5 -0
- graphcore_kernel-0.1.0.dist-info/METADATA +94 -0
- graphcore_kernel-0.1.0.dist-info/RECORD +343 -0
- graphcore_kernel-0.1.0.dist-info/WHEEL +5 -0
- graphcore_kernel-0.1.0.dist-info/entry_points.txt +2 -0
- graphcore_kernel-0.1.0.dist-info/licenses/LICENSE +202 -0
- graphcore_kernel-0.1.0.dist-info/licenses/NOTICE +2 -0
- graphcore_kernel-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"""
|
|
2
|
+
StrategyDirectory - Central registry for all strategy classes in the framework.
|
|
3
|
+
|
|
4
|
+
Uses decorator-based discovery with lazy loading for optimal performance and
|
|
5
|
+
debugger compatibility. Strategies are stored as string paths and imported
|
|
6
|
+
on-demand, allowing hot-reload in debug mode.
|
|
7
|
+
"""
|
|
8
|
+
import logging
|
|
9
|
+
import importlib
|
|
10
|
+
import pkgutil
|
|
11
|
+
import sys
|
|
12
|
+
from typing import Dict, List, Type, Optional
|
|
13
|
+
|
|
14
|
+
from graphcore.errors import InvalidRequest, KernelError
|
|
15
|
+
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class StrategyDirectoryClosedError(KernelError):
|
|
20
|
+
"""Raised when registration is attempted after discovery closes."""
|
|
21
|
+
|
|
22
|
+
code = 'strategy_directory_closed'
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class DuplicateStrategyNameError(KernelError):
|
|
26
|
+
"""Raised when two strategy classes share a name, which requests address
|
|
27
|
+
them by."""
|
|
28
|
+
|
|
29
|
+
code = 'duplicate_strategy_name'
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class StrategyDirectoryClass:
|
|
33
|
+
"""
|
|
34
|
+
Directory for managing strategy class registrations.
|
|
35
|
+
|
|
36
|
+
Uses entity class names as keys (e.g., 'Entity' -> list of strategies that apply to Entity)
|
|
37
|
+
instead of the old hardcoded dictionary.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def __init__(self):
|
|
41
|
+
# Maps entity class name -> list of strategy paths (as strings for lazy loading)
|
|
42
|
+
self._directory: Dict[str, List[str]] = {}
|
|
43
|
+
# Track if running in debug mode with hot reload enabled
|
|
44
|
+
self._debug_reload = False
|
|
45
|
+
# Cache for loaded strategy classes (only used in production mode)
|
|
46
|
+
self._class_cache: Dict[str, Type] = {}
|
|
47
|
+
# strategy_path -> its read_only_strategy value, captured at registration
|
|
48
|
+
self._read_only: Dict[str, bool] = {}
|
|
49
|
+
# strategy_path -> id of the app whose discovery registered it
|
|
50
|
+
self._owners: Dict[str, str] = {}
|
|
51
|
+
# strategy class name -> its path; requests address strategies by name
|
|
52
|
+
self._paths_by_name: Dict[str, str] = {}
|
|
53
|
+
self._closed = False
|
|
54
|
+
|
|
55
|
+
def _check_open(self) -> None:
|
|
56
|
+
if self._closed:
|
|
57
|
+
raise StrategyDirectoryClosedError(
|
|
58
|
+
"Strategy registration is closed after application discovery."
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def close(self) -> None:
|
|
62
|
+
"""Freeze strategy registrations and captured metadata."""
|
|
63
|
+
self._closed = True
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def closed(self) -> bool:
|
|
67
|
+
return self._closed
|
|
68
|
+
|
|
69
|
+
def register_strategy(
|
|
70
|
+
self, entity_class_name: str, strategy_class: Type, app_id: Optional[str] = None,
|
|
71
|
+
) -> None:
|
|
72
|
+
"""
|
|
73
|
+
Register a strategy class for a specific entity type.
|
|
74
|
+
|
|
75
|
+
Stores the strategy as a string path for lazy loading, which allows
|
|
76
|
+
debugpy to properly instrument modules when they're imported on-demand.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
entity_class_name: The name of the entity class this strategy applies to
|
|
80
|
+
strategy_class: The strategy class to register
|
|
81
|
+
app_id: The app whose discovery registers it
|
|
82
|
+
"""
|
|
83
|
+
self._check_open()
|
|
84
|
+
if entity_class_name not in self._directory:
|
|
85
|
+
self._directory[entity_class_name] = []
|
|
86
|
+
|
|
87
|
+
# Convert class to string path for lazy loading
|
|
88
|
+
strategy_path = f"{strategy_class.__module__}.{strategy_class.__name__}"
|
|
89
|
+
named = self._paths_by_name.setdefault(strategy_class.__name__, strategy_path)
|
|
90
|
+
if named != strategy_path:
|
|
91
|
+
raise DuplicateStrategyNameError(
|
|
92
|
+
f"Strategy name {strategy_class.__name__} is registered by both {named} and {strategy_path}"
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
# Check if strategy already registered for this entity
|
|
96
|
+
if strategy_path in self._directory[entity_class_name]:
|
|
97
|
+
logger.warning(
|
|
98
|
+
f"Strategy '{strategy_class.__name__}' already registered for '{entity_class_name}'"
|
|
99
|
+
)
|
|
100
|
+
return
|
|
101
|
+
|
|
102
|
+
self._directory[entity_class_name].append(strategy_path)
|
|
103
|
+
self._read_only[strategy_path] = getattr(strategy_class, 'read_only_strategy', False)
|
|
104
|
+
if app_id is not None:
|
|
105
|
+
self._owners[strategy_path] = app_id
|
|
106
|
+
logger.debug(f"Registered strategy path: {strategy_path} for {entity_class_name}")
|
|
107
|
+
|
|
108
|
+
def is_read_only(self, strategy_class: Type) -> bool:
|
|
109
|
+
"""Whether `strategy_class`'s `read_only_strategy` value, as captured
|
|
110
|
+
at registration, was True.
|
|
111
|
+
|
|
112
|
+
Consulted by `access.policy.required_permission()` instead of a live
|
|
113
|
+
`getattr` on the class, so a later mutation of the attribute can't
|
|
114
|
+
change what permission that decision requires.
|
|
115
|
+
"""
|
|
116
|
+
strategy_path = f"{strategy_class.__module__}.{strategy_class.__name__}"
|
|
117
|
+
return self._read_only.get(strategy_path, False)
|
|
118
|
+
|
|
119
|
+
def owner_of(self, strategy_class: Type) -> Optional[str]:
|
|
120
|
+
"""The app whose discovery registered `strategy_class`, or None."""
|
|
121
|
+
return self._owners.get(f"{strategy_class.__module__}.{strategy_class.__name__}")
|
|
122
|
+
|
|
123
|
+
def enable_debug_reload(self, enabled: bool = True) -> None:
|
|
124
|
+
"""
|
|
125
|
+
Enable or disable hot-reload of strategy modules for debugging.
|
|
126
|
+
|
|
127
|
+
When enabled, strategy modules will be reloaded before each execution,
|
|
128
|
+
allowing breakpoints to be added/modified without restarting.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
enabled: Whether to enable hot-reload mode
|
|
132
|
+
"""
|
|
133
|
+
self._debug_reload = enabled
|
|
134
|
+
if enabled:
|
|
135
|
+
logger.info("Strategy hot-reload enabled (debug mode)")
|
|
136
|
+
# Clear the class cache to force fresh imports
|
|
137
|
+
self._class_cache.clear()
|
|
138
|
+
|
|
139
|
+
def _load_strategy_class(self, strategy_path: str) -> Optional[Type]:
|
|
140
|
+
"""
|
|
141
|
+
Lazily load a strategy class from its string path.
|
|
142
|
+
|
|
143
|
+
In production mode, uses caching for performance.
|
|
144
|
+
In debug mode, forces fresh import to pick up new breakpoints.
|
|
145
|
+
|
|
146
|
+
Args:
|
|
147
|
+
strategy_path: Full path to strategy class (e.g., "module.path.ClassName")
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
The strategy class, or None if loading fails
|
|
151
|
+
"""
|
|
152
|
+
# In production, check cache first
|
|
153
|
+
if not self._debug_reload and strategy_path in self._class_cache:
|
|
154
|
+
return self._class_cache[strategy_path]
|
|
155
|
+
|
|
156
|
+
try:
|
|
157
|
+
# Split path into module and class name
|
|
158
|
+
module_path, class_name = strategy_path.rsplit('.', 1)
|
|
159
|
+
|
|
160
|
+
# In debug mode: force fresh import by clearing from sys.modules
|
|
161
|
+
if self._debug_reload:
|
|
162
|
+
if module_path in sys.modules:
|
|
163
|
+
del sys.modules[module_path]
|
|
164
|
+
logger.debug(f"Cleared {module_path} from sys.modules for fresh import")
|
|
165
|
+
|
|
166
|
+
# Import the module (fresh or cached)
|
|
167
|
+
module = importlib.import_module(module_path)
|
|
168
|
+
|
|
169
|
+
# Get the class from the module
|
|
170
|
+
if not hasattr(module, class_name):
|
|
171
|
+
logger.error(f"Module {module_path} has no attribute {class_name}")
|
|
172
|
+
return None
|
|
173
|
+
|
|
174
|
+
strategy_class = getattr(module, class_name)
|
|
175
|
+
|
|
176
|
+
# Cache in production mode only
|
|
177
|
+
if not self._debug_reload:
|
|
178
|
+
self._class_cache[strategy_path] = strategy_class
|
|
179
|
+
|
|
180
|
+
return strategy_class
|
|
181
|
+
|
|
182
|
+
except Exception as e:
|
|
183
|
+
logger.error(f"Failed to load strategy {strategy_path}: {e}")
|
|
184
|
+
return None
|
|
185
|
+
|
|
186
|
+
def get_strategies(self, entity_class_name: str) -> List[Type]:
|
|
187
|
+
"""
|
|
188
|
+
Get all strategies registered for a specific entity type.
|
|
189
|
+
|
|
190
|
+
Uses lazy loading to import strategy classes on-demand.
|
|
191
|
+
In debug mode, forces fresh imports to pick up new breakpoints.
|
|
192
|
+
|
|
193
|
+
Args:
|
|
194
|
+
entity_class_name: The name of the entity class
|
|
195
|
+
|
|
196
|
+
Returns:
|
|
197
|
+
List of strategy classes, or empty list if none found
|
|
198
|
+
"""
|
|
199
|
+
strategy_paths = self._directory.get(entity_class_name, [])
|
|
200
|
+
|
|
201
|
+
# Lazily load each strategy class from its path
|
|
202
|
+
strategy_classes = []
|
|
203
|
+
for strategy_path in strategy_paths:
|
|
204
|
+
strategy_class = self._load_strategy_class(strategy_path)
|
|
205
|
+
if strategy_class:
|
|
206
|
+
strategy_classes.append(strategy_class)
|
|
207
|
+
|
|
208
|
+
return strategy_classes
|
|
209
|
+
|
|
210
|
+
def by_name(self, strategy_name: str) -> Optional[Type]:
|
|
211
|
+
"""The registered strategy class named `strategy_name`, or None."""
|
|
212
|
+
strategy_path = self._paths_by_name.get(strategy_name)
|
|
213
|
+
return self._load_strategy_class(strategy_path) if strategy_path else None
|
|
214
|
+
|
|
215
|
+
def require(self, strategy_name: str) -> Type:
|
|
216
|
+
"""The registered strategy class named `strategy_name`; raises
|
|
217
|
+
`InvalidRequest` when there is none."""
|
|
218
|
+
strategy_class = self.by_name(strategy_name)
|
|
219
|
+
if strategy_class is None:
|
|
220
|
+
raise InvalidRequest(f"Strategy {strategy_name} is not registered.")
|
|
221
|
+
return strategy_class
|
|
222
|
+
|
|
223
|
+
def get_all_strategies(self) -> Dict[str, List[Type]]:
|
|
224
|
+
"""
|
|
225
|
+
Get all registered strategies organized by entity type.
|
|
226
|
+
|
|
227
|
+
Uses lazy loading to import strategy classes on-demand.
|
|
228
|
+
|
|
229
|
+
Returns:
|
|
230
|
+
Dictionary mapping entity class names to lists of strategy classes
|
|
231
|
+
"""
|
|
232
|
+
result = {}
|
|
233
|
+
for entity_name in self._directory:
|
|
234
|
+
result[entity_name] = self.get_strategies(entity_name)
|
|
235
|
+
return result
|
|
236
|
+
|
|
237
|
+
def get_strategy_directory(self) -> Dict[str, List[dict]]:
|
|
238
|
+
"""
|
|
239
|
+
Get a serialized version of the strategy directory for API responses.
|
|
240
|
+
|
|
241
|
+
Lazily loads strategy classes to inspect their metadata.
|
|
242
|
+
|
|
243
|
+
Returns:
|
|
244
|
+
Dictionary mapping entity class names to lists of strategy metadata
|
|
245
|
+
"""
|
|
246
|
+
result = {}
|
|
247
|
+
|
|
248
|
+
for entity_class_name, strategy_paths in self._directory.items():
|
|
249
|
+
strategy_list = []
|
|
250
|
+
|
|
251
|
+
for strategy_path in strategy_paths:
|
|
252
|
+
# Lazily load the strategy class
|
|
253
|
+
strategy_class = self._load_strategy_class(strategy_path)
|
|
254
|
+
if not strategy_class:
|
|
255
|
+
continue
|
|
256
|
+
|
|
257
|
+
strategy_info = {
|
|
258
|
+
'name': strategy_class.__name__,
|
|
259
|
+
'class_path': strategy_path,
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
# Add base classes for filtering
|
|
263
|
+
strategy_info['base_classes'] = [
|
|
264
|
+
base.__name__ for base in strategy_class.__mro__[1:]
|
|
265
|
+
if base.__name__ not in ('object', 'ABC')
|
|
266
|
+
]
|
|
267
|
+
|
|
268
|
+
# Add description if available
|
|
269
|
+
if hasattr(strategy_class, 'strategy_description'):
|
|
270
|
+
strategy_info['description'] = strategy_class.strategy_description
|
|
271
|
+
|
|
272
|
+
# Add request schema if available
|
|
273
|
+
if hasattr(strategy_class, 'get_request_schema'):
|
|
274
|
+
try:
|
|
275
|
+
strategy_info['schema'] = strategy_class.get_request_schema()
|
|
276
|
+
except Exception as e:
|
|
277
|
+
logger.warning(
|
|
278
|
+
f"Could not get schema for {strategy_class.__name__}: {e}"
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
strategy_list.append(strategy_info)
|
|
282
|
+
|
|
283
|
+
result[entity_class_name] = strategy_list
|
|
284
|
+
|
|
285
|
+
return result
|
|
286
|
+
|
|
287
|
+
def get_strategy_classes(self) -> Dict[str, List[Type]]:
|
|
288
|
+
"""
|
|
289
|
+
Get strategy classes in the same format as the old StrategyDirectory.
|
|
290
|
+
|
|
291
|
+
This method provides backward compatibility.
|
|
292
|
+
|
|
293
|
+
Returns:
|
|
294
|
+
Dictionary mapping entity class names to lists of strategy classes
|
|
295
|
+
"""
|
|
296
|
+
return self.get_all_strategies()
|
|
297
|
+
|
|
298
|
+
def register_pending(self, registrations, app_id: Optional[str] = None) -> None:
|
|
299
|
+
"""
|
|
300
|
+
Register (entity_class, strategy_class) pairs discovered under an owning app.
|
|
301
|
+
"""
|
|
302
|
+
for entity_class, strategy_class in registrations:
|
|
303
|
+
self.register_strategy(entity_class.__name__, strategy_class, app_id)
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
# Global singleton instance
|
|
307
|
+
StrategyDirectory = StrategyDirectoryClass()
|
|
308
|
+
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Where a type-level fact belongs
|
|
2
|
+
|
|
3
|
+
Every entity/strategy type carries facts beyond its code: `shareable`,
|
|
4
|
+
`principal`, `read`, `delete`, `can_contain`, `contained_by`, `ownerless`, `write_once_fields`,
|
|
5
|
+
`CPU_BOUND`, `read_only_strategy`, a type's strategy allowlist and
|
|
6
|
+
required permissions, and more to come. Each new one raises the same
|
|
7
|
+
question: does it belong on the class itself (a plain attribute), or in an
|
|
8
|
+
external registration/policy declaration (a `register_entity`/
|
|
9
|
+
`register_strategy` kwarg, or an `access.policy` call)?
|
|
10
|
+
|
|
11
|
+
Two independent tests. Either one alone forces the external form.
|
|
12
|
+
|
|
13
|
+
## 1. Relational test
|
|
14
|
+
|
|
15
|
+
Does the fact pair this type with another, independently-defined type — an
|
|
16
|
+
entity with a specific strategy class, an entity with a specific role —
|
|
17
|
+
rather than describing this type alone?
|
|
18
|
+
|
|
19
|
+
A pairing can't live as an attribute on either side without one importing
|
|
20
|
+
the other: the entity's module would have to import every strategy that
|
|
21
|
+
might ever target it (impossible — another app can define one without the
|
|
22
|
+
entity author knowing), and the reverse has the same problem. A neutral,
|
|
23
|
+
pair-keyed store is the only place a fact about two independently-authored
|
|
24
|
+
classes can live without an import cycle.
|
|
25
|
+
|
|
26
|
+
This is why `policy.define` and `policy.extend` (`auth/authz/policy.py`)
|
|
27
|
+
exist at all, independent of the test below.
|
|
28
|
+
|
|
29
|
+
## 2. Consistency test
|
|
30
|
+
|
|
31
|
+
Is the fact consulted at more than one decision point that must agree with
|
|
32
|
+
each other within a single request (e.g. the executor's invocation check and
|
|
33
|
+
`EntityService`'s persistence chokepoint reading the same value), or must
|
|
34
|
+
every request observe the same answer for the system's own guarantees to
|
|
35
|
+
hold?
|
|
36
|
+
|
|
37
|
+
If so, the fact is boot-time configuration: capture it once, before serving
|
|
38
|
+
any request, into a frozen store — never re-read live off a mutable
|
|
39
|
+
attribute, because a live read can disagree with itself across those
|
|
40
|
+
decision points, or drift across requests.
|
|
41
|
+
|
|
42
|
+
This is **not** specifically an authorization test, even though every
|
|
43
|
+
current example is authorization-relevant — it's a determinism test.
|
|
44
|
+
`shareable` fails it (read by `allows`, subject validation, broadcast
|
|
45
|
+
routing), which is why it is declared with `policy.define` and frozen when
|
|
46
|
+
the policy closes. `read` (read by the engine's reads, the invocation check
|
|
47
|
+
and broadcast routing), `delete` (read at the deletion chokepoint) and
|
|
48
|
+
`can_contain` and `contained_by` (read at the persistence chokepoint, and reported by
|
|
49
|
+
`graphcore policy`) are policy for the same reason. `principal` fails it
|
|
50
|
+
too and is frozen when `EntityDirectory` closes. Whether a type is a group
|
|
51
|
+
is not a declared fact at all: it is `issubclass(cls, Group)`, which
|
|
52
|
+
cannot change after the class exists.
|
|
53
|
+
|
|
54
|
+
A fact read at exactly one decision site, with nothing else required to
|
|
55
|
+
agree with it and no cross-request invariant depending on it staying
|
|
56
|
+
constant, does not need this — e.g. `Strategy.CPU_BOUND` (read once, at
|
|
57
|
+
dispatch, by the executor's routing decision alone),
|
|
58
|
+
`Strategy.param_schema` (read only by offline codegen), or
|
|
59
|
+
`Entity.ownerless` and `Entity.write_once_fields` (read only by
|
|
60
|
+
`EntityService.save_to_db`).
|
|
61
|
+
|
|
62
|
+
## Otherwise
|
|
63
|
+
|
|
64
|
+
If neither test fires, the fact is an ordinary property of one class,
|
|
65
|
+
inherited and overridable the normal Python way: a plain class attribute,
|
|
66
|
+
read live, no registry entry, no freeze.
|
|
File without changes
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"""Discover graphcore apps visible to a project (entry points + apps/ scan)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
from importlib.metadata import entry_points
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _entry_point_apps() -> list[str]:
|
|
11
|
+
eps = entry_points()
|
|
12
|
+
group = eps.select(group="graphcore.apps") if hasattr(eps, "select") else eps.get("graphcore.apps", [])
|
|
13
|
+
return sorted({ep.value for ep in group})
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def agent_frontend_module(app_name: str) -> str:
|
|
17
|
+
"""The module name of an app's agent frontend package."""
|
|
18
|
+
return f"{app_name}_agent_frontend"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _backend(app_dir: Path) -> tuple[Path, str]:
|
|
22
|
+
return app_dir / "backend", app_dir.name
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _agent_frontend(app_dir: Path) -> tuple[Path, str]:
|
|
26
|
+
return app_dir / "backend", agent_frontend_module(app_dir.name)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _in_project(apps_dir: Path, *layouts) -> list[tuple[Path, str]]:
|
|
30
|
+
"""``(root, module)`` per in-project package: ``apps/<name>/backend/<name>/``
|
|
31
|
+
and ``apps/<name>/backend/<name>_agent_frontend/`` (same nests as published)."""
|
|
32
|
+
if not apps_dir.is_dir():
|
|
33
|
+
return []
|
|
34
|
+
found = []
|
|
35
|
+
for child in sorted(apps_dir.iterdir()):
|
|
36
|
+
for layout in layouts:
|
|
37
|
+
root, module = layout(child)
|
|
38
|
+
if (root / module / "__init__.py").is_file():
|
|
39
|
+
found.append((root, module))
|
|
40
|
+
return found
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _scan_apps_dir(apps_dir: Path) -> list[str]:
|
|
44
|
+
return [module for _, module in _in_project(apps_dir, _backend)]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def add_in_project_apps_to_path(apps_dir: Path | str) -> None:
|
|
48
|
+
"""Put each in-project package root on ``sys.path`` so ``import <name>`` and
|
|
49
|
+
``import <name>_agent_frontend`` work."""
|
|
50
|
+
for root, _ in _in_project(Path(apps_dir), _backend, _agent_frontend):
|
|
51
|
+
path = str(root.resolve())
|
|
52
|
+
if path not in sys.path:
|
|
53
|
+
sys.path.insert(0, path)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def discover_graphcore_apps(apps_dir: Path | str) -> list[str]:
|
|
57
|
+
"""Django app labels for every graphcore app visible to this project.
|
|
58
|
+
|
|
59
|
+
Installed packages exposing a ``graphcore.apps`` entry point, plus
|
|
60
|
+
each ``apps/<name>/backend/<name>/`` package. Deterministic order
|
|
61
|
+
(sorted entry points, then sorted directories), deduplicated.
|
|
62
|
+
"""
|
|
63
|
+
apps_dir = Path(apps_dir)
|
|
64
|
+
seen: set[str] = set()
|
|
65
|
+
result: list[str] = []
|
|
66
|
+
for label in _entry_point_apps() + _scan_apps_dir(apps_dir):
|
|
67
|
+
if label not in seen:
|
|
68
|
+
seen.add(label)
|
|
69
|
+
result.append(label)
|
|
70
|
+
return result
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def list_apps_with_provenance(apps_dir: Path | str) -> list[tuple[str, str]]:
|
|
74
|
+
"""Like discover, but each item is ``(label, provenance)``."""
|
|
75
|
+
apps_dir = Path(apps_dir)
|
|
76
|
+
eps = entry_points()
|
|
77
|
+
group = eps.select(group="graphcore.apps") if hasattr(eps, "select") else eps.get("graphcore.apps", [])
|
|
78
|
+
by_value = {ep.value: ep for ep in group}
|
|
79
|
+
seen: set[str] = set()
|
|
80
|
+
result: list[tuple[str, str]] = []
|
|
81
|
+
for label in sorted(by_value):
|
|
82
|
+
ep = by_value[label]
|
|
83
|
+
dist = ep.dist.name if ep.dist is not None else "unknown"
|
|
84
|
+
result.append((label, f"entry point: {dist}"))
|
|
85
|
+
seen.add(label)
|
|
86
|
+
for label in _scan_apps_dir(apps_dir):
|
|
87
|
+
if label not in seen:
|
|
88
|
+
result.append((label, "apps/ directory"))
|
|
89
|
+
seen.add(label)
|
|
90
|
+
return result
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
AGENT_FRONTEND_ENTRY_POINT_GROUP = "graphcore.agent_frontends"
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def discover_agent_frontends(apps_dir: Path | str) -> list[str]:
|
|
97
|
+
"""Module names of every agent frontend visible to this project.
|
|
98
|
+
|
|
99
|
+
Installed packages exposing a ``graphcore.agent_frontends`` entry point,
|
|
100
|
+
plus each ``apps/<name>/backend/<name>_agent_frontend/`` package,
|
|
101
|
+
which this puts on ``sys.path``. Same order and deduplication as
|
|
102
|
+
``discover_graphcore_apps``.
|
|
103
|
+
"""
|
|
104
|
+
apps_dir = Path(apps_dir)
|
|
105
|
+
add_in_project_apps_to_path(apps_dir)
|
|
106
|
+
group = entry_points().select(group=AGENT_FRONTEND_ENTRY_POINT_GROUP)
|
|
107
|
+
scanned = [module for _, module in _in_project(apps_dir, _agent_frontend)]
|
|
108
|
+
return list(dict.fromkeys([*sorted({ep.value for ep in group}), *scanned]))
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Decorators for registering entities and strategies with the directory system.
|
|
3
|
+
|
|
4
|
+
These decorators mark classes for registration but don't execute registration
|
|
5
|
+
immediately. Registration happens during Django startup, in
|
|
6
|
+
``graphcore.registry.discovery``, which also stamps each class with the id of
|
|
7
|
+
the app that owns it.
|
|
8
|
+
|
|
9
|
+
Deciding whether a new type-level fact belongs here (a ``register_entity``/
|
|
10
|
+
``register_strategy`` kwarg or an ``access.policy`` call) versus as a plain
|
|
11
|
+
class attribute: see ``TYPE_METADATA_PLACEMENT.md`` in this directory.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
_pending_entity_registrations = []
|
|
15
|
+
_pending_strategy_registrations = []
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def register_entity(app_label=None, principal=False):
|
|
19
|
+
"""
|
|
20
|
+
Mark an entity class for registration.
|
|
21
|
+
|
|
22
|
+
The entity is registered with a stable ``entity_type_id`` of
|
|
23
|
+
``<app id>.<ClassName>``. The app id is resolved from the Django app that
|
|
24
|
+
owns the class's module (see ``EntityDirectory.get_entity_type_id``); pass
|
|
25
|
+
``app_label`` only to override that.
|
|
26
|
+
|
|
27
|
+
``principal`` marks the type as eligible to act - a valid grant
|
|
28
|
+
subject in its own right (alongside groups and ``PUBLIC``)
|
|
29
|
+
and a valid backing type for a ``Principal``. The kernel registers two:
|
|
30
|
+
``User`` and ``ServiceAccount``.
|
|
31
|
+
Whether a type can be shared is authorization policy, declared with
|
|
32
|
+
``policy.define``. See ``docs/AUTHORIZATION_TECHNICAL_TDD.md`` (Type
|
|
33
|
+
policy, Groups). A group is ``Group`` or a subclass of it; see
|
|
34
|
+
``domain/entity/group/README.md``.
|
|
35
|
+
|
|
36
|
+
Usage:
|
|
37
|
+
@register_entity()
|
|
38
|
+
class Document(Entity):
|
|
39
|
+
...
|
|
40
|
+
|
|
41
|
+
@register_entity(principal=True)
|
|
42
|
+
class User(Entity):
|
|
43
|
+
...
|
|
44
|
+
"""
|
|
45
|
+
def decorator(cls):
|
|
46
|
+
_pending_entity_registrations.append((cls, app_label, principal))
|
|
47
|
+
return cls
|
|
48
|
+
return decorator
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def register_strategy(entity_class):
|
|
52
|
+
"""
|
|
53
|
+
Mark a strategy class for registration against a specific entity type.
|
|
54
|
+
|
|
55
|
+
Usage:
|
|
56
|
+
@register_strategy(Document)
|
|
57
|
+
class ScrapeFilePath(Strategy):
|
|
58
|
+
...
|
|
59
|
+
"""
|
|
60
|
+
def decorator(strategy_cls):
|
|
61
|
+
_pending_strategy_registrations.append((entity_class, strategy_cls))
|
|
62
|
+
return strategy_cls
|
|
63
|
+
return decorator
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def get_pending_entity_registrations():
|
|
67
|
+
"""Get all pending (entity_class, app_label_override) registrations and clear the list"""
|
|
68
|
+
registrations = _pending_entity_registrations.copy()
|
|
69
|
+
_pending_entity_registrations.clear()
|
|
70
|
+
return registrations
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def get_pending_strategy_registrations():
|
|
74
|
+
"""Get all pending strategy registrations and clear the list"""
|
|
75
|
+
registrations = _pending_strategy_registrations.copy()
|
|
76
|
+
_pending_strategy_registrations.clear()
|
|
77
|
+
return registrations
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Discovery driver: imports every installed graphcore app's entity and strategy
|
|
3
|
+
modules, then drains the decorators' pending registrations into the directories.
|
|
4
|
+
|
|
5
|
+
Django populates its app registry in full before calling any app's ``ready()``,
|
|
6
|
+
so iterating app configs here makes registration independent of INSTALLED_APPS
|
|
7
|
+
ordering. Apps carry no discovery boilerplate: declaring a GraphCoreAppConfig is
|
|
8
|
+
the whole opt-in.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import importlib
|
|
12
|
+
import logging
|
|
13
|
+
import pkgutil
|
|
14
|
+
|
|
15
|
+
from django.apps import apps
|
|
16
|
+
from django.utils.module_loading import import_string
|
|
17
|
+
|
|
18
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
19
|
+
from graphcore.registry.decorators import (
|
|
20
|
+
get_pending_entity_registrations,
|
|
21
|
+
get_pending_strategy_registrations,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger(__name__)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def discover_all() -> None:
|
|
28
|
+
"""Import and register every installed graphcore app's types."""
|
|
29
|
+
for config in apps.get_app_configs():
|
|
30
|
+
app_id = getattr(config, 'graphcore_app_id', None)
|
|
31
|
+
if app_id is None:
|
|
32
|
+
continue
|
|
33
|
+
|
|
34
|
+
for package_name in getattr(config, 'graphcore_packages', ('entities', 'strategies')):
|
|
35
|
+
_import_package(f"{config.name}.{package_name}")
|
|
36
|
+
|
|
37
|
+
DEFAULT_REGISTRY.register_pending(
|
|
38
|
+
get_pending_entity_registrations(),
|
|
39
|
+
get_pending_strategy_registrations(),
|
|
40
|
+
app_id,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
# All entity and strategy classes must be registered before any
|
|
44
|
+
# configurator runs. This gives persistence and authorization the same
|
|
45
|
+
# explicit startup contract: persistence first (so a policy configurator
|
|
46
|
+
# could depend on storage being bound), each in INSTALLED_APPS order.
|
|
47
|
+
for hook_name, target in (
|
|
48
|
+
('graphcore_persistence', DEFAULT_REGISTRY.entities),
|
|
49
|
+
('graphcore_authorization', DEFAULT_REGISTRY.policy),
|
|
50
|
+
):
|
|
51
|
+
for config in apps.get_app_configs():
|
|
52
|
+
configurator_path = getattr(config, hook_name, None)
|
|
53
|
+
if not configurator_path:
|
|
54
|
+
continue
|
|
55
|
+
if hook_name == 'graphcore_authorization':
|
|
56
|
+
with target.declaring_app(config.graphcore_app_id):
|
|
57
|
+
import_string(configurator_path)(target)
|
|
58
|
+
else:
|
|
59
|
+
import_string(configurator_path)(target)
|
|
60
|
+
|
|
61
|
+
for config in apps.get_app_configs():
|
|
62
|
+
routes_path = getattr(config, 'graphcore_routes', None)
|
|
63
|
+
if routes_path:
|
|
64
|
+
with DEFAULT_REGISTRY.routes.declaring_app(config.graphcore_app_id):
|
|
65
|
+
import_string(routes_path)(DEFAULT_REGISTRY.routes)
|
|
66
|
+
|
|
67
|
+
for config in apps.get_app_configs():
|
|
68
|
+
hook_path = getattr(config, 'graphcore_principal_created', None)
|
|
69
|
+
hook = import_string(hook_path) if hook_path else None
|
|
70
|
+
if hook is not None and hook not in DEFAULT_REGISTRY.principal_hooks:
|
|
71
|
+
DEFAULT_REGISTRY.principal_hooks.append(hook)
|
|
72
|
+
|
|
73
|
+
DEFAULT_REGISTRY.finalize()
|
|
74
|
+
logger.info(
|
|
75
|
+
"Registered %d entity types",
|
|
76
|
+
len(DEFAULT_REGISTRY.entities.get_all_entities()),
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _is_test_module(modname: str) -> bool:
|
|
81
|
+
"""Match Django's convention: skip a `tests`/`test` package or a `test_*` module."""
|
|
82
|
+
return any(
|
|
83
|
+
segment == 'tests' or segment == 'test' or segment.startswith('test_')
|
|
84
|
+
for segment in modname.split('.')
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _import_package(package_path: str) -> None:
|
|
89
|
+
"""Import a package and all its submodules, skipping tests.
|
|
90
|
+
|
|
91
|
+
A package that genuinely doesn't exist for this app is fine - most apps
|
|
92
|
+
have no strategies package, say. Anything else, a broken module or a
|
|
93
|
+
missing third-party dependency inside the package, must stop startup
|
|
94
|
+
rather than being silently swallowed as "app has no such package".
|
|
95
|
+
"""
|
|
96
|
+
try:
|
|
97
|
+
package = importlib.import_module(package_path)
|
|
98
|
+
except ModuleNotFoundError as exc:
|
|
99
|
+
if exc.name == package_path:
|
|
100
|
+
return
|
|
101
|
+
raise
|
|
102
|
+
|
|
103
|
+
for _, modname, _ in pkgutil.walk_packages(package.__path__, package.__name__ + "."):
|
|
104
|
+
if _is_test_module(modname):
|
|
105
|
+
continue
|
|
106
|
+
try:
|
|
107
|
+
importlib.import_module(modname)
|
|
108
|
+
except Exception as exc:
|
|
109
|
+
raise ImportError(
|
|
110
|
+
f"Failed to import '{modname}' (app package '{package_path}'): {exc}"
|
|
111
|
+
) from exc
|