Package wtf :: Package ext :: Package db :: Module _tagged
[hide private]
[frames] | no frames]

Source Code for Module wtf.ext.db._tagged

  1  # -*- coding: ascii -*- 
  2  # 
  3  # Copyright 2010-2012 
  4  # Andr\xe9 Malo or his licensors, as applicable 
  5  # 
  6  # Licensed under the Apache License, Version 2.0 (the "License"); 
  7  # you may not use this file except in compliance with the License. 
  8  # You may obtain a copy of the License at 
  9  # 
 10  #     http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, software 
 13  # distributed under the License is distributed on an "AS IS" BASIS, 
 14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 15  # See the License for the specific language governing permissions and 
 16  # limitations under the License. 
 17  """ 
 18  ============================ 
 19   Tagged connection creation 
 20  ============================ 
 21   
 22  Tagged connection creation. 
 23  """ 
 24  __author__ = u"Andr\xe9 Malo" 
 25  __docformat__ = "restructuredtext en" 
 26   
 27  from wtf.ext.db import _connection 
 28  from wtf.ext.db import _decorators 
 29   
 30   
 31  #: Global tag mapping 
 32  #: 
 33  #: :Type: ``dict`` 
 34  _connection_tags = {} 
 35   
 36   
37 -def dbname(tag):
38 """ 39 Determine dbname from tag 40 41 :Parameters: 42 `tag` : ``str`` 43 Connection tag 44 45 :Return: DB name 46 :Rtype: ``str`` 47 """ 48 return _connection_tags[tag]
49 50
51 -def driver(tag):
52 """ 53 Determine driver module 54 55 :Parameters: 56 `tag` : ``str`` 57 Connection tag 58 59 :Return: Driver module 60 :Rtype: ``module`` 61 62 :Exceptions: 63 - `DBConfigurationError` : DB not configured 64 - `KeyError` : DB name not found 65 - `ImportError` : Driver not found 66 """ 67 return _connection.driver(dbname(tag))
68 69
70 -def connect(tag, **kwargs):
71 """ 72 Connect and create a connection 73 74 :Parameters: 75 `tag` : ``str`` 76 Connection tag 77 78 :Return: connection 79 :Rtype: ``Connection`` 80 """ 81 return _connection.connect(dbname(tag), **kwargs)
82 83
84 -def connection(tag, arg=None, translate_exceptions=True):
85 """ 86 Create decorator to inject connection into function 87 88 :Parameters: 89 `tag` : ``str`` 90 Connection tag 91 92 `arg` : ``str`` 93 Argument name. If omitted or ``None``, ``'db'`` is used. 94 95 `translate_exceptions` : ``bool`` 96 Translate adapter exceptions to our ones? 97 98 :Return: Decorator function 99 :Rtype: ``callable`` 100 """ 101 return _decorators.connection(dbname(tag), arg, translate_exceptions)
102 103
104 -def transaction(arg=None):
105 """ 106 Create decorator to wrap a connection into a transaction 107 108 :Parameters: 109 `arg` : ``str`` 110 Argument name. If omitted or ``None``, ``'db'`` is used. 111 112 :Return: Decorator function 113 :Rtype: ``callable`` 114 """ 115 return _decorators.transaction(arg)
116 117
118 -def register_connection_tag(tag, dbname):
119 """ 120 Register a connection tag 121 122 :Parameters: 123 `tag` : ``str`` 124 Tag name 125 126 `dbname` : ``str`` 127 DB name string 128 129 :Exceptions: 130 - `ValueError` : Tag already defined with a different value 131 """ 132 # pylint: disable = W0621 133 if tag in _connection_tags and _connection_tags[tag] != dbname: 134 raise ValueError("Tag %r (=%r) redefined as %r" % ( 135 tag, _connection_tags[tag], dbname 136 )) 137 _connection_tags[tag] = dbname
138