Skip to content

Commit

Permalink
Convert to using f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmueller committed Jan 6, 2024
1 parent 130c1b4 commit a225f93
Show file tree
Hide file tree
Showing 13 changed files with 457 additions and 462 deletions.
8 changes: 4 additions & 4 deletions osc/OscConfigParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def _add_option(self, optname, value=None, line=None, sep='='):
raise configparser.Error('value and line are mutually exclusive')

if value is not None:
line = '%s%s%s' % (optname, sep, value)
line = f'{optname}{sep}{value}'
opt = self._find(optname)
if opt:
opt.format(line)
Expand Down Expand Up @@ -253,10 +253,10 @@ def _read(self, fp, fpname):
#cursect[optname] = "%s\n%s" % (cursect[optname], value)
#self.set(cursect, optname, "%s\n%s" % (self.get(cursect, optname), value))
if cursect == configparser.DEFAULTSECT:
self._defaults[optname] = "%s\n%s" % (self._defaults[optname], value)
self._defaults[optname] = f"{self._defaults[optname]}\n{value}"
else:
# use the raw value here (original version uses raw=False)
self._sections[cursect]._find(optname).value = '%s\n%s' % (self.get(cursect, optname, raw=True), value)
self._sections[cursect]._find(optname).value = f'{self.get(cursect, optname, raw=True)}\n{value}'
# a section header or option header?
else:
# is it a section header?
Expand Down Expand Up @@ -342,7 +342,7 @@ def __str__(self):
first = False
else:
ret.append('')
ret.append('[%s]' % line.name)
ret.append(f'[{line.name}]')
for sline in line._lines:
if sline.name == '__name__':
continue
Expand Down
2 changes: 1 addition & 1 deletion osc/babysitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def run(prg, argv=None):
print(e, file=sys.stderr)
return 2
except oscerr.ExtRuntimeError as e:
print(e.file + ':', e.msg, file=sys.stderr)
print(f"{e.file}:", e.msg, file=sys.stderr)
except oscerr.ServiceRuntimeError as e:
print(e.msg, file=sys.stderr)
except oscerr.WorkingCopyOutdated as e:
Expand Down
2 changes: 1 addition & 1 deletion osc/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, key, *args):
self.key = key

def __str__(self):
return '' + self.key + ' :' + ' '.join(self.args)
return f"{self.key} :{' '.join(self.args)}"


class Checker:
Expand Down
4 changes: 2 additions & 2 deletions osc/cmdln.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def main(self, argv=None):
self.options, self.args = self.argparser.parse_known_args(argv[1:])
unrecognized = [i for i in self.args if i.startswith("-")]
if unrecognized:
self.argparser.error(f"unrecognized arguments: " + " ".join(unrecognized))
self.argparser.error(f"unrecognized arguments: {' '.join(unrecognized)}")

self.post_argparse()

Expand All @@ -257,7 +257,7 @@ def main(self, argv=None):
if arg_names == ["subcmd", "opts"]:
# positional args specified manually via @cmdln.option
if self.args:
self.argparser.error(f"unrecognized arguments: " + " ".join(self.args))
self.argparser.error(f"unrecognized arguments: {' '.join(self.args)}")
cmd(self.options.command, self.options)
elif arg_names == ["subcmd", "opts", "args"]:
# positional args are the remaining (unrecognized) args
Expand Down
342 changes: 171 additions & 171 deletions osc/commandline.py

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions osc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,10 +1383,10 @@ def get_default(name, field):

ini_key = extra.get("ini_key", name)

x = bold(ini_key) + " : " + get_type(name, field)
x = f"{bold(ini_key)} : {get_type(name, field)}"
default = get_default(name, field)
if default:
x += " = " + italic(default)
x += f" = {italic(default)}"
result.append(x)
result.append("")
desc = extra.get("ini_description", None) or field.description or ""
Expand Down Expand Up @@ -1499,7 +1499,7 @@ def parse_apisrv_url(scheme, apisrv):
elif scheme is not None:
url = scheme + apisrv
else:
url = "https://" + apisrv
url = f"https://{apisrv}"
scheme, url, path = urlsplit(url)[0:3]
return scheme, url, path.rstrip('/')

Expand Down Expand Up @@ -1541,7 +1541,7 @@ def get_apiurl_api_host_options(apiurl):
apiurl = sanitize_apiurl(apiurl)
if is_known_apiurl(apiurl):
return config['api_host_options'][apiurl]
raise oscerr.ConfigMissingApiurl('missing credentials for apiurl: \'%s\'' % apiurl,
raise oscerr.ConfigMissingApiurl(f'missing credentials for apiurl: \'{apiurl}\'',
'', apiurl)


Expand Down Expand Up @@ -1602,14 +1602,14 @@ def write_config(fname, cp):
if e.errno != errno.EEXIST:
raise

with open(fname + '.new', 'w') as f:
with open(f"{fname}.new", 'w') as f:
cp.write(f, comments=True)
try:
os.rename(fname + '.new', fname)
os.rename(f"{fname}.new", fname)
os.chmod(fname, 0o600)
except:
if os.path.exists(fname + '.new'):
os.unlink(fname + '.new')
if os.path.exists(f"{fname}.new"):
os.unlink(f"{fname}.new")
raise


Expand Down Expand Up @@ -1642,10 +1642,10 @@ def config_set_option(section, opt, val=None, delete=False, update=True, creds_m

section = sections.get(section.rstrip('/'), section)
if section not in cp.sections():
raise oscerr.ConfigError('unknown section \'%s\'' % section, config['conffile'])
raise oscerr.ConfigError(f'unknown section \'{section}\'', config['conffile'])
if section == 'general' and opt not in general_opts or \
section != 'general' and opt not in api_host_options:
raise oscerr.ConfigError('unknown config option \'%s\'' % opt, config['conffile'])
raise oscerr.ConfigError(f'unknown config option \'{opt}\'', config['conffile'])

if not val and not delete and opt == 'pass' and creds_mgr_descr is not None:
# change password store
Expand Down Expand Up @@ -1758,7 +1758,7 @@ def _get_credentials_manager(url, cp):
if cp.has_option(url, credentials.AbstractCredentialsManager.config_entry):
creds_mgr = credentials.create_credentials_manager(url, cp)
if creds_mgr is None:
msg = 'Unable to instantiate creds mgr (section: %s)' % url
msg = f'Unable to instantiate creds mgr (section: {url})'
conffile = get_configParser.conffile
raise oscerr.ConfigMissingCredentialsError(msg, conffile, url)
return creds_mgr
Expand Down Expand Up @@ -1977,7 +1977,7 @@ def identify_conf():
return '~/.oscrc'

if os.environ.get('XDG_CONFIG_HOME', '') != '':
conffile = os.environ.get('XDG_CONFIG_HOME') + '/osc/oscrc'
conffile = f"{os.environ.get('XDG_CONFIG_HOME')}/osc/oscrc"
else:
conffile = '~/.config/osc/oscrc'

Expand Down
6 changes: 3 additions & 3 deletions osc/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def __init__(self, apiurl, cookiejar_path):
# doing expensive signature auth in multiple processes.
# This usually happens when a user runs multiple osc instances
# from the command-line in parallel.
self.cookiejar_lock_path = self.cookiejar_path + ".lock"
self.cookiejar_lock_path = f"{self.cookiejar_path}.lock"
self.cookiejar_lock_fd = None

@property
Expand Down Expand Up @@ -633,7 +633,7 @@ def ssh_sign(self, data, namespace, keyfile=None):
keyfile = self.guess_keyfile()
else:
if '/' not in keyfile:
keyfile = '~/.ssh/' + keyfile
keyfile = f"~/.ssh/{keyfile}"
keyfile = os.path.expanduser(keyfile)

cmd = [self.ssh_keygen_path, '-Y', 'sign', '-f', keyfile, '-n', namespace, '-q']
Expand Down Expand Up @@ -668,7 +668,7 @@ def add_signature_auth_header(self, req, auth):
auth = self.get_authorization(chal)
if not auth:
return False
auth_val = 'Signature %s' % auth
auth_val = f'Signature {auth}'
req.add('Authorization', auth_val)
return True

Expand Down
Loading

0 comments on commit a225f93

Please sign in to comment.