Skip to content

Commit

Permalink
fix: count all files present, not only recipe files
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Nov 30, 2023
1 parent 708b391 commit 5f525a5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
7 changes: 4 additions & 3 deletions mpl_data_cast/cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import inspect
import pathlib
import time
from typing import List

import click

Expand Down Expand Up @@ -105,14 +106,14 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None:
self.print(f"Processed {self.counter} files (~{rate:.1f} MB/s).")
print("")

def __call__(self, path: pathlib.Path) -> None:
def __call__(self, path_list: List[pathlib.Path]) -> None:
self.counter += 1
name = click.format_filename(path, shorten=True)
name = click.format_filename(path_list[0], shorten=True)
message = f"Processing file {self.counter}: {name}"
rate = self.get_rate()
if rate:
message += f" ({rate:.1f}MB/s)"
self.size += path.stat().st_size
self.size += sum([pp.stat().st_size for pp in path_list])
self.print(message)

def get_rate(self) -> float:
Expand Down
6 changes: 4 additions & 2 deletions mpl_data_cast/gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def on_task_transfer(self) -> None:
for path, tb in result["errors"]:
text += f"PATH {path}:\n{tb}\n\n"
pathlib.Path("mpldc-dump.txt").write_text(text)
self.label_file.setText("")
self.pushButton_transfer.setEnabled(True)


Expand All @@ -206,7 +207,8 @@ def __enter__(self):
def __exit__(self, exc_type, exc_val, exc_tb):
pass

def __call__(self, path) -> None:
def __call__(self, path_list) -> None:
path = path_list[0]
# Let the user know where we are
self.gui.label_file.setText(f"Processing {path}...")

Expand All @@ -219,7 +221,7 @@ def __call__(self, path) -> None:
# go to undetermined state
self.gui.progressBar.setRange(0, 0)

self.counter += 1
self.counter += len(path_list)


class CastingThread(threading.Thread):
Expand Down
67 changes: 32 additions & 35 deletions mpl_data_cast/gui/widget_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from PyQt6 import QtWidgets, QtCore, QtGui, uic

from ..recipe import map_recipe_name_to_class
from ..recipe import IGNORED_FILE_NAMES, map_recipe_name_to_class
from ..util import is_dir_writable


Expand Down Expand Up @@ -61,40 +61,37 @@ def run(self):
# start crawling the directory tree
self.is_counting = True
self.has_counted = False
try:
rcp = recipe(path, path)
except BaseException:
pass
else:
tree_iterator = rcp.get_raw_data_iterator()
while True:
# check whether we have to abort
if (self.must_break
or recipe != self.recipe or path != self.path):
self.num_objects = 0
self.size_objects = 0
break
try:
item = next(tree_iterator)
except StopIteration:
self.has_counted = True
break
except BaseException:
# Windows might encounter PermissionError.
pass
else:
with self.lock:
# check before incrementing
if self.abort_current_count:
self.abort_current_count = False
break
self.num_objects += 1
try:
self.size_objects += sum(
[it.stat().st_size for it in item])
except BaseException:
pass
self.is_counting = False
ignored_files = IGNORED_FILE_NAMES + recipe.ignored_file_names
tree_iterator = path.rglob("*")
while True:
# check whether we have to abort
if (self.must_break
or recipe != self.recipe or path != self.path):
self.num_objects = 0
self.size_objects = 0
break
try:
pp = next(tree_iterator)
except StopIteration:
self.has_counted = True
break
except BaseException:
# Windows might encounter PermissionError.
pass
else:
if pp.is_dir() or pp.name in ignored_files:
continue
with self.lock:
# check before incrementing
if self.abort_current_count:
self.abort_current_count = False
break
self.num_objects += 1
try:
self.size_objects += pp.stat().st_size
except BaseException:
pass
self.is_counting = False
time.sleep(0.5)


Expand Down
6 changes: 3 additions & 3 deletions mpl_data_cast/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def cast(self, path_callback: Callable = None, **kwargs) -> dict:
Parameters
----------
path_callback: Callable
Callable function accepting a path; used for tracking
Callable function accepting a list of paths; used for tracking
the progress (e.g. via the CLI)
Returns
Expand All @@ -72,7 +72,7 @@ def cast(self, path_callback: Callable = None, **kwargs) -> dict:
for path_list in ds_iterator:
known_files += path_list
if path_callback is not None:
path_callback(path_list[0])
path_callback(path_list)
targ_path = self.get_target_path(path_list)
temp_path = self.get_temp_path(path_list)
try:
Expand All @@ -98,7 +98,7 @@ def cast(self, path_callback: Callable = None, **kwargs) -> dict:
continue
else:
if path_callback is not None:
path_callback(pp)
path_callback([pp])
prel = pp.relative_to(self.path_raw)
target_path = self.path_tar / prel
target_path.parent.mkdir(parents=True, exist_ok=True)
Expand Down

0 comments on commit 5f525a5

Please sign in to comment.