Skip to content

Commit

Permalink
Rename is_hidden to should_extract_file to improve clarity (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
athackst committed Oct 9, 2022
1 parent 6fcbf36 commit 98139f3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
19 changes: 12 additions & 7 deletions mkdocs_simple_plugin/simple.py
Expand Up @@ -137,8 +137,8 @@ def match_pattern(name, pattern):

return any(match_pattern(name, pattern) for pattern in self.copy_glob)

def is_hidden(self, filepath):
"""Return true if filepath is hidden."""
def should_extract_file(self, name: str):
"""Check if file should be extracted."""
def has_hidden_attribute(filepath):
"""Returns true if hidden attribute is set."""
try:
Expand All @@ -148,6 +148,7 @@ def has_hidden_attribute(filepath):
return False

def has_hidden_prefix(filepath):
"""Returns true if the file starts with a hidden prefix."""
parts = filepath.split(os.path.sep)

def hidden_prefix(name):
Expand All @@ -157,7 +158,12 @@ def hidden_prefix(name):
for pattern in self.hidden_prefix)
return any(hidden_prefix(part) for part in parts)

return has_hidden_prefix(filepath) or has_hidden_attribute(filepath)
extract = True
if self.ignore_hidden:
is_hidden = has_hidden_prefix(name) or has_hidden_attribute(name)
extract = not is_hidden

return extract

def merge_docs(self, from_dir):
"""Merge docs directory"""
Expand Down Expand Up @@ -190,14 +196,13 @@ def try_extract(self, from_dir: str, name: str, to_dir: str) -> bool:
"""
# Check if it's hidden
path = os.path.join(from_dir, name)
if self.ignore_hidden and self.is_hidden(path):
if not self.should_extract_file(path):
return False
extracted = False
for item in self.semiliterate:
if item.try_extraction(from_dir, name, to_dir):
extracted = True
return True

return extracted
return False

def try_copy_file(self, from_dir: str, name: str, to_dir: str) -> bool:
"""Copy file with the same name to a new directory.
Expand Down
18 changes: 10 additions & 8 deletions tests/test_simple.py
Expand Up @@ -40,19 +40,21 @@ def setUp(self) -> None:
self.setUpPyfakefs()

@patch("os.stat")
def test_is_hidden(self, os_stat):
"""Test is_hidden for correctness."""
def test_should_extract_file(self, os_stat):
"""Test should_extract_file for correctness."""
simple_test = simple.Simple(**self.default_settings)
# Check ignore_hidden
simple_test.ignore_hidden = True
os_stat.return_value.st_file_attributes = 0
self.assertFalse(simple_test.is_hidden('test.md'))
self.assertFalse(simple_test.is_hidden('./folder/test.md'))
self.assertTrue(simple_test.is_hidden('__pycache__'))
self.assertTrue(simple_test.is_hidden('.mkdocsignore'))
self.assertTrue(simple_test.is_hidden(".git/objects/34/49807110bdc8"))
self.assertTrue(simple_test.should_extract_file('test.md'))
self.assertTrue(simple_test.should_extract_file('./folder/test.md'))
self.assertFalse(simple_test.should_extract_file('__pycache__'))
self.assertFalse(simple_test.should_extract_file('.mkdocsignore'))
self.assertFalse(simple_test.should_extract_file(
".git/objects/34/49807110bdc8"))
# Check hidden file attribute
os_stat.return_value.st_file_attributes = stat.FILE_ATTRIBUTE_HIDDEN
self.assertTrue(simple_test.is_hidden('/test/file'))
self.assertFalse(simple_test.should_extract_file('/test/file'))

def test_ignored_default(self):
"""Test ignored files."""
Expand Down

0 comments on commit 98139f3

Please sign in to comment.