Browse Source

added default path for DirChooser

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

27
sartorius_logger/__init__.py

@ -19,6 +19,7 @@ from .parsers import parse_cli_arguments, parse_gui_arguments
try: try:
from gooey import Gooey from gooey import Gooey
except ImportError as exc: except ImportError as exc:
def Gooey(*args, **kargs): def Gooey(*args, **kargs):
msg = "The graphilcal user interface must be installed separately" msg = "The graphilcal user interface must be installed separately"
raise NotImplementedError(msg) raise NotImplementedError(msg)
@ -88,20 +89,29 @@ def _measure_and_log(nr, conn, logger):
def no_progress_bar(iterator): 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 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 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): def gui_progress(iterator):
for i in iterator: for i in iterator:
print(f"measurement {i} of {total}") print(f"measurement {i} of {total}")
yield i yield i
print(f"measurement {total} of {total}") print(f"measurement {total} of {total}")
return gui_progress return gui_progress
def _get_log_file_path(settings): def _get_log_file_path(settings):
""" constructs the path to the log file """ """ constructs the path to the log file """
@ -187,15 +197,16 @@ def cli():
) )
export_as_excel(result) 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(): def gui():
settings = parse_gui_arguments() settings = parse_gui_arguments()
log_file_path = _get_log_file_path(settings) log_file_path = _get_log_file_path(settings)
gpb = gui_progress_factory(settings) gpb = gooey_progress_factory(settings)
result = measure_series( result = measure_series(
settings, progress_bar=gpb, data_logger=DataLogger(log_file_path) settings, progress_bar=gpb, data_logger=DataLogger(log_file_path)
) )

44
sartorius_logger/parsers.py

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

5
tests/test_sartorius_logger.py

@ -41,6 +41,7 @@ def test_get_scale_info(scale_fixture):
"Software Version of Control Unit": "1.2.3", "Software Version of Control Unit": "1.2.3",
} }
def test_get_scale_info_with_timeout(scale_fixture): def test_get_scale_info_with_timeout(scale_fixture):
from sartorius_logger import get_scale_info from sartorius_logger import get_scale_info
from sartoriusb import CMD_INFO_SNR from sartoriusb import CMD_INFO_SNR
@ -269,7 +270,9 @@ def test_export_as_excel(mocker, settings_fixture, logger_fixture):
def test_cli(mocker, settings_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.measure_series", return_value="Results")
mocker.patch("sartorius_logger.export_as_excel") mocker.patch("sartorius_logger.export_as_excel")

Loading…
Cancel
Save