'Fetch updates from the peer repository and import them
into the local database (active pull)
@param task: the synchronization task (sync_task Row)
@param onconflict: callback for automatic conflict resolution
@return: tuple (error, mtime), with error=None if successful,
else error=message, and mtime=modification timestamp
of the youngest record sent'

def pull(self, task, onconflict=None):

    repository = self.repository
    log = repository.log
    error = None
    result = None
    tablename = task.resource_name
    if (tablename == 'mixed'):
        resource = None
        mixed = True
    else:
        try:
            resource = current.s3db.resource(tablename)
        except SyntaxError:
            result = log.FATAL
            error = msg = sys.exc_info()[1]
        mixed = False
    if (not result):
        input_files = self._input_files(task)
        if (not input_files):
            result = log.SUCCESS
            msg = 'No    files    to    import'
    if (not result):
        adapter = None
        backend = repository.backend
        if (not backend):
            backend = 'eden'
        backend = ('s3.sync_adapter.%s' % backend)
        try:
            name = 'S3SyncAdapter'
            api = getattr(__import__(backend, fromlist=[name]), name)
        except ImportError:
            result = log.FATAL
            error = msg = ('Unsupported    back-end:    %s' % backend)
        else:
            adapter = api(repository)
    if result:
        log.write(repository_id=repository.id, resource_name=tablename, transmission=log.OUT, mode=log.PULL, action=None, remote=False, result=result, message=msg)
        return (error, None)
    from ..s3import import S3ImportItem
    strategy = task.strategy
    conflict_policy = task.conflict_policy
    if (not conflict_policy):
        conflict_policy = S3ImportItem.POLICY.MASTER
    update_policy = task.update_policy
    if (not update_policy):
        update_policy = S3ImportItem.POLICY.NEWER
    if (update_policy not in ('THIS', 'OTHER')):
        last_sync = task.last_pull
    else:
        last_sync = None
    error = None
    mtime = None
    for f in input_files:
        current.log.debug(('FileSync:    importing    %s' % f))
        try:
            with open(f, 'r') as source:
                result = adapter.receive([source], resource, strategy=strategy, update_policy=update_policy, conflict_policy=conflict_policy, onconflict=onconflict, last_sync=last_sync, mixed=mixed)
        except IOError:
            msg = sys.exc_info()[1]
            current.log.warning(msg)
            continue
        status = result['status']
        log.write(repository_id=repository.id, resource_name=tablename, transmission=log.OUT, mode=log.PULL, action=('import    %s' % f), remote=result['remote'], result=status, message=result['message'])
        if (status in (log.ERROR, log.FATAL)):
            error = ('Error    while    importing    %s' % f)
            current.log.error(error)
            mtime = None
        else:
            if resource:
                mtime = resource.mtime
            else:
                mtime = current.request.utcnow
            if task.delete_input_files:
                try:
                    os.remove(f)
                except os.error:
                    current.log.warning(('FileSync:    can    not    delete    %s' % f))
                else:
                    current.log.debug(('FileSync:    %s    deleted' % f))
    return (error, mtime)
