def _load_jupyter_server_extension(nb_server_app): if 'JUPYTERLAB_DIR' not in os.environ: os.environ['JUPYTERLAB_DIR'] = os.path.join(sys.prefix, 'share', 'jupyter', 'phoila') if 'JUPYTERLAB_SETTINGS_DIR' not in os.environ: os.environ['JUPYTERLAB_SETTINGS_DIR'] = os.path.join( jupyter_config_path()[0], 'phoila', 'settings') if 'JUPYTERLAB_WORKSPACES_DIR' not in os.environ: os.environ['JUPYTERLAB_WORKSPACES_DIR'] = os.path.join( jupyter_config_path()[0], 'phoila', 'workspaces') config = load_config(nb_server_app) return add_handlers(nb_server_app.web_app, config)
def get_user_settings_dir(): """Get the configured QuantLab app directory. """ settings_dir = os.environ.get('QUANTLAB_SETTINGS_DIR') settings_dir = settings_dir or pjoin(jupyter_config_path()[0], 'lab', 'user-settings') return osp.realpath(settings_dir)
def get_default_jupyter_config(): """Search default jupyter configuration location paths. Return dictionary from configuration json files. """ config = {} try: from jupyter_core.paths import jupyter_config_path except ImportError: # jupyter not installed, must be using IPython return config for parent in jupyter_config_path(): try: for file in os.listdir(parent): if 'nbconvert' in file and file.endswith('.json'): abs_path = os.path.join(parent, file) with open(abs_path) as config_file: config.update(json.load(config_file)) except OSError: # some paths jupyter uses to find configurations # may not exist pass return config
def list_labextensions(self): """List all the labextensions""" print("Known labextensions:") seen = False for config_dir in jupyter_config_path(): config_dir = os.path.join(config_dir, CONFIG_DIR) cm = BaseJSONConfigManager(parent=self, config_dir=config_dir) labextensions = cm.get('labextensions') if labextensions: print(u'config dir: {}'.format(config_dir)) seen = True for name, config in sorted(labextensions.items()): if isinstance(config, bool): config = dict(enabled=config) enabled = config['enabled'] full_dest = find_labextension(name) print(u' {} {}: {}'.format( name, GREEN_ENABLED if enabled else RED_DISABLED, full_dest if not None else RED_X+" Files not found" )) if full_dest is not None: validate_labextension_folder(name, full_dest, self.log) if not seen: print('....None found!')
def list_nbextensions(self): """List all the nbextensions""" config_dirs = [ os.path.join(p, 'nbconfig') for p in jupyter_config_path() ] print("Known nbextensions:") for config_dir in config_dirs: head = u' config dir: {}'.format(config_dir) head_shown = False cm = BaseJSONConfigManager(parent=self, config_dir=config_dir) for section in NBCONFIG_SECTIONS: data = cm.get(section) if 'load_extensions' in data: if not head_shown: # only show heading if there is an nbextension here print(head) head_shown = True print(u' {} section'.format(section)) for require, enabled in data['load_extensions'].items(): print(u' {} {}'.format( require, GREEN_ENABLED if enabled else RED_DISABLED)) if enabled: validate_nbextension(require, logger=self.log)
def list_server_extensions(self): """List all enabled and disabled server extensions, by config path Enabled extensions are validated, potentially generating warnings. """ config_dirs = jupyter_config_path() # Iterate over all locations where extensions might be named. for config_dir in config_dirs: cm = BaseJSONConfigManager(parent=self, config_dir=config_dir) data = cm.get("jupyter_server_config") server_extensions = (data.setdefault("ServerApp", {}).setdefault( "jpserver_extensions", {})) if server_extensions: self.log.info(u'config dir: {}'.format(config_dir)) # Iterate over packages listed in jpserver_extensions. for pkg_name, enabled in server_extensions.items(): # Attempt to get extension metadata _, __ = _get_server_extension_metadata(pkg_name) self.log.info(u' {} {}'.format( pkg_name, GREEN_ENABLED if enabled else RED_DISABLED)) try: self.log.info(" - Validating {}...".format(pkg_name)) version = validate_server_extension(pkg_name) self.log.info(" {} {} {}".format( pkg_name, version, GREEN_OK)) except ExtensionValidationError as err: self.log.warn(" {} {}".format(RED_X, err))
def __init__(self): # # Get config from the env variables or from jupyter_cs3_config.json file # if 'CS3_EXT_CONFIG' in os.environ.keys() and utils.strtobool( os.environ['CS3_EXT_CONFIG']): self.config = {} self.log.info("Reading CS3 config from environment") for name in self.__env_names: env_name = "CS3_" + name.upper() if env_name in os.environ: self.log.info(f"Reading config value {name} = " + str(os.environ[env_name])) self.config[name] = os.environ[env_name] else: self.log.error(f"Unable to read {name}") raise ValueError( f"Unable to read {name} from environment variables") else: self.log.info( f'Reading CS3 config from file {self.__config_dir}{self.__config_file_name}' ) config_path = jupyter_config_path() if self.__config_dir not in config_path: # add self.config_dir to the front, if set manually config_path.insert(0, os.getcwd() + self.__config_dir) cm = ConfigManager(read_config_path=config_path) config_file = cm.get(self.__config_file_name) self.config = config_file.get("cs3") if self.config is None: self.log.error( f'Error while reading cs3 config file {self.__config_dir}{self.__config_file_name}' ) raise IOError(u'Error while reading cs3 config file')
def list_nbextensions(self): """List all the nbextensions""" config_dirs = [os.path.join(p, 'nbconfig') for p in jupyter_config_path()] print("Known bundlerextensions:") for config_dir in config_dirs: head = u' config dir: {}'.format(config_dir) head_shown = False cm = BaseJSONConfigManager(parent=self, config_dir=config_dir) data = cm.get('notebook') if 'bundlerextensions' in data: if not head_shown: # only show heading if there is an nbextension here print(head) head_shown = True for bundler_id, info in data['bundlerextensions'].items(): label = info.get('label') module = info.get('module_name') if label is None or module is None: msg = u' {} {}'.format(bundler_id, RED_DISABLED) else: msg = u' "{}" from {} {}'.format( label, module, GREEN_ENABLED ) print(msg)
def list_labextensions(self): """List all the labextensions""" print("Known labextensions:") for config_dir in jupyter_config_path(): cm = BaseJSONConfigManager(parent=self, config_dir=config_dir) data = cm.get(CONFIG_NAME) labextensions = ( data.setdefault("LabApp", {}) .setdefault("labextensions", {}) ) if labextensions: print(u'config dir: {}'.format(config_dir)) for name, config in sorted(labextensions.items()): if isinstance(config, bool): config = dict(enabled=config) enabled = config['enabled'] full_dest = find_labextension(name) print(u' {} {}: {}'.format( name, GREEN_ENABLED if enabled else RED_DISABLED, full_dest if not None else RED_X+" Files not found" )) if full_dest is not None: validate_labextension_folder(name, full_dest, self.log)
def __init__(self): config_path = jupyter_config_path() if self.__config_dir not in config_path: # add self.config_dir to the front, if set manually config_path.insert(0, os.getcwd() + self.__config_dir) cm = ConfigManager(read_config_path=config_path) config_file = cm.get(self.__config_file_name) config = config_file.get("cs3") # overwriting default values with config file if config is not None: for key in self.config.keys(): if key in config.keys(): self.config[key] = config[key] # overwriting the values with env vars for key in self.config.keys(): env_name = "CS3_" + key.upper() if env_name in os.environ: self.config[key] = os.environ[env_name] if len(self.config["root_dir_list"]) > 0: root_dir_list = tuple(k.strip() for k in self.config["root_dir_list"].split(',')) self.config["root_dir_list"] = root_dir_list else: self.config["root_dir_list"] = tuple() if not self.config["reva_host"]: raise KeyError("Reva host not provided") if not self.config["client_id"]: raise KeyError("Client ID not provided")
def environ( monkeypatch, tmp_path, data_dir, config_dir, ): system_data_dir = tmp_path / 'system_data' system_config_dir = tmp_path / 'system_config' system_path = [str(system_data_dir)] system_config_path = [str(system_config_dir)] # Set global environments variable monkeypatch.setenv('JUPYTER_CONFIG_DIR', str(config_dir)) monkeypatch.setenv('JUPYTER_DATA_DIR', str(data_dir)) # Set paths for each extension. for mod in (paths, ): monkeypatch.setattr(mod, 'SYSTEM_JUPYTER_PATH', system_path) monkeypatch.setattr(mod, 'ENV_JUPYTER_PATH', []) for mod in (paths, extensions_base): monkeypatch.setattr(mod, 'SYSTEM_CONFIG_PATH', system_config_path) monkeypatch.setattr(mod, 'ENV_CONFIG_PATH', []) assert paths.jupyter_config_path() == [str(config_dir) ] + system_config_path assert extensions_base._get_config_dir( user=False) == str(system_config_dir) assert paths.jupyter_path() == [str(data_dir)] + system_path
def get_user_settings_dir(): """Get the configured JupyterLab user settings directory. """ settings_dir = os.environ.get('JUPYTERLAB_SETTINGS_DIR') settings_dir = settings_dir or pjoin(jupyter_config_path()[0], 'lab', 'user-settings') return osp.realpath(settings_dir)
def get_workspaces_dir(): """Get the configured JupyterLab workspaces directory. """ workspaces_dir = os.environ.get('JUPYTERLAB_WORKSPACES_DIR') workspaces_dir = workspaces_dir or pjoin(jupyter_config_path()[0], 'lab', 'workspaces') return osp.realpath(workspaces_dir)
def list_nbextensions(self): """List all the nbextensions""" config_dirs = [os.path.join(p, 'nbconfig') for p in jupyter_config_path()] print("Known nbextensions:") for config_dir in config_dirs: head = u' config dir: {}'.format(config_dir) head_shown = False cm = BaseJSONConfigManager(parent=self, config_dir=config_dir) for section in NBCONFIG_SECTIONS: data = cm.get(section) if 'load_extensions' in data: if not head_shown: # only show heading if there is an nbextension here print(head) head_shown = True print(u' {} section'.format(section)) for require, enabled in data['load_extensions'].items(): print(u' {} {}'.format( require, GREEN_ENABLED if enabled else RED_DISABLED)) if enabled: validate_nbextension(require, logger=self.log)
def _default_config_dict(self): """ load merged config from more jupyter_notebook_config.d files re-uses notebook loading machinery to look through more locations """ manager = ConfigManager(read_config_path=jupyter_config_path()) return manager.get("jupyter_notebook_config").get("StarterManager", {})
def get_config(self): cfg = Config() for config_path in jupyter_config_path(): path = os.path.join(config_path, '{}.py'.format(self.config_name)) if os.path.isfile(path): self.recursive_update(cfg, PyFileConfigLoader(path).load_config()) return cfg
def get_workspaces_dir(): """Get the configured JupyterLab workspaces directory. """ workspaces_dir = os.environ.get('JUPYTERLAB_WORKSPACES_DIR') workspaces_dir = workspaces_dir or pjoin( jupyter_config_path()[0], 'lab', 'workspaces' ) return osp.realpath(workspaces_dir)
def get_user_settings_dir(): """Get the configured JupyterLab user settings directory. """ settings_dir = os.environ.get('JUPYTERLAB_SETTINGS_DIR') settings_dir = settings_dir or pjoin( jupyter_config_path()[0], 'lab', 'user-settings' ) return osp.realpath(settings_dir)
def load_config(self, config_name): cfg = Config() for path in jupyter_config_path(): file = os.path.join(path, config_name) if os.path.isfile(file): new_cfg = PyFileConfigLoader(file).load_config() cfg.merge(new_cfg) return cfg
def _link_jupyter_server_extension(serverapp): # Get the extension manager from the server manager = serverapp.extension_manager logger = serverapp.log # Hack that patches the enabled extensions list, prioritizing # jupyter nbclassic. In the future, it would be much better # to incorporate a dependency injection system in the # Extension manager that allows extensions to list # their dependency tree and sort that way. def sorted_extensions(self): """Dictionary with extension package names as keys and an ExtensionPackage objects as values. """ # Sort the keys and keys = sorted(self.extensions.keys()) keys.remove("nbclassic") keys = ["nbclassic"] + keys return {key: self.extensions[key] for key in keys} manager.__class__.sorted_extensions = property(sorted_extensions) # Look to see if nbclassic is enabled. if so, # link the nbclassic extension here to load # its config. Then, port its config to the serverapp # for backwards compatibility. try: pkg = manager.extensions["nbclassic"] pkg.link_point("jupyter-nbclassic", serverapp) point = pkg.extension_points["jupyter-nbclassic"] nbapp = point.app except Exception: nbapp = NotebookAppTraits() # Proxy NotebookApp traits through serverapp to notebookapp. members = diff_members(serverapp, nbapp) for m in members: proxy(serverapp, nbapp, m) # Find jupyter server extensions listed as notebook server extensions. jupyter_paths = jupyter_config_path() config_dirs = jupyter_paths + [serverapp.config_dir] nbserver_extensions = get_nbserver_extensions(config_dirs) # Link all extensions found in the old locations for # notebook server extensions. for name, enabled in nbserver_extensions.items(): # If the extension is already enabled in the manager, i.e. # because it was discovered already by Jupyter Server # through its jupyter_server_config, then don't re-enable here. if name not in manager.extensions: successful = manager.add_extension(name, enabled=enabled) if successful: logger.info( "{name} | extension was found and enabled by nbclassic. " "Consider moving the extension to Jupyter Server's " "extension paths.".format(name=name)) manager.link_extension(name, serverapp)
def install(nb_path=None, DEBUG=False): install_path = None print('Starting hide_code.js install...') current_dir = path.abspath(path.dirname(__file__)) config_dirs = j_path.jupyter_config_path() site_packages_path = get_site_package_dir() # check for config directory with a "custom" folder # TODO update this logic to check if custom.js file exists for dir in config_dirs: custom_dir = path.join(dir, "custom") if path.isdir(custom_dir): install_path = custom_dir break # last ditch effort in case jupyter config directories don't contain custom/custom.js if install_path == None: print("No config directories contain \"custom\" folder. Trying site-packages...") install_path = path.join(site_packages_path, "notebook/static/custom") if nb_path != None: install_path = nb_path print("Using argument supplied path: " + install_path) if DEBUG: print(install_path) # copy js into static/custom directory in Jupyter/iPython directory if(path.isdir(install_path)): shutil.copyfile(path.join(current_dir, "hide_code.js"), path.join(install_path, "hide_code.js")) print('Copying hide_code.js to ' + install_path) # add require to end of custom.js to auto-load on notebook startup print("Attempting to configure custom.js to auto-load hide__code.js...") try: with open(path.join(current_dir, "auto-load.txt")) as auto: auto_load_txt = auto.read(); auto_loaded = False # check if auto-load.txt is already in custom.js with open(path.join(install_path, "custom.js"), 'r') as customJS: if auto_load_txt in customJS.read(): auto_loaded = True print("Custom.js already configured to auto-load hide_code.js.") if not auto_loaded: # append auto load require to end of custom.js with open(path.join(install_path, "custom.js"), 'a') as customJS: customJS.write(auto_load_txt) print("Configured custom.js to auto-load hide_code.js.") except: print("Custom.js not in custom directory.") else: print('Unable to install into ' + install_path) print('Directory doesn\'t exist.') print('Make sure Jupyter is installed.')
def sparkmanager_config_path(): """ Gets all paths on the system that could store configuration files. These paths are derived from those used by jupyter in addition to the module path """ module_path = os.path.dirname(os.path.abspath(__file__)) jupyter_paths = jupyter_config_path() sparkmanager_paths = [c.replace("jupyter", "sparkmanager") for c in jupyter_paths] + [module_path] return sparkmanager_paths
def test_extension_manager_api(): jpserver_extensions = {"jupyter_server.tests.extension.mockextensions": True} manager = ExtensionManager() assert manager.config_manager expected = _normalize_path(os.path.join(jupyter_config_path()[0], "serverconfig")) assert _normalize_path(manager.config_manager.read_config_path[0]) == expected manager.from_jpserver_extensions(jpserver_extensions) assert len(manager.extensions) == 1 assert "jupyter_server.tests.extension.mockextensions" in manager.extensions
def _load_jupyter_server_extension(serverapp): # Patch the config service manager to find the # proper path for old notebook frontend extensions config_manager = serverapp.config_manager read_config_path = config_manager.read_config_path read_config_path += [ os.path.join(p, 'nbconfig') for p in jupyter_config_path() ] config_manager.read_config_path = read_config_path
def test_jupyter_config_path_env(): path_env = os.pathsep.join([ pjoin('foo', 'bar'), pjoin('bar', 'baz', ''), # trailing / ]) with patch.dict('os.environ', {'JUPYTER_CONFIG_PATH': path_env}): path = jupyter_config_path() assert path[:2] == [pjoin('foo', 'bar'), pjoin('bar', 'baz')]
def load_config(self): paths = jupyter_config_path() paths.insert(0, os.getcwd()) app = NbGrader() app.config_file_paths.append(paths) app.load_config_file() return app.config
def _link_jupyter_server_extension(serverapp): # Get the extension manager from the server manager = serverapp.extension_manager logger = serverapp.log # Hack that patches the enabled extensions list, prioritizing # jupyter nbclassic. In the future, it would be much better # to incorporate a dependency injection system in the # Extension manager that allows extensions to list # their dependency tree and sort that way. def extensions(self): """Dictionary with extension package names as keys and an ExtensionPackage objects as values. """ # Pop out the nbclassic extension to prepend # this extension at the front of the sorted server extensions. nb = self._extensions.get("nbclassic") # Sort all other extensions alphabetically. other_extensions = dict(sorted(self._extensions.items())) # Build a new extensions dictionary, sorted with nbclassic first. sorted_extensions = {"nbclassic": nb} sorted_extensions.update(**other_extensions) return sorted_extensions manager.__class__.extensions = property(extensions) # Look to see if nbclassic is enabled. if so, # link the nbclassic extension here to load # its config. Then, port its config to the serverapp # for backwards compatibility. try: pkg = manager.extensions["nbclassic"] pkg.link_point("jupyter-nbclassic", serverapp) point = pkg.extension_points["jupyter-nbclassic"] nbapp = point.app except Exception: nbapp = NotebookAppTraits() # Proxy NotebookApp traits through serverapp to notebookapp. members = diff_members(serverapp, nbapp) for m in members: proxy(serverapp, nbapp, m) # Find jupyter server extensions listed as notebook server extensions. jupyter_paths = jupyter_config_path() config_dirs = jupyter_paths + [serverapp.config_dir] nbserver_extensions = get_nbserver_extensions(config_dirs) # Link all extensions found in the old locations for # notebook server extensions. manager.from_jpserver_extensions(nbserver_extensions) for name in nbserver_extensions: logger.info("{name} | extension was found and enabled by nbclassic. " "Consider moving the extension to Jupyter Server's " "extension paths.".format(name=name)) manager.link_extension(name, serverapp)
def _load_jupyter_server_extension(jupyter_app): if "JUPYTERLAB_DIR" not in os.environ: os.environ["JUPYTERLAB_DIR"] = os.path.join(sys.prefix, "share", "jupyter", "phoila") if "JUPYTERLAB_SETTINGS_DIR" not in os.environ: os.environ["JUPYTERLAB_SETTINGS_DIR"] = os.path.join( jupyter_config_path()[0], "phoila", "settings") if "JUPYTERLAB_WORKSPACES_DIR" not in os.environ: os.environ["JUPYTERLAB_WORKSPACES_DIR"] = os.path.join( jupyter_config_path()[0], "phoila", "workspaces") settings = jupyter_app.web_app.settings if "mathjax_url" not in settings: settings["mathjax_url"] = jupyter_app.mathjax_url if "mathjax_config" not in settings: settings["mathjax_config"] = jupyter_app.mathjax_config config = load_config(jupyter_app) return add_handlers(jupyter_app, config)
def _default_config_dict(self): """load merged config from more jupyter_*_config.d files re-uses notebook loading machinery to look through more locations """ manager = ConfigManager(read_config_path=jupyter_config_path()) conf = {} for app in ["_", "_notebook_", "_server_"]: conf.update(**manager.get(f"jupyter{app}config").get("StarterManager", {})) return conf
def test_paths(): output = get_jupyter_output("--paths") for d in (jupyter_config_dir(), jupyter_data_dir(), jupyter_runtime_dir()): assert d in output for key in ("config", "data", "runtime"): assert ("%s:" % key) in output for path in (jupyter_config_path(), jupyter_path()): for d in path: assert d in output
def test_paths(): output = get_jupyter_output('--paths') for d in (jupyter_config_dir(), jupyter_data_dir(), jupyter_runtime_dir()): assert d in output for key in ('config', 'data', 'runtime'): assert ('%s:' % key) in output for path in (jupyter_config_path(), jupyter_path()): for d in path: assert d in output
def _default_conf_d_language_servers(self): language_servers = {} # type: KeyedLanguageServerSpecs manager = ConfigManager(read_config_path=jupyter_config_path()) for app in APP_CONFIG_D_SECTIONS: language_servers.update(**manager.get(f"jupyter{app}config").get( self.__class__.__name__, {}).get("language_servers", {})) return language_servers
def find_nbgallery_url(): for config_dir in [os.path.join(p, 'nbconfig') for p in jupyter_config_path()]: cm = BaseJSONConfigManager(config_dir=config_dir) config = cm.get('common') try: return config['nbgallery']['url'] except: # keep going pass # Return from environment, or else nbgallery rails default return os.getenv('NBGALLERY_URL', 'http://localhost:3000')
def test_jupyter_config_path_prefer_env(): with prefer_env, patch.object(site, 'ENABLE_USER_SITE', True): path = jupyter_config_path() # deduplicated expected values values = list(dict.fromkeys([ paths.ENV_CONFIG_PATH[0], jupyter_config_dir(), os.path.join(site.getuserbase(), 'etc', 'jupyter') ])) for p,v in zip(path, values): assert p == v
def check_all(): for folder in jupyter_config_path(): for root, _, files in os.walk(folder): for f in files: if f.endswith("json"): json_pth = os.path.join(root, f) try: with open(json_pth, 'r') as f: json.load(f) print(f"File {json_pth} is OK.") except json.decoder.JSONDecodeError: print('Failed JSON file is: ', json_pth)
def get_labconfig(nbapp): """Get the merged lab configuration.""" # Load server extensions with ConfigManager. # This enables merging on keys, which we want for extension enabling. # Regular config loading only merges at the class level, # so each level (user > env > system) clobbers the previous. config_path = jupyter_config_path() if nbapp.config_dir not in config_path: # add nbapp's config_dir to the front, if set manually config_path.insert(0, nbapp.config_dir) config_path = [os.path.join(p, CONFIG_DIR) for p in config_path] return ConfigManager(read_config_path=config_path)
def load_config(self): paths = jupyter_config_path() paths.insert(0, os.getcwd()) config_found = False full_config = Config() for config in NbGrader._load_config_files("nbgrader_config", path=paths, log=self.log): full_config.merge(config) config_found = True if not config_found: self.log.warning("No nbgrader_config.py file found. Rerun with DEBUG log level to see where nbgrader is looking.") return full_config
def _find_disable_nbextension(section, require, logger=None): """Disable an nbextension from the first config location where it is enabled. Returns True if it changed any config, False otherwise. """ for config_dir in jupyter_config_path(): cm = BaseJSONConfigManager( config_dir=os.path.join(config_dir, 'nbconfig')) d = cm.get(section) if d.get('load_extensions', {}).get(require, None): if logger: logger.info("Disabling %s extension in %s", require, config_dir) cm.update(section, {'load_extensions': {require: None}}) return True return False
def get_labextensions(parent=None): """Get the list of enabled lab extensions""" extensions = [] config_dirs = [os.path.join(p, 'labconfig') for p in jupyter_config_path()] for config_dir in config_dirs: cm = BaseJSONConfigManager(parent=parent, config_dir=config_dir) data = cm.get("jupyterlab_config") labextensions = ( data.setdefault("LabApp", {}) .setdefault("labextensions", {}) ) for name, enabled in labextensions.items(): if enabled: extensions.append(name) return extensions
def list_labextensions(self): """List all the labextensions""" config_dirs = [os.path.join(p, 'labconfig') for p in jupyter_config_path()] print("Known labextensions:") for config_dir in config_dirs: cm = BaseJSONConfigManager(parent=self, config_dir=config_dir) data = cm.get("jupyterlab_config") labextensions = ( data.setdefault("LabApp", {}) .setdefault("labextensions", {}) ) if labextensions: print(u'config dir: {}'.format(config_dir)) for name, enabled in labextensions.items(): print(u' {} {}'.format( name, GREEN_ENABLED if enabled else RED_DISABLED)) validate_labextension(name, self.log)
def list_server_extensions(self): """List all enabled and disabled server extensions, by config path Enabled extensions are validated, potentially generating warnings. """ config_dirs = jupyter_config_path() for config_dir in config_dirs: cm = BaseJSONConfigManager(parent=self, config_dir=config_dir) data = cm.get("jupyter_notebook_config") server_extensions = ( data.setdefault("NotebookApp", {}) .setdefault("nbserver_extensions", {}) ) if server_extensions: print(u'config dir: {}'.format(config_dir)) for import_name, enabled in server_extensions.items(): print(u' {} {}'.format( import_name, GREEN_ENABLED if enabled else RED_DISABLED)) validate_serverextension(import_name, self.log)
def get_default_jupyter_config(): """Search default jupyter configuration location paths. Return dictionary from configuration json files. """ config = {} from jupyter_core.paths import jupyter_config_path for parent in jupyter_config_path(): try: for file in os.listdir(parent): if 'nbconvert' in file and file.endswith('.json'): abs_path = os.path.join(parent, file) with open(abs_path) as config_file: config.update(json.load(config_file)) except OSError: # some paths jupyter uses to find configurations # may not exist pass return config
def build_config(entrypoint, include_none=False): if entrypoint not in entrypoint_configurables: raise ValueError('Config for entrypoint name %r is not defined! Accepted values are %r.' % ( entrypoint, list(entrypoint_configurables.keys()) )) # Get config from disk: disk_config = {} path = jupyter_config_path() path.insert(0, py3compat.getcwd()) for c in _load_config_files('nbdime_config', path=path): recursive_update(disk_config, c, include_none) config = {} configurable = entrypoint_configurables[entrypoint] for c in reversed(configurable.mro()): if issubclass(c, NbdimeConfigurable): recursive_update(config, config_instance(c).configured_traits(c), include_none) if (c.__name__ in disk_config): recursive_update(config, disk_config[c.__name__], include_none) return config
def setUp(self): self.tempdirs = [] self._mock_extensions = [] self.test_dir = self.tempdir() self.data_dir = os.path.join(self.test_dir, 'data') self.config_dir = os.path.join(self.test_dir, 'config') self.system_data_dir = os.path.join(self.test_dir, 'system_data') self.system_config_dir = os.path.join(self.test_dir, 'system_config') self.system_path = [self.system_data_dir] self.system_config_path = [self.system_config_dir] self.patches = [] p = patch.dict('os.environ', { 'JUPYTER_CONFIG_DIR': self.config_dir, 'JUPYTER_DATA_DIR': self.data_dir, }) self.patches.append(p) for mod in (paths, nbextensions): p = patch.object(mod, 'SYSTEM_JUPYTER_PATH', self.system_path) self.patches.append(p) p = patch.object(mod, 'ENV_JUPYTER_PATH', []) self.patches.append(p) for mod in (paths, extensions): p = patch.object(mod, 'SYSTEM_CONFIG_PATH', self.system_config_path) self.patches.append(p) p = patch.object(mod, 'ENV_CONFIG_PATH', []) self.patches.append(p) for p in self.patches: p.start() self.addCleanup(p.stop) # verify our patches self.assertEqual(paths.jupyter_config_path(), [self.config_dir] + self.system_config_path) self.assertEqual(extensions._get_config_dir(user=False), self.system_config_dir) self.assertEqual(paths.jupyter_path(), [self.data_dir] + self.system_path)
def list_labextensions(self): """List all the labextensions""" config_dirs = [os.path.join(p, 'labconfig') for p in jupyter_config_path()] print("Known labextensions:") for config_dir in config_dirs: cm = BaseJSONConfigManager(parent=self, config_dir=config_dir) data = cm.get("jupyterlab_config") labextensions = ( data.setdefault("LabApp", {}) .setdefault("labextensions", {}) ) if labextensions: print(u'config dir: {}'.format(config_dir)) for name, enabled in sorted(labextensions.items()): full_dest = find_labextension(name) print(u' {} {}: {}'.format( name, GREEN_ENABLED if enabled else RED_DISABLED, full_dest if not None else RED_X+" Files not found" )) if full_dest is not None: validate_labextension_folder(name, full_dest, self.log)
def setUp(self): # Any TemporaryDirectory objects appended to this list will be cleaned # up at the end of the test run. self.tempdirs = [] self._mock_extensions = [] @self.addCleanup def cleanup_tempdirs(): for d in self.tempdirs: d.cleanup() self.src = self.tempdir() self.name = 'mockextension' self.files = files = [ pjoin(u'ƒile'), pjoin(u'∂ir', u'ƒile1'), pjoin(u'∂ir', u'∂ir2', u'ƒile2'), ] for file in files: fullpath = os.path.join(self.src, file) parent = os.path.dirname(fullpath) if not os.path.exists(parent): os.makedirs(parent) touch(fullpath) self.test_dir = self.tempdir() self.data_dir = os.path.join(self.test_dir, 'data') self.config_dir = os.path.join(self.test_dir, 'config') self.system_data_dir = os.path.join(self.test_dir, 'system_data') self.system_config_dir = os.path.join(self.test_dir, 'system_config') self.system_path = [self.system_data_dir] self.system_config_path = [self.system_config_dir] self.system_labext = os.path.join(self.system_data_dir, 'labextensions') self.patches = [] p = patch.dict('os.environ', { 'JUPYTER_CONFIG_DIR': self.config_dir, 'JUPYTER_DATA_DIR': self.data_dir, }) self.patches.append(p) for mod in (paths, labextensions): p = patch.object(mod, 'SYSTEM_JUPYTER_PATH', self.system_path) self.patches.append(p) p = patch.object(mod, 'ENV_JUPYTER_PATH', []) self.patches.append(p) for mod in (paths, labextensions): p = patch.object(mod, 'SYSTEM_CONFIG_PATH', self.system_config_path) self.patches.append(p) p = patch.object(mod, 'ENV_CONFIG_PATH', []) self.patches.append(p) for p in self.patches: p.start() self.addCleanup(p.stop) # verify our patches self.assertEqual(paths.jupyter_config_path(), [self.config_dir] + self.system_config_path) self.assertEqual(labextensions._get_config_dir(user=False), os.path.join(self.system_config_dir, CONFIG_DIR)) self.assertEqual(paths.jupyter_path(), [self.data_dir] + self.system_path)
def config_file_paths(self): path = jupyter_config_path() if self.config_dir not in path: path.insert(0, self.config_dir) path.insert(0, os.getcwd()) return path
def install(nb_path=None, server_config=True, DEBUG=False): install_path = None print('Starting hide_code.js install...') current_dir = path.abspath(path.dirname(__file__)) config_dirs = j_path.jupyter_config_path() site_packages_path = Utils.get_site_package_dir() # check for config directory with a "custom" folder # TODO update this logic to check if custom.js file exists for dir in config_dirs: custom_dir = path.join(dir, "custom") if path.isdir(custom_dir): install_path = custom_dir break # last ditch effort in case jupyter config directories don't contain custom/custom.js if install_path == None: print("No config directories contain \"custom\" folder. Trying site-packages...") install_path = path.join(site_packages_path, "notebook", "static", "custom") if nb_path != None: install_path = nb_path print("Using argument supplied path: " + install_path) if DEBUG: print(install_path) # copy js into static/custom directory in Jupyter/iPython directory if path.isdir(install_path): shutil.copyfile(path.join(current_dir, "hide_code.js"), path.join(install_path, "hide_code.js")) print('Copying hide_code.js to ' + install_path) # add require to end of custom.js to auto-load on notebook startup print("Attempting to configure custom.js to auto-load hide_code.js...") try: with open(path.join(current_dir, "auto-load.txt")) as auto: auto_load_txt = auto.read(); auto_loaded = False # check if auto-load.txt is already in custom.js with open(path.join(install_path, "custom.js"), 'r') as customJS: if auto_load_txt in customJS.read(): auto_loaded = True print("Custom.js already configured to auto-load hide_code.js.") if not auto_loaded: # append auto load require to end of custom.js with open(path.join(install_path, "custom.js"), 'a') as customJS: customJS.write(auto_load_txt) print("Configured custom.js to auto-load hide_code.js.") except: print("Custom.js not in custom directory.") else: print('Unable to install into ' + install_path) print('Directory doesn\'t exist.') print('Make sure Jupyter is installed.') if server_config: print("Attempting to configure auto-loading for hide_code export handlers.") try: # Activate the Python server extension server_cm = ConfigManager(config_dir=j_path.jupyter_config_dir()) cfg = server_cm.get('jupyter_notebook_config') server_extensions = (cfg.setdefault('NotebookApp', {}) .setdefault('server_extensions', []) ) extension = 'hide_code.hide_code' if extension not in server_extensions: cfg['NotebookApp']['server_extensions'] += [extension] server_cm.update('jupyter_notebook_config', cfg) print('Configured jupyter to auto-load hide_code export handlers.') else: print("Jupyter already configured to auto-load export handlers.") except: print('Unable to install server extension.')
def _default_read_config_path(self): return [os.path.join(p, 'nbconfig') for p in jupyter_config_path()]