Coverage for /home/tbone/.local/share/hatch/env/virtual/importnb-aVRh-lqt/test.interactive/lib/python3.9/site-packages/importnb/utils/ipython.py: 0%

45 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-10-03 09:31 -0700

1# coding: utf-8 

2import ast 

3import json 

4import os 

5import sys 

6from pathlib import Path 

7 

8from IPython import get_ipython, paths 

9from IPython.core import profiledir 

10 

11 

12def get_config(profile="default"): 

13 profile_dir = profiledir.ProfileDir() 

14 try: 

15 profile = profile_dir.find_profile_dir_by_name(paths.get_ipython_dir(), profile) 

16 except profiledir.ProfileDirError: 

17 os.makedirs(paths.get_ipython_dir(), exist_ok=True) 

18 profile = profile_dir.create_profile_dir_by_name(paths.get_ipython_dir(), profile) 

19 return Path(profile.location, "ipython_config.json") 

20 

21 

22def load_config(): 

23 location = get_config() 

24 try: 

25 with location.open() as file: 

26 config = json.load(file) 

27 except (FileNotFoundError, getattr(json, "JSONDecodeError", ValueError)): 

28 config = {} 

29 

30 if "InteractiveShellApp" not in config: 

31 config["InteractiveShellApp"] = {} 

32 

33 if "extensions" not in config["InteractiveShellApp"]: 

34 config["InteractiveShellApp"]["extensions"] = [] 

35 

36 return config, location 

37 

38 

39def install(project="importnb"): 

40 """install the importnb extension""" 

41 config, location = load_config() 

42 projects = [project] 

43 if not installed(project): 

44 config["InteractiveShellApp"]["extensions"].extend(projects) 

45 

46 with location.open("w") as file: 

47 json.dump(config, file) 

48 

49 print("""✅ {}""".format(projects)) 

50 

51 

52def installed(project): 

53 config, location = load_config() 

54 return project in config.get("InteractiveShellApp", {}).get("extensions", []) 

55 

56 

57def uninstall(project="importnb"): 

58 """uninstall the importnb extension""" 

59 config, location = load_config() 

60 projects = [project] 

61 config["InteractiveShellApp"]["extensions"] = [ 

62 ext for ext in config["InteractiveShellApp"]["extensions"] if ext not in projects 

63 ] 

64 

65 with location.open("w") as file: 

66 json.dump(config, file) 

67 print("""❌ {}.""".format(projects))