server/sources/remoterql.py
changeset 8675 b60329e40e26
parent 8674 001c1592060a
child 8695 358d8bed9626
equal deleted inserted replaced
8674:001c1592060a 8675:b60329e40e26
    46     def __init__(self, eids):
    46     def __init__(self, eids):
    47         self.eids = eids
    47         self.eids = eids
    48 
    48 
    49 class RemoteSource(AbstractSource):
    49 class RemoteSource(AbstractSource):
    50     """Generic external repository source"""
    50     """Generic external repository source"""
    51 
       
    52     CNX_TYPE = None # Must be ovewritted !
       
    53 
    51 
    54     # boolean telling if modification hooks should be called when something is
    52     # boolean telling if modification hooks should be called when something is
    55     # modified in this source
    53     # modified in this source
    56     should_call_hooks = False
    54     should_call_hooks = False
    57     # boolean telling if the repository should connect to this source during
    55     # boolean telling if the repository should connect to this source during
   110         self.config = processed_config
   108         self.config = processed_config
   111         self._skip_externals = processed_config['skip-external-entities']
   109         self._skip_externals = processed_config['skip-external-entities']
   112         if source_entity is not None:
   110         if source_entity is not None:
   113             self.latest_retrieval = source_entity.latest_retrieval
   111             self.latest_retrieval = source_entity.latest_retrieval
   114 
   112 
   115     def _get_connection(self):
   113     def _entity_update(self, source_entity):
   116         """open and return a connection to the source"""
   114         super(RemoteSource, self)._entity_update(source_entity)
   117         self.info('connecting to source %(base-url)s with user %(cubicweb-user)s',
   115         if self.urls and len(self.urls) > 1:
   118                   self.config)
   116             raise ValidationError(source_entity.eid, {'url': _('can only have one url')})
   119         cnxprops = ConnectionProperties(cnxtype=self.CNX_TYPE)
       
   120         return dbapi.connect(login=self.config['cubicweb-user'],
       
   121                              password=self.config['cubicweb-password'],
       
   122                              cnxprops=cnxprops)
       
   123 
   117 
   124     def get_connection(self):
   118     def get_connection(self):
   125         try:
   119         try:
   126             return self._get_connection()
   120             return self._get_connection()
   127         except ConnectionError, ex:
   121         except ConnectionError, ex:
   128             self.critical("can't get connection to source %s: %s", self.uri, ex)
   122             self.critical("can't get connection to source %s: %s", self.uri, ex)
   129             return ConnectionWrapper()
   123             return ConnectionWrapper()
   130 
   124 
       
   125     def _get_connection(self):
       
   126         """open and return a connection to the source"""
       
   127         self.info('connecting to source %s as user %s',
       
   128                   self.urls[0], self.config['cubicweb-user'])
       
   129         # XXX check protocol according to source type (zmq / pyro)
       
   130         return dbapi.connect(self.urls[0], login=self.config['cubicweb-user'],
       
   131                              password=self.config['cubicweb-password'])
   131 
   132 
   132     def reset_caches(self):
   133     def reset_caches(self):
   133         """method called during test to reset potential source caches"""
   134         """method called during test to reset potential source caches"""
   134         self._query_cache = TimedCache(1800)
   135         self._query_cache = TimedCache(1800)
   135 
   136 
   236 
   237 
   237     def synchronize(self, mtime=None):
   238     def synchronize(self, mtime=None):
   238         """synchronize content known by this repository with content in the
   239         """synchronize content known by this repository with content in the
   239         external repository
   240         external repository
   240         """
   241         """
   241         self.info('synchronizing remote %s source %s', (self.CNX_TYPE, self.uri))
   242         self.info('synchronizing remote source %s', self.uri)
   242         cnx = self.get_connection()
   243         cnx = self.get_connection()
   243         try:
   244         try:
   244             extrepo = cnx._repo
   245             extrepo = cnx._repo
   245         except AttributeError:
   246         except AttributeError:
   246             # fake connection wrapper returned when we can't connect to the
   247             # fake connection wrapper returned when we can't connect to the