entities/schemaobjs.py
changeset 6627 430b7703b3fd
parent 6578 7abd07ff0471
child 6632 78878f5a8166
equal deleted inserted replaced
6626:5c20a005bddc 6627:430b7703b3fd
    22 import re
    22 import re
    23 from socket import gethostname
    23 from socket import gethostname
    24 
    24 
    25 from logilab.common.decorators import cached
    25 from logilab.common.decorators import cached
    26 from logilab.common.textutils import text_to_dict
    26 from logilab.common.textutils import text_to_dict
       
    27 from logilab.common.configuration import OptionError
    27 
    28 
    28 from yams.schema import role_name
    29 from yams.schema import role_name
    29 
    30 
    30 from cubicweb import ValidationError
    31 from cubicweb import ValidationError
    31 from cubicweb.schema import ERQLExpression, RRQLExpression
    32 from cubicweb.schema import ERQLExpression, RRQLExpression
    32 
    33 
    33 from cubicweb.entities import AnyEntity, fetch_config
    34 from cubicweb.entities import AnyEntity, fetch_config
    34 
    35 
    35 
    36 
    36 class CWSource(AnyEntity):
    37 class _CWSourceCfgMixIn(object):
       
    38     @property
       
    39     def dictconfig(self):
       
    40         return self.config and text_to_dict(self.config) or {}
       
    41 
       
    42     def update_config(self, skip_unknown=False, **config):
       
    43         from cubicweb.server import SOURCE_TYPES
       
    44         from cubicweb.server.serverconfig import (SourceConfiguration,
       
    45                                                   generate_source_config)
       
    46         cfg = self.dictconfig
       
    47         cfg.update(config)
       
    48         options = SOURCE_TYPES[self.type].options
       
    49         sconfig = SourceConfiguration(self._cw.vreg.config, options=options)
       
    50         for opt, val in cfg.iteritems():
       
    51             try:
       
    52                 sconfig.set_option(opt, val)
       
    53             except OptionError:
       
    54                 if skip_unknown:
       
    55                     continue
       
    56                 raise
       
    57         cfgstr = unicode(generate_source_config(sconfig), self._cw.encoding)
       
    58         self.set_attributes(config=cfgstr)
       
    59 
       
    60 
       
    61 class CWSource(_CWSourceCfgMixIn, AnyEntity):
    37     __regid__ = 'CWSource'
    62     __regid__ = 'CWSource'
    38     fetch_attrs, fetch_order = fetch_config(['name', 'type'])
    63     fetch_attrs, fetch_order = fetch_config(['name', 'type'])
    39 
       
    40     @property
       
    41     def dictconfig(self):
       
    42         return self.config and text_to_dict(self.config) or {}
       
    43 
    64 
    44     @property
    65     @property
    45     def host_config(self):
    66     def host_config(self):
    46         dictconfig = self.dictconfig
    67         dictconfig = self.dictconfig
    47         host = gethostname()
    68         host = gethostname()
    54     @property
    75     @property
    55     def host_configs(self):
    76     def host_configs(self):
    56         return self.reverse_cw_host_config_of
    77         return self.reverse_cw_host_config_of
    57 
    78 
    58 
    79 
    59 class CWSourceHostConfig(AnyEntity):
    80 class CWSourceHostConfig(_CWSourceCfgMixIn, AnyEntity):
    60     __regid__ = 'CWSourceHostConfig'
    81     __regid__ = 'CWSourceHostConfig'
    61     fetch_attrs, fetch_order = fetch_config(['match_host', 'config'])
    82     fetch_attrs, fetch_order = fetch_config(['match_host', 'config'])
    62 
       
    63     @property
       
    64     def dictconfig(self):
       
    65         return self.config and text_to_dict(self.config) or {}
       
    66 
    83 
    67     def match(self, hostname):
    84     def match(self, hostname):
    68         return re.match(self.match_host, hostname)
    85         return re.match(self.match_host, hostname)
    69 
    86 
    70 
    87