Skip to content

Commit

Permalink
construct path_patterns in build_path via domains; fixes #78
Browse files Browse the repository at this point in the history
  • Loading branch information
tyarkoni committed Jul 24, 2018
1 parent cdf5f58 commit 216efb7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
29 changes: 15 additions & 14 deletions grabbit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,8 @@ def parse_file_entities(self, filename, domains=None):
result = self._index_file(root, f, domains, update_layout=False)
return result.entities

def build_path(self, source, path_patterns=None, strict=False):
def build_path(self, source, path_patterns=None, strict=False,
domains=None):
''' Constructs a target filename for a file or dictionary of entities.
Args:
Expand All @@ -950,6 +951,11 @@ def build_path(self, source, path_patterns=None, strict=False):
strict (bool): If True, all entities must be matched inside a
pattern in order to be a valid match. If False, extra entities
will be ignored so long as all mandatory entities are found.
domains (str, list): Optional name(s) of domain(s) to scan for
path patterns. If None, all domains are scanned. If two or more
domains are provided, the order determines the precedence of
path patterns (i.e., earlier domains will have higher
precedence).
'''

if isinstance(source, six.string_types):
Expand All @@ -962,7 +968,11 @@ def build_path(self, source, path_patterns=None, strict=False):
source = source.entities

if path_patterns is None:
path_patterns = self.path_patterns
if domains is None:
domains = list(self.domains.keys())
path_patterns = []
for dom in listify(domains):
path_patterns.extend(self.domains[dom].path_patterns)

return build_path(source, path_patterns, strict)

Expand Down Expand Up @@ -1035,18 +1045,7 @@ def write_contents_to_file(self, entities, path_patterns=None,
All available domains are used.
"""
if path_patterns:
path = build_path(entities, path_patterns, strict)
else:
path_patterns = [self.path_patterns]
if domains is None:
domains = list(self.domains.keys())
for dom in domains:
path_patterns.append(self.domains[dom].path_patterns)
for pp in path_patterns:
path = build_path(entities, pp, strict)
if path is not None:
break
path = self.build_path(entities, path_patterns, strict, domains)

if path is None:
raise ValueError("Cannot construct any valid filename for "
Expand All @@ -1058,6 +1057,8 @@ def write_contents_to_file(self, entities, path_patterns=None,
root=self.root)

if index:
# TODO: Default to using only domains that have at least one
# tagged entity in the generated file.
if index_domains is None:
index_domains = list(self.domains.keys())
self._index_file(self.root, path, index_domains)
Expand Down
2 changes: 2 additions & 0 deletions grabbit/extensions/writable.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def build_path(entities, path_patterns, strict=False):
if new_path:
return new_path

return None


def write_contents_to_file(path, contents=None, link_to=None,
content_mode='text', root=None, conflicts='fail'):
Expand Down

0 comments on commit 216efb7

Please sign in to comment.