Skip to content

Commit

Permalink
Change: format-string to f-string
Browse files Browse the repository at this point in the history
  • Loading branch information
y0urself committed Sep 20, 2021
1 parent 707c895 commit 13967b0
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 163 deletions.
10 changes: 4 additions & 6 deletions pontos/changelog/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def add_skeleton(
git_tag_prefix: str = 'v',
git_space: str = 'greenbone',
) -> str:
git_tag = '{}{}'.format(git_tag_prefix, new_version)
git_tag = f'{git_tag_prefix}{new_version}'
tokens = _tokenize(markdown)
updated_markdown = ''

Expand Down Expand Up @@ -82,7 +82,7 @@ def update(
returns updated markdown and change log for further processing.
"""

git_tag = '{}{}'.format(git_tag_prefix, new_version)
git_tag = f'{git_tag_prefix}{new_version}'
tokens = _tokenize(markdown)
unreleased_heading_count = 0
changelog = ''
Expand Down Expand Up @@ -142,7 +142,7 @@ def _prepare_changelog(
if tt == 'unreleased':
if new_version:
tc = __UNRELEASED_HEADER_MATCHER.sub(f'[{new_version}]', tc)
tc += ' - {}'.format(date.today().isoformat())
tc += f' - {date.today().isoformat()}'
output += tc
elif tt == 'unreleased_link':
if new_version:
Expand Down Expand Up @@ -207,8 +207,6 @@ def _tokenize(
) -> List[Tuple[int, str, int, str],]:
toks, remainder = __CHANGELOG_SCANNER.scan(markdown)
if remainder != '':
raise ChangelogError(
"unrecognized tokens in markdown: {}".format(remainder)
)
raise ChangelogError(f"unrecognized tokens in markdown: {remainder}")

return toks
8 changes: 4 additions & 4 deletions pontos/terminal/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Signs(Enum):
NONE = ' '

def __str__(self):
return '{}'.format(self.value)
return f'{self.value}'


STATUS_LEN = 2
Expand Down Expand Up @@ -71,16 +71,16 @@ def _print_status(
if status == Signs.NONE:
first_line += ' '
else:
first_line += '{} '.format(color(status))
first_line += f'{color(status)} '
if self._indent > 0:
first_line += ' ' * self._indent
usable_width = width - STATUS_LEN - self._indent
while usable_width < len(message):
part_line = ' ' * (self._indent + STATUS_LEN)
part = message[:usable_width]
message = message[usable_width:]
output += '{}{}\n'.format(part_line, part)
output += '{}{}'.format(first_line, message)
output += f'{part_line}{part}\n'
output += f'{first_line}{message}'
if new_line:
print(style(output))
else:
Expand Down
16 changes: 5 additions & 11 deletions pontos/version/cmake_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ def __init__(self, *, cmake_lists_path: Path = None):
if not cmake_lists_path:
cmake_lists_path = Path.cwd() / 'CMakeLists.txt'
if not cmake_lists_path.exists():
raise VersionError(
'{} file not found.'.format(str(cmake_lists_path))
)
raise VersionError(f'{str(cmake_lists_path)} file not found.')

self.__cmake_filepath = cmake_lists_path
self.parser = initialize_default_parser()
Expand Down Expand Up @@ -83,9 +81,7 @@ def update_version(self, version: str, *, develop: bool = False):
previous_version = cmvp.get_current_version()
new_content = cmvp.update_version(version, develop=develop)
self.__cmake_filepath.write_text(new_content)
self.__print(
'Updated version from {} to {}'.format(previous_version, version)
)
self.__print(f'Updated version from {previous_version} to {version}')

def print_current_version(self):
content = self.__cmake_filepath.read_text()
Expand All @@ -99,9 +95,7 @@ def get_current_version(self) -> str:

def verify_version(self, version: str):
if not is_version_pep440_compliant(version):
raise VersionError(
"Version {} is not PEP 440 compliant.".format(version)
)
raise VersionError(f"Version {version} is not PEP 440 compliant.")

self.__print('OK')

Expand Down Expand Up @@ -139,7 +133,7 @@ def get_current_version(self) -> str:
def update_version(self, new_version: str, *, develop: bool = False) -> str:
if not is_version_pep440_compliant(new_version):
raise VersionError(
"version {} is not pep 440 compliant.".format(new_version)
f"version {new_version} is not pep 440 compliant."
)

new_version = safe_version(new_version)
Expand Down Expand Up @@ -239,7 +233,7 @@ def _tokenize(
]:
toks, remainder = self.__cmake_scanner.scan(content)
if remainder != '':
print('WARNING: unrecognized cmake tokens: {}'.format(remainder))
print(f'WARNING: unrecognized cmake tokens: {remainder}')

line_num = 0

Expand Down
43 changes: 15 additions & 28 deletions pontos/version/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ def get_version_from_pyproject_toml(pyproject_toml_path: Path = None) -> str:
pyproject_toml_path = path.parent.parent / 'pyproject.toml'

if not pyproject_toml_path.exists():
raise VersionError(
'{} file not found.'.format(str(pyproject_toml_path))
)
raise VersionError(f'{str(pyproject_toml_path)} file not found.')

pyproject_toml = tomlkit.parse(pyproject_toml_path.read_text())
if (
Expand All @@ -66,9 +64,7 @@ def get_version_from_pyproject_toml(pyproject_toml_path: Path = None) -> str:
return pyproject_toml['tool']['poetry']['version']

raise VersionError(
'Version information not found in {} file.'.format(
str(pyproject_toml_path)
)
f'Version information not found in {str(pyproject_toml_path)} file.'
)


Expand Down Expand Up @@ -117,9 +113,7 @@ def get_current_version(self) -> str:
version_module = importlib.import_module(module_name)
except ModuleNotFoundError:
raise VersionError(
'Could not load version from {}. Import failed.'.format(
module_name
)
f'Could not load version from {module_name}. Import failed.'
) from None

return version_module.__version__
Expand Down Expand Up @@ -189,9 +183,8 @@ def verify_version(self, version: str) -> None:
current_version = self.get_current_version()
if not is_version_pep440_compliant(current_version):
raise VersionError(
"The version {} in {} is not PEP 440 compliant.".format(
current_version, str(self.version_file_path)
)
f"The version {current_version} in "
f"{str(self.version_file_path)} is not PEP 440 compliant."
)

pyproject_version = get_version_from_pyproject_toml(
Expand All @@ -200,20 +193,17 @@ def verify_version(self, version: str) -> None:

if pyproject_version != current_version:
raise VersionError(
"The version {} in {} doesn't match the current "
"version {}.".format(
pyproject_version,
str(self.pyproject_toml_path),
current_version,
)
f"The version {pyproject_version} in "
f"{str(self.pyproject_toml_path)} doesn't match the current "
f"version {current_version}."
)

if version != 'current':
provided_version = strip_version(version)
if provided_version != current_version:
raise VersionError(
"Provided version {} does not match the current "
"version {}.".format(provided_version, current_version)
f"Provided version {provided_version} does not match the "
f"current version {current_version}."
)

self._print('OK')
Expand All @@ -232,7 +222,7 @@ def run(self, args=None) -> Union[int, str]:

if not self.pyproject_toml_path.exists():
raise VersionError(
'Could not find {} file.'.format(str(self.pyproject_toml_path))
f'Could not find {str(self.pyproject_toml_path)} file.'
)

try:
Expand All @@ -256,9 +246,7 @@ def __init__(self, *, pyproject_toml_path=None):
pyproject_toml_path = Path.cwd() / 'pyproject.toml'

if not pyproject_toml_path.exists():
raise VersionError(
'{} file not found.'.format(str(pyproject_toml_path))
)
raise VersionError(f'{str(pyproject_toml_path)} file not found.')

pyproject_toml = tomlkit.parse(pyproject_toml_path.read_text())

Expand All @@ -268,9 +256,8 @@ def __init__(self, *, pyproject_toml_path=None):
or 'version' not in pyproject_toml['tool']['pontos']
):
raise VersionError(
'[tool.pontos.version] section missing in {}.'.format(
str(pyproject_toml_path)
)
'[tool.pontos.version] section missing '
f'in {str(pyproject_toml_path)}.'
)

pontos_version_settings = pyproject_toml['tool']['pontos']['version']
Expand All @@ -282,7 +269,7 @@ def __init__(self, *, pyproject_toml_path=None):
except tomlkit.exceptions.NonExistentKey:
raise VersionError(
'version-module-file key not set in [tool.pontos.version] '
'section of {}.'.format(str(pyproject_toml_path))
f'section of {str(pyproject_toml_path)}.'
) from None

super().__init__(
Expand Down
81 changes: 24 additions & 57 deletions tests/changelog/test_changelog_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
import unittest
from pontos import changelog

UNRELEASED = """
## Unreleased
### fixed
so much
### added
so little
### changed
I don't recognize it anymore
### security
[Unreleased]: https://github.com/greenbone/pontos/compare/v1.0.0...master
"""


class ChangelogTestCase(unittest.TestCase):
def test_return_none_when_no_unreleased_information_found(self):
Expand All @@ -35,37 +47,17 @@ def test_return_none_when_no_unreleased_information_found(self):
self.assertEqual(cl, '')

def test_find_unreleased_information_before_another_version(self):
unreleased = """
## Unreleased
### fixed
so much
### added
so little
### changed
I don't recognize it anymore
### security
[Unreleased]: https://github.com/greenbone/pontos/compare/v1.0.0...master
"""
test_md = """
test_md = f"""
# Changelog
something, somehing
- unreleased
- not unreleased
{}
{UNRELEASED}
## 1.0.0
### added
- cool stuff 1
- cool stuff 2
""".format(
unreleased
)
"""
changed = """
## Unreleased
### fixed
Expand All @@ -74,6 +66,7 @@ def test_find_unreleased_information_before_another_version(self):
so little
### changed
I don't recognize it anymore
### security
[Unreleased]: https://github.com/greenbone/pontos/compare/v1.0.0...master
"""

Expand All @@ -82,39 +75,14 @@ def test_find_unreleased_information_before_another_version(self):


def test_find_unreleased_information_no_other_version(self):
unreleased = """
## Unreleased
### fixed
so much
### added
so little
### changed
I don't recognize it anymore
### security
[Unreleased]: https://github.com/greenbone/pontos/compare/v1.0.0...master
"""
test_md = """
{}
""".format(
unreleased
)
test_md = UNRELEASED

_, result = changelog.update(test_md, '', '')
self.assertEqual(result.strip(), unreleased.strip())
self.assertEqual(result.strip(), UNRELEASED.strip())


def test_find_unreleased_information_before_after_another_version(self):
unreleased = """
## Unreleased
### fixed
so much
### added
so little
### changed
I don't recognize it anymore
### security
[Unreleased]: https://github.com/greenbone/pontos/compare/v1.0.0...master
"""
test_md = """
test_md = f"""
# Changelog
something, somehing
- unreleased
Expand All @@ -123,9 +91,8 @@ def test_find_unreleased_information_before_after_another_version(self):
### added
- cool stuff 1
- cool stuff 2
{}
""".format(
unreleased
)
{UNRELEASED}
"""

_, result = changelog.update(test_md, '', '')
self.assertEqual(result.strip(), unreleased.strip())
self.assertEqual(result.strip(), UNRELEASED.strip())
Loading

0 comments on commit 13967b0

Please sign in to comment.