Skip to content
Snippets Groups Projects
Commit 9f344c6d authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

RF: Match objects keep a ref to the associated FileTree

parent 8cd0f064
No related branches found
No related tags found
No related merge requests found
...@@ -244,16 +244,18 @@ class Match(object): ...@@ -244,16 +244,18 @@ class Match(object):
""" """
def __init__(self, filename, short_name, variables): def __init__(self, filename, short_name, tree, variables):
"""Create a ``Match`` object. All arguments are added as attributes. """Create a ``Match`` object. All arguments are added as attributes.
:arg filename: name of existing file :arg filename: name of existing file
:arg short_name: template identifier :arg short_name: template identifier
:arg tree: :class:`.FileTree` which contains this ``Match``
:arg variables: Dictionary of ``{variable : value}`` mappings :arg variables: Dictionary of ``{variable : value}`` mappings
containing all variables present in the file name. containing all variables present in the file name.
""" """
self.__filename = filename self.__filename = filename
self.__short_name = short_name self.__short_name = short_name
self.__tree = tree
self.__variables = dict(variables) self.__variables = dict(variables)
...@@ -267,6 +269,11 @@ class Match(object): ...@@ -267,6 +269,11 @@ class Match(object):
return self.__short_name return self.__short_name
@property
def tree(self):
return self.__tree
@property @property
def variables(self): def variables(self):
return dict(self.__variables) return dict(self.__variables)
...@@ -276,6 +283,7 @@ class Match(object): ...@@ -276,6 +283,7 @@ class Match(object):
return (isinstance(other, Match) and return (isinstance(other, Match) and
self.filename == other.filename and self.filename == other.filename and
self.short_name == other.short_name and self.short_name == other.short_name and
self.tree is other.tree and
self.variables == other.variables) self.variables == other.variables)
...@@ -301,11 +309,13 @@ def scan(tree : FileTree) -> List[Match]: ...@@ -301,11 +309,13 @@ def scan(tree : FileTree) -> List[Match]:
"""Scans the directory of the given ``FileTree`` to find all files which """Scans the directory of the given ``FileTree`` to find all files which
match a tree template. match a tree template.
:return: list of :class:`Match` objects :arg tree: :class:`.FileTree` to scan
:returns: list of :class:`Match` objects
""" """
matches = [] matches = []
for template in tree.templates: for template in tree.templates:
for filename in tree.get_all(template, glob_vars='all'): for filename in tree.get_all(template, glob_vars='all'):
if not op.isfile(filename): if not op.isfile(filename):
...@@ -313,7 +323,7 @@ def scan(tree : FileTree) -> List[Match]: ...@@ -313,7 +323,7 @@ def scan(tree : FileTree) -> List[Match]:
variables = dict(tree.extract_variables(template, filename)) variables = dict(tree.extract_variables(template, filename))
matches.append(Match(filename, template, variables)) matches.append(Match(filename, template, tree, variables))
for tree_name, sub_tree in tree.sub_trees.items(): for tree_name, sub_tree in tree.sub_trees.items():
matches.extend(scan(sub_tree)) matches.extend(scan(sub_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