From 914d8ccb42befc04ec5b759dd84b242591bb5e91 Mon Sep 17 00:00:00 2001 From: Tommy Carpenter Date: Mon, 5 Mar 2018 11:25:44 -0500 Subject: Enforce most pep8 compliance in tox Also fix some pep8 failings Change-Id: I6904c171c898d510ac2b7e2435b5a80dcdf1be13 Issue-ID: DCAEGEN2-348 Signed-off-by: Tommy Carpenter --- config_binding_service/client.py | 48 +++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 18 deletions(-) (limited to 'config_binding_service/client.py') diff --git a/config_binding_service/client.py b/config_binding_service/client.py index 8facf35..2c04684 100644 --- a/config_binding_service/client.py +++ b/config_binding_service/client.py @@ -40,6 +40,7 @@ class CantGetConfig(Exception): """ Represents an exception where a required key in consul isn't there """ + def __init__(self, code, response): self.code = code self.response = response @@ -49,6 +50,7 @@ class BadRequest(Exception): """ Exception to be raised when the user tried to do something they shouldn't """ + def __init__(self, response): self.code = 400 self.response = response @@ -84,7 +86,8 @@ def _consul_get_all_as_transaction(service_component_name): new_res = {} for res in result: key = res["KV"]["Key"] - new_res[key] = json.loads(base64.b64decode(res["KV"]["Value"]).decode("utf-8")) + new_res[key] = json.loads(base64.b64decode( + res["KV"]["Value"]).decode("utf-8")) if service_component_name not in new_res: raise CantGetConfig(404, "") @@ -108,18 +111,23 @@ def _get_connection_info_from_consul(service_component_name): TODO: WARNING: FIXTHIS: CALLINTHENATIONALARMY: This tries to determine that a service_component_name is a cdap application by inspecting service_component_name and name munging. However, this would force all CDAP applications to have cdap_app in their name. A much better way to do this is to do some kind of catalog_lookup here, OR MAYBE change this API so that the component_type is passed in somehow. THis is a gaping TODO. """ - _logger.info("Retrieving connection information for {0}".format(service_component_name)) - res = requests.get("{0}/v1/catalog/service/{1}".format(CONSUL, service_component_name)) + _logger.info("Retrieving connection information for {0}".format( + service_component_name)) + res = requests.get( + "{0}/v1/catalog/service/{1}".format(CONSUL, service_component_name)) res.raise_for_status() services = res.json() if services == []: - _logger.info("Warning: config and rels keys were both valid, but there is no component named {0} registered in Consul!".format(service_component_name)) - return None #later will get filtered out + _logger.info("Warning: config and rels keys were both valid, but there is no component named {0} registered in Consul!".format( + service_component_name)) + return None # later will get filtered out ip_addr = services[0]["ServiceAddress"] port = services[0]["ServicePort"] if "cdap_app" in service_component_name: - redirectish_url = "http://{0}:{1}/application/{2}".format(ip_addr, port, service_component_name) - _logger.info("component is a CDAP application; trying the broker redirect on {0}".format(redirectish_url)) + redirectish_url = "http://{0}:{1}/application/{2}".format( + ip_addr, port, service_component_name) + _logger.info("component is a CDAP application; trying the broker redirect on {0}".format( + redirectish_url)) res = requests.get(redirectish_url) res.raise_for_status() details = res.json() @@ -138,7 +146,7 @@ def _replace_rels_template(rels, template_identifier): for rel in rels: if template_identifier in rel and template_identifier is not "": returnl.append(rel) - #returnl now contains a list of DNS names (possible empty), now resolve them (or not if they are not regustered) + # returnl now contains a list of DNS names (possible empty), now resolve them (or not if they are not regustered) return list(filter(lambda x: x is not None, map(_get_connection_info_from_consul, returnl))) @@ -159,10 +167,11 @@ def _replace_value(v, rels, dmaap): - the split below sees if we have v = x,y,z... so we can support {{x,y,z,....}} - the lambda is because we can't fold operators in Python, wanted fold(+, L) where + when applied to lists in python is list concatenation """ - if isinstance(v, six.string_types): #do not try to replace anything that is not a string + if isinstance(v, six.string_types): # do not try to replace anything that is not a string match_on_rels = re.match(template_match_rels, v) if match_on_rels: - template_identifier = match_on_rels.groups()[0].strip() #now holds just x,.. of {{x,...}} + # now holds just x,.. of {{x,...}} + template_identifier = match_on_rels.groups()[0].strip() rtpartial = partial(_replace_rels_template, rels) return reduce(lambda a, b: a + b, map(rtpartial, template_identifier.split(",")), []) match_on_dmaap = re.match(template_match_dmaap, v) @@ -176,7 +185,7 @@ def _replace_value(v, rels, dmaap): So now component developers have to possible handle dicts and [], and we have to communicate that to them """ return _replace_dmaap_template(dmaap, template_identifier) - return v #was not a match or was not a string, return value as is + return v # was not a match or was not a string, return value as is def _recurse(config, rels, dmaap): @@ -195,7 +204,7 @@ def _recurse(config, rels, dmaap): return config if isinstance(config, six.string_types): return _replace_value(config, rels, dmaap) - #not a dict, not a list, not a string, nothing to do. + # not a dict, not a list, not a string, nothing to do. return config @@ -217,7 +226,7 @@ def resolve_override(config, rels=[], dmaap={}): Explicitly take in a config, rels, dmaap and try to resolve it. Useful for testing where you dont want to put the test values in consul """ - #use deepcopy to make sure that config is not touched + # use deepcopy to make sure that config is not touched return _recurse(copy.deepcopy(config), rels, dmaap) @@ -228,10 +237,10 @@ def resolve_all(service_component_name): allk = _consul_get_all_as_transaction(service_component_name) returnk = {} - #replace the config with the resolved config + # replace the config with the resolved config returnk["config"] = resolve(service_component_name) - #concatenate the items + # concatenate the items for k in allk: if "policies" in k: if "policies" not in returnk: @@ -245,7 +254,8 @@ def resolve_all(service_component_name): returnk["policies"]["items"].append(allk[k]) else: if not(k == service_component_name or k.endswith(":rels") or k.endswith(":dmaap")): - suffix = k.split(":")[1] #this would blow up if you had a key in consul without a : but this shouldnt happen + # this would blow up if you had a key in consul without a : but this shouldnt happen + suffix = k.split(":")[1] returnk[suffix] = allk[k] return returnk @@ -256,8 +266,10 @@ def get_key(key, service_component_name): Try to fetch a key k from Consul of the form service_component_name:k """ if key == "policies": - raise BadRequest(":policies is a complex folder and should be retrieved using the service_component_all API") - response = requests.get("{0}/v1/kv/{1}:{2}".format(CONSUL, service_component_name, key)) + raise BadRequest( + ":policies is a complex folder and should be retrieved using the service_component_all API") + response = requests.get( + "{0}/v1/kv/{1}:{2}".format(CONSUL, service_component_name, key)) try: response.raise_for_status() except requests.exceptions.HTTPError as exc: -- cgit 1.2.3-korg