jp (merge-request,common): fixed URIFinder when metadata are not needed:

new "meta_map" attribute which allows to select destination argument name, or to discard unused metadata.
This commit is contained in:
Goffi 2018-04-02 08:23:22 +02:00
commit 2b44df2294
2 changed files with 38 additions and 14 deletions

View file

@ -121,7 +121,7 @@ class Get(base.CommandBase):
exit_code=C.EXIT_BRIDGE_ERRBACK))
def start(self):
common.URIFinder(self, os.getcwd(), 'merge requests', self.getRequests)
common.URIFinder(self, os.getcwd(), 'merge requests', self.getRequests, meta_map={})
class Import(base.CommandBase):
@ -155,7 +155,7 @@ class Import(base.CommandBase):
def start(self):
self.repository = os.path.expanduser(os.path.abspath(self.args.repository))
common.URIFinder(self, self.repository, 'merge requests', self.importRequest)
common.URIFinder(self, self.repository, 'merge requests', self.importRequest, meta_map={})
class MergeRequest(base.CommandBase):

View file

@ -670,19 +670,26 @@ class Table(object):
class URIFinder(object):
"""Helper class to find URIs in well-known locations"""
def __init__(self, command, path, key, callback):
def __init__(self, command, path, key, callback, meta_map=None):
"""
@param command(CommandBase): command instance
args of this instance will be updated with found values
@param path(unicode): absolute path to use as a starting point to look for URIs
@param key(unicode): key to look for
@param callback(callable): method to call once URIs are found (or not)
@param meta_map(dict, None): if not None, map metadata to arg name
key is metadata used attribute name
value is name to actually use, or None to ignore
use empty dict to only retrieve URI
possible keys are currently:
- labels
"""
if not command.args.service and not command.args.node:
self.host = command.host
self.args = command.args
self.key = key
self.callback = callback
self.meta_map = meta_map
self.host.bridge.URIFind(path,
[key],
callback=self.URIFindCb,
@ -692,6 +699,33 @@ class URIFinder(object):
else:
callback()
def setMetadataList(self, uri_data, key):
"""Helper method to set list of values from metadata
@param uri_data(dict): data of the found URI
@param key(unicode): key of the value to retrieve
"""
new_values_json = uri_data.get(key)
if uri_data is not None:
if self.meta_map is None:
dest = key
else:
dest = self.meta_map.get(key)
if dest is None:
return
try:
values = getattr(self.args, key)
except AttributeError:
raise exceptions.InternalError(u'there is no "{key}" arguments'.format(
key=key))
else:
if values is None:
values = []
values.extend(json.loads(new_values_json))
setattr(self.args, dest, values)
def URIFindCb(self, uris_data):
try:
uri_data = uris_data[self.key]
@ -700,18 +734,8 @@ class URIFinder(object):
self.host.quit(C.EXIT_NOT_FOUND)
else:
uri = uri_data[u'uri']
labels_raw = uri_data.get(u'labels')
if labels_raw is not None:
try:
labels = self.args.labels
except AttributeError:
raise exceptions.InternalError(u'there is no "labels" arguments')
else:
if labels is None:
labels = []
labels.extend(json.loads(labels_raw))
self.args.labels = labels
self.setMetadataList(uri_data, u'labels')
parsed_uri = xmpp_uri.parseXMPPUri(uri)
try:
self.args.service = parsed_uri[u'path']