Created
June 30, 2011 16:03
-
-
Save ChristinaMeno/1056549 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _build_coherent_name(field, parent): | |
return parent and '.'.join((parent, field)) or field | |
def _verify_fields_recursive(expected, data, parent=None): | |
analysis = {} | |
for field, field_info in expected.items(): | |
coherent_name = _build_coherent_name(field, parent) | |
status = field in data and 'found' or 'missing', | |
required = field_info.get('required') and 'required' or 'optional', | |
analysis[coherent_name] = { | |
'class': '%(status)s_%(required)s' % locals(), | |
'message': "%(status)s %(required)s field '%(coherent_name)s'" % locals(), | |
} | |
if field in data and '_keys' in field_info: | |
analysis.update(_verify_fields_recursive(field_info['_keys'], data[field], | |
parent=coherent_name)) | |
for field in set(data) - set(analysis): | |
coherent_name = _build_coherent_name(field, parent) | |
analysis[coherent_name] = { | |
'class': 'unknown_field', | |
'message': "'%s' is an unknown field." % coherent_name, | |
} | |
return analysis |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment