Fix basic action outputs
This commit is contained in:
parent
638f0fa690
commit
3afc739a1e
@ -8,8 +8,11 @@ from ..exceptions import WrongResult
|
|||||||
class Action:
|
class Action:
|
||||||
parent = None
|
parent = None
|
||||||
contextualize = []
|
contextualize = []
|
||||||
colorize = {
|
regexps = {
|
||||||
'[^ ]*([^:]*):': {1: 0},
|
r'([\w]+):': '{cyan}\\1{gray}:{reset}',
|
||||||
|
r'(^|\n)( *)\- ': '\\1\\2{red}-{reset} ',
|
||||||
|
r'([^ =]+)=': '{blue}\\1{red}={reset} ',
|
||||||
|
r'=': '{blue}={reset} ',
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
@ -50,6 +53,8 @@ class Action:
|
|||||||
|
|
||||||
|
|
||||||
def sibblings(self, f=None, **filters):
|
def sibblings(self, f=None, **filters):
|
||||||
|
if not self.parent:
|
||||||
|
return []
|
||||||
return self.actions_filter(
|
return self.actions_filter(
|
||||||
[a for a in self.parent.actions if a is not self],
|
[a for a in self.parent.actions if a is not self],
|
||||||
f, **filters
|
f, **filters
|
||||||
@ -87,23 +92,54 @@ class Action:
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
def output_factory(self, *args, **kwargs):
|
def output_factory(self, *args, **kwargs):
|
||||||
kwargs.setdefault('regexps', self.colorize)
|
kwargs.setdefault('regexps', self.regexps)
|
||||||
return Output(*args, **kwargs)
|
return Output(*args, **kwargs)
|
||||||
|
|
||||||
async def __call__(self, *args, **kwargs):
|
async def __call__(self, *args, **kwargs):
|
||||||
self.status = 'running'
|
|
||||||
self.output = self.output_factory(*args, **kwargs)
|
self.output = self.output_factory(*args, **kwargs)
|
||||||
|
self.output_start()
|
||||||
|
self.status = 'running'
|
||||||
try:
|
try:
|
||||||
result = await self.call(*args, **kwargs)
|
result = await self.call(*args, **kwargs)
|
||||||
except WrongResult as e:
|
except WrongResult as e:
|
||||||
print(e)
|
self.output_fail(e)
|
||||||
self.status = 'fail'
|
self.status = 'fail'
|
||||||
|
result = e.proc.rc
|
||||||
else:
|
else:
|
||||||
|
self.output_success()
|
||||||
if self.status == 'running':
|
if self.status == 'running':
|
||||||
self.status = 'success'
|
self.status = 'success'
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def output_start(self):
|
||||||
|
self.output.start(self)
|
||||||
|
|
||||||
|
def output_fail(self, exception=None):
|
||||||
|
self.output.fail(self, exception)
|
||||||
|
|
||||||
|
def output_success(self):
|
||||||
|
self.output.success(self)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return ' '.join([type(self).__name__] + list(self.args) + [
|
||||||
|
f'{k}={v}'
|
||||||
|
for k, v in self.kwargs.items()
|
||||||
|
])
|
||||||
|
|
||||||
|
def colorized(self):
|
||||||
|
return ' '.join([
|
||||||
|
self.output.colors['pink1']
|
||||||
|
+ type(self).__name__
|
||||||
|
+ self.output.colors['reset']
|
||||||
|
] + list(self.args) + [
|
||||||
|
f'{self.output.colors["blue"]}{k}{self.output.colors["gray"]}={self.output.colors["green2"]}{v}'
|
||||||
|
for k, v in self.kwargs_output().items()
|
||||||
|
] + [self.output.colors['reset']])
|
||||||
|
|
||||||
def callable(self):
|
def callable(self):
|
||||||
async def cb(*a, **k):
|
async def cb(*a, **k):
|
||||||
return await self(*a, **k)
|
return await self(*a, **k)
|
||||||
return cb
|
return cb
|
||||||
|
|
||||||
|
def kwargs_output(self):
|
||||||
|
return self.kwargs
|
||||||
|
|||||||
@ -5,11 +5,11 @@ from shlax import *
|
|||||||
|
|
||||||
class GitLabCIConfig(Script):
|
class GitLabCIConfig(Script):
|
||||||
async def call(self, *args, write=True, **kwargs):
|
async def call(self, *args, write=True, **kwargs):
|
||||||
|
self.context['definition'] = kwargs
|
||||||
await super().call(*args, **kwargs)
|
await super().call(*args, **kwargs)
|
||||||
self.kwargs = kwargs
|
for name, definition in self.context['definition'].items():
|
||||||
for name, definition in self.context.items():
|
self.context['definition'][name] = definition
|
||||||
self.kwargs[name] = definition
|
output = yaml.dump(self.context['definition'])
|
||||||
output = yaml.dump(self.kwargs)
|
|
||||||
self.output(output)
|
self.output(output)
|
||||||
if write:
|
if write:
|
||||||
with open('.gitlab-ci.yml', 'w+') as f:
|
with open('.gitlab-ci.yml', 'w+') as f:
|
||||||
@ -18,4 +18,10 @@ class GitLabCIConfig(Script):
|
|||||||
|
|
||||||
class Job(Action):
|
class Job(Action):
|
||||||
async def call(self, *args, **kwargs):
|
async def call(self, *args, **kwargs):
|
||||||
self.context[self.args[0]] = self.kwargs
|
self.context['definition'][self.args[0]] = self.kwargs
|
||||||
|
|
||||||
|
def output_start(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def output_success(self):
|
||||||
|
pass
|
||||||
|
|||||||
@ -1,18 +1,12 @@
|
|||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from .colors import colors
|
||||||
|
|
||||||
|
|
||||||
class Output:
|
class Output:
|
||||||
prefixes = dict()
|
prefixes = dict()
|
||||||
colors = (
|
colors = colors
|
||||||
'\x1b[1;36;45m',
|
|
||||||
'\x1b[1;36;41m',
|
|
||||||
'\x1b[1;36;40m',
|
|
||||||
'\x1b[1;37;45m',
|
|
||||||
'\x1b[1;32m',
|
|
||||||
'\x1b[1;37;44m',
|
|
||||||
'\u001b[30;1m',
|
|
||||||
)
|
|
||||||
|
|
||||||
def color(self, code=None):
|
def color(self, code=None):
|
||||||
if not code:
|
if not code:
|
||||||
@ -55,15 +49,19 @@ class Output:
|
|||||||
else ''
|
else ''
|
||||||
)
|
)
|
||||||
+ self.highlight(line, highlight)
|
+ self.highlight(line, highlight)
|
||||||
|
+ self.colors['reset']
|
||||||
).encode('utf8'))
|
).encode('utf8'))
|
||||||
|
|
||||||
if flush:
|
if flush:
|
||||||
|
self.write(b'\n')
|
||||||
self.flush()
|
self.flush()
|
||||||
|
|
||||||
def cmd(self, line):
|
def cmd(self, line):
|
||||||
self(
|
self(
|
||||||
self.colorize(251, '+ ')
|
self.colorize(251, '+ ')
|
||||||
+ self.highlight(line, 'bash'),
|
+ '\x1b[1;38;5;15;48;5;244m'
|
||||||
|
+ self.highlight(line, 'bash')
|
||||||
|
+ self.colors['reset'],
|
||||||
highlight=False
|
highlight=False
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -84,14 +82,30 @@ class Output:
|
|||||||
return line
|
return line
|
||||||
|
|
||||||
for regexp, colors in self.regexps.items():
|
for regexp, colors in self.regexps.items():
|
||||||
match = re.match(regexp, line)
|
line = re.sub(regexp, colors.format(**self.colors), line)
|
||||||
if not match:
|
|
||||||
continue
|
|
||||||
|
|
||||||
for group, color in colors.items():
|
|
||||||
res = match.group(group)
|
|
||||||
if not res:
|
|
||||||
continue
|
|
||||||
line = line.replace(res, self.colorize(color, res))
|
|
||||||
|
|
||||||
return line
|
return line
|
||||||
|
|
||||||
|
def start(self, action):
|
||||||
|
self(''.join([
|
||||||
|
self.colors['orange'],
|
||||||
|
'[!] START ',
|
||||||
|
self.colors['reset'],
|
||||||
|
action.colorized(),
|
||||||
|
]))
|
||||||
|
|
||||||
|
def success(self, action):
|
||||||
|
self(''.join([
|
||||||
|
self.colors['green'],
|
||||||
|
'[√] SUCCESS ',
|
||||||
|
self.colors['reset'],
|
||||||
|
action.colorized(),
|
||||||
|
]))
|
||||||
|
|
||||||
|
def fail(self, action, exception=None):
|
||||||
|
self(''.join([
|
||||||
|
self.colors['red'],
|
||||||
|
'[x] FAIL ',
|
||||||
|
self.colors['reset'],
|
||||||
|
action.colorized(),
|
||||||
|
]))
|
||||||
|
|||||||
@ -16,9 +16,9 @@ def write():
|
|||||||
|
|
||||||
def test_output_regexps(write):
|
def test_output_regexps(write):
|
||||||
output = Output(
|
output = Output(
|
||||||
regexps={'.*': {0: 0}},
|
regexps={'^(.*)$': '{red}\\1'},
|
||||||
write=write,
|
write=write,
|
||||||
flush=lambda: None,
|
flush=lambda: None,
|
||||||
)
|
)
|
||||||
output('foo')
|
output('foo')
|
||||||
assert write.output == output.colorize(0, 'foo')
|
assert write.output.strip() == output.colors['red'] + 'foo' + output.colors['reset']
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user