cubicweb/hooks/syncsources.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 06 Oct 2016 12:11:23 +0200
changeset 11775 39cf9e55ada8
parent 11773 054a947b5415
child 12142 db2fc87348ab
permissions -rw-r--r--
[source] Drop source mapping handling It was only used by the cwxmlparser which has been deleted. This is too complex for litle benefit, we don't want to maintain that in cubicweb.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11763
diff changeset
     1
# copyright 2010-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
6957
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     3
#
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     4
# This file is part of CubicWeb.
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     5
#
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
     9
# any later version.
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    10
#
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    14
# details.
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    15
#
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    18
"""hooks for repository sources synchronization"""
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    19
10666
7f6b5f023884 [py3k] replace '_ = unicode' in global scope (closes #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9619
diff changeset
    20
from cubicweb import _
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8190
diff changeset
    21
7024
f4e798cf29b4 [source hooks] filter out case where source configuration doesn't have to be reloaded
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6957
diff changeset
    22
from socket import gethostname
f4e798cf29b4 [source hooks] filter out case where source configuration doesn't have to be reloaded
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6957
diff changeset
    23
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    24
from logilab.common.decorators import clear_cache
6957
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6945
diff changeset
    25
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8190
diff changeset
    26
from cubicweb import validation_error
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
    27
from cubicweb.predicates import is_instance
6724
24bf6f181d0e [pyro source] store pyro source mapping file into the database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    28
from cubicweb.server import SOURCE_TYPES, hook
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11763
diff changeset
    30
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
class SourceHook(hook.Hook):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
    __abstract__ = True
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
    category = 'cw.sources'
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
7399
972ed1843bd8 [multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7049
diff changeset
    36
# repo sources synchronization #################################################
972ed1843bd8 [multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7049
diff changeset
    37
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
class SourceAddedOp(hook.Operation):
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7735
diff changeset
    39
    entity = None # make pylint happy
6724
24bf6f181d0e [pyro source] store pyro source mapping file into the database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    40
    def postcommit_event(self):
9619
a4e97a31cc11 [hooks/syncsources] use a cnx not a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9448
diff changeset
    41
        self.cnx.repo.add_source(self.entity)
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    42
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
class SourceAddedHook(SourceHook):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
    __regid__ = 'cw.sources.added'
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
    __select__ = SourceHook.__select__ & is_instance('CWSource')
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
    events = ('after_add_entity',)
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
    def __call__(self):
6945
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    48
        try:
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    49
            sourcecls = SOURCE_TYPES[self.entity.type]
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    50
        except KeyError:
8557
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
    51
            msg = _('Unknown source type')
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8190
diff changeset
    52
            raise validation_error(self.entity, {('type', 'subject'): msg})
8557
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
    53
        # ignore creation of the system source done during database
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
    54
        # initialisation, as config for this source is in a file and handling
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
    55
        # is done separatly (no need for the operation either)
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
    56
        if self.entity.name != 'system':
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
    57
            sourcecls.check_conf_dict(self.entity.eid, self.entity.host_config,
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
    58
                                      fail_if_unknown=not self._cw.vreg.config.repairing)
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
    59
            SourceAddedOp(self._cw, entity=self.entity)
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    61
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
class SourceRemovedOp(hook.Operation):
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7735
diff changeset
    63
    uri = None # make pylint happy
7650
278fe9c1f3ad [repo] closes #1821172: fix source deletion
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
    64
    def postcommit_event(self):
9619
a4e97a31cc11 [hooks/syncsources] use a cnx not a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9448
diff changeset
    65
        self.cnx.repo.remove_source(self.uri)
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    66
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    67
class SourceRemovedHook(SourceHook):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    68
    __regid__ = 'cw.sources.removed'
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    69
    __select__ = SourceHook.__select__ & is_instance('CWSource')
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    70
    events = ('before_delete_entity',)
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    71
    def __call__(self):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    72
        if self.entity.name == 'system':
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8190
diff changeset
    73
            msg = _("You cannot remove the system source")
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8190
diff changeset
    74
            raise validation_error(self.entity, {None: msg})
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    75
        SourceRemovedOp(self._cw, uri=self.entity.name)
6724
24bf6f181d0e [pyro source] store pyro source mapping file into the database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    76
6945
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    77
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    78
class SourceConfigUpdatedOp(hook.DataOperationMixIn, hook.Operation):
6945
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    79
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    80
    def precommit_event(self):
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    81
        self.__processed = []
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    82
        for source in self.get_data():
9619
a4e97a31cc11 [hooks/syncsources] use a cnx not a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9448
diff changeset
    83
            if not self.cnx.deleted_in_transaction(source.eid):
7650
278fe9c1f3ad [repo] closes #1821172: fix source deletion
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
    84
                conf = source.repo_source.check_config(source)
278fe9c1f3ad [repo] closes #1821172: fix source deletion
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
    85
                self.__processed.append( (source, conf) )
6945
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    86
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    87
    def postcommit_event(self):
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    88
        for source, conf in self.__processed:
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    89
            source.repo_source.update_config(source, conf)
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
    90
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    91
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    92
class SourceRenamedOp(hook.LateOperation):
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7735
diff changeset
    93
    oldname = newname = None # make pylint happy
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    94
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    95
    def postcommit_event(self):
9619
a4e97a31cc11 [hooks/syncsources] use a cnx not a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9448
diff changeset
    96
        repo = self.cnx.repo
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    97
        # XXX race condition
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    98
        source = repo.sources_by_uri.pop(self.oldname)
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
    99
        source.uri = self.newname
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
   100
        source.public_config['uri'] = self.newname
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
   101
        repo.sources_by_uri[self.newname] = source
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
   102
        clear_cache(repo, 'source_defs')
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
   103
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
   104
6945
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
   105
class SourceUpdatedHook(SourceHook):
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
   106
    __regid__ = 'cw.sources.configupdate'
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
   107
    __select__ = SourceHook.__select__ & is_instance('CWSource')
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
   108
    events = ('before_update_entity',)
6945
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
   109
    def __call__(self):
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
   110
        if 'name' in self.entity.cw_edited:
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
   111
            oldname, newname = self.entity.cw_edited.oldnewvalue('name')
8557
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
   112
            if oldname == 'system':
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
   113
                msg = _("You cannot rename the system source")
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
   114
                raise validation_error(self.entity, {('name', 'subject'): msg})
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
   115
            SourceRenamedOp(self._cw, oldname=oldname, newname=newname)
10956
208c9ac8edbb update repo sources on update of source's entities .url attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10666
diff changeset
   116
        if 'config' in self.entity.cw_edited or 'url' in self.entity.cw_edited:
8557
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
   117
            if self.entity.name == 'system' and self.entity.config:
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
   118
                msg = _("Configuration of the system source goes to "
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
   119
                        "the 'sources' file, not in the database")
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
   120
                raise validation_error(self.entity, {('config', 'subject'): msg})
8756f932ca83 [source synchronization hooks] Fix/enhance system source hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8556
diff changeset
   121
            SourceConfigUpdatedOp.get_instance(self._cw).add_data(self.entity)
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
   122
6945
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
   123
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
   124
class SourceHostConfigUpdatedHook(SourceHook):
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
   125
    __regid__ = 'cw.sources.hostconfigupdate'
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
   126
    __select__ = SourceHook.__select__ & is_instance('CWSourceHostConfig')
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
   127
    events = ('after_add_entity', 'after_update_entity', 'before_delete_entity',)
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6944
diff changeset
   128
    def __call__(self):
7049
f2ae6202754c [source host alias] match_host is the attribute, match is the method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7024
diff changeset
   129
        if self.entity.match(gethostname()):
7024
f4e798cf29b4 [source hooks] filter out case where source configuration doesn't have to be reloaded
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6957
diff changeset
   130
            if self.event == 'after_update_entity' and \
f4e798cf29b4 [source hooks] filter out case where source configuration doesn't have to be reloaded
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6957
diff changeset
   131
                   not 'config' in self.entity.cw_edited:
f4e798cf29b4 [source hooks] filter out case where source configuration doesn't have to be reloaded
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6957
diff changeset
   132
                return
f4e798cf29b4 [source hooks] filter out case where source configuration doesn't have to be reloaded
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6957
diff changeset
   133
            try:
7735
71adfd6bab38 [source] update system table and internal structures on source renaming. Closes #1896721
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7650
diff changeset
   134
                SourceConfigUpdatedOp.get_instance(self._cw).add_data(self.entity.cwsource)
7024
f4e798cf29b4 [source hooks] filter out case where source configuration doesn't have to be reloaded
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6957
diff changeset
   135
            except IndexError:
f4e798cf29b4 [source hooks] filter out case where source configuration doesn't have to be reloaded
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6957
diff changeset
   136
                # XXX no source linked to the host config yet
f4e798cf29b4 [source hooks] filter out case where source configuration doesn't have to be reloaded
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6957
diff changeset
   137
                pass