-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make FormEncode optional and add support for seedlist urls
* Added support for mongodb seedlist URL * Make formencode optional, only required for people relying on `ming.configure`
- Loading branch information
1 parent
082e380
commit 7c78d3b
Showing
5 changed files
with
98 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,58 @@ | ||
import six | ||
|
||
from formencode import schema, validators | ||
from formencode.variabledecode import variable_decode | ||
|
||
from ming.exc import MingConfigError | ||
from ming.session import Session | ||
from ming.datastore import create_datastore | ||
|
||
class AuthenticateSchema(schema.Schema): | ||
name=validators.UnicodeString(not_empty=True) | ||
password=validators.UnicodeString(not_empty=True) | ||
|
||
class DatastoreSchema(schema.Schema): | ||
allow_extra_fields=True | ||
def variable_decode(**kwargs): | ||
try: | ||
from formencode.variabledecode import variable_decode as fe_variable_decode | ||
except ImportError: | ||
raise MingConfigError("Need to install FormEncode to use ``ming.configure``") | ||
return fe_variable_decode(kwargs) | ||
|
||
uri=validators.UnicodeString(if_missing=None, if_empty=None) | ||
database=validators.UnicodeString(if_missing=None, if_empty=None) | ||
authenticate=AuthenticateSchema(if_missing=None, if_empty=None) | ||
connect_retry=validators.Number(if_missing=3, if_empty=0) | ||
auto_ensure_indexes = validators.StringBool(if_missing=True) | ||
# pymongo | ||
tz_aware=validators.Bool(if_missing=False) | ||
|
||
def configure(**kwargs): | ||
""" | ||
Given a (flat) dictionary of config values, creates DataStores | ||
and saves them by name | ||
""" | ||
config = variable_decode(kwargs) | ||
configure_from_nested_dict(config['ming']) | ||
config = variable_decode(**kwargs) | ||
configure_from_nested_dict(config["ming"]) | ||
|
||
|
||
def configure_from_nested_dict(config): | ||
try: | ||
from formencode import schema, validators | ||
except ImportError: | ||
raise MingConfigError("Need to install FormEncode to use ``ming.configure``") | ||
|
||
class AuthenticateSchema(schema.Schema): | ||
name = validators.UnicodeString(not_empty=True) | ||
password = validators.UnicodeString(not_empty=True) | ||
|
||
class DatastoreSchema(schema.Schema): | ||
allow_extra_fields = True | ||
|
||
uri = validators.UnicodeString(if_missing=None, if_empty=None) | ||
database = validators.UnicodeString(if_missing=None, if_empty=None) | ||
authenticate = AuthenticateSchema(if_missing=None, if_empty=None) | ||
connect_retry = validators.Number(if_missing=3, if_empty=0) | ||
auto_ensure_indexes = validators.StringBool(if_missing=True) | ||
# pymongo | ||
tz_aware = validators.Bool(if_missing=False) | ||
|
||
datastores = {} | ||
for name, datastore in six.iteritems(config): | ||
args = DatastoreSchema.to_python(datastore, None) | ||
database = args.pop('database', None) | ||
database = args.pop("database", None) | ||
if database is None: | ||
datastores[name] = create_datastore(**args) | ||
else: | ||
datastores[name] = create_datastore(database, **args) | ||
datastores[name] = create_datastore(database=database, **args) | ||
Session._datastores = datastores | ||
# bind any existing sessions | ||
for name, session in six.iteritems(Session._registry): | ||
session.bind = datastores.get(name, None) | ||
session._name = name | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ deps = nose | |
pytz | ||
WebOb | ||
webtest | ||
formencode | ||
# python-spidermonkey | ||
commands = | ||
pip install coverage | ||
|