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

1# coding: utf-8

2 import ast

3 import json

4 import os

5 import sys

6 from pathlib import Path

7

8 from IPython import get_ipython, paths

9 from IPython.core import profiledir

10

11

12 def 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

22 def 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

39 def 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

52 def installed(project):

53 config, location = load_config()

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

55

56

57 def 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))