# Source code for connector.components.synchronizer

# -*- coding: utf-8 -*-

"""

Synchronizer
============

A synchronizer orchestrates a synchronization with a backend. It's the actor
who runs the flow and glues the logic of an import or export (or else).
It uses other components for specialized tasks.

For instance, it will use the mappings to convert the data between both
systems, the backend adapters to read or write data on the backend and the
binders to create the link between them.

"""

[docs]class Synchronizer(AbstractComponent):
""" Base class for synchronizers """

_name = 'base.synchronizer'
_inherit = 'base.connector'

#: usage of the component used as mapper, can be customized in sub-classes
_base_mapper_usage = 'mapper'
#: usage of the component used as backend adapter,
#: can be customized in sub-classes

def __init__(self, work_context):
super(Synchronizer, self).__init__(work_context)
self._binder = None
self._mapper = None

[docs]    def run(self):
""" Run the synchronization """
raise NotImplementedError

@property
def mapper(self):
""" Return an instance of Mapper for the synchronization.

The instanciation is delayed because some synchronisations do
not need such an unit and the unit may not exist.

It looks for a Component with _usage being equal to
_base_mapper_usage.

:rtype: :py:class:odoo.addons.component.core.Component
"""
if self._mapper is None:
self._mapper = self.component(usage=self._base_mapper_usage)
return self._mapper

@property
def binder(self):
""" Return an instance of Binder for the synchronization.

The instanciation is delayed because some synchronisations do
not need such an unit and the unit may not exist.

:rtype: :py:class:odoo.addons.component.core.Component
"""
if self._binder is None:
self._binder = self.binder_for()
return self._binder

@property
""" Return an instance of BackendAdapter for the
synchronization.

The instanciation is delayed because some synchronisations do
not need such an unit and the unit may not exist.

It looks for a Component with _usage being equal to
_base_backend_adapter_usage.

:rtype: :py:class:odoo.addons.component.core.Component
"""
)

[docs]class Exporter(AbstractComponent):
""" Synchronizer for exporting data from Odoo to a backend """

_name = 'base.exporter'
_inherit = 'base.synchronizer'
_usage = 'exporter'
#: usage of the component used as mapper, can be customized in sub-classes
_base_mapper_usage = 'export.mapper'

[docs]class Importer(AbstractComponent):
""" Synchronizer for importing data from a backend to Odoo """

_name = 'base.importer'
_inherit = 'base.synchronizer'
_usage = 'importer'
#: usage of the component used as mapper, can be customized in sub-classes
_base_mapper_usage = 'import.mapper'

[docs]class Deleter(AbstractComponent):
""" Synchronizer for deleting a record on the backend """

_name = 'base.deleter'
_inherit = 'base.synchronizer'
#: usage of the component used as mapper, can be customized in sub-classes
_usage = 'deleter'