Advertisement
Guest User

Untitled

a guest
Sep 21st, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. from typing import Union
  2. from urllib.parse import urlparse
  3.  
  4. import rethinkdb as r
  5. from toolz.dicttoolz import merge, valfilter
  6.  
  7. _default_config = dict(host='localhost',
  8. port=r.DEFAULT_PORT,
  9. db=None,
  10. auth_key=None,
  11. user='admin',
  12. password=None,
  13. timeout=20)
  14.  
  15.  
  16. def parse_rethinkdb_url(url: str) -> dict:
  17. parse_ret = urlparse(url)
  18. config = {
  19. "host": parse_ret.hostname,
  20. "port": parse_ret.port,
  21. "user": parse_ret.username,
  22. "password": parse_ret.password,
  23. "db": parse_ret.path[1:] if len(parse_ret) > 1 else None
  24. }
  25.  
  26. config = valfilter(lambda x: x is not None, config)
  27.  
  28. return merge(_default_config, config)
  29.  
  30.  
  31. def connect(conf_or_url: Union[str, dict]) -> r.Connection:
  32. if isinstance(conf_or_url, str):
  33. conf = parse_rethinkdb_url(conf_or_url)
  34. else:
  35. conf = conf_or_url
  36.  
  37. return r.connect(**conf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement