This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path__init__.py
More file actions
224 lines (185 loc) · 7.86 KB
/
Copy path__init__.py
File metadata and controls
224 lines (185 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
# Copyright (c) 2015, 2018 IBM. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Cloudant / CouchDB Python client library API package
"""
__version__ = '2.15.1-SNAPSHOT'
# pylint: disable=wrong-import-position
import contextlib
import warnings
# pylint: disable=wrong-import-position
from .client import Cloudant, CouchDB
from ._common_util import CloudFoundryService
warnings.warn('The module cloudant is now end-of-life. The replacement is ibmcloudant.',
DeprecationWarning)
@contextlib.contextmanager
def cloudant(user, passwd, **kwargs):
"""
Provides a context manager to create a Cloudant session and
provide access to databases, docs etc.
:param str user: Username used to connect to Cloudant.
:param str passwd: Authentication token used to connect to Cloudant.
:param str account: The Cloudant account name. If the account parameter
is present, it will be used to construct the Cloudant service URL.
:param str url: If the account is not present and the url parameter is
present then it will be used to set the Cloudant service URL. The
url must be a fully qualified http/https URL.
:param str x_cloudant_user: Override the X-Cloudant-User setting used to
authenticate. This is needed to authenticate on one's behalf,
eg with an admin account. This parameter must be accompanied
by the url parameter. If the url parameter is omitted then
the x_cloudant_user parameter setting is ignored.
:param str encoder: Optional json Encoder object used to encode
documents for storage. Defaults to json.JSONEncoder.
For example:
.. code-block:: python
# cloudant context manager
from cloudant import cloudant
with cloudant(USERNAME, PASSWORD, account=ACCOUNT_NAME) as client:
# Context handles connect() and disconnect() for you.
# Perform library operations within this context. Such as:
print client.all_dbs()
# ...
"""
cloudant_session = Cloudant(user, passwd, **kwargs)
cloudant_session.connect()
yield cloudant_session
cloudant_session.disconnect()
@contextlib.contextmanager
def cloudant_iam(account_name, api_key, **kwargs):
"""
Provides a context manager to create a Cloudant session using IAM
authentication and provide access to databases, docs etc.
:param account_name: Cloudant account name.
:param api_key: IAM authentication API key.
For example:
.. code-block:: python
# cloudant context manager
from cloudant import cloudant_iam
with cloudant_iam(ACCOUNT_NAME, API_KEY) as client:
# Context handles connect() and disconnect() for you.
# Perform library operations within this context. Such as:
print client.all_dbs()
# ...
"""
cloudant_session = Cloudant.iam(account_name, api_key, **kwargs)
cloudant_session.connect()
yield cloudant_session
cloudant_session.disconnect()
@contextlib.contextmanager
def cloudant_bluemix(vcap_services, instance_name=None, service_name=None, **kwargs):
"""
Provides a context manager to create a Cloudant session and provide access
to databases, docs etc.
:param vcap_services: VCAP_SERVICES environment variable
:type vcap_services: dict or str
:param str instance_name: Optional Bluemix instance name. Only required if
multiple Cloudant instances are available.
:param str service_name: Optional Bluemix service name.
:param str encoder: Optional json Encoder object used to encode
documents for storage. Defaults to json.JSONEncoder.
Loads all configuration from the specified VCAP_SERVICES Cloud Foundry
environment variable. The VCAP_SERVICES variable contains connection
information to access a service instance. For example:
.. code-block:: json
{
"VCAP_SERVICES": {
"cloudantNoSQLDB": [
{
"credentials": {
"apikey": "some123api456key"
"username": "example",
"password": "xxxxxxx",
"host": "example.cloudant.com",
"port": 443,
"url": "https://example:xxxxxxx@example.cloudant.com"
},
"syslog_drain_url": null,
"label": "cloudantNoSQLDB",
"provider": null,
"plan": "Lite",
"name": "Cloudant NoSQL DB"
}
]
}
}
See `Cloud Foundry Environment Variables <http://docs.cloudfoundry.org/
devguide/deploy-apps/environment-variable.html#VCAP-SERVICES>`_.
Example usage:
.. code-block:: python
import os
# cloudant_bluemix context manager
from cloudant import cloudant_bluemix
with cloudant_bluemix(os.getenv('VCAP_SERVICES'), 'Cloudant NoSQL DB') as client:
# Context handles connect() and disconnect() for you.
# Perform library operations within this context. Such as:
print client.all_dbs()
# ...
"""
cloudant_session = Cloudant.bluemix(
vcap_services,
instance_name=instance_name,
service_name=service_name,
**kwargs
)
cloudant_session.connect()
yield cloudant_session
cloudant_session.disconnect()
@contextlib.contextmanager
def couchdb(user, passwd, **kwargs):
"""
Provides a context manager to create a CouchDB session and
provide access to databases, docs etc.
:param str user: Username used to connect to CouchDB.
:param str passwd: Passcode used to connect to CouchDB.
:param str url: URL for CouchDB server.
:param str encoder: Optional json Encoder object used to encode
documents for storage. Defaults to json.JSONEncoder.
For example:
.. code-block:: python
# couchdb context manager
from cloudant import couchdb
with couchdb(USERNAME, PASSWORD, url=COUCHDB_URL) as client:
# Context handles connect() and disconnect() for you.
# Perform library operations within this context. Such as:
print client.all_dbs()
# ...
"""
couchdb_session = CouchDB(user, passwd, **kwargs)
couchdb_session.connect()
yield couchdb_session
couchdb_session.disconnect()
@contextlib.contextmanager
def couchdb_admin_party(**kwargs):
"""
Provides a context manager to create a CouchDB session in Admin Party mode
and provide access to databases, docs etc.
:param str url: URL for CouchDB server.
:param str encoder: Optional json Encoder object used to encode
documents for storage. Defaults to json.JSONEncoder.
For example:
.. code-block:: python
# couchdb_admin_party context manager
from cloudant import couchdb_admin_party
with couchdb_admin_party(url=COUCHDB_URL) as client:
# Context handles connect() and disconnect() for you.
# Perform library operations within this context. Such as:
print client.all_dbs()
# ...
"""
couchdb_session = CouchDB(None, None, True, **kwargs)
couchdb_session.connect()
yield couchdb_session
couchdb_session.disconnect()