Skip to content

Commit

Permalink
chore: use f-string insted of % formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed May 7, 2024
1 parent b22df8c commit aeafaa5
Show file tree
Hide file tree
Showing 113 changed files with 355 additions and 439 deletions.
4 changes: 2 additions & 2 deletions tests/odf_xliff/test_odf_xliff.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def args(src, tgt, **kwargs):
for flag, value in kwargs.items():
value = str(value)
if len(flag) == 1:
arg_list.append("-%s" % flag)
arg_list.append(f"-{flag}")
else:
arg_list.append("--%s" % flag)
arg_list.append(f"--{flag}")
if value is not None:
arg_list.append(value)
return arg_list
Expand Down
2 changes: 1 addition & 1 deletion tests/translate/convert/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def run_command(self, *argv, **kwargs):
kwoptions.update(kwargs)
for key, value in kwoptions.items():
if value is True:
argv.append("--%s" % key)
argv.append(f"--{key}")
else:
argv.append(f"--{key}={value}")
try:
Expand Down
2 changes: 1 addition & 1 deletion tests/translate/convert/test_php2po.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_convertphpempty(self):
def test_unicode(self):
"""Checks that unicode entries convert properly."""
unistring = "Norsk bokm\u00e5l"
phpsource = """$lang['nb'] = '%s';""" % unistring
phpsource = f"""$lang['nb'] = '{unistring}';"""
pofile = self.php2po(phpsource)
pounit = self.singleelement(pofile)
print(repr(pofile.units[0].target))
Expand Down
2 changes: 1 addition & 1 deletion tests/translate/convert/test_po2dtd.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_accesskey_types(self):
dtdfile.makeindex()
assert (
dtd.unquotefromdtd(
dtdfile.id_index["simple.%s" % accesskey].definition
dtdfile.id_index[f"simple.{accesskey}"].definition
)
== "a"
)
Expand Down
2 changes: 1 addition & 1 deletion tests/translate/convert/test_po2prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def test_delimiters(self):
proptemplate = """prop %s value\n"""
propexpected = """prop %s translated\n"""
for delim in ["=", ":", ""]:
print("testing '%s' as delimiter" % delim)
print(f"testing '{delim}' as delimiter")
propfile = self.merge2prop(proptemplate % delim, posource)
print(propfile)
assert propfile == propexpected % delim
Expand Down
4 changes: 2 additions & 2 deletions tests/translate/convert/test_po2tmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def test_targetlanguage(self):
tmx = self.po2tmx(minipo, targetlanguage="xh")
print("The generated xml:")
print(bytes(tmx))
tuv = tmx.document.findall(".//%s" % tmx.namespaced("tuv"))[1]
tuv = tmx.document.findall(".//{}".format(tmx.namespaced("tuv")))[1]
# tag[0] will be the source, we want the target tuv
assert tuv.get("{%s}lang" % XML_NS) == "xh"
assert tuv.get(f"{{{XML_NS}}}lang") == "xh"

def test_multiline(self):
"""Test multiline po entry."""
Expand Down
10 changes: 5 additions & 5 deletions tests/translate/convert/test_po2xliff.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def getcontexttuples(node, namespace):
Returns all the information in the context nodes as a list of tuples
of (type, text).
"""
contexts = node.findall(".//{%s}context" % namespace)
contexts = node.findall(f".//{{{namespace}}}context")
return [(context.get("context-type"), getText(context)) for context in contexts]

def test_locationcomments(self):
Expand All @@ -145,7 +145,7 @@ def test_locationcomments(self):
assert xliff.translate("one") == "kunye"
assert len(xliff.units) == 1
node = xliff.units[0].xmlelement
contextgroups = node.findall(".//%s" % xliff.namespaced("context-group"))
contextgroups = node.findall(".//{}".format(xliff.namespaced("context-group")))
assert len(contextgroups) == 2
for group in contextgroups:
assert group.get("name") == "po-reference"
Expand All @@ -170,7 +170,7 @@ def test_othercomments(self):
assert xliff.translate("one") == "kunye"
assert len(xliff.units) == 1
node = xliff.units[0].xmlelement
contextgroups = node.findall(".//%s" % xliff.namespaced("context-group"))
contextgroups = node.findall(".//{}".format(xliff.namespaced("context-group")))
assert len(contextgroups) == 1
for group in contextgroups:
assert group.get("name") == "po-entry"
Expand All @@ -193,7 +193,7 @@ def test_automaticcomments(self):
assert xliff.translate("one") == "kunye"
assert len(xliff.units) == 1
node = xliff.units[0].xmlelement
contextgroups = node.findall(".//%s" % xliff.namespaced("context-group"))
contextgroups = node.findall(".//{}".format(xliff.namespaced("context-group")))
assert len(contextgroups) == 1
for group in contextgroups:
assert group.get("name") == "po-entry"
Expand All @@ -218,7 +218,7 @@ def test_header(self):
assert unit.source == unit.target == "Content-Type: text/plain; charset=UTF-8\n"
assert unit.xmlelement.get("restype") == "x-gettext-domain-header"
assert unit.xmlelement.get("approved") != "yes"
assert unit.xmlelement.get("{%s}space" % XML_NS) == "preserve"
assert unit.xmlelement.get(f"{{{XML_NS}}}space") == "preserve"
assert (
unit.getnotes("po-translator") == "Pulana Translation for bla\nHallo Ma!"
)
Expand Down
63 changes: 15 additions & 48 deletions tests/translate/convert/test_pot2po.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ def singleunit(pofile):

def test_convertpot_blank(self):
"""Checks that the convertpot function is working for a simple file initialisation."""
potsource = (
"""#: simple.label%ssimple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr ""\n"""
% po.lsep
)
potsource = f"""#: simple.label{po.lsep}simple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr ""\n"""
newpo = self.convertpot(potsource)
assert str(self.singleunit(newpo)) == potsource

Expand Down Expand Up @@ -60,27 +57,15 @@ def test_convertpot_blank_plurals(self):

def test_merging_simple(self):
"""Checks that the convertpot function is working for a simple merge."""
potsource = (
"""#: simple.label%ssimple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr ""\n"""
% po.lsep
)
posource = (
"""#: simple.label%ssimple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr "&Hart gekoeerde nuwe lyne\\n"\n"""
% po.lsep
)
potsource = f"""#: simple.label{po.lsep}simple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr ""\n"""
posource = f"""#: simple.label{po.lsep}simple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr "&Hart gekoeerde nuwe lyne\\n"\n"""
newpo = self.convertpot(potsource, posource)
assert str(self.singleunit(newpo)) == posource

def test_merging_messages_marked_fuzzy(self):
"""Test that when we merge PO files with a fuzzy message that it remains fuzzy."""
potsource = (
"""#: simple.label%ssimple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr ""\n"""
% po.lsep
)
posource = (
"""#: simple.label%ssimple.accesskey\n#, fuzzy\nmsgid "A &hard coded newline.\\n"\nmsgstr "&Hart gekoeerde nuwe lyne\\n"\n"""
% po.lsep
)
potsource = f"""#: simple.label{po.lsep}simple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr ""\n"""
posource = f"""#: simple.label{po.lsep}simple.accesskey\n#, fuzzy\nmsgid "A &hard coded newline.\\n"\nmsgstr "&Hart gekoeerde nuwe lyne\\n"\n"""
newpo = self.convertpot(potsource, posource)
assert str(self.singleunit(newpo)) == posource

Expand Down Expand Up @@ -122,18 +107,9 @@ def test_merging_msgid_change(self):

def test_merging_location_change(self):
"""Tests that if the location changes but the msgid stays the same that we merge."""
potsource = (
"""#: new_simple.label%snew_simple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr ""\n"""
% po.lsep
)
posource = (
"""#: simple.label%ssimple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr "&Hart gekoeerde nuwe lyne\\n"\n"""
% po.lsep
)
poexpected = (
"""#: new_simple.label%snew_simple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr "&Hart gekoeerde nuwe lyne\\n"\n"""
% po.lsep
)
potsource = f"""#: new_simple.label{po.lsep}new_simple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr ""\n"""
posource = f"""#: simple.label{po.lsep}simple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr "&Hart gekoeerde nuwe lyne\\n"\n"""
poexpected = f"""#: new_simple.label{po.lsep}new_simple.accesskey\nmsgid "A &hard coded newline.\\n"\nmsgstr "&Hart gekoeerde nuwe lyne\\n"\n"""
newpo = self.convertpot(potsource, posource)
print(newpo)
assert str(self.singleunit(newpo)) == poexpected
Expand All @@ -143,18 +119,9 @@ def test_merging_location_and_whitespace_change(self):
test that even if the location changes that if the msgid
only has whitespace changes we can still merge.
"""
potsource = (
"""#: singlespace.label%ssinglespace.accesskey\nmsgid "&We have spaces"\nmsgstr ""\n"""
% po.lsep
)
posource = (
"""#: doublespace.label%sdoublespace.accesskey\nmsgid "&We have spaces"\nmsgstr "&One het spasies"\n"""
% po.lsep
)
poexpected = (
"""#: singlespace.label%ssinglespace.accesskey\n#, fuzzy\nmsgid "&We have spaces"\nmsgstr "&One het spasies"\n"""
% po.lsep
)
potsource = f"""#: singlespace.label{po.lsep}singlespace.accesskey\nmsgid "&We have spaces"\nmsgstr ""\n"""
posource = f"""#: doublespace.label{po.lsep}doublespace.accesskey\nmsgid "&We have spaces"\nmsgstr "&One het spasies"\n"""
poexpected = f"""#: singlespace.label{po.lsep}singlespace.accesskey\n#, fuzzy\nmsgid "&We have spaces"\nmsgstr "&One het spasies"\n"""
newpo = self.convertpot(potsource, posource)
print(newpo)
assert str(self.singleunit(newpo)) == poexpected
Expand Down Expand Up @@ -429,8 +396,8 @@ def test_header_initialisation(self):
"X-Generator: Translate Toolkit 0.10rc2\n"
"""
newpo = self.convertpot(potsource, posource)
print("Output Header:\n%s" % newpo)
print("Expected Header:\n%s" % expected)
print(f"Output Header:\n{newpo}")
print(f"Expected Header:\n{expected}")
assert bytes(newpo).decode("utf-8") == expected

def test_merging_comments(self):
Expand Down Expand Up @@ -816,8 +783,8 @@ def test_small_strings(self):
msgstr ""
"""
newpo = self.convertpot(potsource, posource)
print("Output:\n%s" % newpo)
print("Expected:\n%s" % expected)
print(f"Output:\n{newpo}")
print(f"Expected:\n{expected}")
assert bytes(newpo).decode("utf-8") == expected


Expand Down
6 changes: 3 additions & 3 deletions tests/translate/convert/test_prop2po.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_tab_at_start_of_value(self):
def test_unicode(self):
"""Checks that unicode entries convert properly."""
unistring = r"Norsk bokm\u00E5l"
propsource = "nb = %s\n" % unistring
propsource = f"nb = {unistring}\n"
pofile = self.prop2po(propsource)
pounit = self.singleelement(pofile)
print(repr(pofile.units[0].target))
Expand Down Expand Up @@ -182,7 +182,7 @@ def test_dont_translate(self):
def test_emptyproperty(self):
"""Checks that empty property definitions survive into po file, bug 15."""
for delimiter in ["=", ""]:
propsource = "# comment\ncredit%s" % delimiter
propsource = f"# comment\ncredit{delimiter}"
pofile = self.prop2po(propsource)
pounit = self.singleelement(pofile)
assert pounit.getlocations() == ["credit"]
Expand All @@ -194,7 +194,7 @@ def test_emptyproperty(self):
def test_emptyproperty_translated(self):
"""Checks that if we translate an empty property it makes it into the PO."""
for delimiter in ["=", ""]:
proptemplate = "credit%s" % delimiter
proptemplate = f"credit{delimiter}"
propsource = "credit=Translators Names"
pofile = self.prop2po(propsource, proptemplate)
pounit = self.singleelement(pofile)
Expand Down
4 changes: 2 additions & 2 deletions tests/translate/lang/test_af.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def test_capsstart():

def test_transliterate_cyrillic():
def trans(text):
print(("Orig: %s" % text).encode("utf-8"))
print((f"Orig: {text}").encode())
trans = af.tranliterate_cyrillic(text)
print(("Trans: %s" % trans).encode("utf-8"))
print((f"Trans: {trans}").encode())
return trans

assert trans("Борис Николаевич Ельцин") == "Boris Nikolajewitj Jeltsin"
4 changes: 2 additions & 2 deletions tests/translate/misc/test_multistring.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def test_repr(self):
t = multistring.multistring
s1 = t("test")
assert repr(s1) == "multistring(['test'])"
assert eval("multistring.%s" % repr(s1)) == s1
assert eval(f"multistring.{s1!r}") == s1

s2 = t(["test", "mé"])
assert repr(s2) == "multistring(['test', 'mé'])"
assert eval("multistring.%s" % repr(s2)) == s2
assert eval(f"multistring.{s2!r}") == s2

def test_replace(self):
t = multistring.multistring
Expand Down
4 changes: 2 additions & 2 deletions tests/translate/storage/test_cpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ def test_multiline_obsolete(self):
"""Tests for correct output of mulitline obsolete messages."""
posource = '#~ msgid ""\n#~ "Old thing\\n"\n#~ "Second old thing"\n#~ msgstr ""\n#~ "Ou ding\\n"\n#~ "Tweede ou ding"\n'
pofile = self.poparse(posource)
print("Source:\n%s" % posource)
print("Output:\n%s" % bytes(pofile))
print(f"Source:\n{posource}")
print(f"Output:\n{bytes(pofile)}")
assert len(pofile.units) == 1
assert pofile.units[0].isobsolete()
assert not pofile.units[0].istranslatable()
Expand Down
2 changes: 1 addition & 1 deletion tests/translate/storage/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TestDirectory:
def setup_method(self, method):
"""Sets up a test directory."""
print("setup_method called on", self.__class__.__name__)
self.testdir = "%s_testdir" % (self.__class__.__name__)
self.testdir = f"{self.__class__.__name__}_testdir"
self.cleardir(self.testdir)
os.mkdir(self.testdir)

Expand Down
2 changes: 1 addition & 1 deletion tests/translate/storage/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def givefile(filename, content):
class BaseTestFactory:
def setup_method(self, method):
"""Sets up a test directory."""
self.testdir = "%s_testdir" % (self.__class__.__name__)
self.testdir = f"{self.__class__.__name__}_testdir"
self.cleardir(self.testdir)
os.mkdir(self.testdir)

Expand Down
2 changes: 1 addition & 1 deletion tests/translate/storage/test_flatxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _store_to_string(store):

@staticmethod
def _encoded_file(string, encoding="utf-8"):
xmldecl = '<?xml version="1.0" encoding="%s"?>' % encoding
xmldecl = f'<?xml version="1.0" encoding="{encoding}"?>'
return BytesIO((xmldecl + string).encode())

def test_root_config_detect(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/translate/storage/test_php.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def test_multiline(self):
assert len(phpfile.units) == 1
phpunit = phpfile.units[0]
assert phpunit.name == "$lang['multiline']"
assert phpunit.source == "Line1%sLine2" % lineending
assert phpunit.source == f"Line1{lineending}Line2"

def test_parsing_arrays(self):
"""Parse the array syntax."""
Expand Down
6 changes: 3 additions & 3 deletions tests/translate/storage/test_po.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,14 +624,14 @@ def test_makeobsolete_msgidcomments(self):
"""Tests making a unit with msgidcomments obsolete."""
posource = '#: first.c\nmsgid ""\n"_: first.c\\n"\n"test"\nmsgstr "rest"\n\n#: second.c\nmsgid ""\n"_: second.c\\n"\n"test"\nmsgstr "rest"'
poexpected = '#~ msgid ""\n#~ "_: first.c\\n"\n#~ "test"\n#~ msgstr "rest"\n'
print("Source:\n%s" % posource)
print("Expected:\n%s" % poexpected)
print(f"Source:\n{posource}")
print(f"Expected:\n{poexpected}")
pofile = self.poparse(posource)
unit = pofile.units[0]
assert not unit.isobsolete()
unit.makeobsolete()
assert unit.isobsolete()
print("Result:\n%s" % pofile)
print(f"Result:\n{pofile}")
assert str(unit) == poexpected

def test_multiline_obsolete(self):
Expand Down
10 changes: 4 additions & 6 deletions tests/translate/storage/test_poheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,9 @@ def test_updatecontributor():
pofile.header().addnote("Khaled Hosny <khaledhosny@domain.org>, 2006, 2007, 2008.")
pofile.updatecontributor("Khaled Hosny", "khaledhosny@domain.org")
print(bytes(pofile))
assert (
"# Khaled Hosny <khaledhosny@domain.org>, 2006, 2007, 2008, %s."
% time.strftime("%Y")
in bytes(pofile).decode("utf-8")
)
assert "# Khaled Hosny <khaledhosny@domain.org>, 2006, 2007, 2008, {}.".format(
time.strftime("%Y")
) in bytes(pofile).decode("utf-8")


def test_updatecontributor_header():
Expand All @@ -292,7 +290,7 @@ def test_updatecontributor_header():

# Manually build expected output
expected = posource.replace(
"msgid", "# Grasvreter, %s.\nmsgid" % time.strftime("%Y")
"msgid", "# Grasvreter, {}.\nmsgid".format(time.strftime("%Y"))
)
assert bytes(pofile).decode("utf-8") == expected

Expand Down
11 changes: 4 additions & 7 deletions tests/translate/storage/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def test_key_value_delimiters_simple(self):
"""
delimiters = [":", "=", " "]
for delimiter in delimiters:
propsource = "key%svalue" % delimiter
propsource = f"key{delimiter}value"
print(f"source: '{propsource}'\ndelimiter: '{delimiter}'")
propfile = self.propparse(propsource)
assert len(propfile.units) == 1
Expand All @@ -434,18 +434,15 @@ def test_comments(self):
"""Checks that we handle # and ! comments."""
markers = ["#", "!"]
for comment_marker in markers:
propsource = (
"""%s A comment
propsource = f"""{comment_marker} A comment
key=value
"""
% comment_marker
)
propfile = self.propparse(propsource)
print(repr(propsource))
print("Comment marker: '%s'" % comment_marker)
print(f"Comment marker: '{comment_marker}'")
assert len(propfile.units) == 1
propunit = propfile.units[0]
assert propunit.comments == ["%s A comment" % comment_marker]
assert propunit.comments == [f"{comment_marker} A comment"]

def test_latin1(self):
"""Checks that we handle non-escaped latin1 text."""
Expand Down
Loading

0 comments on commit aeafaa5

Please sign in to comment.