dvcignore: refactor from_file(s) (#6002)

This commit is contained in:
Ruslan Kuprieiev
2021-05-13 12:23:58 +02:00
committed by GitHub
parent 6d941e6cb5
commit a5b080645c
2 changed files with 11 additions and 16 deletions
+9 -14
View File
@@ -52,16 +52,12 @@ class DvcIgnorePatterns(DvcIgnore):
]
@classmethod
def from_files(cls, ignore_file_path, fs, root):
assert os.path.isabs(ignore_file_path)
dirname = os.path.normpath(os.path.dirname(ignore_file_path))
ignore_file_rel_path = os.path.relpath(ignore_file_path, root)
with fs.open(ignore_file_path, encoding="utf-8") as fobj:
def from_file(cls, path, fs, name):
assert os.path.isabs(path)
dirname = os.path.normpath(os.path.dirname(path))
with fs.open(path, encoding="utf-8") as fobj:
path_spec_lines = [
PatternInfo(
line,
"{}:{}:{}".format(ignore_file_rel_path, line_no + 1, line),
)
PatternInfo(line, "{}:{}:{}".format(name, line_no + 1, line),)
for line_no, line in enumerate(
map(str.strip, fobj.readlines())
)
@@ -198,11 +194,10 @@ class DvcIgnoreFilter:
old_pattern = trie.longest_prefix(dirname).value
matches = old_pattern.matches(dirname, DvcIgnore.DVCIGNORE_FILE, False)
ignore_file_path = os.path.join(dirname, DvcIgnore.DVCIGNORE_FILE)
if not matches and self.fs.exists(ignore_file_path):
new_pattern = DvcIgnorePatterns.from_files(
ignore_file_path, self.fs, self.root_dir,
)
path = os.path.join(dirname, DvcIgnore.DVCIGNORE_FILE)
if not matches and self.fs.exists(path):
name = os.path.relpath(path, self.root_dir)
new_pattern = DvcIgnorePatterns.from_file(path, self.fs, name)
if old_pattern:
trie[dirname] = DvcIgnorePatterns(
*merge_patterns(
+2 -2
View File
@@ -9,8 +9,8 @@ from dvc.ignore import DvcIgnorePatterns
def mock_dvcignore(dvcignore_path, patterns):
fs = MagicMock()
with patch.object(fs, "open", mock_open(read_data="\n".join(patterns))):
ignore_patterns = DvcIgnorePatterns.from_files(
dvcignore_path, fs, None
ignore_patterns = DvcIgnorePatterns.from_file(
dvcignore_path, fs, "mocked"
)
return ignore_patterns