magpie.cli.sync_resources¶
Synchronize local and remote resources.
To implement a new service, see magpie.cli.sync_services.SyncServiceInterface.
See also
Attributes¶
Classes¶
Functions¶
Obtains the children resource node structure of the input service or resource. |
|
|
Validates the resource structure. |
|
Defines argument parser options for logging operations. |
|
Uses argument parser options to setup logging level from specified flags. |
|
Immediately sets the logger level to avoid duplicate log outputs from the root logger and this logger when |
Main function to sync resources with remote server. |
|
|
Merge resources_local and resources_remote, adding the following keys to the output: |
|
Sorts a nested resource dictionary. |
|
Make sure the RemoteResourcesSyncInfo entry exists in the database. |
|
Request remote resources, depending on service type. |
|
Delete all RemoteResource based on a Service.resource_id. |
|
Creates a main resource for a service, whether one currently exists or not. |
|
Writes remote resources to database. |
|
|
Reads remote resources from the RemoteResources table. No external request is made. |
|
|
Obtain the date-time of the last known sync event for a service. |
|
Get remote resources for all services of a certain type. |
|
Get remote resources for a single service. |
|
Main function to get all remote resources for each service and write to database. |
|
|
|
|
|
Main entry point for cron service. |
Module Contents¶
- magpie.cli.sync_resources.get_resource_children(resource: magpie.typedefs.ServiceOrResourceType, db_session: sqlalchemy.orm.session.Session, tree_service_builder: ziggurat_foundations.models.services.resource_tree.ResourceTreeService | None = None, limit_depth: int | None = None) magpie.typedefs.NestedResourceNodes[source]¶
Obtains the children resource node structure of the input service or resource.
- Parameters:
resource – Initial resource where to start building the tree from.
db_session – Database connection to retrieve resources.
tree_service_builder – Utility that build the tree (default:
models.RESOURCE_TREE_SERVICE).limit_depth – Maximum depth to look for children resources (very deep if not specified, could be slow).
- Returns:
{node: Resource, children: {node_id: <recursive>}}
- magpie.cli.sync_resources.SYNC_SERVICES_TYPES: Dict[magpie.typedefs.Str, Type[SyncServiceInterface]][source]¶
- class magpie.cli.sync_resources.SyncServiceDefault(service_name, url)[source]¶
Bases:
SyncServiceInterface- property max_depth: None¶
The max depth at which remote resources are fetched.
- magpie.cli.sync_resources.is_valid_resource_schema(resources: magpie.typedefs.AnyNestedChildrenTree) bool[source]¶
Validates the resource structure.
Expected dictionary is a tree of the following form:
{ "resource_name_1": { "children": { "resource_name_3": {"children": {}}, "resource_name_4": {"children": {}} } } "resource_name_2": {"children": {}} }
- magpie.cli.sync_resources.make_logging_options(parser: argparse.ArgumentParser) None[source]¶
Defines argument parser options for logging operations.
- magpie.cli.sync_resources.setup_logger_from_options(logger: logging.Logger, args: argparse.Namespace) None[source]¶
Uses argument parser options to setup logging level from specified flags.
Setup both the specific CLI logger that is provided and the generic magpie logger.
- magpie.cli.sync_resources.get_logger(name: magpie.typedefs.Str, level: int | None = None, force_stdout: bool = None, message_format: magpie.typedefs.Str | None = None, datetime_format: magpie.typedefs.Str | None = None) logging.Logger[source]¶
Immediately sets the logger level to avoid duplicate log outputs from the root logger and this logger when level is
logging.NOTSET.
- magpie.cli.sync_resources.merge_local_and_remote_resources(resources_local: magpie.typedefs.RemoteResourceTree, service_sync_type: magpie.typedefs.Str, service_id: int, session: sqlalchemy.orm.session.Session) magpie.typedefs.RemoteResourceTree[source]¶
Main function to sync resources with remote server.
- magpie.cli.sync_resources._merge_resources(resources_local: magpie.typedefs.RemoteResourceTree, resources_remote: magpie.typedefs.RemoteResourceTree, max_depth: int | None = None) magpie.typedefs.RemoteResourceTree[source]¶
Merge resources_local and resources_remote, adding the following keys to the output:
remote_id: id of the RemoteResource
matches_remote: True or False depending if the resource is present on the remote server
- Returns:
Dictionary tree of the merged resources.
- magpie.cli.sync_resources._sort_resources(resources: magpie.typedefs.RemoteResourceTree) None[source]¶
Sorts a nested resource dictionary.
The dictionary is expected to be valid as per
sync_services.is_valid_resource_schema().- Returns:
None. Inplace modification.
- magpie.cli.sync_resources._ensure_sync_info_exists(service_resource_id: int, session: sqlalchemy.orm.session.Session) None[source]¶
Make sure the RemoteResourcesSyncInfo entry exists in the database.
- magpie.cli.sync_resources._get_remote_resources(service: magpie.models.Service) magpie.typedefs.JSON[source]¶
Request remote resources, depending on service type.
- Parameters:
service – service for which to fetch remove children resources
- Returns:
nested content as
{"node_name": {"children": JSON, "resource_type": "resource_type"}}
- magpie.cli.sync_resources._delete_records(service_id, session)[source]¶
Delete all RemoteResource based on a Service.resource_id.
- magpie.cli.sync_resources._create_main_resource(service_id: int, session: sqlalchemy.orm.session.Session) None[source]¶
Creates a main resource for a service, whether one currently exists or not.
Each RemoteResourcesSyncInfo has a main RemoteResource of the same name as the service. This is similar to the Service and Resource relationship.
- magpie.cli.sync_resources._update_db(remote_resources: magpie.typedefs.RemoteResourceTree, service_id: int, session: sqlalchemy.orm.session.Session) None[source]¶
Writes remote resources to database.
- magpie.cli.sync_resources._format_resource_tree(children: magpie.typedefs.ServiceResourceNodeTree) magpie.typedefs.RemoteResourceTree[source]¶
- magpie.cli.sync_resources._query_remote_resources_in_database(service_id: int, session: sqlalchemy.orm.session.Session) magpie.typedefs.RemoteResourceTree[source]¶
Reads remote resources from the RemoteResources table. No external request is made.
- Returns:
Dictionary of the form defined in
sync_services.is_valid_resource_schema().
- magpie.cli.sync_resources.get_last_sync(service_id: int, session: sqlalchemy.orm.session.Session) datetime.datetime | None[source]¶
Obtain the date-time of the last known sync event for a service.
- magpie.cli.sync_resources.fetch_all_services_by_type(service_type: magpie.typedefs.Str, session: sqlalchemy.orm.session.Session) None[source]¶
Get remote resources for all services of a certain type.
- Parameters:
service_type – Service type for which all corresponding services will be synchronized.
session – Database connexion to apply synchronization changes.
- magpie.cli.sync_resources.fetch_single_service(service: magpie.models.Service | int, session: sqlalchemy.orm.session.Session) None[source]¶
Get remote resources for a single service.
- Parameters:
service – Specific service for which to synchronize remote resources.
session – Database connexion to apply synchronization changes.
- magpie.cli.sync_resources.fetch(settings: magpie.typedefs.SettingsType | None = None) None[source]¶
Main function to get all remote resources for each service and write to database.