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

Replace %-strings with f-strings #115

Merged
merged 1 commit into from
Sep 2, 2022
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
2 changes: 1 addition & 1 deletion src/behaving/fsinspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ def clear(self):
else:
os.unlink(fn)
except OSError:
logger.error("Could not delete %s" % fn)
logger.error(f"Could not delete {fn}")
exit(1)
9 changes: 2 additions & 7 deletions src/behaving/mail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ def setup(context):
try:
if not os.path.isdir(path):
os.mkdir(path)
logger.info(
"No default mail path for mailmock is specified. Using %s" % path
)
logger.info(f"No default mail path for mailmock is specified. Using {path}")
except OSError:
logger.error(
"No default mail path for mailmock is specified. Unable to create %s"
% path
)
logger.error(f"No default mail path for mailmock is specified. Unable to create {path}")
exit(1)
context.mail_path = path
context.mail = FSInspector(context.mail_path)
Expand Down
6 changes: 3 additions & 3 deletions src/behaving/mail/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

def getUniqueFilename(recipient_dir, ext="tmp"):
filename = time.strftime("%Y-%m-%d-%H%M%S", time.gmtime(time.time()))
dest = os.path.join(recipient_dir, "%s.%s" % (filename, ext))
dest = os.path.join(recipient_dir, f"{filename}.{ext}")
i = 0
while os.path.isfile(dest):
i += 1
if i > 1000:
raise IOError("Tried too many filenames like: %s" % dest)
raise IOError(f"Tried too many filenames like: {dest}")
fname = filename + "_" + str(i)
dest = os.path.join(recipient_dir, "%s.%s" % (fname, ext))
dest = os.path.join(recipient_dir, f"{fname}.{ext}")

return dest

Expand Down
5 changes: 2 additions & 3 deletions src/behaving/notifications/gcm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ def setup(context):
try:
if not os.path.isdir(path):
os.mkdir(path)
logger.info("No default gcm path for gcmmock is specified. Using %s" % path)
logger.info(f"No default gcm path for gcmmock is specified. Using {path}")
except OSError:
logger.error(
"No default gcm path for gcmmock is specified. Unable to create %s"
% path
f"No default gcm path for gcmmock is specified. Unable to create {path}"
)
exit(1)
context.gcm_path = path
Expand Down
2 changes: 1 addition & 1 deletion src/behaving/notifications/gcm/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def do_POST(self):
except OSError:
self.send_error(
400,
"Device directory [%s] could not be created" % recipient_dir,
f"Device directory [{recipient_dir}] could not be created",
)
return

Expand Down
6 changes: 3 additions & 3 deletions src/behaving/personas/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def given_a_persona(context, name):
if hasattr(context, "browser"):
if single_browser and hasattr(context, "is_connected"):
return
context.execute_steps('Given browser "%s"' % name)
context.execute_steps(f'Given browser "{name}"')
if single_browser:
context.is_connected = True

Expand Down Expand Up @@ -48,11 +48,11 @@ def key_is_val(context, key, val):
@persona_vars
def key_is_dict(context, key):
assert context.persona is not None, u"no persona is setup"
assert type(context.persona[key]) == dict, u"%s is not a dictionary" % type(key)
assert type(context.persona[key]) == dict, f"{type(key)} is not a dictionary"


@step(u'I clone persona "{source}" to "{target}"')
def clone_persona(context, source, target):
assert source in context.personas, u"Persona %s does not exist" % source
assert source in context.personas, f"Persona {source} does not exist"
if target not in context.personas:
context.personas[target] = Persona(context.personas.get(source))
5 changes: 2 additions & 3 deletions src/behaving/sms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ def setup(context):
try:
if not os.path.isdir(path):
os.mkdir(path)
logger.info("No default sms path for smsmock is specified. Using %s" % path)
logger.info(f"No default sms path for smsmock is specified. Using {path}")
except OSError:
logger.error(
"No default sms path for smsmock is specified. Unable to create %s"
% path
f"No default sms path for smsmock is specified. Unable to create {path}"
)
exit(1)
context.sms_path = path
Expand Down
2 changes: 1 addition & 1 deletion src/behaving/sms/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def do_POST(self):
code = body.split(None)[-1]
p = subprocess.Popen("pbcopy", stdin=subprocess.PIPE)
p.communicate(code.encode("utf-8"))
body = "%s and has been copied to the clipboard" % body
body = f"{body} and has been copied to the clipboard"
if notifier:
notifier.notify(body, title=to)

Expand Down
4 changes: 2 additions & 2 deletions src/behaving/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def parse_text(context, text, expression):
parser = parse.compile(expr)
res = parser.parse(text)

assert res, u"expression not found"
assert res.named, u"expression not found"
assert res, "expression not found"
assert res.named, "expression not found"
for key, val in res.named.items():
context.persona[key] = val
4 changes: 2 additions & 2 deletions src/behaving/web/steps/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def wait_for_timeout(context, timeout):
def show_element_by_id(context, id):
assert context.browser.find_by_id(id)
context.browser.execute_script(
'document.getElementById("%s").style.display="inline";' % id
f'document.getElementById("{id}").style.display="inline";'
)


Expand All @@ -45,7 +45,7 @@ def show_element_by_id(context, id):
def hide_element_by_id(context, id):
assert context.browser.find_by_id(id)
context.browser.execute_script(
'document.getElementById("%s").style.display="none";' % id
f'document.getElementById("{id}").style.display="none";'
)


Expand Down
8 changes: 2 additions & 6 deletions src/behaving/web/steps/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,14 @@ def _element_should_not_be_visible(context, css: str, timeout: int):

def _n_elements_should_be_visible(context, expected: str, css: str, timeout: int):
check = lambda: len(find_visible_by_css(context, css)) == expected
assert _retry(check, timeout), "Didn't find exactly {:d} visible elements".format(
expected
)
assert _retry(check, timeout), f"Didn't find exactly {expected:d} visible elements"


def _at_least_n_elements_should_be_visible(
context, expected: str, css: str, timeout: int
):
check = lambda: len(find_visible_by_css(context, css)) >= expected
assert _retry(check, timeout), "Didn't find at least {:d} visible elements".format(
expected
)
assert _retry(check, timeout), f"Didn't find at least {expected:d} visible elements"


@step('the element with the css selector "{css}" should be visible')
Expand Down
18 changes: 9 additions & 9 deletions src/behaving/web/steps/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def i_fill_in_field(context, name, value):
# Chrome does not clear, so we need to do manually
if context.browser.driver_name == "Chrome":
context.execute_steps('When I clear field "%s"' % name)
context.execute_steps(f'When I clear field "{name}"')
context.browser.fill(name, value)


Expand Down Expand Up @@ -86,7 +86,7 @@ def i_select(context, value, name):
context.browser.select(name, value)
except ElementDoesNotExist:
inp = context.browser.find_by_xpath(
"//input[@name='%s'][@value='%s']" % (name, value)
f"//input[@name='{name}'][@value='{value}']"
)
assert inp, u"Element not found"
inp.first.check()
Expand All @@ -107,7 +107,7 @@ def i_focus(context, name):
elem = context.browser.driver.find_element_by_name(name)
assert elem, u"Element not found"
context.browser.execute_script(
'document.getElementsByName("%s")[0].focus();' % name
f'document.getElementsByName("{name}")[0].focus();'
)


Expand Down Expand Up @@ -157,15 +157,15 @@ def i_attach(context, name, path):
@persona_vars
def set_html_content_to_element_with_id(context, id, contents):
assert context.browser.evaluate_script(
"document.getElementById('%s').innerHTML = '%s'" % (id, contents)
f"document.getElementById('{id}').innerHTML = '{contents}'"
), u"Element not found or could not set HTML content"


@step('I set the inner HTML of the element with class "{klass}" to "{contents}"')
@persona_vars
def set_html_content_to_element_with_class(context, klass, contents):
assert context.browser.evaluate_script(
"document.getElementsByClassName('%s')[0].innerHTML = '%s'" % (klass, contents)
f"document.getElementsByClassName('{klass}')[0].innerHTML = '{contents}'"
), u"Element not found or could not set HTML content"


Expand Down Expand Up @@ -218,7 +218,7 @@ def is_disabled(context, name):
def field_is_valid(context, name):
assert context.browser.find_by_name(name), u"Element not found"
assert context.browser.evaluate_script(
"document.getElementsByName('%s')[0].checkValidity()" % name
f"document.getElementsByName('{name}')[0].checkValidity()"
), "Field is invalid"


Expand All @@ -228,7 +228,7 @@ def field_is_valid(context, name):
def field_is_invalid(context, name):
assert context.browser.find_by_name(name), u"Element not found"
assert not context.browser.evaluate_script(
"document.getElementsByName('%s')[0].checkValidity()" % name
f"document.getElementsByName('{name}')[0].checkValidity()"
), "Field is valid"


Expand All @@ -237,7 +237,7 @@ def field_is_invalid(context, name):
def field_is_required(context, name):
assert context.browser.find_by_name(name), u"Element not found"
assert context.browser.evaluate_script(
"document.getElementsByName('%s')[0].getAttribute('required')" % name
f"document.getElementsByName('{name}')[0].getAttribute('required')"
), "Field is not required"


Expand All @@ -246,7 +246,7 @@ def field_is_required(context, name):
def field_is_not_required(context, name):
assert context.browser.find_by_name(name), u"Element not found"
assert not context.browser.evaluate_script(
"document.getElementsByName('%s')[0].getAttribute('required')" % name
f"document.getElementsByName('{name}')[0].getAttribute('required')"
), "Field is required"


Expand Down
4 changes: 2 additions & 2 deletions src/behaving/web/steps/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def click_link_with_text_that_contains(context, text):
text = text.replace('"', '\\"') # Escape all double quotes
text = text.replace("'", """', "'", '""") # Escape all single quotes
if "'" in text:
xpath = "//a[contains(string(), concat('%s'))]" % text
xpath = f"//a[contains(string(), concat('{text}'))]"
else:
xpath = "//a[contains(string(), '%s')]" % text
xpath = f"//a[contains(string(), '{text}')]"
anchors = context.browser.find_by_xpath(xpath)
assert anchors, "Link not found"
anchors[0].click()
2 changes: 1 addition & 1 deletion src/behaving/web/steps/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def record_browser_session(context):
@step(u"I only used one browser session")
def only_used_one_browser_session(context):
sessions = len(context.current_sessions)
assert sessions == 1, "Oops, I used %s browsers sessions!" % sessions
assert sessions == 1, f"Oops, I used {sessions} browsers sessions!"


@step(u"I pause the tests")
Expand Down
4 changes: 2 additions & 2 deletions src/behaving/web/steps/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def remember_window(context, window, name):
def lookup_window(context, name):
""" Finds a Selenium WebDriver window handle by name """
assert hasattr(context, "name_to_window_"), "No saved windows"
assert name in context.name_to_window_, "{} not found in saved windows".format(name)
assert name in context.name_to_window_, f"{name} not found in saved windows"
return context.name_to_window_[name]


Expand All @@ -25,7 +25,7 @@ def name_window(context, name):
@when(u'I open a new window named "{name}" at "{url}"')
def open_window(context, name, url):
driver = context.browser.driver
driver.execute_script("window.open('{}', '{}')".format(url, name))
driver.execute_script(f"window.open('{url}', '{name}')")
new_window = driver.window_handles[-1]
remember_window(context, new_window, name)
# this is Selenium's way of switching windows
Expand Down