Esempio n. 1
0
def test_load_datafile(codec, dataset):
    obj = codec.cls()

    with pytest.raises(IOError):
        munge.load_datafile('nonexistant', data_dir)

    # default value
    assert None == munge.load_datafile('nonexistant', data_dir, default=None)
    assert 'DEFAULT' == munge.load_datafile('nonexistant', data_dir, default='DEFAULT')

    data = munge.load_datafile(dataset.filename, this_dir)
    assert data

    # test hardset extension
    assert obj.extensions
    for ext in obj.extensions:
        fq_path = "%s.%s" % (dataset.filename, ext)
        data = munge.load_datafile(fq_path, this_dir, default=None)
        if data:
            break
        data = None
    assert data

    # test default search path '.'
    #files = munge.find_datafile('set0', data_dir)
    files = munge.find_datafile(fq_path, this_dir)
    assert 1 == len(files)
    relpath = os.path.relpath(files[0][1])
    data = munge.load_datafile(relpath)
    assert data

    with pytest.raises(IOError):
        munge.load_datafile(dataset.filename, this_dir, codecs={})
Esempio n. 2
0
File: config.py Progetto: 20c/munge
    def read(self, config_dir=None, config_name=None, clear=False):
        """
        read config from config_dir
        if config_dir is None, clear to default config
        clear will clear to default before reading new file
        """

        # TODO should probably allow config_dir to be a list as well
        # get name of config directory
        if not config_dir:
            config_dir = self.defaults.get("config_dir", None)
        if not config_dir:
            raise KeyError("config_dir not set")

        # get name of config file
        if not config_name:
            config_name = self.defaults.get("config_name", None)
        if not config_name:
            raise KeyError("config_name not set")

        conf_path = os.path.expanduser(config_dir)
        if not os.path.exists(conf_path):
            raise IOError("config dir not found at %s" % (conf_path,))

        config = munge.load_datafile(config_name, conf_path, default=None)

        if not config:
            raise IOError("config file not found in %s" % (conf_path,))

        if clear:
            self.clear()

        munge.util.recursive_update(self.data, config)
        self._meta_config_dir = conf_path
        return self
Esempio n. 3
0
    def read(self, config_dir=None, clear=False):
        """ read config from config_dir
            if config_dir is None, clear to default config
            clear will clear to default before reading new file
        """

        if not config_dir:
            if not self.defaults['config_dir']:
                raise IOError("default config dir not set")
            config_dir = self.defaults['config_dir']

        conf_path = os.path.expanduser(config_dir)
        if not os.path.exists(conf_path):
            raise IOError("config dir not found at %s" % (conf_path,))

        if clear:
            self.clear()

        config = munge.load_datafile('config', conf_path, default=None)

        if not config:
            raise IOError("config file not found in %s" % (conf_path,))

        munge.util.recursive_update(self.data, config)
        self._meta_config_dir = conf_path

        return self
Esempio n. 4
0
def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    parser = argparse.ArgumentParser(description="netom")
    parser.add_argument("--version", action="version",
                        version="{}s version {}".format("%(prog)", netom.__version__))
    # parser.add_argument("--output-format", help="output format (yaml, json, text)")

    parser.add_argument("command")
    parser.add_argument("render_call")
    parser.add_argument("model_version")
    parser.add_argument("model_type")
    parser.add_argument("data_file")
    args = parser.parse_args(argv)

    # get dict of options and update config
    argd = vars(args)

    command = argd["command"]
    render_call = argd["render_call"]
    model_version = argd["model_version"]
    model_type = argd["model_type"]

    render = netom.Render(model_version, model_type)
    data = munge.load_datafile(argd["data_file"])

    with io.StringIO() as fobj:
        getattr(render, render_call)(data, fobj)
        print("---")
        print(fobj.getvalue())

    return 0
Esempio n. 5
0
def read_config(conf_dir=DEFAULT_CONFIG_DIR):
    "Find and read config file for a directory, return None if not found."

    conf_path = os.path.expanduser(conf_dir)
    if not os.path.exists(conf_path):
        # only throw if not default
        if conf_dir != DEFAULT_CONFIG_DIR:
            raise OSError(f"Config directory not found at {conf_path}")

    return munge.load_datafile("config", conf_path, default=None)
Esempio n. 6
0
def get_config(conf_dir=default_conf_dir):
    if not conf_dir:
        return default_config()

    conf_path = os.path.expanduser(conf_dir)
    if not os.path.exists(conf_path):
        # only throw if not default
        if conf_dir != default_conf_dir:
            raise IOError("config dir not found at %s" % (conf_path,))

    data = default_config()
    config = munge.load_datafile('config', conf_path, default=None)
    if config:
        munge.util.recursive_update(data, config)
        data['__config_dir__'] = conf_path

    return data
Esempio n. 7
0
def get_config(conf_dir=default_conf_dir):
    if not conf_dir:
        return default_config()

    conf_path = os.path.expanduser(conf_dir)
    if not os.path.exists(conf_path):
        # only throw if not default
        if conf_dir != default_conf_dir:
            raise IOError("config dir not found at %s" % (conf_path,))

    data = default_config()
    config = munge.load_datafile('config', conf_path, default=None)
    if config:
        munge.util.recursive_update(data, config)
        data['__config_dir__'] = conf_path

    return data
Esempio n. 8
0
    def update_pyproject_version(self, repo_plugin, version):
        """
        Writes a new version to the pyproject.toml file
        if it exists
        """

        try:
            pyproject_path = os.path.join(repo_plugin.checkout_path,
                                          "pyproject.toml")
            pyproject = munge.load_datafile(
                "pyproject.toml", search_path=(repo_plugin.checkout_path))
            pyproject["tool"]["poetry"]["version"] = version

            codec = munge.get_codec("toml")

            with open(pyproject_path, "w") as fh:
                codec().dump(pyproject, fh)
            return pyproject_path

        except OSError as exc:
            if "not found" in str(exc):
                return
Esempio n. 9
0
    def read(self, config_dir=None, clear=False, config_file=None):
        """
        The munge Config's read function only allows to read from
        a config directory, but we also want to be able to read
        straight from a config file as well
        """

        if config_file:
            data_file = os.path.basename(config_file)
            data_path = os.path.dirname(config_file)

            if clear:
                self.clear()

            config = munge.load_datafile(data_file, data_path, default=None)

            if not config:
                raise IOError("Config file not found: %s" % config_file)

            munge.util.recursive_update(self.data, config)
            self._meta_config_dir = data_path
            return
        else:
            return super(Config, self).read(config_dir=config_dir, clear=clear)
Esempio n. 10
0
 def load(self, changelog_filepath):
     data = munge.load_datafile(changelog_filepath)
     return data
Esempio n. 11
0
def get_mapping_file(filename):
    status_msg("getting mapping from file %s..." % filename)
    data = munge.load_datafile(filename, '.')
    status_msg("got %d email mappings" % len(data))
    return data
Esempio n. 12
0
def get_users_file(filename):
    status_msg("getting users from file %s..." % filename)
    users = munge.load_datafile(filename, '.')
    status_msg("got %d users" % len(users))
    return users
Esempio n. 13
0
File: daemon.py Progetto: 20c/vaping
 def _extract_config_from_dir(self, config_dir):
     try:
         data = load_datafile("config", config_dir)
     except OSError:
         raise OSError("config dir not found")
     return data