Ejemplo n.º 1
0
    def test_safe_load(self):
        d = yaml.safe_load('foo: bar\nbaz: 123\n')
        self.assertEqual(len(d), 2)
        self.assertEqual(d['foo'], 'bar')
        self.assertEqual(d['baz'], 123)

        # Should error -- it's not safe to load an instance of a user-defined class
        with self.assertRaises(base_yaml.YAMLError):
            yaml.safe_load('!!python/object:test.test_helpers.YAMLTest {}')
Ejemplo n.º 2
0
 def __init__(self, raw: str):
     d = yaml.safe_load(raw) or {}
     self._raw = raw
     self._services = {
         name: Service(name, service)
         for name, service in d.get('services', {}).items()
     }
Ejemplo n.º 3
0
    def from_yaml(
            cls, metadata: typing.Union[str, typing.TextIO],
            actions: typing.Optional[typing.Union[str, typing.TextIO]] = None):
        """Instantiate a CharmMeta from a YAML description of metadata.yaml.

        Args:
            metadata: A YAML description of charm metadata (name, relations, etc.)
                This can be a simple string, or a file-like object. (passed to `yaml.safe_load`).
            actions: YAML description of Actions for this charm (eg actions.yaml)
        """
        meta = yaml.safe_load(metadata)
        raw_actions = {}
        if actions is not None:
            raw_actions = yaml.safe_load(actions)
            if raw_actions is None:
                raw_actions = {}
        return cls(meta, raw_actions)
Ejemplo n.º 4
0
 def __init__(self, raw: typing.Union[str, typing.Dict] = None):
     if isinstance(raw, str):
         d = yaml.safe_load(raw) or {}
     else:
         d = raw or {}
     self.summary = d.get('summary', '')
     self.description = d.get('description', '')
     self.services = {name: Service(name, service)
                      for name, service in d.get('services', {}).items()}
Ejemplo n.º 5
0
    def _load_config_defaults(self, charm_config):
        """Load default values from config.yaml.

        Handle the case where a user doesn't supply explicit config snippets.
        """
        filename = inspect.getfile(self._charm_cls)
        charm_dir = pathlib.Path(filename).parents[1]

        if charm_config is None:
            config_path = charm_dir / 'config.yaml'
            if config_path.is_file():
                charm_config = config_path.read_text()
                self._charm_dir = charm_dir
            else:
                # The simplest of config that the framework can support
                charm_config = '{}'
        elif isinstance(charm_config, str):
            charm_config = dedent(charm_config)
        charm_config = yaml.safe_load(charm_config)
        charm_config = charm_config.get('options', {})
        return {key: value['default'] for key, value in charm_config.items()
                if 'default' in value}