Skip to content
Snippets Groups Projects
Commit fa643260 authored by Michiel Cottaar's avatar Michiel Cottaar
Browse files

ENH: look for sub-trees in parent tree directory

parent f9bde009
Branches extra_tree_dir
No related tags found
No related merge requests found
......@@ -360,6 +360,7 @@ class FileTree(object):
filename = parse.search_tree(tree_name)
tree_name = op.splitext(op.basename(filename))[0]
filename = Path(filename)
dirname = str(filename.parent)
templates = {}
nspaces_level = []
......@@ -392,7 +393,8 @@ class FileTree(object):
else:
sub_dir = current_filename.parents[0]
_, sub_tree, short_name = parse.read_subtree_line(line, sub_dir)
with parse.extra_tree_dirs([dirname]):
_, sub_tree, short_name = parse.read_subtree_line(line, sub_dir)
if short_name in sub_trees:
raise ValueError("Name of sub_tree {short_name} used multiple times in {tree_name}.tree".format(**locals()))
......
import glob
import os.path as op
from . import filetree
from contextlib import contextmanager
from pathlib import PurePath
from typing import Tuple, List
import re
......@@ -9,6 +10,25 @@ import re
tree_directories = ['.', op.join(op.split(__file__)[0], 'trees')]
@contextmanager
def extra_tree_dirs(extra_dirs):
"""Temporarily insert ``extra_dirs`` to the beginning of :attr:`tree_directories`.
:arg extra_dirs: Sequence of additional tree file directories to search.
"""
global tree_directories
old_tree_directories = list(tree_directories)
tree_directories = list(extra_dirs) + list(tree_directories)
try:
yield
finally:
tree_directories = old_tree_directories
def search_tree(name: str) -> str:
"""
Searches for the file defining the specific tree
......
->format (format)
\ No newline at end of file
......@@ -4,6 +4,7 @@ from pathlib import PurePath
import os.path as op
import pytest
from glob import glob
import os
def same_path(p1, p2):
......@@ -161,3 +162,14 @@ def test_extract_vars_but():
assert {'p1': 'opt_file', 'p2': 'test'} == tree.update(p1='opt_file').extract_variables('fn', fn)
assert {'p1': 'opt', 'p2': 'file_test'} == tree.update(p1='opt').extract_variables('fn', fn)
assert {'p1': 'opt_{p3}', 'p2': 'test', 'p3': 'file'} == tree.update(p1='opt_{p3}').extract_variables('fn', fn)
def test_read_local_sub_children():
"""
Look for trees defined in the same folder as the test directory
"""
directory = op.split(__file__)[0]
if op.abspath(op.curdir) == op.abspath(directory):
# ensure current directory is not the test directory, which would cause the test to be too easy
os.chdir('..')
filetree.FileTree.read(op.join(directory, 'local_parent.tree'))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment