Browse Source

added default path for DirChooser

master
Holger Frey 5 years ago
parent
commit
bc2d5ba95f
  1. 29
      sartorius_logger/__init__.py
  2. 44
      sartorius_logger/parsers.py
  3. 7
      tests/test_sartorius_logger.py

29
sartorius_logger/__init__.py

@ -19,6 +19,7 @@ from .parsers import parse_cli_arguments, parse_gui_arguments @@ -19,6 +19,7 @@ from .parsers import parse_cli_arguments, parse_gui_arguments
try:
from gooey import Gooey
except ImportError as exc:
def Gooey(*args, **kargs):
msg = "The graphilcal user interface must be installed separately"
raise NotImplementedError(msg)
@ -88,20 +89,29 @@ def _measure_and_log(nr, conn, logger): @@ -88,20 +89,29 @@ def _measure_and_log(nr, conn, logger):
def no_progress_bar(iterator):
"""" as stub function for not displaying a progress bar """
"""" a stub function for not displaying a progress bar """
return iterator
def gui_progress_factory(settings):
"""" as stub function for not displaying a progress bar """
def gooey_progress_factory(settings):
"""" progress information, tailored to Gooey """
total = settings.measurements
print(f"measuring every {settings.interval.value}{settings.interval.unit} for {settings.duration.value}{settings.duration.unit}")
print(
(
f"measuring every {settings.interval.value}{settings.interval.unit} "
f"for {settings.duration.value}{settings.duration.unit}"
)
)
def gui_progress(iterator):
for i in iterator:
print(f"measurement {i} of {total}")
yield i
print(f"measurement {total} of {total}")
return gui_progress
def _get_log_file_path(settings):
""" constructs the path to the log file """
@ -187,15 +197,16 @@ def cli(): @@ -187,15 +197,16 @@ def cli():
)
export_as_excel(result)
@Gooey(program_name="SartoriusLogger",
progress_regex="^measurement (?P<current>\d+) of (?P<total>\d+)",
progress_expr="current / total * 100"
)
@Gooey(
program_name="SartoriusLogger",
progress_regex="^measurement (?P<current>\d+) of (?P<total>\d+)",
progress_expr="current / total * 100",
)
def gui():
settings = parse_gui_arguments()
log_file_path = _get_log_file_path(settings)
gpb = gui_progress_factory(settings)
gpb = gooey_progress_factory(settings)
result = measure_series(
settings, progress_bar=gpb, data_logger=DataLogger(log_file_path)
)

44
sartorius_logger/parsers.py

@ -54,7 +54,9 @@ def parse_cli_arguments(): @@ -54,7 +54,9 @@ def parse_cli_arguments():
def parse_gui_arguments():
from gooey import GooeyParser
""" parses command line interface arguments """
desktop_path = Path.home() / "Desktop"
parser = GooeyParser(
description="Make time series measurements with a Sartorius scale.",
epilog=(
@ -63,10 +65,36 @@ def parse_gui_arguments(): @@ -63,10 +65,36 @@ def parse_gui_arguments():
"A relative directory path starts at the Desktop."
),
)
parser.add_argument("port", nargs="?", default="COM4", metavar="COMPORT", help="Serial Port that connects the Scale, defaults to 'COM4'")
parser.add_argument("-d", "--duration", nargs="?", default="30m", help="Measurement duration, e.g. 10s, 30m, 2h")
parser.add_argument("-i", "--interval", nargs="?", default="10s", help="Measurement interval, e.g. 5s, 1m, 1h")
parser.add_argument("-o", "--output", nargs="?", metavar="DIRECTORY", widget='DirChooser', help="Select Output Directory, defaults to 'Desktop'")
parser.add_argument(
"port",
nargs="?",
default="COM4",
metavar="COMPORT",
help="Serial Port that connects the Scale, defaults to 'COM4'",
)
parser.add_argument(
"-d",
"--duration",
nargs="?",
default="30m",
help="Measurement duration, e.g. 10s, 30m, 2h",
)
parser.add_argument(
"-i",
"--interval",
nargs="?",
default="10s",
help="Measurement interval, e.g. 5s, 1m, 1h",
)
parser.add_argument(
"-o",
"--output",
nargs="?",
metavar="DIRECTORY",
widget="DirChooser",
help="Select Output Directory, defaults to 'Desktop'",
gooey_options={"default_path": str(desktop_path)},
)
raw_arguments = parser.parse_args()
return _normalize_cli_arguments(raw_arguments)
@ -91,11 +119,11 @@ def _normalize_cli_arguments(raw_arguments): @@ -91,11 +119,11 @@ def _normalize_cli_arguments(raw_arguments):
# directory stuff
dir_path = _check_output_directory_path(raw_arguments.output)
measurements = 1 + (
duration.seconds // interval.seconds
)
measurements = 1 + (duration.seconds // interval.seconds)
return Settings(duration, interval, dir_path, raw_arguments.port, measurements)
return Settings(
duration, interval, dir_path, raw_arguments.port, measurements
)
def _check_output_directory_path(raw_path):

7
tests/test_sartorius_logger.py

@ -41,6 +41,7 @@ def test_get_scale_info(scale_fixture): @@ -41,6 +41,7 @@ def test_get_scale_info(scale_fixture):
"Software Version of Control Unit": "1.2.3",
}
def test_get_scale_info_with_timeout(scale_fixture):
from sartorius_logger import get_scale_info
from sartoriusb import CMD_INFO_SNR
@ -269,7 +270,9 @@ def test_export_as_excel(mocker, settings_fixture, logger_fixture): @@ -269,7 +270,9 @@ def test_export_as_excel(mocker, settings_fixture, logger_fixture):
def test_cli(mocker, settings_fixture):
mocker.patch("sartorius_logger.parse_cli_arguments", return_value=settings_fixture)
mocker.patch(
"sartorius_logger.parse_cli_arguments", return_value=settings_fixture
)
mocker.patch("sartorius_logger.measure_series", return_value="Results")
mocker.patch("sartorius_logger.export_as_excel")
@ -283,7 +286,7 @@ def test_cli(mocker, settings_fixture): @@ -283,7 +286,7 @@ def test_cli(mocker, settings_fixture):
assert measure_series.call_count == 1
print(measure_series.call_args)
call_args, call_kargs = measure_series.call_args
assert call_args == (settings_fixture, )
assert call_args == (settings_fixture,)
assert call_kargs["progress_bar"] == tqdm
assert isinstance(call_kargs["data_logger"], DataLogger)
assert export_as_excel.call_count == 1

Loading…
Cancel
Save