Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Union
- from urllib.parse import urlparse
- import rethinkdb as r
- from toolz.dicttoolz import merge, valfilter
- _default_config = dict(host='localhost',
- port=r.DEFAULT_PORT,
- db=None,
- auth_key=None,
- user='admin',
- password=None,
- timeout=20)
- def parse_rethinkdb_url(url: str) -> dict:
- parse_ret = urlparse(url)
- config = {
- "host": parse_ret.hostname,
- "port": parse_ret.port,
- "user": parse_ret.username,
- "password": parse_ret.password,
- "db": parse_ret.path[1:] if len(parse_ret) > 1 else None
- }
- config = valfilter(lambda x: x is not None, config)
- return merge(_default_config, config)
- def connect(conf_or_url: Union[str, dict]) -> r.Connection:
- if isinstance(conf_or_url, str):
- conf = parse_rethinkdb_url(conf_or_url)
- else:
- conf = conf_or_url
- return r.connect(**conf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement