|
1 | 1 | from pymongo import MongoClient, ReadPreference, uri_parser
|
2 |
| -from mongoengine.python_support import IS_PYMONGO_3 |
| 2 | +from mongoengine.python_support import (IS_PYMONGO_3, str_types) |
3 | 3 |
|
4 | 4 | __all__ = ['ConnectionError', 'connect', 'register_connection',
|
5 | 5 | 'DEFAULT_CONNECTION_NAME']
|
@@ -56,25 +56,35 @@ def register_connection(alias, name=None, host=None, port=None,
|
56 | 56 | 'authentication_source': authentication_source
|
57 | 57 | }
|
58 | 58 |
|
59 |
| - # Handle uri style connections |
60 | 59 | conn_host = conn_settings['host']
|
61 |
| - if conn_host.startswith('mongomock://'): |
62 |
| - conn_settings['is_mock'] = True |
63 |
| - # `mongomock://` is not a valid url prefix and must be replaced by `mongodb://` |
64 |
| - conn_settings['host'] = conn_host.replace('mongomock://', 'mongodb://', 1) |
65 |
| - elif '://' in conn_host: |
66 |
| - uri_dict = uri_parser.parse_uri(conn_host) |
67 |
| - conn_settings.update({ |
68 |
| - 'name': uri_dict.get('database') or name, |
69 |
| - 'username': uri_dict.get('username'), |
70 |
| - 'password': uri_dict.get('password'), |
71 |
| - 'read_preference': read_preference, |
72 |
| - }) |
73 |
| - uri_options = uri_dict['options'] |
74 |
| - if 'replicaset' in uri_options: |
75 |
| - conn_settings['replicaSet'] = True |
76 |
| - if 'authsource' in uri_options: |
77 |
| - conn_settings['authentication_source'] = uri_options['authsource'] |
| 60 | + # host can be a list or a string, so if string, force to a list |
| 61 | + if isinstance(conn_host, str_types): |
| 62 | + conn_host = [conn_host] |
| 63 | + |
| 64 | + resolved_hosts = [] |
| 65 | + for entity in conn_host: |
| 66 | + # Handle uri style connections |
| 67 | + if entity.startswith('mongomock://'): |
| 68 | + conn_settings['is_mock'] = True |
| 69 | + # `mongomock://` is not a valid url prefix and must be replaced by `mongodb://` |
| 70 | + resolved_hosts.append(entity.replace('mongomock://', 'mongodb://', 1)) |
| 71 | + elif '://' in entity: |
| 72 | + uri_dict = uri_parser.parse_uri(entity) |
| 73 | + resolved_hosts.append(entity) |
| 74 | + conn_settings.update({ |
| 75 | + 'name': uri_dict.get('database') or name, |
| 76 | + 'username': uri_dict.get('username'), |
| 77 | + 'password': uri_dict.get('password'), |
| 78 | + 'read_preference': read_preference, |
| 79 | + }) |
| 80 | + uri_options = uri_dict['options'] |
| 81 | + if 'replicaset' in uri_options: |
| 82 | + conn_settings['replicaSet'] = True |
| 83 | + if 'authsource' in uri_options: |
| 84 | + conn_settings['authentication_source'] = uri_options['authsource'] |
| 85 | + else: |
| 86 | + resolved_hosts.append(entity) |
| 87 | + conn_settings['host'] = resolved_hosts |
78 | 88 |
|
79 | 89 | # Deprecated parameters that should not be passed on
|
80 | 90 | kwargs.pop('slaves', None)
|
|
0 commit comments