From b5462f8f8822c39a482fe29490bee87638759687 Mon Sep 17 00:00:00 2001 From: Jakub Warmuz Date: Fri, 21 Nov 2014 22:26:51 +0100 Subject: [PATCH] Clean acme module. --- letsencrypt/client/acme.py | 40 ++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/letsencrypt/client/acme.py b/letsencrypt/client/acme.py index e8ffeeb74..4812aa10d 100644 --- a/letsencrypt/client/acme.py +++ b/letsencrypt/client/acme.py @@ -5,28 +5,44 @@ import pkg_resources import jsonschema -schemata = {schema: json.load(open(pkg_resources.resource_filename( - __name__, "schemata/%s.json" % schema))) for schema in [ - "authorization", "authorizationRequest", "certificate", "certificateRequest", - "challenge", "challengeRequest", "defer", "error", "revocation", - "revocationRequest", "statusRequest"] +SCHEMATA = { + schema: json.load(open(pkg_resources.resource_filename( + __name__, "schemata/%s.json" % schema))) for schema in [ + "authorization", + "authorizationRequest", + "certificate", + "certificateRequest", + "challenge", + "challengeRequest", + "defer", + "error", + "revocation", + "revocationRequest", + "statusRequest" + ] } + def acme_object_validate(j): """Validate a JSON object against the ACME protocol using JSON Schema. + Success will return None; failure to validate will raise a jsonschema.ValidationError exception describing the reason that the - object could not be validated successfully.""" + object could not be validated successfully. + """ j = json.loads(j) if not isinstance(j, dict): raise jsonschema.ValidationError("this is not a dictionary object") if "type" not in j: raise jsonschema.ValidationError("missing type field") - if j["type"] not in schemata: + if j["type"] not in SCHEMATA: raise jsonschema.ValidationError("unknown type %s" % j["type"]) - jsonschema.validate(j, schemata[j["type"]]) + jsonschema.validate(j, SCHEMATA[j["type"]]) -def pretty(s): - """Return a pretty-printed version of any JSON string (useful when - printing out protocol messages for debugging purposes.""" - return json.dumps(json.loads(s), indent=4) + +def pretty(json_string): + """Return a pretty-printed version of any JSON string. + + Useful when printing out protocol messages for debugging purposes. + """ + return json.dumps(json.loads(json_string), indent=4)