Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/diff/diff_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def testSetUp(self):

def testUnifiedDiff(self):
results = list(self.diff.unified_diff())
self.assertTrue(results[0].startswith('--- ' + self.file1.name))
self.assertTrue(results[1].startswith('+++ ' + self.file2.name))
self.assertTrue(results[0].startswith(f'--- {self.file1.name}'))
self.assertTrue(results[1].startswith(f'+++ {self.file2.name}'))
self.assertEqual(
results[2:],
[
Expand Down
9 changes: 4 additions & 5 deletions fire/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ def WrappedJoin(items, separator=' | ', width=80):
else:
lines.append(current_line.rstrip())
current_line = item
elif len(current_line) + len(item) + len(separator) <= width:
current_line += item + separator
else:
if len(current_line) + len(item) + len(separator) <= width:
current_line += item + separator
else:
lines.append(current_line.rstrip())
current_line = item + separator
lines.append(current_line.rstrip())
current_line = item + separator

lines.append(current_line)
return lines
Expand Down
9 changes: 4 additions & 5 deletions fire/formatting_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ def initialize_or_disable():
mode = ctypes.wintypes.DWORD()
if kernel32.GetConsoleMode(out_handle, ctypes.byref(mode)) == 0:
wrap = True
if not mode.value & enable_virtual_terminal_processing:
if kernel32.SetConsoleMode(
out_handle, mode.value | enable_virtual_terminal_processing) == 0:
# kernel32.SetConsoleMode to enable ANSI sequences failed
wrap = True
if not mode.value & enable_virtual_terminal_processing and kernel32.SetConsoleMode(
out_handle, mode.value | enable_virtual_terminal_processing) == 0:
# kernel32.SetConsoleMode to enable ANSI sequences failed
wrap = True
colorama.init(wrap=wrap)
else:
os.environ['ANSI_COLORS_DISABLED'] = '1'
Expand Down
35 changes: 15 additions & 20 deletions fire/helptext.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ def _NameSection(component, info, trace=None, verbose=False):
summary = custom_descriptions.GetSummary(component, available_space,
LINE_LENGTH)

if summary:
text = current_command + ' - ' + summary
else:
text = current_command
text = f'{current_command} - {summary}' if summary else current_command
return ('NAME', text)


Expand Down Expand Up @@ -273,12 +270,12 @@ def _UsageDetailsSections(component, actions_grouped_by_kind):

def _GetSummary(info):
docstring_info = info['docstring_info']
return docstring_info.summary if docstring_info.summary else None
return docstring_info.summary or None


def _GetDescription(info):
docstring_info = info['docstring_info']
return docstring_info.description if docstring_info.description else None
return docstring_info.description or None


def _GetArgsAndFlagsString(spec, metadata):
Expand Down Expand Up @@ -374,11 +371,7 @@ def _GetActionsGroupedByKind(component, verbose=False):

def _GetCurrentCommand(trace=None, include_separators=True):
"""Returns current command for the purpose of generating help text."""
if trace:
current_command = trace.GetCommand(include_separators=include_separators)
else:
current_command = ''
return current_command
return trace.GetCommand(include_separators=include_separators) if trace else ''


def _CreateOutputSection(name, content):
Expand Down Expand Up @@ -411,7 +404,7 @@ def _CreateArgItem(arg, docstring_info, spec):
arg_string = formatting.BoldUnderline(arg.upper())

arg_type = _GetArgType(arg, spec)
arg_type = 'Type: {}'.format(arg_type) if arg_type else ''
arg_type = f'Type: {arg_type}' if arg_type else ''
available_space = max_str_length - len(arg_type)
arg_type = (
formatting.EllipsisTruncate(arg_type, available_space, max_str_length))
Expand Down Expand Up @@ -462,14 +455,14 @@ def _CreateFlagItem(flag, docstring_info, spec, required=False,
# We need to handle the case where there is a default of None, but otherwise
# the argument has another type.
if arg_default == 'None':
arg_type = 'Optional[{}]'.format(arg_type)
arg_type = f'Optional[{arg_type}]'

arg_type = 'Type: {}'.format(arg_type) if arg_type else ''
arg_type = f'Type: {arg_type}' if arg_type else ''
available_space = max_str_length - len(arg_type)
arg_type = (
formatting.EllipsisTruncate(arg_type, available_space, max_str_length))

arg_default = 'Default: {}'.format(arg_default) if arg_default else ''
arg_default = f'Default: {arg_default}' if arg_default else ''
available_space = max_str_length - len(arg_default)
arg_default = (
formatting.EllipsisTruncate(arg_default, available_space, max_str_length))
Expand All @@ -495,7 +488,7 @@ def _GetArgType(arg, spec):
if arg in spec.annotations:
arg_type = spec.annotations[arg]
try:
if sys.version_info[0:2] >= (3, 3):
if sys.version_info[:2] >= (3, 3):
return arg_type.__qualname__
return arg_type.__name__
except AttributeError:
Expand Down Expand Up @@ -538,7 +531,7 @@ def _CreateItem(name, description, indent=2):
def _GetArgDescription(name, docstring_info):
if docstring_info.args:
for arg_in_docstring in docstring_info.args:
if arg_in_docstring.name in (name, '*' + name, '**' + name):
if arg_in_docstring.name in (name, f'*{name}', f'**{name}'):
return arg_in_docstring.description
return None

Expand Down Expand Up @@ -708,10 +701,12 @@ def _GetCallableAvailabilityLines(spec):
args_with_defaults = spec.args[len(spec.args) - len(spec.defaults):]

# TODO(dbieber): Handle args_with_no_defaults if not accepts_positional_args.
optional_flags = [('--' + flag) for flag in itertools.chain(
args_with_defaults, _KeywordOnlyArguments(spec, required=False))]
optional_flags = [
f'--{flag}' for flag in itertools.chain(
args_with_defaults, _KeywordOnlyArguments(spec, required=False))
]
required_flags = [
('--' + flag) for flag in _KeywordOnlyArguments(spec, required=True)
f'--{flag}' for flag in _KeywordOnlyArguments(spec, required=True)
]

# Flags section:
Expand Down
8 changes: 4 additions & 4 deletions fire/helptext_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def testHelpTextFunctionWithKwargsAndDefaults(self):
help_screen)

@testutils.skipIf(
sys.version_info[0:2] < (3, 5),
sys.version_info[:2] < (3, 5),
'Python < 3.5 does not support type hints.')
def testHelpTextFunctionWithDefaultsAndTypes(self):
component = (
Expand All @@ -145,7 +145,7 @@ def testHelpTextFunctionWithDefaultsAndTypes(self):
self.assertNotIn('NOTES', help_screen)

@testutils.skipIf(
sys.version_info[0:2] < (3, 5),
sys.version_info[:2] < (3, 5),
'Python < 3.5 does not support type hints.')
def testHelpTextFunctionWithTypesAndDefaultNone(self):
component = (
Expand All @@ -163,7 +163,7 @@ def testHelpTextFunctionWithTypesAndDefaultNone(self):
self.assertNotIn('NOTES', help_screen)

@testutils.skipIf(
sys.version_info[0:2] < (3, 5),
sys.version_info[:2] < (3, 5),
'Python < 3.5 does not support type hints.')
def testHelpTextFunctionWithTypes(self):
component = tc.py3.WithTypes().double # pytype: disable=module-attr
Expand All @@ -181,7 +181,7 @@ def testHelpTextFunctionWithTypes(self):
help_screen)

@testutils.skipIf(
sys.version_info[0:2] < (3, 5),
sys.version_info[:2] < (3, 5),
'Python < 3.5 does not support type hints.')
def testHelpTextFunctionWithLongTypes(self):
component = tc.py3.WithTypes().long_type # pytype: disable=module-attr
Expand Down
7 changes: 3 additions & 4 deletions fire/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class CallableWithKeywordArgument(object):

def __call__(self, **kwargs):
for key, value in kwargs.items():
print('%s: %s' % (key, value))
print(f'{key}: {value}')

def print_msg(self, msg):
print(msg)
Expand Down Expand Up @@ -395,8 +395,7 @@ def example_generator(n):
[0, 1, 2, 3]

"""
for i in range(n):
yield i
yield from range(n)


def simple_set():
Expand Down Expand Up @@ -538,7 +537,7 @@ def wrapper(*args, **kwargs):

@simple_decorator
def decorated_method(name='World'):
return 'Hello %s' % name
return f'Hello {name}'


# pylint: disable=g-doc-args,g-doc-return-or-yield
Expand Down
2 changes: 1 addition & 1 deletion fire/test_components_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def triple(self, *, count):
return count * 3

def with_default(self, *, x="x"):
print("x: " + x)
print(f"x: {x}")


class LruCacheDecoratedMethod(object):
Expand Down
25 changes: 12 additions & 13 deletions fire/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def display(arg1, arg2='!'):
def _Quote(self, arg):
if arg.startswith('--') and '=' in arg:
prefix, value = arg.split('=', 1)
return pipes.quote(prefix) + '=' + pipes.quote(value)
return f'{pipes.quote(prefix)}={pipes.quote(value)}'
return pipes.quote(arg)

def GetCommand(self, include_separators=True):
Expand Down Expand Up @@ -301,15 +301,14 @@ def ErrorAsStr(self):
def __str__(self):
if self.HasError():
return self.ErrorAsStr()
else:
# Format is: {action} "{target}" ({filename}:{lineno})
string = self._action
if self._target is not None:
string += ' "{target}"'.format(target=self._target)
if self._filename is not None:
path = self._filename
if self._lineno is not None:
path += ':{lineno}'.format(lineno=self._lineno)

string += ' ({path})'.format(path=path)
return string
# Format is: {action} "{target}" ({filename}:{lineno})
string = self._action
if self._target is not None:
string += ' "{target}"'.format(target=self._target)
if self._filename is not None:
path = self._filename
if self._lineno is not None:
path += ':{lineno}'.format(lineno=self._lineno)

string += ' ({path})'.format(path=path)
return string