Skip to content

Commit

Permalink
Skip hidden sub-directories
Browse files Browse the repository at this point in the history
  • Loading branch information
ondratu committed May 22, 2024
1 parent b9e8535 commit 8206811
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
ChangeLog
=========
0.8.1dev0
* Skip hidden sub-directories

0.8.0 (2024-05-22)
* Send additional version headers
* Add printer dialog handling
Expand Down
2 changes: 1 addition & 1 deletion prusa/connect/printer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
)
from .util import RetryingSession, get_timestamp

__version__ = "0.8.0"
__version__ = "0.8.1dev0"
__date__ = "22 May 2024" # version date
__copyright__ = "(c) 2024 Prusa 3D"
__author_name__ = "Prusa Link Developers"
Expand Down
15 changes: 9 additions & 6 deletions prusa/connect/printer/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def get_file_type(file):

class File:
"""A node of a Filesystem representing either a file or a folder"""

def __init__(self,
name: str,
is_dir: bool = False,
Expand Down Expand Up @@ -156,7 +157,7 @@ def get(self, parts: typing.Iterable[str]):
Return the node identified by `parts`, which is a collection of
names that will be matched on the path to it.
:param parts: Path identifying a node you are looking fo
:param parts: Path identifying a node you are looking for
:return: the found node
:raise TypeError if `parts` is string and not a collection of strings
"""
Expand Down Expand Up @@ -279,6 +280,7 @@ def set_attrs(self, abs_path):

class Storage:
"""Represent a storage"""

def __init__(self,
tree: File,
storage: str,
Expand Down Expand Up @@ -365,6 +367,7 @@ class Filesystem:
A filesystem translates from physical representation on the storage to
virtual. This virtual one is then sent to Connect.
"""

def __init__(self,
sep: str = "/",
event_cb: Optional[EventCallback] = None):
Expand Down Expand Up @@ -544,16 +547,16 @@ def from_dir(self, dirpath: str, storage: str):
root.set_attrs(dirpath)

for abs_dir, dirs, files in walk(dirpath):
dirname = abs_dir[len(dirpath):]
dirnames = abs_dir[len(dirpath):].split(path.sep)

# skip hidden folders
if dirname.startswith("."):
if any(map(lambda e: e.startswith("."), dirnames)):
continue

if not dirname:
if not dirnames:
parent = root
else:
parent = root.get(dirname.split(path.sep))
parent = root.get(dirnames)

for name in dirs:
node = parent.add(name, is_dir=True)
Expand Down Expand Up @@ -928,4 +931,4 @@ def send_file_changed(self,
flags.MOVE_SELF: process_delete,
})

WATCH_FLAGS = reduce(lambda x, y: x|y, HANDLERS.keys())
WATCH_FLAGS = reduce(lambda x, y: x | y, HANDLERS.keys())
31 changes: 31 additions & 0 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def inotify(queue, nodes):
directory. This returns the path to the dir on storage, the Inotify
handler and filesystem as a tuple: (path, handler, filesystem).
"""

def create_on_storage(root_dir, node):
parts = node.abs_parts()
parts.insert(0, root_dir)
Expand Down Expand Up @@ -138,6 +139,7 @@ def fs(nodes):

class TestFile:
"""Test the methods of the File class"""

def test_add(self):
root = File("root", is_dir=True)
assert not root.children
Expand Down Expand Up @@ -329,6 +331,7 @@ def test_abs_path(self, nodes):

class TestFilesystem:
"""Test Filesystem class interface."""

def test_storage(self, fs):
assert len(fs.storage_dict) == 1
assert "storage" in fs.storage_dict
Expand Down Expand Up @@ -385,6 +388,33 @@ def test_from_hidden_dir(self, fs_from_dir, fs):
with pytest.raises(AttributeError):
assert not h.is_dir

@patch("prusa.connect.printer.files.stat",
return_value=os.stat_result((33188, 267912, 64768, 1, 0, 0, 3044,
1599740701, 1596120005, 1596120005)))
@patch("prusa.connect.printer.files.path.abspath", return_value='/a')
@patch("prusa.connect.printer.files.walk",
return_value=[('/a', ['b'], ['1.gcode']), ('/a/b', ['.c'], []),
('/a/b/.c', [], ['2.sl1', '3.txt'])])
def test_from_hidden_subdir(self, *mocks):
fs = Filesystem()
fs.from_dir('/somewhere/on/the/disk/a', 'a')
assert not fs.get("/a/b/.c/2.sl1")
assert not fs.get("/a/b/.c")

@patch("prusa.connect.printer.files.stat",
return_value=os.stat_result((33188, 267912, 64768, 1, 0, 0, 3044,
1599740701, 1596120005, 1596120005)))
@patch("prusa.connect.printer.files.path.abspath", return_value='/a')
@patch("prusa.connect.printer.files.walk",
return_value=[('/a', ['b'], []), ('/a/b', ['.c'], []),
('/a/b/.c', ['d'], []), ('/a/b/.c/d', [], ['1.sl1'])])
def test_from_hidden_subsubdir(self, *mocks):
fs = Filesystem()
fs.from_dir('/somewhere/on/the/disk/a', 'a')
assert not fs.get("/a/b/.c")
assert not fs.get("/a/b/.c/d")
assert not fs.get("/a/b/.c/d/1.sl1")

def test_get_root(self, fs):
a = fs.get("storage")
assert a.name == "storage"
Expand Down Expand Up @@ -472,6 +502,7 @@ def test_to_dict_legacy(self, fs):

class TestINotify:
"""Test events from Inotify class."""

def test_CREATE_file(self, inotify):
"""Test that creating a file is reflected in the Filesystem
and that also Connect is notified by the means of an Event
Expand Down

0 comments on commit 8206811

Please sign in to comment.