def load_yaml(filepath, logger=None): """ Load data stored in YAML file, and return their Python representation. :param str filepath: Path to a file. ``~`` or ``~<username>`` are expanded before using. :param gluetool.log.ContextLogger logger: Logger used for logging. :rtype: object :returns: structures representing data in the file. :raises gluetool.glue.GlueError: if it was not possible to successfully load content of the file. """ if not filepath: raise GlueError('File path is not valid: {}'.format(filepath)) logger = logger or Logging.get_logger() real_filepath = normalize_path(filepath) logger.debug("attempt to load YAML from '{}' (maps to '{}')".format( filepath, real_filepath)) if not os.path.exists(real_filepath): raise GlueError("File '{}' does not exist".format(filepath)) try: with open(real_filepath, 'r') as f: data = YAML.load(f) logger.debug("loaded YAML data from '{}':\n{}".format( filepath, format_dict(data))) return data except ruamel.yaml.YAMLError as e: raise GlueError("Unable to load YAML file '{}': {}".format( filepath, str(e)))
def _assert_logging(log, record_count, cmd, stdout=None, stderr=None, stdout_index=4, stderr_index=5): # pylint: disable=too-many-arguments assert len(log.records) == record_count # assert all([r.levelno == logging.DEBUG for r in records]) assert log.records[0].message == 'command:\n{}'.format(format_dict(cmd)) if stdout is not None: assert log.records[stdout_index].message == stdout if stderr is not None: assert log.records[stderr_index].message == stderr
def test_sanity(log, tmpdir): data = { 'some-key': [1, 2, 3, 5, 7], 'some-other-key': { 'yet-another-key': [9, 11, 13] } } filepath = str(create_yaml(tmpdir, 'sanity', data)) loaded = load_yaml(filepath) assert data == loaded assert log.records[-1].message == "loaded YAML data from '{}':\n{}".format( filepath, format_dict(data))