dbapi.py
changeset 189 46fea7075866
parent 169 0e031b66cb0b
child 322 0d9aca19b3d0
equal deleted inserted replaced
188:97cc951f9f3c 189:46fea7075866
    43         return Repository(config, vreg=vreg)
    43         return Repository(config, vreg=vreg)
    44     else: # method == 'pyro'
    44     else: # method == 'pyro'
    45         from Pyro import core, naming, config as pyroconfig
    45         from Pyro import core, naming, config as pyroconfig
    46         from Pyro.errors import NamingError, ProtocolError
    46         from Pyro.errors import NamingError, ProtocolError
    47         core.initClient(banner=0)
    47         core.initClient(banner=0)
    48         pyroconfig.PYRO_NS_DEFAULTGROUP = ':' + config['pyro-ns-group']
    48         nsid = ':%s.%s' % (config['pyro-ns-group'], database)
    49         locator = naming.NameServerLocator()
    49         locator = naming.NameServerLocator()
    50         # resolve the Pyro object
    50         # resolve the Pyro object
    51         try:
    51         try:
    52             nshost, nsport = config['pyro-ns-host'], config['pyro-ns-port']
    52             nshost, nsport = config['pyro-ns-host'], config['pyro-ns-port']
    53             uri = locator.getNS(nshost, nsport).resolve(database)
    53             uri = locator.getNS(nshost, nsport) .resolve(nsid)
    54         except ProtocolError:
    54         except ProtocolError:
    55             raise ConnectionError('Could not connect to the Pyro name server '
    55             raise ConnectionError('Could not connect to the Pyro name server '
    56                                   '(host: %s:%i)' % (nshost, nsport))
    56                                   '(host: %s:%i)' % (nshost, nsport))
    57         except NamingError:
    57         except NamingError, ex:
    58             raise ConnectionError('Could not get repository for %s '
    58             raise ConnectionError('Could not get repository for %s '
    59                                   '(not registered in Pyro),'
    59                                   '(not registered in Pyro), '
    60                                   'you may have to restart your server-side '
    60                                   'you may have to restart your server-side '
    61                                   'application' % database)
    61                                   'application' % nsid)
    62         return core.getProxyForURI(uri)
    62         return core.getProxyForURI(uri)
    63         
    63         
    64 def repo_connect(repo, user, password, cnxprops=None):
    64 def repo_connect(repo, user, password, cnxprops=None):
    65     """Constructor to create a new connection to the CubicWeb repository.
    65     """Constructor to create a new connection to the CubicWeb repository.
    66     
    66     
    72     if cnxprops.cnxtype == 'inmemory':
    72     if cnxprops.cnxtype == 'inmemory':
    73         cnx.vreg = repo.vreg
    73         cnx.vreg = repo.vreg
    74     return cnx
    74     return cnx
    75     
    75     
    76 def connect(database=None, user=None, password=None, host=None,
    76 def connect(database=None, user=None, password=None, host=None,
    77             group=None, cnxprops=None, port=None, setvreg=True, mulcnx=True):
    77             group=None, cnxprops=None, port=None, setvreg=True, mulcnx=True,
       
    78             initlog=True):
    78     """Constructor for creating a connection to the CubicWeb repository.
    79     """Constructor for creating a connection to the CubicWeb repository.
    79     Returns a Connection object.
    80     Returns a Connection object.
    80 
    81 
    81     When method is 'pyro' and setvreg is True, use a special registry class
    82     When method is 'pyro' and setvreg is True, use a special registry class
    82     (MulCnxCubicWebRegistry) made to deal with connections to differents instances
    83     (MulCnxCubicWebRegistry) made to deal with connections to differents instances
    95     repo = get_repository(method, database, config=config)
    96     repo = get_repository(method, database, config=config)
    96     if method == 'inmemory':
    97     if method == 'inmemory':
    97         vreg = repo.vreg
    98         vreg = repo.vreg
    98     elif setvreg:
    99     elif setvreg:
    99         if mulcnx:
   100         if mulcnx:
   100             vreg = MulCnxCubicWebRegistry(config)
   101             vreg = MulCnxCubicWebRegistry(config, initlog=initlog)
   101         else:
   102         else:
   102             vreg = CubicWebRegistry(config)
   103             vreg = CubicWebRegistry(config, initlog=initlog)
   103         vreg.set_schema(repo.get_schema())
   104         vreg.set_schema(repo.get_schema())
   104     else:
   105     else:
   105         vreg = None
   106         vreg = None
   106     cnx = repo_connect(repo, user, password, cnxprops)
   107     cnx = repo_connect(repo, user, password, cnxprops)
   107     cnx.vreg = vreg
   108     cnx.vreg = vreg