def test_parse_url(): django = munge.get_codec('django') mysql = munge.get_codec('mysql') json = munge.get_codec('json') yaml = munge.get_codec('yaml') conf = config.parse_url('yaml:test') assert yaml == conf.cls assert 'test' == conf.url.path conf = config.parse_url('test.yaml') assert yaml == conf.cls assert 'test.yaml' == conf.url.path conf = config.parse_url('tyam:test', extra_schemes) assert yaml == conf.cls assert 'test' == conf.url.path conf = config.parse_url('django:///home/user/project/settings_dir.settings?app_name/model') assert django == conf.cls assert '/home/user/project/settings_dir.settings' == conf.url.path assert 'app_name/model' == conf.url.query conf = config.parse_url('json:http://example.com/test.txt') assert json == conf.cls assert 'http://example.com/test.txt' == conf.url.path # assert 'app_name/model' == conf.url.query with pytest.raises(ValueError): config.parse_url('nonexistant:test', extra_schemes)
def write(self, config_dir=None, config_name=None, codec=None): """ writes config to config_dir using config_name """ # get name of config directory if not config_dir: config_dir = self._meta_config_dir if not config_dir: raise IOError("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") if codec: codec = munge.get_codec(codec)() else: codec = munge.get_codec(self.defaults["codec"])() config_dir = os.path.expanduser(config_dir) if not os.path.exists(config_dir): os.mkdir(config_dir) codec.dumpu(self.data, os.path.join(config_dir, "config." + codec.extension))
def write(self, config_dir=None, codec=None): if not config_dir: config_dir = self._meta_config_dir if not config_dir: raise IOError("config_dir not set") if codec: codec = munge.get_codec(codec)() else: codec = munge.get_codec(self.defaults['codec'])() config_dir = os.path.expanduser(config_dir) if not os.path.exists(config_dir): os.mkdir(config_dir) codec.dumpu(self.data, os.path.join(config_dir, 'config.' + codec.extension))
def generate_clean(self, data_file, **kwargs): """ Will generate a clean CHANGELOG.(yaml|json) file with just an `unreleased` section in it. Will fail if a file already exists **Arguments** - data_file (`str`): file path to a CHANGELOG.(yaml|json) file this is where the output will be written to """ if os.path.exists(data_file): raise ValueError(f"File already exists: {data_file}") changelog = { "Unreleased": {section: [] for section in CHANGELOG_SECTIONS} } codec = os.path.splitext(data_file)[1][1:] codec = munge.get_codec(codec) self.log.info(f"Generating {data_file}") with open(data_file, "w+") as fh: codec().dump(changelog, fh)
def generate_datafile(self, md_file, data_file, **kwargs): """ Generates a changelog data file (yaml, json etc.) from a CHANGELOG.md file that follows the 20c changelog format. **Arguments** - md_file (`str`): file path to a CHANGELOG.md file - data_file (`str`): file path to a CHANGELOG.(yaml|json) file this is where the output will be written to **Keyword Arguments** - print (`bool=False`): if True print the generated changelog to stdout instead of writing to a file """ changelog = self.md_to_dict(md_file) codec = os.path.splitext(data_file)[1][1:] codec = munge.get_codec(codec) self.log.info(f"Generating {data_file}") if kwargs.get("print"): print(codec().dumps(changelog)) return with open(data_file, "w+") as fh: codec().dump(changelog, fh)
def test_load_config_files(data_config_daemon): codec = munge.get_codec('yaml')() data = codec.loads(data_config_daemon.yml) data['vaping'] = dict(home_dir=os.path.relpath(data_config_daemon.path)) daemon = vaping.daemon.Vaping(config=data) # print(data_config_daemon.dumps(daemon.config.data)) assert data_config_daemon.expected == daemon.config.data
def write_config(data, conf_dir=DEFAULT_CONFIG_DIR, codec="yaml", backup_existing=False): """ Write config values to a file. Arguments: - conf_dir<str>: path to output directory - codec<str>: output field format - backup_existing<bool>: if a config file exists, make a copy before overwriting """ if not codec: codec = "yaml" codec = munge.get_codec(codec)() conf_dir = os.path.expanduser(conf_dir) if not os.path.exists(conf_dir): os.mkdir(conf_dir) # Check for existing file, back up if necessary outpath = os.path.join(conf_dir, "config." + codec.extensions[0]) if backup_existing and os.path.exists(outpath): os.rename(outpath, outpath + ".bak") codec.dump(data, open(outpath, "w"))
def expose_vars(cls, env, plugin_config): """ Loop through the files specified in the `vars` plugin config property and expose them to the config template environment Note that this is different than the `load_vars` method which will expose the same vars to the template environment for rendering the templates **Arguments** - env (`dict`): template environment - plugin_config (`dict`) **Returns** `dict<filepath, error>`: dict of ioerrors mapped to filepath """ errors = {} for filepath in plugin_config.get("vars", []): ext = os.path.splitext(filepath)[1][1:] codec = munge.get_codec(ext) try: with open(filepath) as fh: data = codec().load(fh) update(env, data) except OSError as exc: errors[filepath] = exc return errors
def execute(self, command=None, **kwargs): fmt = kwargs.get("format") ctx = self.ctl.ctx print(f"current config from {ctx.home}") fmt = "yml" codec = munge.get_codec(fmt)() print(codec.dumps(ctx.config.data))
def write_config(data, conf_dir=default_conf_dir, codec=None): if not codec: codec = munge.get_codec('yaml')() conf_dir = os.path.expanduser(conf_dir) if not os.path.exists(conf_dir): os.mkdir(conf_dir) codec.dump(data, open(os.path.join(conf_dir, 'config.' + codec.extensions[0]), 'w'))
def test_load_config_files(data_config_daemon): codec = munge.get_codec('yaml')() data = codec.loads(data_config_daemon.yml) data['vaping'] = dict(home_dir=os.path.realpath(data_config_daemon.path)) daemon = vaping.daemon.Vaping(config=data) # print(data_config_daemon.dumps(daemon.config.data)) data_config_daemon.expected["vaping"]["home_dir"] = os.path.realpath(data_config_daemon.expected["vaping"]["home_dir"]) assert data_config_daemon.expected == daemon.config.data
def write_config(data, conf_dir=default_conf_dir, codec=None): if not codec: codec=munge.get_codec('yaml')() conf_dir = os.path.expanduser(conf_dir) if not os.path.exists(conf_dir): os.mkdir(conf_dir) codec.dump(data, open(os.path.join(conf_dir, 'config.' + codec.extensions[0]), 'w'))
def get(config, output_format, poids): """ get an object from peeringdb """ pdb = client.PeeringDB() codec = munge.get_codec(output_format)() for poid in poids: res = parse_objid(poid) data = pdb.get(res[0], res[1]) codec.dump(data, sys.stdout)
def get(config, depth, output_format, poids): """ get an object from peeringdb """ pdb = client.PeeringDB(conf_dir=config) codec = munge.get_codec(output_format)() for poid in poids: (typ, pk) = util.split_ref(poid) data = pdb.get(typ, pk, depth=depth) codec.dump(data, sys.stdout)
def test_load_config_files(data_config_daemon): codec = munge.get_codec("yaml")() data = codec.loads(data_config_daemon.yml) data["vaping"] = dict(home_dir=os.path.realpath(data_config_daemon.path)) daemon = vaping.daemon.Vaping(config=data) # print(data_config_daemon.dumps(daemon.config.data)) data_config_daemon.expected["vaping"]["home_dir"] = os.path.realpath( data_config_daemon.expected["vaping"]["home_dir"]) assert data_config_daemon.expected == daemon.config.data
def export(self, contentType): """ Export message to specified contentType via munge contentType <str> - eg. "json", "yaml" """ cls = munge.get_codec(contentType) codec = cls() return codec.dumps(self.__dict__())
def __init__(self, transport_content_type="json"): threading.Thread.__init__(self) EventMixin.__init__(self) self.close_when_ready = False self.transport_content_type = transport_content_type # munge codec instance from transport_content_type self.codecClass = munge.get_codec(transport_content_type) self.codec = self.codecClass()
def try_dump_obj(args): res = parse_objid(args.cmd) if not res: return False pdb = client.PeeringDB() data = pdb.get(res[0], res[1]) codec = munge.get_codec(args.output_format)() codec.dump(data, sys.stdout) return True
def load_vars(self): """ Loop through the files specified in the `vars` plugin config property and expose them to the template environment """ update(self._tmpl_env, self.ctl.ctx.tmpl["env"]) for filepath in self.get_config("vars"): ext = os.path.splitext(filepath)[1][1:] codec = munge.get_codec(ext) with open(self.render_tmpl(filepath)) as fh: data = codec().load(fh) update(self._tmpl_env, data)
def get_type(name, extra_schemes={}): """ cls: class ctor open: open func open_args: what to pass to open """ if name in extra_schemes: return extra_schemes[name] codec = munge.get_codec(name) if codec: return {'cls': codec} return {}
def probe(self): codec = munge.get_codec('yaml')() msg = {} msg['data'] = [] msg['ts'] = (datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds() # FIXME use poll for host in self.hosts: args = shlex.split(self.command.format(host=host)) proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # TODO poll, timeout, maybe from parent process for better control? with proc.stdout: #msg['data'].append(proc.stdout.read()) msg['data'].append(codec.load(proc.stdout)) return msg
def main(argv=None): if argv is None: argv = sys.argv[1:] ctx = Context() parser = argparse.ArgumentParser(description="rdap") add_options(parser, Context.option_list()) parser.add_argument( "--version", action="version", version="{}s version {}".format("%(prog)", rdap.__version__), ) parser.add_argument("--output-format", help="output format (yaml, json, text)") parser.add_argument("--show-requests", action="store_true", help="show all requests") parser.add_argument("--parse", action="store_true", help="parse data into object before display") parser.add_argument("query", nargs="+") args = parser.parse_args(argv) # get dict of options and update config argd = vars(args) ctx.update_options(argd) client = rdap.RdapClient(ctx.config) output_format = argd.get("output_format") if not output_format: output_format = ctx.config.get_nested("rdap", "output_format") codec = munge.get_codec(output_format)() for each in argd["query"]: obj = client.get(each) if argd.get("parse", False): print(codec.dumps(obj.parsed())) else: print(codec.dumps(obj.data)) if argd.get("show_requests", False): print("# Requests") for each in client.history: print("{} {}".format(*each))
def release(self, version, data_file, **kwargs): """ Adds the specified release to the changelog. This will validate and move all items under "unreleased" to a new section for the specified release version **Arguments** - version (`str`): version mame (eg. tag name) - data_file (`str`): file path to a CHANGELOG.(yaml|json) file """ changelog = self.load(data_file) if version in changelog: raise ValueError( f"Release {version} already exists in {data_file}") release_section = {} for change_type, changes in list( changelog.get("Unreleased", {}).items()): if changes: release_section[change_type] = [change for change in changes] if not release_section: raise ValueError("No items exist in unreleased to be moved") changelog[version] = release_section changelog["Unreleased"] = { section: [] for section in CHANGELOG_SECTIONS } changelog = self.sort_changelog(changelog) ext = os.path.splitext(data_file)[1][1:] codec = munge.get_codec(ext) with open(data_file, "w+") as fh: codec().dump(changelog, fh) self.log.info(f"Updated {data_file}") self.generate(self.get_config("md_file"), data_file)
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
def render(self, data): cls = munge.get_codec(self.type) codec = cls() return codec.dumps(data)
def find_cls(name, extra_schemes={}): if name in extra_schemes: return munge.get_codec(extra_schemes[name]['type']) return munge.get_codec(name)
def conf_dump(config, output_format): """ output current config """ cfg = peeringdb.config.get_config(config) codec = munge.get_codec(output_format)() codec.dump(cfg, sys.stdout) return 0
import os import pytest import sys import shutil import munge from munge import config test_dir = os.path.relpath(os.path.dirname(__file__)) data_dir = os.path.join(test_dir, 'data') conf0_dir = os.path.join(data_dir, 'conf0') extra_schemes = { 'tyam': { 'type': 'yaml', 'cls': munge.get_codec('yaml') } } def test_parse_url(): django = munge.get_codec('django') mysql = munge.get_codec('mysql') json = munge.get_codec('json') yaml = munge.get_codec('yaml') conf = config.parse_url('yaml:test') assert yaml == conf.cls assert 'test' == conf.url.path conf = config.parse_url('test.yaml') assert yaml == conf.cls
def print_dict(self, data): codec = munge.get_codec('yaml')() click.echo(codec.dumps(data))
def handle(config, output_format, **_): codec = munge.get_codec(output_format)() codec.dump(config, sys.stdout)