misc: Run pre-commit run on all files in repo
The following command was run: ``` pre-commit run --all-files ``` This ensures all the files in the repository are formatted to pass our checks. Change-Id: Ia2fe3529a50ad925d1076a612d60a4280adc40de Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/62572 Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com> Maintainer: Jason Lowe-Power <power.jg@gmail.com> Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
This commit is contained in:
committed by
Bobby Bruce
parent
64add0e04d
commit
2bc5a8b71a
@@ -45,6 +45,7 @@ import inspect
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
class lookup(object):
|
||||
def __init__(self, formatter, frame, *args, **kwargs):
|
||||
self.frame = frame
|
||||
@@ -64,10 +65,10 @@ class lookup(object):
|
||||
if item in self.kwargs:
|
||||
return self.kwargs[item]
|
||||
|
||||
if item == '__file__':
|
||||
if item == "__file__":
|
||||
return self.frame.f_code.co_filename
|
||||
|
||||
if item == '__line__':
|
||||
if item == "__line__":
|
||||
return self.frame.f_lineno
|
||||
|
||||
if self.formatter.locals and item in self.frame.f_locals:
|
||||
@@ -89,6 +90,7 @@ class lookup(object):
|
||||
pass
|
||||
raise IndexError("Could not find '%s'" % item)
|
||||
|
||||
|
||||
class code_formatter_meta(type):
|
||||
pattern = r"""
|
||||
(?:
|
||||
@@ -102,44 +104,48 @@ class code_formatter_meta(type):
|
||||
%(delim)s(?P<invalid>) # ill-formed delimiter exprs
|
||||
)
|
||||
"""
|
||||
|
||||
def __init__(cls, name, bases, dct):
|
||||
super(code_formatter_meta, cls).__init__(name, bases, dct)
|
||||
if 'pattern' in dct:
|
||||
if "pattern" in dct:
|
||||
pat = cls.pattern
|
||||
else:
|
||||
# tuple expansion to ensure strings are proper length
|
||||
lb,rb = cls.braced
|
||||
lb1,lb2,rb2,rb1 = cls.double_braced
|
||||
lb, rb = cls.braced
|
||||
lb1, lb2, rb2, rb1 = cls.double_braced
|
||||
pat = code_formatter_meta.pattern % {
|
||||
'delim' : re.escape(cls.delim),
|
||||
'ident' : cls.ident,
|
||||
'pos' : cls.pos,
|
||||
'lb' : re.escape(lb),
|
||||
'rb' : re.escape(rb),
|
||||
'ldb' : re.escape(lb1+lb2),
|
||||
'rdb' : re.escape(rb2+rb1),
|
||||
}
|
||||
"delim": re.escape(cls.delim),
|
||||
"ident": cls.ident,
|
||||
"pos": cls.pos,
|
||||
"lb": re.escape(lb),
|
||||
"rb": re.escape(rb),
|
||||
"ldb": re.escape(lb1 + lb2),
|
||||
"rdb": re.escape(rb2 + rb1),
|
||||
}
|
||||
cls.pattern = re.compile(pat, re.VERBOSE | re.DOTALL | re.MULTILINE)
|
||||
|
||||
|
||||
class code_formatter(object, metaclass=code_formatter_meta):
|
||||
delim = r'$'
|
||||
ident = r'[_A-z]\w*'
|
||||
pos = r'[0-9]+'
|
||||
braced = r'{}'
|
||||
double_braced = r'{{}}'
|
||||
delim = r"$"
|
||||
ident = r"[_A-z]\w*"
|
||||
pos = r"[0-9]+"
|
||||
braced = r"{}"
|
||||
double_braced = r"{{}}"
|
||||
|
||||
globals = True
|
||||
locals = True
|
||||
fix_newlines = True
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self._data = []
|
||||
self._dict = {}
|
||||
self._indent_level = 0
|
||||
self._indent_spaces = 4
|
||||
self.globals = kwargs.pop('globals', type(self).globals)
|
||||
self.locals = kwargs.pop('locals', type(self).locals)
|
||||
self._fix_newlines = \
|
||||
kwargs.pop('fix_newlines', type(self).fix_newlines)
|
||||
self.globals = kwargs.pop("globals", type(self).globals)
|
||||
self.locals = kwargs.pop("locals", type(self).locals)
|
||||
self._fix_newlines = kwargs.pop(
|
||||
"fix_newlines", type(self).fix_newlines
|
||||
)
|
||||
|
||||
if args:
|
||||
self.__call__(args)
|
||||
@@ -171,38 +177,44 @@ class code_formatter(object, metaclass=code_formatter_meta):
|
||||
# Add a comment to inform which file generated the generated file
|
||||
# to make it easier to backtrack and modify generated code
|
||||
frame = inspect.currentframe().f_back
|
||||
if re.match(r'^\.(cc|hh|c|h)$', extension) is not None:
|
||||
f.write(f'''/**
|
||||
if re.match(r"^\.(cc|hh|c|h)$", extension) is not None:
|
||||
f.write(
|
||||
f"""/**
|
||||
* DO NOT EDIT THIS FILE!
|
||||
* File automatically generated by
|
||||
* {frame.f_code.co_filename}:{frame.f_lineno}
|
||||
*/
|
||||
|
||||
''')
|
||||
elif re.match(r'^\.py$', extension) is not None:
|
||||
f.write(f'''#
|
||||
"""
|
||||
)
|
||||
elif re.match(r"^\.py$", extension) is not None:
|
||||
f.write(
|
||||
f"""#
|
||||
# DO NOT EDIT THIS FILE!
|
||||
# File automatically generated by
|
||||
# {frame.f_code.co_filename}:{frame.f_lineno}
|
||||
#
|
||||
|
||||
''')
|
||||
elif re.match(r'^\.html$', extension) is not None:
|
||||
f.write(f'''<!--
|
||||
"""
|
||||
)
|
||||
elif re.match(r"^\.html$", extension) is not None:
|
||||
f.write(
|
||||
f"""<!--
|
||||
DO NOT EDIT THIS FILE!
|
||||
File automatically generated by
|
||||
{frame.f_code.co_filename}:{frame.f_lineno}
|
||||
-->
|
||||
|
||||
''')
|
||||
"""
|
||||
)
|
||||
|
||||
for data in self._data:
|
||||
f.write(data)
|
||||
f.close()
|
||||
|
||||
def __str__(self):
|
||||
data = ''.join(self._data)
|
||||
self._data = [ data ]
|
||||
data = "".join(self._data)
|
||||
self._data = [data]
|
||||
return data
|
||||
|
||||
def __getitem__(self, item):
|
||||
@@ -231,21 +243,21 @@ class code_formatter(object, metaclass=code_formatter_meta):
|
||||
self._data.append(data)
|
||||
return
|
||||
|
||||
initial_newline = not self._data or self._data[-1] == '\n'
|
||||
initial_newline = not self._data or self._data[-1] == "\n"
|
||||
for line in data.splitlines():
|
||||
if line:
|
||||
if self._indent_level:
|
||||
self._data.append(' ' * self._indent_level)
|
||||
self._data.append(" " * self._indent_level)
|
||||
self._data.append(line)
|
||||
|
||||
if line or not initial_newline:
|
||||
self._data.append('\n')
|
||||
self._data.append("\n")
|
||||
|
||||
initial_newline = False
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
if not args:
|
||||
self._data.append('\n')
|
||||
self._data.append("\n")
|
||||
return
|
||||
|
||||
format = args[0]
|
||||
@@ -254,51 +266,56 @@ class code_formatter(object, metaclass=code_formatter_meta):
|
||||
frame = inspect.currentframe().f_back
|
||||
|
||||
l = lookup(self, frame, *args, **kwargs)
|
||||
|
||||
def convert(match):
|
||||
ident = match.group('lone')
|
||||
ident = match.group("lone")
|
||||
# check for a lone identifier
|
||||
if ident:
|
||||
indent = match.group('indent') # must be spaces
|
||||
lone = '%s' % (l[ident], )
|
||||
indent = match.group("indent") # must be spaces
|
||||
lone = "%s" % (l[ident],)
|
||||
|
||||
def indent_lines(gen):
|
||||
for line in gen:
|
||||
yield indent
|
||||
yield line
|
||||
return ''.join(indent_lines(lone.splitlines(True)))
|
||||
|
||||
return "".join(indent_lines(lone.splitlines(True)))
|
||||
|
||||
# check for an identifier, braced or not
|
||||
ident = match.group('ident') or match.group('b_ident')
|
||||
ident = match.group("ident") or match.group("b_ident")
|
||||
if ident is not None:
|
||||
return '%s' % (l[ident], )
|
||||
return "%s" % (l[ident],)
|
||||
|
||||
# check for a positional parameter, braced or not
|
||||
pos = match.group('pos') or match.group('b_pos')
|
||||
pos = match.group("pos") or match.group("b_pos")
|
||||
if pos is not None:
|
||||
pos = int(pos)
|
||||
if pos > len(args):
|
||||
raise ValueError \
|
||||
('Positional parameter #%d not found in pattern' % pos,
|
||||
code_formatter.pattern)
|
||||
return '%s' % (args[int(pos)], )
|
||||
raise ValueError(
|
||||
"Positional parameter #%d not found in pattern" % pos,
|
||||
code_formatter.pattern,
|
||||
)
|
||||
return "%s" % (args[int(pos)],)
|
||||
|
||||
# check for a double braced expression
|
||||
eval_expr = match.group('eval')
|
||||
eval_expr = match.group("eval")
|
||||
if eval_expr is not None:
|
||||
result = eval(eval_expr, {}, l)
|
||||
return '%s' % (result, )
|
||||
return "%s" % (result,)
|
||||
|
||||
# check for an escaped delimiter
|
||||
if match.group('escaped') is not None:
|
||||
return '$'
|
||||
if match.group("escaped") is not None:
|
||||
return "$"
|
||||
|
||||
# At this point, we have to match invalid
|
||||
if match.group('invalid') is None:
|
||||
if match.group("invalid") is None:
|
||||
# didn't match invalid!
|
||||
raise ValueError('Unrecognized named group in pattern',
|
||||
code_formatter.pattern)
|
||||
raise ValueError(
|
||||
"Unrecognized named group in pattern",
|
||||
code_formatter.pattern,
|
||||
)
|
||||
|
||||
i = match.start('invalid')
|
||||
i = match.start("invalid")
|
||||
if i == 0:
|
||||
colno = 1
|
||||
lineno = 1
|
||||
@@ -307,52 +324,64 @@ class code_formatter(object, metaclass=code_formatter_meta):
|
||||
colno = i - sum(len(z) for z in lines)
|
||||
lineno = len(lines)
|
||||
|
||||
raise ValueError('Invalid format string: line %d, col %d' %
|
||||
(lineno, colno))
|
||||
raise ValueError(
|
||||
"Invalid format string: line %d, col %d" % (lineno, colno)
|
||||
)
|
||||
|
||||
d = code_formatter.pattern.sub(convert, format)
|
||||
self._append(d)
|
||||
|
||||
__all__ = [ "code_formatter" ]
|
||||
|
||||
if __name__ == '__main__':
|
||||
__all__ = ["code_formatter"]
|
||||
|
||||
if __name__ == "__main__":
|
||||
from .code_formatter import code_formatter
|
||||
|
||||
f = code_formatter()
|
||||
|
||||
class Foo(dict):
|
||||
def __init__(self, **kwargs):
|
||||
self.update(kwargs)
|
||||
|
||||
def __getattr__(self, attr):
|
||||
return self[attr]
|
||||
|
||||
x = "this is a test"
|
||||
l = [ [Foo(x=[Foo(y=9)])] ]
|
||||
l = [[Foo(x=[Foo(y=9)])]]
|
||||
|
||||
y = code_formatter()
|
||||
y('''
|
||||
y(
|
||||
"""
|
||||
{
|
||||
this_is_a_test();
|
||||
}
|
||||
''')
|
||||
f(' $y')
|
||||
f('''$__file__:$__line__
|
||||
{''')
|
||||
"""
|
||||
)
|
||||
f(" $y")
|
||||
f(
|
||||
"""$__file__:$__line__
|
||||
{"""
|
||||
)
|
||||
f("${{', '.join(str(x) for x in range(4))}}")
|
||||
f('${x}')
|
||||
f('$x')
|
||||
f("${x}")
|
||||
f("$x")
|
||||
f.indent()
|
||||
for i in range(5):
|
||||
f('$x')
|
||||
f('$i')
|
||||
f('$0', "zero")
|
||||
f('$1 $0', "zero", "one")
|
||||
f('${0}', "he went")
|
||||
f('${0}asdf', "he went")
|
||||
f("$x")
|
||||
f("$i")
|
||||
f("$0", "zero")
|
||||
f("$1 $0", "zero", "one")
|
||||
f("${0}", "he went")
|
||||
f("${0}asdf", "he went")
|
||||
f.dedent()
|
||||
|
||||
f('''
|
||||
f(
|
||||
"""
|
||||
${{l[0][0]["x"][0].y}}
|
||||
}
|
||||
''', 1, 9)
|
||||
""",
|
||||
1,
|
||||
9,
|
||||
)
|
||||
|
||||
print(f, end=' ')
|
||||
print(f, end=" ")
|
||||
|
||||
Reference in New Issue
Block a user