コード例 #1
0
def enable_plugins(plugin_filename=None):
    """Forces a plugin file to be parsed.

    A plugin file is a means of creating custom fields, quantities,
    data objects, colormaps, and other code classes and objects to be used
    in yt scripts without modifying the yt source directly.

    If ``plugin_filename`` is omitted, this function will look for a plugin file at
    ``$HOME/.config/yt/my_plugins.py``, which is the prefered behaviour for a
    system-level configuration.

    Warning: a script using this function will only be reproducible if your plugin
    file is shared with it.
    """
    import yt
    from yt.config import config_dir, old_config_dir, ytcfg
    from yt.fields.my_plugin_fields import my_plugins_fields

    if plugin_filename is not None:
        _fn = plugin_filename
        if not os.path.isfile(_fn):
            raise FileNotFoundError(_fn)
    else:
        # Determine global plugin location. By decreasing priority order:
        # - absolute path
        # - CONFIG_DIR
        # - obsolete config dir.
        my_plugin_name = ytcfg.get("yt", "plugin_filename")
        for base_prefix in ("", config_dir(), old_config_dir()):
            if os.path.isfile(os.path.join(base_prefix, my_plugin_name)):
                _fn = os.path.join(base_prefix, my_plugin_name)
                break
        else:
            raise FileNotFoundError(
                "Could not find a global system plugin file.")

        if _fn.startswith(old_config_dir()):
            mylog.warning(
                "Your plugin file is located in a deprecated directory. "
                "Please move it from %s to %s",
                os.path.join(old_config_dir(), my_plugin_name),
                os.path.join(config_dir(), my_plugin_name),
            )

    mylog.info("Loading plugins from %s", _fn)
    ytdict = yt.__dict__
    execdict = ytdict.copy()
    execdict["add_field"] = my_plugins_fields.add_field
    with open(_fn) as f:
        code = compile(f.read(), _fn, "exec")
        exec(code, execdict, execdict)
    ytnamespace = list(ytdict.keys())
    for k in execdict.keys():
        if k not in ytnamespace:
            if callable(execdict[k]):
                setattr(yt, k, execdict[k])
コード例 #2
0
    def __str__(self):
        from yt.config import config_dir

        try:
            conf = os.path.join(config_dir(), "yt", "yt.toml")
        except Exception:
            # this is really not a good time to raise another exception
            conf = "yt's configuration file"
        return f"You need to set an API key for {self.service!r} in {conf} as {self.config_name!r}"
コード例 #3
0
 def setUpClass(cls):
     cls.xdg_config_home = os.environ.get("XDG_CONFIG_HOME")
     cls.tmpdir = tempfile.mkdtemp()
     os.environ["XDG_CONFIG_HOME"] = cls.tmpdir
     with open(YTConfig.get_global_config_file(), mode="w") as fh:
         fh.write(_DUMMY_CFG_TOML)
     cls.plugin_path = os.path.join(config_dir(),
                                    ytcfg.get("yt", "plugin_filename"))
     with open(cls.plugin_path, mode="w") as fh:
         fh.write(TEST_PLUGIN_FILE)