ipython.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from __future__ import print_function
  2. from .main import load_dotenv, find_dotenv
  3. from IPython.core.magic import Magics, magics_class, line_magic
  4. from IPython.core.magic_arguments import (argument, magic_arguments,
  5. parse_argstring)
  6. @magics_class
  7. class IPythonDotEnv(Magics):
  8. @magic_arguments()
  9. @argument(
  10. '-o', '--override', action='store_true',
  11. help="Indicate to override existing variables"
  12. )
  13. @argument(
  14. '-v', '--verbose', action='store_true',
  15. help="Indicate function calls to be verbose"
  16. )
  17. @argument('dotenv_path', nargs='?', type=str, default='.env',
  18. help='Search in increasingly higher folders for the `dotenv_path`')
  19. @line_magic
  20. def dotenv(self, line):
  21. args = parse_argstring(self.dotenv, line)
  22. # Locate the .env file
  23. dotenv_path = args.dotenv_path
  24. try:
  25. dotenv_path = find_dotenv(dotenv_path, True, True)
  26. except IOError:
  27. print("cannot find .env file")
  28. return
  29. # Load the .env file
  30. load_dotenv(dotenv_path, verbose=args.verbose, override=args.override)
  31. def load_ipython_extension(ipython):
  32. """Register the %dotenv magic."""
  33. ipython.register_magics(IPythonDotEnv)