Ejemplo n.º 1
0
class Screen:
    """Allow to create screen objects For AS400 session. Screen objects are
    descriptive object of AS400 that describe how the screen looks like.
    Example would be a cursor location for a given screen.

    Various descriptive attributes:

    AddCursorPos: Sets the cursor position for the screen description to the
                  given position.

    AddString: Adds a string at the given location to the screen description.

    Usage:

    >>> cur_screen = Screen()
    >>> cur_screen.describe("cursor", x=10, y=20)
    >>> cur_screen.wait(5)
    >>> "wait up to 5 seconds for cursor to appear on (10, 20)"

    >>> text_screen = Screen()
    >>> text_screen.describe("text", string="Exit", x=10, y=10, case=True)
    >>> text_screen.wait(5)
    >>> "wait up to 5 seconds for Exit (case sensitive) on (10, 10) location"


    Below are extra descriptions method availabel at users despose.

    AddNumFields: Adds the number of fields to the screen description.

    AddNumInputFields: Adds the number of fields to the screen description.

    AddOIAInhibitStatus: Sets the type of OIA monitoring for the screen
                         description.

    AddStringInRect: Adds a string in the given rectangle to the screen
                     description.

    Clear: Removes all description elements from the screen description."""
    def __init__(self, instance):
        """Initialize screen objects, description added by `describe` method."""
        self.instance = instance
        self.screen = CreateObject("PCOMM.autECLScreenDesc")
        self.desc = {}

    def __repr__(self):
        return "<Screen: {}>".format(self.desc)

    def describe(self, dtype, x, y, **kwargs):
        """allows to describe the screen object. allowed methods are
        AddCursorPos and AddString.

        :param dtype: descriptive object type: cursor or text.
        :param x: x-axis location.
        :param y: y-axis location.
        :param kwargs: usable in case of text description. Acceptable parameter
                       are string: str and case: bool.
                       by default text matching is not case sensitive

        :return: None"""
        valid_desc = ["cursor", "text"]
        if dtype not in valid_desc:
            raise ValueError("Only cursor or text object are allowed.")

        if dtype == "cursor":
            self.desc = {"cursor": {"x": x, "y": y}}
            self.screen.AddCursorPos(x, y)
        else:
            string = kwargs.get("string")
            case = kwargs.get("bool", False)
            self.desc = {
                "text": {
                    "string": string,
                    "x": x,
                    "y": y,
                    "case": case
                }
            }
            self.screen.AddString(string, x, y, case)

    def wait(self, second):
        """For given screen object wait for specified time.

        :param instance: pass the instance object.
        :param second: wait time in second.

        :return: None"""
        return self.instance.ps.WaitForScreen(self.screen, second * 1000)