def spec_from_yaml(cls, text=None, path=None): if None not in (text, path): raise ValueError( "Tool specification may be text or path, not both") data = None if path: data = yaml_load_path(path) elif text: data = yaml_load(text) if not data: raise ValueError("Tool specification is missing") # Reduce the spec to something usable by the constructor """ Example: _package: path.to.package: _title: Name of this group _description: | Longer description of this command group # Other keys define commands by naming the class that implements each word-or-phrase: name-of-class # Or by defining subcommands, using a nested structure: word-or-phrase: _package: optional _title: optional _description: optional word-or-phrase: module.suffix """ return cls.spec_from_dict(data)
def test_load_path_file(): d = os.path.dirname(__file__) srcpath = os.path.join(d, 'data3.yaml') data = yaml_load_path(srcpath) assert data.data3 == 333
def test_load_path_dir(): d = os.path.dirname(__file__) srcpath = os.path.join(d, 'data.d') data = yaml_load_path(srcpath) assert len(data) == 2 s = {x for d in data for x in d.items()} assert s == {('file1', 'content1'), ('file2', 'content2')}
import sys from rjgtoys.yaml import yaml_load_path, yaml_dump for path in sys.argv[1:]: print(f"Loading {path}") data = yaml_load_path(path) # Note attribute-style access print(f" Loaded {data.name}: {data.title}") yaml_dump(data, sys.stdout)
def test_load_path_fails(): with pytest.raises(YamlCantLoad): yaml_load_path('/dev/null')