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:
parent
8e515c0be7
commit
2b44df2294
2 changed files with 38 additions and 14 deletions
|
|
@ -121,7 +121,7 @@ class Get(base.CommandBase):
|
||||||
exit_code=C.EXIT_BRIDGE_ERRBACK))
|
exit_code=C.EXIT_BRIDGE_ERRBACK))
|
||||||
|
|
||||||
def start(self):
|
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):
|
class Import(base.CommandBase):
|
||||||
|
|
@ -155,7 +155,7 @@ class Import(base.CommandBase):
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self.repository = os.path.expanduser(os.path.abspath(self.args.repository))
|
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):
|
class MergeRequest(base.CommandBase):
|
||||||
|
|
|
||||||
|
|
@ -670,19 +670,26 @@ class Table(object):
|
||||||
class URIFinder(object):
|
class URIFinder(object):
|
||||||
"""Helper class to find URIs in well-known locations"""
|
"""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
|
@param command(CommandBase): command instance
|
||||||
args of this instance will be updated with found values
|
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 path(unicode): absolute path to use as a starting point to look for URIs
|
||||||
@param key(unicode): key to look for
|
@param key(unicode): key to look for
|
||||||
@param callback(callable): method to call once URIs are found (or not)
|
@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:
|
if not command.args.service and not command.args.node:
|
||||||
self.host = command.host
|
self.host = command.host
|
||||||
self.args = command.args
|
self.args = command.args
|
||||||
self.key = key
|
self.key = key
|
||||||
self.callback = callback
|
self.callback = callback
|
||||||
|
self.meta_map = meta_map
|
||||||
self.host.bridge.URIFind(path,
|
self.host.bridge.URIFind(path,
|
||||||
[key],
|
[key],
|
||||||
callback=self.URIFindCb,
|
callback=self.URIFindCb,
|
||||||
|
|
@ -692,6 +699,33 @@ class URIFinder(object):
|
||||||
else:
|
else:
|
||||||
callback()
|
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):
|
def URIFindCb(self, uris_data):
|
||||||
try:
|
try:
|
||||||
uri_data = uris_data[self.key]
|
uri_data = uris_data[self.key]
|
||||||
|
|
@ -700,18 +734,8 @@ class URIFinder(object):
|
||||||
self.host.quit(C.EXIT_NOT_FOUND)
|
self.host.quit(C.EXIT_NOT_FOUND)
|
||||||
else:
|
else:
|
||||||
uri = uri_data[u'uri']
|
uri = uri_data[u'uri']
|
||||||
labels_raw = uri_data.get(u'labels')
|
|
||||||
|
|
||||||
if labels_raw is not None:
|
self.setMetadataList(uri_data, u'labels')
|
||||||
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
|
|
||||||
parsed_uri = xmpp_uri.parseXMPPUri(uri)
|
parsed_uri = xmpp_uri.parseXMPPUri(uri)
|
||||||
try:
|
try:
|
||||||
self.args.service = parsed_uri[u'path']
|
self.args.service = parsed_uri[u'path']
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue