cubicweb/hooks/syncsources.py
branch3.25
changeset 12142 db2fc87348ab
parent 11775 39cf9e55ada8
child 12150 6260c88e0ff5
equal deleted inserted replaced
12141:29d032bb70d8 12142:db2fc87348ab
    33     category = 'cw.sources'
    33     category = 'cw.sources'
    34 
    34 
    35 
    35 
    36 # repo sources synchronization #################################################
    36 # repo sources synchronization #################################################
    37 
    37 
    38 class SourceAddedOp(hook.Operation):
       
    39     entity = None # make pylint happy
       
    40     def postcommit_event(self):
       
    41         self.cnx.repo.add_source(self.entity)
       
    42 
       
    43 class SourceAddedHook(SourceHook):
    38 class SourceAddedHook(SourceHook):
    44     __regid__ = 'cw.sources.added'
    39     __regid__ = 'cw.sources.added'
    45     __select__ = SourceHook.__select__ & is_instance('CWSource')
    40     __select__ = SourceHook.__select__ & is_instance('CWSource')
    46     events = ('after_add_entity',)
    41     events = ('after_add_entity',)
    47     def __call__(self):
    42     def __call__(self):
    54         # initialisation, as config for this source is in a file and handling
    49         # initialisation, as config for this source is in a file and handling
    55         # is done separatly (no need for the operation either)
    50         # is done separatly (no need for the operation either)
    56         if self.entity.name != 'system':
    51         if self.entity.name != 'system':
    57             sourcecls.check_conf_dict(self.entity.eid, self.entity.host_config,
    52             sourcecls.check_conf_dict(self.entity.eid, self.entity.host_config,
    58                                       fail_if_unknown=not self._cw.vreg.config.repairing)
    53                                       fail_if_unknown=not self._cw.vreg.config.repairing)
    59             SourceAddedOp(self._cw, entity=self.entity)
       
    60 
    54 
    61 
       
    62 class SourceRemovedOp(hook.Operation):
       
    63     uri = None # make pylint happy
       
    64     def postcommit_event(self):
       
    65         self.cnx.repo.remove_source(self.uri)
       
    66 
    55 
    67 class SourceRemovedHook(SourceHook):
    56 class SourceRemovedHook(SourceHook):
    68     __regid__ = 'cw.sources.removed'
    57     __regid__ = 'cw.sources.removed'
    69     __select__ = SourceHook.__select__ & is_instance('CWSource')
    58     __select__ = SourceHook.__select__ & is_instance('CWSource')
    70     events = ('before_delete_entity',)
    59     events = ('before_delete_entity',)
    71     def __call__(self):
    60     def __call__(self):
    72         if self.entity.name == 'system':
    61         if self.entity.name == 'system':
    73             msg = _("You cannot remove the system source")
    62             msg = _("You cannot remove the system source")
    74             raise validation_error(self.entity, {None: msg})
    63             raise validation_error(self.entity, {None: msg})
    75         SourceRemovedOp(self._cw, uri=self.entity.name)
       
    76 
       
    77 
       
    78 class SourceConfigUpdatedOp(hook.DataOperationMixIn, hook.Operation):
       
    79 
       
    80     def precommit_event(self):
       
    81         self.__processed = []
       
    82         for source in self.get_data():
       
    83             if not self.cnx.deleted_in_transaction(source.eid):
       
    84                 conf = source.repo_source.check_config(source)
       
    85                 self.__processed.append( (source, conf) )
       
    86 
       
    87     def postcommit_event(self):
       
    88         for source, conf in self.__processed:
       
    89             source.repo_source.update_config(source, conf)
       
    90 
       
    91 
       
    92 class SourceRenamedOp(hook.LateOperation):
       
    93     oldname = newname = None # make pylint happy
       
    94 
       
    95     def postcommit_event(self):
       
    96         repo = self.cnx.repo
       
    97         # XXX race condition
       
    98         source = repo.sources_by_uri.pop(self.oldname)
       
    99         source.uri = self.newname
       
   100         source.public_config['uri'] = self.newname
       
   101         repo.sources_by_uri[self.newname] = source
       
   102         clear_cache(repo, 'source_defs')
       
   103 
    64 
   104 
    65 
   105 class SourceUpdatedHook(SourceHook):
    66 class SourceUpdatedHook(SourceHook):
   106     __regid__ = 'cw.sources.configupdate'
    67     __regid__ = 'cw.sources.configupdate'
   107     __select__ = SourceHook.__select__ & is_instance('CWSource')
    68     __select__ = SourceHook.__select__ & is_instance('CWSource')
   110         if 'name' in self.entity.cw_edited:
    71         if 'name' in self.entity.cw_edited:
   111             oldname, newname = self.entity.cw_edited.oldnewvalue('name')
    72             oldname, newname = self.entity.cw_edited.oldnewvalue('name')
   112             if oldname == 'system':
    73             if oldname == 'system':
   113                 msg = _("You cannot rename the system source")
    74                 msg = _("You cannot rename the system source")
   114                 raise validation_error(self.entity, {('name', 'subject'): msg})
    75                 raise validation_error(self.entity, {('name', 'subject'): msg})
   115             SourceRenamedOp(self._cw, oldname=oldname, newname=newname)
       
   116         if 'config' in self.entity.cw_edited or 'url' in self.entity.cw_edited:
    76         if 'config' in self.entity.cw_edited or 'url' in self.entity.cw_edited:
   117             if self.entity.name == 'system' and self.entity.config:
    77             if self.entity.name == 'system' and self.entity.config:
   118                 msg = _("Configuration of the system source goes to "
    78                 msg = _("Configuration of the system source goes to "
   119                         "the 'sources' file, not in the database")
    79                         "the 'sources' file, not in the database")
   120                 raise validation_error(self.entity, {('config', 'subject'): msg})
    80                 raise validation_error(self.entity, {('config', 'subject'): msg})
   121             SourceConfigUpdatedOp.get_instance(self._cw).add_data(self.entity)
       
   122 
       
   123 
       
   124 class SourceHostConfigUpdatedHook(SourceHook):
       
   125     __regid__ = 'cw.sources.hostconfigupdate'
       
   126     __select__ = SourceHook.__select__ & is_instance('CWSourceHostConfig')
       
   127     events = ('after_add_entity', 'after_update_entity', 'before_delete_entity',)
       
   128     def __call__(self):
       
   129         if self.entity.match(gethostname()):
       
   130             if self.event == 'after_update_entity' and \
       
   131                    not 'config' in self.entity.cw_edited:
       
   132                 return
       
   133             try:
       
   134                 SourceConfigUpdatedOp.get_instance(self._cw).add_data(self.entity.cwsource)
       
   135             except IndexError:
       
   136                 # XXX no source linked to the host config yet
       
   137                 pass