Coverage for /home/tbone/mambaforge/lib/python3.9/site-packages/importnb/utils/ipython.py: 0%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# coding: utf-8
2import ast
3import json
4import os
5import sys
6from pathlib import Path
8from IPython import get_ipython, paths
9from IPython.core import profiledir
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")
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 = {}
30 if "InteractiveShellApp" not in config:
31 config["InteractiveShellApp"] = {}
33 if "extensions" not in config["InteractiveShellApp"]:
34 config["InteractiveShellApp"]["extensions"] = []
36 return config, location
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)
46 with location.open("w") as file:
47 json.dump(config, file)
49 print("""✅ {}""".format(projects))
52def installed(project):
53 config, location = load_config()
54 return project in config.get("InteractiveShellApp", {}).get("extensions", [])
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 ]
65 with location.open("w") as file:
66 json.dump(config, file)
67 print("""❌ {}.""".format(projects))