Skip to content

Commit beac4a1

Browse files
authored
Merge pull request #337 from dirkmueller/fstrings
Use f-strings
2 parents f7c1132 + 66c4032 commit beac4a1

10 files changed

Lines changed: 29 additions & 31 deletions

spec_cleaner/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ def process_args(argv: List[str]) -> Dict[str, Any]:
138138

139139
# the spec must exist for us to do anything
140140
if not os.path.exists(options.specfile):
141-
raise RpmWrongArgsError('{0} does not exist.'.format(options.specfile))
141+
raise RpmWrongArgsError(f'{options.specfile} does not exist.')
142142

143143
# the path for output must exist and the file must not be there unless
144144
# force is specified
145145
if options.output:
146146
options.output = os.path.expanduser(options.output)
147147
if not options.force and os.path.exists(options.output):
148-
raise RpmWrongArgsError('{0} already exists.'.format(options.output))
148+
raise RpmWrongArgsError(f'{options.output} already exists.')
149149

150150
# convert options to dict
151151
options_dict = vars(options)
@@ -162,14 +162,14 @@ def main() -> int:
162162
try:
163163
options = process_args(sys.argv[1:])
164164
except RpmWrongArgsError as exception:
165-
sys.stderr.write('ERROR: {0}\n'.format(exception))
165+
sys.stderr.write(f'ERROR: {exception}\n')
166166
return 1
167167

168168
try:
169169
cleaner = RpmSpecCleaner(options)
170170
cleaner.run()
171171
except RpmExceptionError as exception:
172-
sys.stderr.write('ERROR: {0}\n'.format(exception))
172+
sys.stderr.write(f'ERROR: {exception}\n')
173173
return 1
174174

175175
return 0

spec_cleaner/dependency_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def consume_chars(regex, string):
6363
return string[0:end], string[end:]
6464
else:
6565
raise NoMatchExceptionError(
66-
'Expected match failed (string: "%s", regex: "%s" )' % (string, regex.pattern)
66+
f'Expected match failed (string: "{string}", regex: "{regex.pattern}" )'
6767
)
6868

6969

@@ -79,7 +79,7 @@ def matching_bracket(bracket):
7979
elif bracket == '(':
8080
return ')'
8181
raise Exception(
82-
'Undefined bracket matching - add defintion of "%s" to ' 'matching_bracket()' % bracket
82+
f'Undefined bracket matching - add defintion of "{bracket}" to matching_bracket()'
8383
)
8484

8585

spec_cleaner/fileutils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ def open_datafile(name: str) -> IO[str]:
2424
homedir = os.getenv('HOME', '~') + '/.local/'
2525

2626
possible_paths = (
27-
'{0}/../data/{1}'.format(os.path.dirname(os.path.realpath(__file__)), name),
28-
'{0}/share/spec-cleaner/{1}'.format(homedir, name),
29-
'{0}/share/spec-cleaner/{1}'.format(sysconfig.get_path('data'), name),
30-
'{0}/share/spec-cleaner/{1}'.format(sys.prefix, name),
27+
f'{os.path.dirname(os.path.realpath(__file__))}/../data/{name}',
28+
f'{homedir}/share/spec-cleaner/{name}',
29+
f"{sysconfig.get_path('data')}/share/spec-cleaner/{name}",
30+
f'{sys.prefix}/share/spec-cleaner/{name}',
3131
)
3232

3333
for path in possible_paths:
@@ -38,7 +38,7 @@ def open_datafile(name: str) -> IO[str]:
3838
else:
3939
return _file
4040
# file not found
41-
raise RpmExceptionError("File '{}' not found in datadirs".format(name))
41+
raise RpmExceptionError(f"File '{name}' not found in datadirs")
4242

4343

4444
def open_stringio_spec(name: str) -> IO[str]:

spec_cleaner/rpmcopyright.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ def __init__(self, options):
2222
self.year = options['copyright_year']
2323
self.copyrights = []
2424
self.buildrules = []
25-
self.distro_copyright = '# Copyright (c) {0} SUSE LLC and contributors'.format(self.year)
25+
self.distro_copyright = f'# Copyright (c) {self.year} SUSE LLC and contributors'
2626
self.vimmodeline = ''
2727

2828
def _add_pkg_header(self):
2929
"""Add specfile name to the Copyright section."""
3030
specname = os.path.splitext(os.path.basename(self.spec))[0]
3131
self.lines.append(
32-
"""#
33-
# spec file for package {0}
34-
#""".format(
35-
specname
36-
)
32+
f"""#
33+
# spec file for package {specname}
34+
#"""
3735
)
3836

3937
def _add_copyright(self):
@@ -84,7 +82,7 @@ def add(self, line: str) -> None:
8482
copyright_match = self.reg.re_copyright_string.match(line)
8583
if copyright_match and not self.reg.re_suse_copyright.search(line):
8684
# always replace whitespace garbage on copyright line
87-
line = '# Copyright (c) {0}'.format(copyright_match.group(1))
85+
line = f'# Copyright (c) {copyright_match.group(1)}'
8886
self.copyrights.append(line)
8987
elif self.reg.re_rootforbuild.match(line):
9088
self.buildrules.append('# needsrootforbuild')

spec_cleaner/rpmfiles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def _move_license_from_doc(self, line: str) -> str:
8080
licences += match.group()
8181
line = self.reg.re_doclicense.sub('', line, 1)
8282
match = self.reg.re_doclicense.search(line)
83-
Section.add(self, '%license {}'.format(licences))
83+
Section.add(self, f'%license {licences}')
8484
return line
8585

8686
def _expand_python_sitelib(self, line: str) -> str:

spec_cleaner/rpmhelpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def add_group(group):
248248
items += add_group(subgroup)
249249
return items
250250
else:
251-
raise RpmExceptionError('Unknown type of group in preamble: %s' % type(group))
251+
raise RpmExceptionError(f'Unknown type of group in preamble: {type(group)}')
252252

253253

254254
def find_pkgconfig_statement(elements):

spec_cleaner/rpmpreamble.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def _fix_pypi_source(self, url):
215215
(
216216
'https',
217217
'files.pythonhosted.org',
218-
'/packages/{}/{}/{}/{}'.format(pkg_type, modname[0], modname, filename),
218+
f'/packages/{pkg_type}/{modname[0]}/{modname}/{filename}',
219219
'',
220220
'',
221221
'',
@@ -284,7 +284,7 @@ def _pkgname_to_brackety(token, brackety, conversions):
284284
# then add each pkgconfig to the list
285285
# print pkgconf_list
286286
for j in convers_list:
287-
name = '{0}({1})'.format(brackety, j)
287+
name = f'{brackety}({j})'
288288
converted.append(RpmRequiresToken(name, token.operator, token.version))
289289
return converted
290290

@@ -487,7 +487,7 @@ def add(self, line):
487487
source = self._fix_pypi_source(source)
488488
if secure_source_available:
489489
source = self._make_secure_url(source, skip_availabilty_check=True)
490-
self._add_line_value_to('source', source, key='Source%s' % match.group(1))
490+
self._add_line_value_to('source', source, key=f'Source{match.group(1)}')
491491
return
492492

493493
elif self.reg.re_patch.match(line):
@@ -498,15 +498,15 @@ def add(self, line):
498498
else:
499499
zero = ''
500500
self._add_line_value_to(
501-
'patch', match.group(3), key='%sPatch%s%s' % (match.group(1), zero, match.group(2)),
501+
'patch', match.group(3), key=f'{match.group(1)}Patch{zero}{match.group(2)}',
502502
)
503503
return
504504

505505
elif self.reg.re_buildoption_phase.match(line):
506506
match = self.reg.re_buildoption_phase.match(line)
507507
value = match.group(2)
508508
self._add_line_value_to(
509-
'buildoption_phase', value, key='BuildOption{0}'.format(match.group(1))
509+
'buildoption_phase', value, key=f'BuildOption{match.group(1)}'
510510
)
511511
return
512512

@@ -624,7 +624,7 @@ def add(self, line):
624624
else:
625625
value = match.group(2)
626626
self._add_line_value_to(
627-
'requires_phase', value, key='Requires{0}'.format(match.group(1))
627+
'requires_phase', value, key=f'Requires{match.group(1)}'
628628
)
629629
return
630630

@@ -663,7 +663,7 @@ def add(self, line):
663663
language = match.group(1)
664664
# and what value is there
665665
content = match.group(2)
666-
self._add_line_value_to('summary_localized', content, key='Summary{0}'.format(language))
666+
self._add_line_value_to('summary_localized', content, key=f'Summary{language}')
667667
return
668668

669669
elif self.reg.re_group.match(line):

spec_cleaner/rpmpreambleelements.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def _sort_helper_key(self, a):
154154
# if this is a list then all items except last are comment or whitespace
155155
key = str(a[-1])
156156
else:
157-
raise RpmExceptionError('Unknown type during sort: %s' % a)
157+
raise RpmExceptionError(f'Unknown type during sort: {a}')
158158

159159
# Special case is the category grouping where we have to get the number in
160160
# after the value
@@ -321,7 +321,7 @@ def compile_category_prefix(self, category, key=None):
321321
elif category in self.category_to_key:
322322
key = self.category_to_key[category]
323323
else:
324-
raise RpmExceptionError('Unhandled category in preamble: %s' % category)
324+
raise RpmExceptionError(f'Unhandled category in preamble: {category}')
325325

326326
# append : only if the thing is not known macro
327327
if not key.startswith('%'):

spec_cleaner/rpmprep.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def _prepare_patch(self, line: str) -> str:
7777
match = self.reg.re_patch_prep.match(line)
7878
if match:
7979
line = self.strip_useless_spaces(
80-
'%%patch -P %s %s' % (match.group(1), match.group(2))
80+
f'%patch -P {match.group(1)} {match.group(2)}'
8181
)
8282

8383
return line

spec_cleaner/rpmscriplets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ def _collapse_multiline_ldconfig(self) -> None:
5252
if self.lines[1] == '/sbin/ldconfig':
5353
pkg = self.lines[0]
5454
self.lines = []
55-
self.lines.append('{0} -p /sbin/ldconfig'.format(pkg))
55+
self.lines.append(f'{pkg} -p /sbin/ldconfig')

0 commit comments

Comments
 (0)