Exemple #1
0
def persist_config(config_file=None, mode=0o600):
    """Context manager that can be used to modify a config object

    On exit of the context manager, the config will be written back to disk, 
    by default with user-only (600) permissions.
    """

    if config_file is None:
        config_file = os.path.join(jupyter_config_dir(), 'jupyter_notebook_config.json')

    loader = JSONFileConfigLoader(os.path.basename(config_file), os.path.dirname(config_file))
    try:
        config = loader.load_config()
    except ConfigFileNotFound:
        config = Config()

    yield config

    with io.open(config_file, 'w', encoding='utf8') as f:
        f.write(cast_unicode(json.dumps(config, indent=2)))

    try:
        os.chmod(config_file, mode)
    except Exception as e:
        tb = traceback.format_exc()
        warnings.warn("Failed to set permissions on %s:\n%s" % (config_file, tb),
            RuntimeWarning)
def persist_config(config_file=None, mode=0o600):
    """Context manager that can be used to modify a config object

    On exit of the context manager, the config will be written back to disk,
    by default with user-only (600) permissions.
    """

    if config_file is None:
        config_file = os.path.join(jupyter_config_dir(),
                                   "jupyter_server_config.json")

    os.makedirs(os.path.dirname(config_file), exist_ok=True)

    loader = JSONFileConfigLoader(os.path.basename(config_file),
                                  os.path.dirname(config_file))
    try:
        config = loader.load_config()
    except ConfigFileNotFound:
        config = Config()

    yield config

    with open(config_file, "w", encoding="utf8") as f:
        f.write(json.dumps(config, indent=2))

    try:
        os.chmod(config_file, mode)
    except Exception:
        tb = traceback.format_exc()
        warnings.warn(f"Failed to set permissions on {config_file}:\n{tb}",
                      RuntimeWarning)
Exemple #3
0
def _activate(profile, mixed):
    dname = os.path.dirname(__file__)

    with jconfig(profile) as config:
        if 'NotebookApp' in config:
            if (config['NotebookApp'].get('tornado_setting',
                                          {})) or (config['NotebookApp'].get(
                                              'contents_manager_class', {})):
                # TODO, manually merge tornado setting if exist
                # but cannot do anything automatically if contents_manager_class is set
                raise ValueError(
                    'You already got some configuration that will conflict with google drive. Bailin out'
                )
        if mixed:
            drive_config = JSONFileConfigLoader('mixed_contents.json',
                                                dname).load_config()
        else:
            drive_config = JSONFileConfigLoader('jupyter_notebook_config.json',
                                                dname).load_config()
        config.merge(drive_config)
        if not JUPYTER:
            log.info('Activating Google Drive integration for profile "%s"' %
                     profile)
        else:
            log.info('Activating Google Drive integration')
def persist_config(config_file=None, mode=0o600):
    """Context manager that can be used to modify a config object

    On exit of the context manager, the config will be written back to disk,
    by default with user-only (600) permissions.
    """

    if config_file is None:
        config_file = os.path.join(jupyter_config_dir(), 'jupyter_server_config.json')

    loader = JSONFileConfigLoader(os.path.basename(config_file), os.path.dirname(config_file))
    try:
        config = loader.load_config()
    except ConfigFileNotFound:
        config = Config()

    yield config

    with io.open(config_file, 'w', encoding='utf8') as f:
        f.write(cast_unicode(json.dumps(config, indent=2)))

    try:
        os.chmod(config_file, mode)
    except Exception as e:
        tb = traceback.format_exc()
        warnings.warn("Failed to set permissions on %s:\n%s" % (config_file, tb),
            RuntimeWarning)
Exemple #5
0
    def __enter__(self):
        if JUPYTER:
            self.pdir = jupyter_config_dir()
            self.cff_name = 'jupyter_notebook_config.json'
        else:
            self.pdir = locate_profile(self.profile)
            self.cff_name = 'ipython_notebook_config.json'

        jc = JSONFileConfigLoader(self.cff_name, self.pdir)

        try:
            self.config = jc.load_config();
        except (ConfigFileNotFound,ValueError):
            self.config = Config()
        return self.config
Exemple #6
0
    def __enter__(self):
        if JUPYTER:
            self.pdir = jupyter_config_dir()
            self.cff_name = 'jupyter_notebook_config.json'
        else:
            self.pdir = locate_profile(self.profile)
            self.cff_name = 'ipython_notebook_config.json'

        jc = JSONFileConfigLoader(self.cff_name, self.pdir)

        try:
            self.config = jc.load_config();
        except (ConfigFileNotFound,ValueError):
            self.config = Config()
        return self.config
Exemple #7
0
def load_config(path=None):
    """Load a Python or JSON config file."""
    if not path or not op.exists(path):
        return Config()
    path = op.realpath(path)
    dirpath, filename = op.split(path)
    file_ext = op.splitext(path)[1]
    logger.debug("Load config file `%s`.", path)
    if file_ext == '.py':
        config = PyFileConfigLoader(filename, dirpath,
                                    log=logger).load_config()
    elif file_ext == '.json':
        config = JSONFileConfigLoader(filename, dirpath,
                                      log=logger).load_config()
    return config
Exemple #8
0
def load_config(path=None):
    """Load a Python or JSON config file and return a `Config` instance."""
    if not path:
        return Config()
    path = Path(path)
    if not path.exists():  # pragma: no cover
        return Config()
    file_ext = path.suffix
    logger.debug("Load config file `%s`.", path)
    if file_ext == '.py':
        config = PyFileConfigLoader(path.name, str(path.parent),
                                    log=logger).load_config()
    elif file_ext == '.json':
        config = JSONFileConfigLoader(path.name, str(path.parent),
                                      log=logger).load_config()
    return config
Exemple #9
0
def _activate(profile):
    dname = os.path.dirname(__file__)

    with jconfig(profile) as config:
        if "Exporter" in config:
            if (config["Exporter"].get("preprocessors",{})):
                # manually merge setting if exist
                if not 'pre_markdown.MarkdownPreprocessor' in config.Exporter.preprocessors:
                    config['Exporter']['preprocessors'].append('pre_markdown.MarkdownPreprocessor')
                if not 'pre_cit2c.BibTexPreprocessor' in config.Exporter.preprocessors:
                    config['Exporter']['preprocessors'].append('pre_cite2c.BibTexPreprocessor')
            else:
                my_config  = JSONFileConfigLoader('jupyter_nbconvert_config.json', dname).load_config()
                config.merge(my_config)

        if not JUPYTER:
            log.info('Activating preprocessors for profile "%s"' % profile)
        else:
            log.info('Activating preprocessors')