您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

42 行
1.3 KiB

  1. from __future__ import print_function
  2. from IPython.core.magic import Magics, line_magic, magics_class # type: ignore
  3. from IPython.core.magic_arguments import (argument, magic_arguments, # type: ignore
  4. parse_argstring) # type: ignore
  5. from .main import find_dotenv, load_dotenv
  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)