Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Expanding on @Josh_Bode's answer, here's my own PyYAML solution, which has the advantage of being a self-contained subclass of <code>yaml.Loader</code>. It doesn't depend on any module-level globals, or on modifying the global state of the <code>yaml</code> module.</p> <pre class="lang-py prettyprint-override"><code>import yaml, os class IncludeLoader(yaml.Loader): """ yaml.Loader subclass handles "!include path/to/foo.yml" directives in config files. When constructed with a file object, the root path for includes defaults to the directory containing the file, otherwise to the current working directory. In either case, the root path can be overridden by the `root` keyword argument. When an included file F contain its own !include directive, the path is relative to F's location. Example: YAML file /home/frodo/one-ring.yml: --- Name: The One Ring Specials: - resize-to-wearer Effects: - !include path/to/invisibility.yml YAML file /home/frodo/path/to/invisibility.yml: --- Name: invisibility Message: Suddenly you disappear! Loading: data = IncludeLoader(open('/home/frodo/one-ring.yml', 'r')).get_data() Result: {'Effects': [{'Message': 'Suddenly you disappear!', 'Name': 'invisibility'}], 'Name': 'The One Ring', 'Specials': ['resize-to-wearer']} """ def __init__(self, *args, **kwargs): super(IncludeLoader, self).__init__(*args, **kwargs) self.add_constructor('!include', self._include) if 'root' in kwargs: self.root = kwargs['root'] elif isinstance(self.stream, file): self.root = os.path.dirname(self.stream.name) else: self.root = os.path.curdir def _include(self, loader, node): oldRoot = self.root filename = os.path.join(self.root, loader.construct_scalar(node)) self.root = os.path.dirname(filename) data = yaml.load(open(filename, 'r')) self.root = oldRoot return data </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload