Skip to content

Commit

Permalink
馃悰 Add time to check to Mass Production achievement
Browse files Browse the repository at this point in the history
Modifications of the file should reset the counter.

Closes #4984
  • Loading branch information
foosel committed Apr 8, 2024
1 parent 5f4d02d commit 6221dec
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/octoprint/filemanager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,12 @@ def get_size(self, location, path):
except Exception:
return -1

def get_lastmodified(self, location: str, path: str) -> int:
try:
return self._storage(location).get_lastmodified(path)
except Exception:
return -1

def has_analysis(self, location, path):
return self._storage(location).has_analysis(path)

Expand Down
43 changes: 40 additions & 3 deletions src/octoprint/filemanager/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ def get_size(self, path=None, recursive=False) -> int:
"""
raise NotImplementedError()

def get_lastmodified(self, path: str = None, recursive: bool = False) -> int:
"""
Get the modification date of the specified ``path`` or ``path``'s subtree.
Args:
path (str or None): Path for which to determine the modification date. If left our or
set to None, defaults to storage root.
recursive (bool): Whether to determine only the date of the specified ``path`` (False, default) or
the whole ``path``'s subtree (True).
"""
raise NotImplementedError()

def file_in_path(self, path, filepath):
"""
Returns whether the file indicated by ``file`` is inside ``path`` or not.
Expand Down Expand Up @@ -580,9 +592,11 @@ def _analysis_backlog_generator(self, path=None):
yield entry.name, entry.path, printer_profile_id
elif os.path.isdir(entry.path):
for sub_entry in self._analysis_backlog_generator(entry.path):
yield self.join_path(entry.name, sub_entry[0]), sub_entry[
1
], sub_entry[2]
yield (
self.join_path(entry.name, sub_entry[0]),
sub_entry[1],
sub_entry[2],
)

def last_modified(self, path=None, recursive=False):
if path is None:
Expand Down Expand Up @@ -622,6 +636,29 @@ def get_size(self, path=None, recursive=False):

return size

def get_lastmodified(self, path: str = None, recursive: bool = False) -> int:
if path is None:
path = self.basefolder

path, name = self.sanitize(path)
path = os.path.join(path, name)

# shortcut for individual files
if os.path.isfile(path):
return int(os.stat(path).st_mtime)

last_modified = 0
for entry in os.scandir(path):
if entry.is_file():
last_modified = max(last_modified, entry.stat().st_mtime)
elif recursive and entry.is_dir():
last_modified = max(
last_modified,
self.get_lastmodified(entry.path, recursive=recursive),
)

return int(last_modified)

def file_in_path(self, path, filepath):
filepath = self.sanitize_path(filepath)
path = self.sanitize_path(path)
Expand Down
5 changes: 4 additions & 1 deletion src/octoprint/plugins/achievements/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ def on_event(self, event, payload, *args, **kwargs):

## consecutive prints of same file

loc = f"{payload['origin']}:{payload['path']}"
lastmodified = self._file_manager.get_lastmodified(
payload.get("origin"), payload.get("path")
)
loc = f"{payload['origin']}:{payload['path']}:{lastmodified}"
if loc == self._data.state.file_last_print:
self._data.state.consecutive_prints_of_same_file += 1
if self._data.state.consecutive_prints_of_same_file >= 5:
Expand Down

0 comments on commit 6221dec

Please sign in to comment.