boartty/gertty/commentlink.py
James E. Blair 6d95ea2dff Genericize the change list and add inter-change links
Make the change list display not only changes for a project, but
any arbitrary set of changes represented by a query.  This will
enable searching, inter-change linking, and custom dashboards.

The search syntax is extremly basic at the moment, but will be
extended to attempt to match the Gerrit webui search syntax as
closely as possible.

Use the new feature to implement a new kind of commentlink that
matches on Iabcdef style change-ids to "search" for matching
changes.

Change-Id: Icbcb166962fd96718ab56eb4118fc5ea43a4ba8b
2014-07-12 20:23:44 -07:00

93 lines
3.1 KiB
Python

# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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 re
import urwid
import mywid
class TextReplacement(object):
def __init__(self, config):
if isinstance(config, basestring):
self.color = None
self.text = config
else:
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:app.openURL(self.url.format(**data)))
return link
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.search(self.query.format(**data)))
return link
class CommentLink(object):
def __init__(self, config):
self.match = re.compile(config['match'], re.M)
self.replacements = []
for r in config['replacements']:
if 'text' in r:
self.replacements.append(TextReplacement(r['text']))
if 'link' in r:
self.replacements.append(LinkReplacement(r['link']))
if 'search' in r:
self.replacements.append(SearchReplacement(r['search']))
def run(self, app, chunks):
ret = []
for chunk in chunks:
if not isinstance(chunk, basestring):
# We don't currently support nested commentlinks; if
# we have something that isn't a string, just append
# it to the output.
ret.append(chunk)
continue
if not chunk:
ret += [chunk]
while chunk:
m = self.match.search(chunk)
if not m:
ret.append(chunk)
break
before = chunk[:m.start()]
after = chunk[m.end():]
if before:
ret.append(before)
ret += [r.replace(app, m.groupdict()) for r in self.replacements]
chunk = after
return ret