Skip to content

Commit

Permalink
Prompt to add actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Dec 27, 2020
1 parent b172c12 commit 0be51a7
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 22 deletions.
3 changes: 2 additions & 1 deletion notebooks/profile_default/startup/ipython_startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def _display_settings():
line += f" -- {settings.url}"

if line:
print(line, end="\n\n")
print(line)
_prompts.linebreak(force=True)


if __name__ == "__main__":
Expand Down
5 changes: 2 additions & 3 deletions pomace/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def handle(self):
self.run_loop()
finally:
utils.quit_browser()
print()
prompts.linebreak()

def update_settings(self):
if self.option("root"):
Expand Down Expand Up @@ -72,8 +72,6 @@ class RunCommand(BaseCommand):
{--root= : Directory to load models from}
"""

RELOAD_ACTIONS = "<reload actions>"

def run_loop(self):
self.clear_screen()
page = models.autopage()
Expand All @@ -97,6 +95,7 @@ def clear_screen(self):

def display_url(self, page):
self.line(f"<fg=white;options=bold>{page}</>")
prompts.linebreak(force=True)


class CleanCommand(BaseCommand):
Expand Down
2 changes: 2 additions & 0 deletions pomace/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def __call__(self, *args, **kwargs) -> "Page":
def _call_method(self, page, *args, **kwargs) -> "Page":
while self._trying_locators(*args, **kwargs):
log.error(f"No locators able to find {self.name!r}")
shared.linebreak = False
mode, value = prompts.mode_and_value()
if mode:
self.locators.append(Locator(mode, value))
Expand Down Expand Up @@ -312,6 +313,7 @@ def autopage() -> Page:
if len(matching_pages) > 1:
for page in matching_pages:
log.warn(f"Multiple pages matched: {page}")
shared.linebreak = False
return matching_pages[0]

log.info(f"Creating new page: {shared.browser.url}")
Expand Down
86 changes: 69 additions & 17 deletions pomace/prompts.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
import sys
from functools import wraps
from typing import Optional, Tuple

import log

from . import browser, enums
from . import browser, enums, shared
from .config import settings


Expand All @@ -20,9 +21,28 @@


RELOAD_ACTIONS = "<reload actions>"
ADD_ACTION = "<add action>"
CANCEL = "<cancel>"


def linebreak(*, force: bool = False):
if not shared.linebreak or force:
print()
shared.linebreak = True


def include_linebreaks(function):
@wraps(function)
def wrapper(*args, **kwargs):
linebreak()
value = function(*args, **kwargs)
linebreak()
return value

return wrapper


@include_linebreaks
def browser_if_unset():
if settings.browser.name:
return
Expand All @@ -31,16 +51,17 @@ def browser_if_unset():
settings.browser.name = os.getenv("BROWSER", "firefox")
return

shared.linebreak = False
command = bullet.Bullet(
prompt="\nSelect a browser for automation: ",
prompt="Select a browser for automation: ",
bullet=" ● ",
choices=browser.NAMES,
)
value = command.launch()
print()
settings.browser.name = value.lower()


@include_linebreaks
def url_if_unset(domains=None):
if settings.url:
return
Expand All @@ -49,17 +70,18 @@ def url_if_unset(domains=None):
settings.url = "http://example.com"
return

shared.linebreak = False
if domains:
command = bullet.Bullet(
prompt="\nStarting domain: ", bullet=" ● ", choices=domains
prompt="Starting domain: ", bullet=" ● ", choices=domains
)
else:
command = bullet.Input(prompt="\nStarting domain: ", strip=True)
command = bullet.Input(prompt="Starting domain: ", strip=True)
value = command.launch()
print()
settings.url = f"https://{value}"


@include_linebreaks
def secret_if_unset(name: str):
if settings.get_secret(name, _log=False):
return
Expand All @@ -68,47 +90,77 @@ def secret_if_unset(name: str):
settings.set_secret(name, "<unset>")
return

command = bullet.Input(prompt=f"{name}: ")
value = command.launch()
print()
value = named_value(name)
settings.set_secret(name, value)


@include_linebreaks
def action(page) -> Optional[str]:
choices = [RELOAD_ACTIONS] + dir(page)
shared.linebreak = False
choices = [RELOAD_ACTIONS] + dir(page) + [ADD_ACTION]
command = bullet.Bullet(
prompt="\nSelect an action: ", bullet=" ● ", choices=choices
prompt="Select an action: ",
bullet=" ● ",
choices=choices,
)
value = command.launch()
print()
return None if value == RELOAD_ACTIONS else value

if value == RELOAD_ACTIONS:
return None

if value == ADD_ACTION:
verb, name = verb_and_name()
return f"{verb}_{name}"

return value


@include_linebreaks
def named_value(name: str) -> Optional[str]:
if not bullet:
return None

shared.linebreak = False
command = bullet.Input(prompt="Value for " + name.replace("_", " ") + ": ")
value = command.launch()
return value


@include_linebreaks
def verb_and_name() -> Tuple[str, str]:
choices = [verb.value for verb in enums.Verb]
command = bullet.Bullet(
prompt="Select element verb: ",
bullet=" ● ",
choices=choices,
)
verb = command.launch()
linebreak(force=True)

shared.linebreak = False
command = bullet.Input("Name of element: ")
name = command.launch().lower().replace(" ", "_")
return verb, name


@include_linebreaks
def mode_and_value() -> Tuple[str, str]:
if not bullet:
return "", ""

choices = [CANCEL] + [mode.value for mode in enums.Mode]
command = bullet.Bullet(
prompt="\nSelect element locator: ",
prompt="Select element locator: ",
bullet=" ● ",
choices=choices,
)
mode = command.launch()
linebreak(force=True)

if mode == CANCEL:
print()
return "", ""

command = bullet.Input("\nValue to match: ")
shared.linebreak = False
command = bullet.Input("Value to match: ")
value = command.launch()
print()
return mode, value
1 change: 1 addition & 0 deletions pomace/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@


browser: Browser = None
linebreak: bool = False
1 change: 0 additions & 1 deletion pomace/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def fake():
return Fake()

def it_includes_zip_code(expect, fake):
print(repr(fake.zip_code))
expect(fake.zip_code).isinstance(str)

def describe_person():
Expand Down

0 comments on commit 0be51a7

Please sign in to comment.