Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
90a0eb2
re-work test harness
prashantmital Nov 16, 2018
bded8a4
add JSON spec-test files
prashantmital Nov 16, 2018
3ef53cc
add new options from specification
prashantmital Nov 16, 2018
fb04075
change exception raised by invalid value of zlibcompressionlevel
prashantmital Nov 16, 2018
f7a7f6f
add dummy PEM files for tests
prashantmital Nov 19, 2018
1991e18
refactor URI parsing logic
prashantmital Nov 27, 2018
17a506b
cleanup option name dictionaries
prashantmital Nov 27, 2018
3bf9167
cleanup validation code and extend functionality to kwargs
prashantmital Nov 27, 2018
1152e41
robustify deprecated option handling
prashantmital Nov 27, 2018
57bbe35
robustify socketKeepAlive deprecation warning test
prashantmital Nov 28, 2018
d270963
preserve case information when parsing options
prashantmital Nov 28, 2018
776e2e8
try warnings magic
prashantmital Nov 28, 2018
f32a3bd
break out spec tests into individual tests
prashantmital Nov 28, 2018
f6e6911
stringify test names to avoid py2.x unicode errors
prashantmital Nov 28, 2018
baa4ab0
clear warning registry during spec test setup
prashantmital Nov 29, 2018
9635bd6
remove 'path' from uri option names
prashantmital Dec 4, 2018
fa60f0f
remove unnecessary trailing commas
prashantmital Dec 5, 2018
5a0a061
augment comment to specify MongoClient constructor options
prashantmital Dec 5, 2018
bdbfed0
add link to python warnings-related issue
prashantmital Dec 5, 2018
8f4f973
incorporate new JSON tests
prashantmital Dec 18, 2018
495c35c
undo removal of public API methods
prashantmital Dec 18, 2018
c93dd85
undo removal of public API methods
prashantmital Dec 18, 2018
ee724cc
get more spec tests to pass
prashantmital Dec 18, 2018
e2b3e0d
get spec tests to pass
prashantmital Dec 19, 2018
e4d3b4c
ensure options are compared correctly (they were not being compared b…
prashantmital Dec 19, 2018
8e30865
cleanup cruft
prashantmital Dec 21, 2018
46a4597
update docs
prashantmital Dec 24, 2018
f426fb2
add missing link
prashantmital Dec 24, 2018
8c93d05
fix typo
prashantmital Dec 24, 2018
01efc7c
update/resync test JSON files
prashantmital Jan 4, 2019
478f238
update docstring
prashantmital Jan 8, 2019
0a73bbe
use more efficient loop pattern
prashantmital Jan 8, 2019
7829c96
compressor validator now accepts iterables as well as strings
prashantmital Jan 11, 2019
be89558
use try except instead of type comparison
prashantmital Jan 11, 2019
5788f44
implement case insensitive dictionary
prashantmital Jan 14, 2019
afd789c
add some tests to check proper parsing and warning when ingesting tls…
prashantmital Jan 15, 2019
e030962
fix failing py3 tests
prashantmital Jan 15, 2019
fb5a20b
update docstrings
prashantmital Jan 17, 2019
f691860
update docstrings
prashantmital Jan 17, 2019
43da062
update docstrings
prashantmital Jan 17, 2019
eb4c175
make CaseInsensitiveDictionary private
prashantmital Jan 17, 2019
d5082fd
explicitly specified values of tlsAllow* options override values impl…
prashantmital Jan 17, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
implement case insensitive dictionary
  • Loading branch information
prashantmital committed Jan 14, 2019
commit 5788f44a622f10ca943755d3ff6f7eee5f202da4
31 changes: 0 additions & 31 deletions pymongo/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,37 +711,6 @@ def get_validated_options(options, warn=True):
return validated_options


def _handle_option_deprecations(options):
"""Issue appropriate warnings when deprecated options are present in the
options dictionary. Removes deprecated option key, value pairs if the
options dictionary is found to also have the renamed option."""
undeprecated_options = {}
lc_optnames = [n.lower() for n in options]
for key, value in iteritems(options):
optname = str(key).lower()
if optname in URI_OPTIONS_DEPRECATION_MAP:
renamed_key = URI_OPTIONS_DEPRECATION_MAP[optname]
if renamed_key.lower() in lc_optnames:
warnings.warn("Deprecated option '%s' ignored in favor of "
"'%s'." % (str(key), renamed_key))
continue
warnings.warn("Option '%s' is deprecated, use '%s' instead." % (
str(key), renamed_key))
undeprecated_options[str(key)] = value
return undeprecated_options


def _normalize_options(options):
"""Renames keys in the options dictionary to their internally-used
names."""
normalized_options = {}
for key, value in iteritems(options):
optname = str(key).lower()
intname = INTERNAL_URI_OPTION_NAME_MAP.get(optname, key)
normalized_options[intname] = options[key]
return normalized_options


# List of write-concern-related options.
WRITE_CONCERN_OPTIONS = frozenset([
'w',
Expand Down
11 changes: 7 additions & 4 deletions pymongo/mongo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
from pymongo.topology import Topology
from pymongo.topology_description import TOPOLOGY_TYPE
from pymongo.settings import TopologySettings
from pymongo.uri_parser import (CaseInsensitiveDictionary,
_handle_option_deprecations,
_normalize_options)
from pymongo.write_concern import DEFAULT_WRITE_CONCERN


Expand Down Expand Up @@ -565,12 +568,12 @@ def __init__(
keyword_opts['connect'] = connect

# Validate kwargs options.
keyword_opts = dict(common.validate(k, v)
for k, v in keyword_opts.items())
keyword_opts = CaseInsensitiveDictionary(
dict(common.validate(k, v) for k, v in keyword_opts.items()))
# Handle deprecated options in kwarg list.
keyword_opts = common._handle_option_deprecations(keyword_opts)
keyword_opts = _handle_option_deprecations(keyword_opts)
# Change kwarg option names to those used internally.
keyword_opts = common._normalize_options(keyword_opts)
keyword_opts = _normalize_options(keyword_opts)
# Augment URI options with kwarg options, overriding the former.
opts.update(keyword_opts)
# Username and password passed as kwargs override user info in URI.
Expand Down
152 changes: 138 additions & 14 deletions pymongo/uri_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
except ImportError:
_HAVE_DNSPYTHON = False

from bson.py3compat import PY3, string_type
from bson.py3compat import abc, iteritems, string_type, PY3

if PY3:
from urllib.parse import unquote_plus
else:
from urllib import unquote_plus

from pymongo.common import (
get_validated_options, _handle_option_deprecations, _normalize_options)
get_validated_options, URI_OPTIONS_DEPRECATION_MAP, INTERNAL_URI_OPTION_NAME_MAP)
from pymongo.errors import ConfigurationError, InvalidURI


Expand All @@ -42,6 +42,80 @@
DEFAULT_PORT = 27017


class CaseInsensitiveDictionary(abc.MutableMapping):
def __init__(self, *args, **kwargs):
self.__casedkeys = {}
self.__data = {}
self.update(dict(*args, **kwargs))

def __contains__(self, key):
return key.lower() in self.__data

def __len__(self):
return len(self.__data)

def __iter__(self):
return (self.__casedkeys[key] for key in self.__casedkeys)

def __repr__(self):
return str(self.__data)

def __setitem__(self, key, value):
lc_key = key.lower()
self.__casedkeys[lc_key] = key
self.__data[lc_key] = value

def __getitem__(self, key):
return self.__data[key.lower()]

def __delitem__(self, key):
lc_key = key.lower()
del self.__casedkeys[lc_key]
del self.__data[lc_key]

def get(self, key, default=None):
lc_key = key.lower()
if lc_key in self:
return self.__data[lc_key]
return default

def pop(self, key, *args, **kwargs):
lc_key = key.lower()
self.__casedkeys.pop(lc_key, None)
return self.__data.pop(lc_key, *args, **kwargs)

def popitem(self):
lc_key, cased_key = self.__casedkeys.popitem()
value = self.__data.pop(lc_key)
return cased_key, value

def clear(self):
self.__casedkeys.clear()
self.__data.clear()

def setdefault(self, key, default=None):
lc_key = key.lower()
if key in self:
return self.__data[lc_key]
else:
self.__casedkeys[lc_key] = key
self.__data[lc_key] = default
return default

def update(self, other):
for key in other:
self[key] = other[key]

def cased_key(self, key):
return self.__casedkeys[key.lower()]

def as_dict(self):
lc_data = {}
for lc_key in self.__data:
lc_data[lc_key] = self.__data[lc_key]
return lc_data


def parse_userinfo(userinfo):
"""Validates the format of user information in a MongoDB URI.
Reserved characters like ':', '/', '+' and '@' must be escaped
Expand Down Expand Up @@ -130,26 +204,76 @@ def parse_host(entity, default_port=DEFAULT_PORT):
return host.lower(), port


_IMPLICIT_TLSINSECURE_OPTS = {"tlsallowinvalidcertificates",
"tlsallowinvalidhostnames"}


def _parse_options(opts, delim):
"""Helper method for split_options which creates the options dict.
Also handles the creation of a list for the URI tag_sets/
readpreferencetags portion and the use of the tlsInsecure option."""
options = {}
options = CaseInsensitiveDictionary()
for opt in opts.split(delim):
key, value = opt.split("=")
optname = str(key).lower()
if optname == 'readpreferencetags':
options.setdefault(optname, []).append(value)
cased_key, value = opt.split("=")
cased_key = str(cased_key)
lc_key = cased_key.lower()
if lc_key == 'readpreferencetags':
options.setdefault(cased_key, []).append(value)
else:
if optname == 'tlsinsecure':
options['tlsAllowInvalidCertificates'] = value
options['tlsAllowInvalidHostnames'] = value
if key in options:
warnings.warn("Duplicate URI option '%s'." % (str(key),))
options[str(key)] = unquote_plus(value)
if cased_key in options:
warnings.warn("Duplicate URI option '%s'." % (cased_key,))
options[cased_key] = unquote_plus(value)
if (lc_key in _IMPLICIT_TLSINSECURE_OPTS and
'tlsinsecure' in options):
warn_msg = "URI option '%s' overrides value implied by '%s'."
warnings.warn(warn_msg % (
cased_key, options.cased_key('tlsinsecure')))
if lc_key == 'tlsinsecure':
for implicit_option in _IMPLICIT_TLSINSECURE_OPTS:
if implicit_option in options:
warn_msg = "URI option '%s' implicitly overrides '%s'."
warnings.warn(warn_msg % (
cased_key, options.cased_key(implicit_option)))
options.pop(implicit_option)

tls_insecure = options.get('tlsinsecure')
if tls_insecure is not None:
for implicit_option in _IMPLICIT_TLSINSECURE_OPTS:
options.setdefault(implicit_option, tls_insecure)

return options


def _handle_option_deprecations(options):
"""Issue appropriate warnings when deprecated options are present in the
options dictionary. Removes deprecated option key, value pairs if the
options dictionary is found to also have the renamed option."""
undeprecated_options = CaseInsensitiveDictionary()
for key, value in iteritems(options):
optname = str(key).lower()
if optname in URI_OPTIONS_DEPRECATION_MAP:
renamed_key = URI_OPTIONS_DEPRECATION_MAP[optname]
if renamed_key.lower() in options:
warnings.warn("Deprecated option '%s' ignored in favor of "
"'%s'." % (str(key), renamed_key))
continue
warnings.warn("Option '%s' is deprecated, use '%s' instead." % (
str(key), renamed_key))
undeprecated_options[str(key)] = value
return undeprecated_options


def _normalize_options(options):
"""Renames keys in the options dictionary to their internally-used
names."""
normalized_options = {}
for key, value in iteritems(options):
optname = str(key).lower()
intname = INTERNAL_URI_OPTION_NAME_MAP.get(optname, key)
normalized_options[intname] = options[key]
return normalized_options


def validate_options(opts, warn=False):
"""Validates and normalizes options passed in a MongoDB URI.

Expand Down Expand Up @@ -430,4 +554,4 @@ def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False):
pprint.pprint(parse_uri(sys.argv[1]))
except InvalidURI as exc:
print(exc)
sys.exit(0)
sys.exit(0)