Skip to content

Commit

Permalink
Format codebase with black and check formatting in CQ
Browse files Browse the repository at this point in the history
Apply rules set by https://gerrit-review.googlesource.com/c/git-repo/+/362954/ across the codebase and fix any lingering errors caught
by flake8. Also check black formatting in run_tests (and CQ).

Bug: b/267675342
Change-Id: I972d77649dac351150dcfeb1cd1ad0ea2efc1956
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/363474
Reviewed-by: Mike Frysinger <[email protected]>
Tested-by: Gavin Mak <[email protected]>
Commit-Queue: Gavin Mak <[email protected]>
  • Loading branch information
Gavin Mak authored and LUCI committed Mar 22, 2023
1 parent 1604cf2 commit ea2e330
Show file tree
Hide file tree
Showing 79 changed files with 20,808 additions and 17,789 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[flake8]
max-line-length = 80
per-file-ignores =
# E501: line too long
tests/test_git_superproject.py: E501
extend-ignore =
# E203: Whitespace before ':'
# See https://github.com/PyCQA/pycodestyle/issues/373
Expand Down
314 changes: 159 additions & 155 deletions color.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,196 +17,200 @@

import pager

COLORS = {None: -1,
'normal': -1,
'black': 0,
'red': 1,
'green': 2,
'yellow': 3,
'blue': 4,
'magenta': 5,
'cyan': 6,
'white': 7}

ATTRS = {None: -1,
'bold': 1,
'dim': 2,
'ul': 4,
'blink': 5,
'reverse': 7}
COLORS = {
None: -1,
"normal": -1,
"black": 0,
"red": 1,
"green": 2,
"yellow": 3,
"blue": 4,
"magenta": 5,
"cyan": 6,
"white": 7,
}

ATTRS = {None: -1, "bold": 1, "dim": 2, "ul": 4, "blink": 5, "reverse": 7}

RESET = "\033[m"


def is_color(s):
return s in COLORS
return s in COLORS


def is_attr(s):
return s in ATTRS
return s in ATTRS


def _Color(fg=None, bg=None, attr=None):
fg = COLORS[fg]
bg = COLORS[bg]
attr = ATTRS[attr]

if attr >= 0 or fg >= 0 or bg >= 0:
need_sep = False
code = "\033["

if attr >= 0:
code += chr(ord('0') + attr)
need_sep = True

if fg >= 0:
if need_sep:
code += ';'
need_sep = True

if fg < 8:
code += '3%c' % (ord('0') + fg)
else:
code += '38;5;%d' % fg

if bg >= 0:
if need_sep:
code += ';'

if bg < 8:
code += '4%c' % (ord('0') + bg)
else:
code += '48;5;%d' % bg
code += 'm'
else:
code = ''
return code
fg = COLORS[fg]
bg = COLORS[bg]
attr = ATTRS[attr]

if attr >= 0 or fg >= 0 or bg >= 0:
need_sep = False
code = "\033["

if attr >= 0:
code += chr(ord("0") + attr)
need_sep = True

if fg >= 0:
if need_sep:
code += ";"
need_sep = True

if fg < 8:
code += "3%c" % (ord("0") + fg)
else:
code += "38;5;%d" % fg

if bg >= 0:
if need_sep:
code += ";"

if bg < 8:
code += "4%c" % (ord("0") + bg)
else:
code += "48;5;%d" % bg
code += "m"
else:
code = ""
return code


DEFAULT = None


def SetDefaultColoring(state):
"""Set coloring behavior to |state|.
"""Set coloring behavior to |state|.
This is useful for overriding config options via the command line.
"""
if state is None:
# Leave it alone -- return quick!
return
This is useful for overriding config options via the command line.
"""
if state is None:
# Leave it alone -- return quick!
return

global DEFAULT
state = state.lower()
if state in ('auto',):
DEFAULT = state
elif state in ('always', 'yes', 'true', True):
DEFAULT = 'always'
elif state in ('never', 'no', 'false', False):
DEFAULT = 'never'
global DEFAULT
state = state.lower()
if state in ("auto",):
DEFAULT = state
elif state in ("always", "yes", "true", True):
DEFAULT = "always"
elif state in ("never", "no", "false", False):
DEFAULT = "never"


class Coloring(object):
def __init__(self, config, section_type):
self._section = 'color.%s' % section_type
self._config = config
self._out = sys.stdout

on = DEFAULT
if on is None:
on = self._config.GetString(self._section)
if on is None:
on = self._config.GetString('color.ui')

if on == 'auto':
if pager.active or os.isatty(1):
self._on = True
else:
self._on = False
elif on in ('true', 'always'):
self._on = True
else:
self._on = False
def __init__(self, config, section_type):
self._section = "color.%s" % section_type
self._config = config
self._out = sys.stdout

on = DEFAULT
if on is None:
on = self._config.GetString(self._section)
if on is None:
on = self._config.GetString("color.ui")

if on == "auto":
if pager.active or os.isatty(1):
self._on = True
else:
self._on = False
elif on in ("true", "always"):
self._on = True
else:
self._on = False

def redirect(self, out):
self._out = out
def redirect(self, out):
self._out = out

@property
def is_on(self):
return self._on
@property
def is_on(self):
return self._on

def write(self, fmt, *args):
self._out.write(fmt % args)
def write(self, fmt, *args):
self._out.write(fmt % args)

def flush(self):
self._out.flush()
def flush(self):
self._out.flush()

def nl(self):
self._out.write('\n')
def nl(self):
self._out.write("\n")

def printer(self, opt=None, fg=None, bg=None, attr=None):
s = self
c = self.colorer(opt, fg, bg, attr)
def printer(self, opt=None, fg=None, bg=None, attr=None):
s = self
c = self.colorer(opt, fg, bg, attr)

def f(fmt, *args):
s._out.write(c(fmt, *args))
return f
def f(fmt, *args):
s._out.write(c(fmt, *args))

def nofmt_printer(self, opt=None, fg=None, bg=None, attr=None):
s = self
c = self.nofmt_colorer(opt, fg, bg, attr)
return f

def f(fmt):
s._out.write(c(fmt))
return f
def nofmt_printer(self, opt=None, fg=None, bg=None, attr=None):
s = self
c = self.nofmt_colorer(opt, fg, bg, attr)

def colorer(self, opt=None, fg=None, bg=None, attr=None):
if self._on:
c = self._parse(opt, fg, bg, attr)
def f(fmt):
s._out.write(c(fmt))

def f(fmt, *args):
output = fmt % args
return ''.join([c, output, RESET])
return f
else:
return f

def f(fmt, *args):
return fmt % args
return f
def colorer(self, opt=None, fg=None, bg=None, attr=None):
if self._on:
c = self._parse(opt, fg, bg, attr)

def nofmt_colorer(self, opt=None, fg=None, bg=None, attr=None):
if self._on:
c = self._parse(opt, fg, bg, attr)
def f(fmt, *args):
output = fmt % args
return "".join([c, output, RESET])

def f(fmt):
return ''.join([c, fmt, RESET])
return f
else:
def f(fmt):
return fmt
return f

def _parse(self, opt, fg, bg, attr):
if not opt:
return _Color(fg, bg, attr)

v = self._config.GetString('%s.%s' % (self._section, opt))
if v is None:
return _Color(fg, bg, attr)

v = v.strip().lower()
if v == "reset":
return RESET
elif v == '':
return _Color(fg, bg, attr)

have_fg = False
for a in v.split(' '):
if is_color(a):
if have_fg:
bg = a
return f
else:
fg = a
elif is_attr(a):
attr = a

return _Color(fg, bg, attr)
def f(fmt, *args):
return fmt % args

return f

def nofmt_colorer(self, opt=None, fg=None, bg=None, attr=None):
if self._on:
c = self._parse(opt, fg, bg, attr)

def f(fmt):
return "".join([c, fmt, RESET])

return f
else:

def f(fmt):
return fmt

return f

def _parse(self, opt, fg, bg, attr):
if not opt:
return _Color(fg, bg, attr)

v = self._config.GetString("%s.%s" % (self._section, opt))
if v is None:
return _Color(fg, bg, attr)

v = v.strip().lower()
if v == "reset":
return RESET
elif v == "":
return _Color(fg, bg, attr)

have_fg = False
for a in v.split(" "):
if is_color(a):
if have_fg:
bg = a
else:
fg = a
elif is_attr(a):
attr = a

return _Color(fg, bg, attr)
Loading

0 comments on commit ea2e330

Please sign in to comment.