Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for applications to register their own UI function #32

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 113 additions & 51 deletions src/pysweepme/UserInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,85 +20,147 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import annotations

import time
from typing import Callable

from .FolderManager import getFoMa


def get_input(msg) -> str:
"""
This function can be used by drivers or CustomFunction scripts to ask the user for input. If used with SweepMe!,
this function will be overwritten by SweepMe! to create a graphical user interface.

Args:
msg: String of the message that is displayed to the user
class UIHandler:
"""Manages calls to user interface functions and allows applications to register alternative functions."""

_get_input: Callable[[str], str] | None = None
_message_box: Callable[[str, bool], None] | None = None
_message_info: Callable[[str], None] | None = None
_message_balloon: Callable[[str], None] | None = None

Returns:
str: response message
@classmethod
def register_get_input(cls, get_input_function: Callable[[str], str]) -> None:
"""Register a handler for the get_input() function.

"""
Args:
get_input_function: Function that shall handle calls to get_input().

"""
cls._get_input = get_input_function

@classmethod
def get_input(cls, msg: str) -> str:
"""Ask the user for input.

This function can be used by drivers or CustomFunction scripts to ask the user for input. If used with SweepMe!,
this function will be overwritten by SweepMe! to create a graphical user interface.

return input(str(msg))
Args:
msg: String of the message that is displayed to the user.

Returns:
str: response message
"""
if callable(cls._get_input):
return cls._get_input(msg)

def message_box(msg, blocking: bool = False):
"""
This function can be used by drivers or CustomFunction scripts to show a message to the user. If used with SweepMe!,
this function will be overwritten by SweepMe! to create a graphical user interface. Otherwise, it will be just
printed to the console. Any application can redefine this function to redirect the message to the user.
return input(str(msg))

Args:
msg: String of the message that is displayed to the user
@classmethod
def register_message_box(cls, message_box_function: Callable[[str, bool], None]) -> None:
"""Register a handler for the message_box() function.

Returns:
None
Args:
message_box_function: Function that shall handle calls to message_box().
"""
cls._message_box = message_box_function

"""
if blocking:
# This makes sure that the use needs to accept the dialog before the measurement continues
input(f"Message (confirm with enter): {str(msg)}")
else:
# The message is just shown with
print(f"Message: {str(msg)}")
@classmethod
def message_box(cls, msg: str, blocking: bool) -> None:
"""Show a message to the user.

This function can be used by drivers or CustomFunction scripts to show a message to the user.
If used with SweepMe!, this function will be overwritten by SweepMe! to create a graphical message box.
By default, it will be just printed to the console.

def message_info(msg):
"""
This function can be used by drivers or CustomFunction scripts to show a message to the user. If used with SweepMe!,
this function will be overwritten by SweepMe! to create a graphical user interface. Otherwise, it will be just
printed to the console. Any application can redefine this function to redirect the message to the user.
Args:
msg: String of the message that is displayed to the user.
blocking: True to require the user to acknowledge the message.
"""
if callable(cls._message_box):
cls._message_box(msg, blocking)
return

Args:
msg: String of the message that is displayed to the user
if blocking:
# This makes sure that the use needs to accept the dialog before the measurement continues
input(f"Message (confirm with enter): {msg!s}")
else:
# The message is just shown with a print which should work in every application that uses pysweepme
print(f"Message: {msg!s}") # noqa: T201

Returns:
None
@classmethod
def register_message_info(cls, message_info_function: Callable[[str], None]) -> None:
"""Register a handler for the message_info() function.

"""
Args:
message_info_function: Function that shall handle calls to message_info().
"""
cls._message_info = message_info_function

print("Info:", msg)
@classmethod
def message_info(cls, msg: str) -> None:
"""Show an info to the user.

This function can be used by drivers or CustomFunction scripts to show a message to the user.
If used with SweepMe!, this function will be overwritten by SweepMe! to add an entry in the general tab
of the main window. By default, it will be just printed to the console.

def message_balloon(msg):
"""
This function can be used by drivers or CustomFunction scripts to show a message to the user. If used with SweepMe!,
this function will be overwritten by SweepMe! to create a graphical user interface. Otherwise, it will be just
printed to the console. Any application can redefine this function to redirect the message to the user.
Args:
msg: String of the message that is displayed to the user.
"""
if callable(cls._message_info):
cls._message_info(msg)
return

Args:
msg: String of the message that is displayed to the user
# The message is just shown with a print which should work in every application that uses pysweepme
print("Info:", msg) # noqa: T201

Returns:
None
@classmethod
def register_message_balloon(cls, message_balloon_function: Callable[[str], None]) -> None:
"""Register a handler for the message_balloon() function.

"""
Args:
message_balloon_function: Function that shall handle calls to message_balloon().
"""
cls._message_balloon = message_balloon_function

@classmethod
def message_balloon(cls, msg: str) -> None:
"""Show a message balloon to the user.

print("Balloon message:", msg)
This function can be used by drivers or CustomFunction scripts to show a message to the user.
If used with SweepMe!, this function will be overwritten by SweepMe! to create a message balloon in the
system tray. By default, it will be just printed to the console.

def message_log(msg, logfilepath=None):
Args:
msg: String of the message that is displayed to the user.
"""
if callable(cls._message_balloon):
cls._message_balloon(msg)
return

# The message is just shown with a print which should work in every application that uses pysweepme
print("Balloon message:", msg) # noqa: T201


get_input = UIHandler.get_input
message_box = UIHandler.message_box
message_info = UIHandler.message_info
message_balloon = UIHandler.message_balloon


def message_log(msg, logfilepath=None):
if not logfilepath:
logfilepath = getFoMa().get_file("LOGBOOK")

with open(logfilepath, "a") as logfile:
year, month, day, hour, min, sec = time.localtime()[:6]
logfile.write(("%02d/%02d/%04d %02d:%02d:%02d" % (day, month, year, hour, min, sec) + " - " + str(msg) + "\n"))
logfile.write("%02d/%02d/%04d %02d:%02d:%02d" % (day, month, year, hour, min, sec) + " - " + str(msg) + "\n")
2 changes: 1 addition & 1 deletion src/pysweepme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# SOFTWARE.


__version__ = "1.5.6.6"
__version__ = "1.5.6.7"

import sys

Expand Down