diff options
author | Richard Ipsum <richard.ipsum@codethink.co.uk> | 2015-05-11 16:31:47 +0100 |
---|---|---|
committer | Richard Ipsum <richard.ipsum@codethink.co.uk> | 2015-05-19 09:19:56 +0000 |
commit | 5dd1f23b77a4d1937fc309efa73d208278ab8de4 (patch) | |
tree | 6b07ef61f25ee317d6605ff9c3c8fe5294587fa0 /distbuild/protocol.py | |
parent | ddef6ab1ae5c4d54e651c9be6653a50e2a44c04b (diff) | |
download | morph-5dd1f23b77a4d1937fc309efa73d208278ab8de4.tar.gz |
Use protocol to validate incoming requests
Change-Id: I16680439b131e63d30eeff91814a1af643af6246
Diffstat (limited to 'distbuild/protocol.py')
-rw-r--r-- | distbuild/protocol.py | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/distbuild/protocol.py b/distbuild/protocol.py index 9aab6a6d..44552ae1 100644 --- a/distbuild/protocol.py +++ b/distbuild/protocol.py @@ -129,13 +129,13 @@ _optional_fields = { } -def message(message_type, **kwargs): - known_types = _required_fields.keys() - assert message_type in known_types - +def _validate(message_type, **kwargs): required_fields = _required_fields[message_type] optional_fields = _optional_fields.get(message_type, []) + known_types = _required_fields.keys() + assert message_type in known_types + for name in required_fields: assert name in kwargs, 'field %s is required' % name @@ -143,7 +143,25 @@ def message(message_type, **kwargs): assert (name in required_fields or name in optional_fields), \ 'field %s is not allowed' % name +def message(message_type, **kwargs): + _validate(message_type, **kwargs) + msg = dict(kwargs) msg['type'] = message_type return msg +def is_valid_message(msg): + + if 'type' not in msg: + return False + + msg_type = msg['type'] + del msg['type'] + + try: + _validate(msg_type, **msg) + return True + except AssertionError: + return False + finally: + msg['type'] = msg_type |