1
0
mirror of https://github.com/httpie/cli.git synced 2025-08-10 22:42:05 +02:00

Modernize the code base with f-strings (#1068)

This commit is contained in:
Mickaël Schoentgen
2021-05-25 20:49:07 +02:00
committed by GitHub
parent 39314887c4
commit 0ff0874fa3
9 changed files with 43 additions and 63 deletions

View File

@@ -284,8 +284,7 @@ class HTTPieArgumentParser(argparse.ArgumentParser):
invalid.append(option)
if invalid:
msg = 'unrecognized arguments: %s'
self.error(msg % ' '.join(invalid))
self.error(f'unrecognized arguments: {" ".join(invalid)}')
def _body_from_file(self, fd):
"""Read the data from a file-like object.
@@ -381,8 +380,8 @@ class HTTPieArgumentParser(argparse.ArgumentParser):
for key, file in self.args.files.items():
if key != '':
self.error(
'Invalid file fields (perhaps you meant --form?): %s'
% ','.join(self.args.files.keys()))
'Invalid file fields (perhaps you meant --form?):'
f' {",".join(self.args.files.keys())}')
if request_file is not None:
self.error("Can't read request from multiple files")
request_file = file
@@ -407,10 +406,7 @@ class HTTPieArgumentParser(argparse.ArgumentParser):
def check_options(value, option):
unknown = set(value) - OUTPUT_OPTIONS
if unknown:
self.error('Unknown output options: {0}={1}'.format(
option,
','.join(unknown)
))
self.error(f'Unknown output options: {option}={",".join(unknown)}')
if self.args.verbose:
self.args.all = True

View File

@@ -30,7 +30,7 @@ from ..ssl import AVAILABLE_SSL_VERSION_ARG_MAPPING, DEFAULT_SSL_CIPHERS
parser = HTTPieArgumentParser(
prog='http',
description='%s <https://httpie.org>' % __doc__.strip(),
description=f'{__doc__.strip()} <https://httpie.org>',
epilog=dedent('''
For every --OPTION there is also a --no-OPTION that reverts OPTION
to its default value.
@@ -267,7 +267,7 @@ output_processing.add_argument(
'''.format(
default=DEFAULT_STYLE,
available_styles='\n'.join(
'{0}{1}'.format(8 * ' ', line.strip())
f' {line.strip()}'
for line in wrap(', '.join(sorted(AVAILABLE_STYLES)), 60)
).strip(),
auto_style=AUTO_STYLE,
@@ -330,7 +330,7 @@ output_processing.add_argument(
'''.format(
option_list='\n'.join(
(8 * ' ') + option for option in DEFAULT_FORMAT_OPTIONS).strip()
f' {option}' for option in DEFAULT_FORMAT_OPTIONS).strip()
)
)
@@ -383,12 +383,12 @@ output_options.add_argument(
'--verbose', '-v',
dest='verbose',
action='store_true',
help='''
help=f'''
Verbose output. Print the whole request as well as the response. Also print
any intermediary requests/responses (such as redirects).
It's a shortcut for: --all --print={0}
It's a shortcut for: --all --print={''.join(OUTPUT_OPTIONS)}
'''.format(''.join(OUTPUT_OPTIONS))
'''
)
output_options.add_argument(
'--all',
@@ -562,7 +562,7 @@ auth.add_argument(
name=plugin.name,
package=(
'' if issubclass(plugin, BuiltinAuthPlugin)
else ' (provided by %s)' % plugin.package_name
else f' (provided by {plugin.package_name})'
),
description=(
'' if not plugin.description else

View File

@@ -89,13 +89,11 @@ def process_header_arg(arg: KeyValueArg) -> Optional[str]:
def process_empty_header_arg(arg: KeyValueArg) -> str:
if arg.value:
raise ParseError(
'Invalid item "%s" '
'(to specify an empty header use `Header;`)'
% arg.orig
)
return arg.value
if not arg.value:
return arg.value
raise ParseError(
f'Invalid item {arg.orig!r} (to specify an empty header use `Header;`)'
)
def process_query_param_arg(arg: KeyValueArg) -> str:
@@ -109,7 +107,7 @@ def process_file_upload_arg(arg: KeyValueArg) -> Tuple[str, IO, str]:
try:
f = open(os.path.expanduser(filename), 'rb')
except IOError as e:
raise ParseError('"%s": %s' % (arg.orig, e))
raise ParseError(f'{arg.orig!r}: {e}')
return (
os.path.basename(filename),
f,
@@ -142,12 +140,11 @@ def load_text_file(item: KeyValueArg) -> str:
with open(os.path.expanduser(path), 'rb') as f:
return f.read().decode()
except IOError as e:
raise ParseError('"%s": %s' % (item.orig, e))
raise ParseError(f'{item.orig!r}: {e}')
except UnicodeDecodeError:
raise ParseError(
'"%s": cannot embed the content of "%s",'
f'{item.orig!r}: cannot embed the content of {item.value!r},'
' not a UTF8 or ASCII-encoded text file'
% (item.orig, item.value)
)
@@ -155,4 +152,4 @@ def load_json(arg: KeyValueArg, contents: str) -> JSONType:
try:
return load_json_preserve_order(contents)
except ValueError as e:
raise ParseError('"%s": %s' % (arg.orig, e))
raise ParseError(f'{arg.orig!r}: {e}')