Console interface to Gerrit Code Review
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.0 KiB

# Copyright 2014 OpenStack Foundation
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import collections
import ordereddict
import re
import six
import urwid
from gertty import mywid
OrderedDict = collections.OrderedDict
except AttributeError:
OrderedDict = ordereddict.OrderedDict
class TextReplacement(object):
def __init__(self, config):
if isinstance(config, six.string_types):
self.color = None
self.text = config
self.color = config.get('color')
self.text = config['text']
def replace(self, app, data):
if self.color:
return (self.color.format(**data), self.text.format(**data))
return (None, self.text.format(**data))
class LinkReplacement(object):
def __init__(self, config):
self.url = config['url']
self.text = config['text']
def replace(self, app, data):
link = mywid.Link(self.text.format(**data), 'link', 'focused-link')
urwid.connect_signal(link, 'selected',
lambda link:self.activate(app, self.url.format(**data)))
return link
def activate(self, app, url):
result = app.parseInternalURL(url)
if result is not None:
return app.openInternalURL(result)
return app.openURL(url)
class SearchReplacement(object):
def __init__(self, config):
self.query = config['query']
self.text = config['text']
def replace(self, app, data):
link = mywid.Link(self.text.format(**data), 'link', 'focused-link')
urwid.connect_signal(link, 'selected',
lambda link:app.doSearch(self.query.format(**data)))
return link
class CommentLink(object):
def __init__(self, config):
self.match = re.compile(config['match'], re.M)
self.test_result = config.get('test-result', None)
self.replacements = []
for r in config['replacements']:
if 'text' in r:
if 'link' in r:
if 'search' in r:
def getTestResults(self, app, text):
if self.test_result is None:
return {}
ret = OrderedDict()
for line in text.split('\n'):
m =
if m:
repl = [r.replace(app, m.groupdict()) for r in self.replacements]
job = self.test_result.format(**m.groupdict())
ret[job] = repl + ['\n']
return ret
def run(self, app, chunks):
ret = []
for chunk in chunks:
if not isinstance(chunk, six.string_types):
# We don't currently support nested commentlinks; if
# we have something that isn't a string, just append
# it to the output.
if not chunk:
ret += [chunk]
while chunk:
m =
if not m:
before = chunk[:m.start()]
after = chunk[m.end():]
if before:
ret += [r.replace(app, m.groupdict()) for r in self.replacements]
chunk = after
return ret