Changeset - 3619e58c1ef5
[Not reviewed]
0 7 1
Lance Edgar (lance) - 3 years ago 2021-12-30 15:48:50
lance@edbob.org
Deprecate the name `NewDataSyncConsumer` and update docs
8 files changed with 107 insertions and 20 deletions:
0 comments (0 inline, 0 general)
docs/api/index.rst
Show inline comments
 
@@ -21,6 +21,7 @@ attributes and method signatures etc.
 
   rattail/csvutil
 
   rattail/datasync/index
 
   rattail/datasync/consumers
 
   rattail/datasync/rattail
 
   rattail/db/auth
 
   rattail/db/cache
 
   rattail/db/changes
docs/api/rattail/datasync/consumers.rst
Show inline comments
 
@@ -3,4 +3,15 @@
 
==============================
 

	
 
.. automodule:: rattail.datasync.consumers
 

	
 
.. autoclass:: DataSyncConsumer
 
   :members:
 

	
 
.. autoclass:: DataSyncImportConsumer
 
   :members:
 

	
 
.. autoclass:: FromRattailConsumer
 

	
 
.. autoclass:: NullTestConsumer
 

	
 
.. autoclass:: ErrorTestConsumer
docs/api/rattail/datasync/index.rst
Show inline comments
 
@@ -3,3 +3,9 @@
 
====================
 

	
 
.. automodule:: rattail.datasync
 

	
 
.. toctree::
 
   :maxdepth: 1
 

	
 
   consumers
 
   rattail
docs/api/rattail/datasync/rattail.rst
Show inline comments
 
new file 100644
 

	
 
``rattail.datasync.rattail``
 
============================
 

	
 
.. automodule:: rattail.datasync.rattail
 
   :members:
rattail/app.py
Show inline comments
 
@@ -29,6 +29,7 @@ from __future__ import unicode_literals, absolute_import
 
import os
 
# import re
 
import tempfile
 
import warnings
 

	
 
import six
 

	
 
@@ -330,7 +331,7 @@ class AppHandler(object):
 

	
 
        return designated
 

	
 
    def get_designated_import_handler(self, key, require=False, **kwargs):
 
    def get_import_handler(self, key, require=False, **kwargs):
 
        """
 
        Return the designated import/export handler instance, per the
 
        given handler type key.
 
@@ -368,6 +369,12 @@ class AppHandler(object):
 
            raise RuntimeError("Cannot locate designated handler for key: {}".format(
 
                key))
 

	
 
    def get_designated_import_handler(self, *args, **kwargs):
 
        warnings.warn("method is deprecated, please use "
 
                      "AppHandler.get_import_handler() instead",
 
                      DeprecationWarning)
 
        return self.get_import_handler(*args, **kwargs)
 

	
 
    def get_designated_import_handler_spec(self, key, require=False, **kwargs):
 
        spec = self.config.get('rattail.importing',
 
                               '{}.handler'.format(key))
rattail/datasync/__init__.py
Show inline comments
 
@@ -27,5 +27,9 @@ DataSync Daemon
 
from __future__ import unicode_literals, absolute_import
 

	
 
from .watchers import DataSyncWatcher
 
from .consumers import DataSyncConsumer, NewDataSyncImportConsumer, FromRattailConsumer
 
from .consumers import (DataSyncConsumer,
 
                        DataSyncImportConsumer,
 
                        # TODO: deprecate / remove this
 
                        NewDataSyncImportConsumer,
 
                        FromRattailConsumer)
 
from .rattail import FromRattailToRattailExportConsumer, FromRattailToRattailImportConsumer
rattail/datasync/consumers.py
Show inline comments
 
@@ -26,6 +26,8 @@ DataSync Consumers
 

	
 
from __future__ import unicode_literals, absolute_import
 

	
 
import warnings
 

	
 
from rattail import importing
 
from rattail.db import Session
 
from rattail.config import parse_list
 
@@ -102,19 +104,45 @@ class DataSyncConsumer(object):
 
        """
 

	
 

	
 
class NewDataSyncImportConsumer(DataSyncConsumer):
 
class DataSyncImportConsumer(DataSyncConsumer):
 
    """
 
    Base class for DataSync consumer which is able to leverage a (set of)
 
    importer(s) to do the heavy lifting.
 
    Base class for any DataSync Consumer which leverages an Import
 
    Handler (and ultimately its Importers) to do the heavy lifting.
 
    See :doc:`rattail-manual:data/importing/index` for more about
 
    Importers etc.
 

	
 
    .. attribute:: handler
 

	
 
       Reference to the import/export handler to be leveraged by this
 
       datasync consumer.
 

	
 
       The handler is automatically instantiated when the consumer is
 
       instantiated, usually based on :attr:`handler_key`.
 

	
 
    .. attribute:: handler_key
 

	
 
       This should be the "key" of the import handler to be leveraged
 
       by the datasync consumer.  This will be a string designating
 
       the data target/source/direction, for example
 
       ``'to_rattail.from_corepos_api.import'``.
 

	
 
    .. note::
 
       This assumes "new-style" importers based on
 
       ``rattail.importing.Importer``.
 
       The handler itself is obtained by invoking
 
       :meth:`rattail.app.AppHandler.get_import_handler()`.
 

	
 
       Note that for now, ``handler_key`` is not strictly required, as
 
       :attr:`handler_spec` will be used as a fallback.  But
 
       eventually that will likely change, with ``handler_spec`` being
 
       deprecated altogether and ``handler_key`` becoming required
 
       (unless you override :meth:`get_handler()`).
 

	
 
    .. attribute:: handler_spec
 

	
 
       This should be a "spec" string referencing the import handler class from
 
       which the importers should be obtained.
 
       This attribute should be considered deprecated; please use
 
       :attr:`handler_key` instead.
 

	
 
       If this attribute is used, it should be a "spec" string
 
       referencing the import handler class from which the importers
 
       should be obtained.
 

	
 
    .. attribute:: default_importers_only
 

	
 
@@ -136,22 +164,37 @@ class NewDataSyncImportConsumer(DataSyncConsumer):
 

	
 
       This is a list of model names, as they are found in the import handler.
 
    """
 
    # TODO: deprecate / remove this?
 
    handler_key = None
 

	
 
    # TODO: deprecate / remove this
 
    handler_spec = None
 

	
 
    handler_key = None
 
    default_importers_only = True
 
    model_map = {}
 
    skip_local_models = []
 

	
 
    def __init__(self, *args, **kwargs):
 
        super(NewDataSyncImportConsumer, self).__init__(*args, **kwargs)
 
        super(DataSyncImportConsumer, self).__init__(*args, **kwargs)
 

	
 
        spec = kwargs.pop('handler_spec', None)
 
        self.handler = self.get_handler(spec)
 
        self.importers = self.get_importers()
 

	
 
    def get_handler(self, spec):
 
    def get_handler(self, spec=None):
 
        """
 
        Return the appropriate import handler instance.
 

	
 
        If ``spec`` is provided, handler will conform to that.
 
        Otherwise the "designated" handler is got according to
 
        :attr:`handler_key`, by invoking
 
        :meth:`rattail.app.AppHandler.get_import_handler()`.  Or if
 
        ``handler_key`` is not set, then for now, :attr:`handler_spec`
 
        may be used as fallback.
 

	
 
        An error of some sort will be raised if no handler can be got.
 

	
 
        :returns: The import handler instance.
 
        """
 
        if not spec:
 
            if self.handler_key:
 
                return self.app.get_designated_import_handler(self.handler_key,
 
@@ -282,7 +325,16 @@ class NewDataSyncImportConsumer(DataSyncConsumer):
 
        return importer.get_local_object(key)
 

	
 

	
 
class FromRattailConsumer(NewDataSyncImportConsumer):
 
class NewDataSyncImportConsumer(DataSyncImportConsumer):
 

	
 
    def __init__(self, *args, **kwargs):
 
        super(NewDataSyncImportConsumer, self).__init__(*args, **kwargs)
 
        warnings.warn("class is deprecated, please use "
 
                      "DataSyncImportConsumder instead",
 
                      DeprecationWarning)
 

	
 

	
 
class FromRattailConsumer(DataSyncImportConsumer):
 
    """
 
    Base class for consumers which get their data from Rattail.
 
    """
rattail/datasync/rattail.py
Show inline comments
 
@@ -32,7 +32,7 @@ from sqlalchemy import orm
 

	
 
from rattail.db import Session, model
 
from rattail.db.util import make_topo_sortkey
 
from rattail.datasync import DataSyncWatcher, DataSyncConsumer, NewDataSyncImportConsumer
 
from rattail.datasync import DataSyncWatcher, DataSyncConsumer, DataSyncImportConsumer
 
from rattail.config import parse_list
 

	
 

	
 
@@ -52,9 +52,9 @@ class RattailWatcher(DataSyncWatcher):
 

	
 
    def get_changes(self, lastrun):
 
        """
 
        Checks the :class:`rattail.db.model:Change` table in a Rattail
 
        database, to see if there are any pending changes for the datasync
 
        daemon.
 
        Checks the :class:`~rattail.db.model.core.Change` table in a
 
        Rattail database, to see if there are any pending changes for
 
        the datasync daemon.
 
        """
 
        session = Session(bind=self.engine)
 
        changes = session.query(model.Change).all()
 
@@ -346,7 +346,7 @@ class RattailConsumer(DataSyncConsumer):
 
            session.delete(assignment)
 

	
 

	
 
class FromRattailToRattailBase(NewDataSyncImportConsumer):
 
class FromRattailToRattailBase(DataSyncImportConsumer):
 
    """
 
    Base class for Rattail -> Rattail datasync consumers
 
    """
0 comments (0 inline, 0 general)