codekingpro/portable-devtools
115k
1from IPython.core.magic import Magics, line_magic, magics_class # type: ignore2from IPython.core.magic_arguments import (3 argument,4 magic_arguments,5 parse_argstring,6) # type: ignore7 8from .main import find_dotenv, load_dotenv9 10 11@magics_class12class IPythonDotEnv(Magics):13 @magic_arguments()14 @argument(15 "-o",16 "--override",17 action="store_true",18 help="Indicate to override existing variables",19 )20 @argument(21 "-v",22 "--verbose",23 action="store_true",24 help="Indicate function calls to be verbose",25 )26 @argument(27 "dotenv_path",28 nargs="?",29 type=str,30 default=".env",31 help="Search in increasingly higher folders for the `dotenv_path`",32 )33 @line_magic34 def dotenv(self, line):35 args = parse_argstring(self.dotenv, line)36 # Locate the .env file37 dotenv_path = args.dotenv_path38 try:39 dotenv_path = find_dotenv(dotenv_path, True, True)40 except IOError:41 print("cannot find .env file")42 return43 44 # Load the .env file45 load_dotenv(dotenv_path, verbose=args.verbose, override=args.override)46 47 48def load_ipython_extension(ipython):49 """Register the %dotenv magic."""50 ipython.register_magics(IPythonDotEnv)51 