def get_static_page_config(app_settings_dir=None, logger=None): # Start with the deprecated `share/jupyter/lab/settings/page_config.json` data cm = ConfigManager(config_dir_name="labconfig") page_config = cm.get('page_config') # TODO: remove in JupyterLab 4.0 if app_settings_dir: keyname = 'disabled_labextensions' old_page_config = pjoin(app_settings_dir, 'page_config.json') if osp.exists(old_page_config): if logger: logger.warn('** Upgrading deprecated page_config in %s' % old_page_config) logger.warn( '** This will no longer have an effect in JupyterLab 4.0') logger.warn('') with open(old_page_config) as fid: data = json.load(fid) os.remove(old_page_config) # Convert disabled_labextensions list to a dict oldKey = "disabledExtensions" if oldKey in data: data[keyname] = dict((key, True) for key in data[oldKey]) del data[oldKey] recursive_update(page_config, data) cm.set('page_config', page_config) return page_config
def _get_config_manager(level): """Get the location of config files for the current context Returns the string to the environment """ allowed = ["all", "user", "sys_prefix", "system", "app", "extension"] if level not in allowed: raise ValueError(f"Page config level must be one of: {allowed}") config_name = "labconfig" if level == "all": return ConfigManager(config_dir_name=config_name) if level == "user": config_dir = jupyter_config_dir() elif level == "sys_prefix": # Delayed import since this gets monkey-patched in tests from jupyter_core.paths import ENV_CONFIG_PATH config_dir = ENV_CONFIG_PATH[0] else: config_dir = SYSTEM_CONFIG_PATH[0] full_config_path = osp.join(config_dir, config_name) return ConfigManager(read_config_path=[full_config_path], write_config_dir=full_config_path)
def _get_overrides(app_settings_dir): """Get overrides settings from `app_settings_dir`.""" overrides, error = {}, "" overrides_path = os.path.join(app_settings_dir, 'overrides.json') if os.path.exists(overrides_path): with open(overrides_path, encoding='utf-8') as fid: try: overrides = json.load(fid) except Exception as e: error = e # Allow `default_settings_overrides.json` files in <jupyter_config>/labconfig dirs # to allow layering of defaults cm = ConfigManager(config_dir_name="labconfig") recursive_update(overrides, cm.get('default_setting_overrides')) return overrides, error
def _get_overrides(app_settings_dir): """Get overrides settings from `app_settings_dir`. The ordering of paths is: - {app_settings_dir}/overrides.d/*.{json,json5} (many, namespaced by package) - {app_settings_dir}/overrides.{json,json5} (singleton, owned by the user) """ overrides, error = {}, "" overrides_d = os.path.join(app_settings_dir, 'overrides.d') # find (and sort) the conf.d overrides files all_override_paths = sorted([ *(glob(os.path.join(overrides_d, '*.json'))), *(glob(os.path.join(overrides_d, '*.json5'))), ]) all_override_paths += [ os.path.join(app_settings_dir, 'overrides.json'), os.path.join(app_settings_dir, 'overrides.json5') ] for overrides_path in all_override_paths: if not os.path.exists(overrides_path): continue with open(overrides_path, encoding='utf-8') as fid: try: if overrides_path.endswith('.json5'): path_overrides = json5.load(fid) else: path_overrides = json.load(fid) for plugin_id, config in path_overrides.items(): recursive_update(overrides.setdefault(plugin_id, {}), config) print(overrides_path, overrides) except Exception as e: error = e # Allow `default_settings_overrides.json` files in <jupyter_config>/labconfig dirs # to allow layering of defaults cm = ConfigManager(config_dir_name="labconfig") for plugin_id, config in cm.get('default_setting_overrides').items(): recursive_update(overrides.setdefault(plugin_id, {}), config) return overrides, error
def get_static_page_config(app_settings_dir=None, logger=None, level='all'): """Get the static page config for JupyterLab Params ------ app_settings_dir: path, optional The path of the JupyterLab application settings directory logger: logger, optional An optional logging object level: string, optional ['all'] The level at which to get config: can be 'all', 'user', 'sys_prefix', or 'system' """ # Start with the deprecated `share/jupyter/lab/settings/page_config.json` data allowed = ['all', 'user', 'sys_prefix', 'system'] if level not in allowed: raise ValueError(f'Page config level must be one of: {allowed}') if level == 'all': cm = ConfigManager(config_dir_name="labconfig") else: user = level == 'user' sys_prefix = level == 'sys_prefix' config_dir = _get_config_dir(level) cm = ConfigManager(config_dir_name="labconfig", read_config_path=[config_dir], write_config_dir=os.path.join( config_dir, "labconfig")) page_config = cm.get('page_config') # TODO: remove in JupyterLab 4.0 if app_settings_dir: keyname = 'disabled_labextensions' old_page_config = pjoin(app_settings_dir, 'page_config.json') if osp.exists(old_page_config): if logger: logger.warn('** Upgrading deprecated page_config in %s' % old_page_config) logger.warn( '** This will no longer have an effect in JupyterLab 4.0') logger.warn('') with open(old_page_config) as fid: data = json.load(fid) os.remove(old_page_config) # Convert disabled_labextensions list to a dict oldKey = "disabledExtensions" if oldKey in data: data[keyname] = dict((key, True) for key in data[oldKey]) del data[oldKey] recursive_update(page_config, data) cm.set('page_config', page_config) return page_config
def get_nbserver_extensions(config_dirs): cm = ConfigManager(read_config_path=config_dirs) section = cm.get("jupyter_notebook_config") extensions = section.get('NotebookApp', {}).get('nbserver_extensions', {}) return extensions
def write_page_config(page_config): """Write page config to disk""" cm = ConfigManager(config_dir_name='labconfig') cm.set('page_config', page_config)