Last active
August 29, 2015 14:20
-
-
Save creotiv/13357bf5a7778077d943 to your computer and use it in GitHub Desktop.
Protocol Interface
This file contains hidden or 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
| class ProtocolException(Exception): | |
| pass | |
| class ProtocolParam(object): | |
| def __init__(self, check_type): | |
| self.check_type = check_type | |
| def is_param(self, check_type=None): | |
| if not check_type: | |
| check_type = self.check_type | |
| return issubclass(check_type.__class__, ProtocolParam) | |
| def check(self, data): | |
| raise NotImplementedError() | |
| def getType(self): | |
| raise NotImplementedError() | |
| class ListProtocolParam(ProtocolParam): | |
| def check(self, data): | |
| if not isinstance(data,self.getType()): | |
| raise ProtocolException('List "%s..." %s not instance of %s' % (str(data)[0:20], type(data), self.getType())) | |
| _type = type(self.check_type) | |
| if self.is_param(self.check_type): | |
| _type = self.check_type.getType() | |
| for el in data: | |
| if not isinstance(el,_type): | |
| raise ProtocolException('Element %s of list "%s..." %s not instance of %s' % (str(el)[0:20], str(data)[0:20], type(el), _type)) | |
| if self.is_param(): | |
| self.check_type.check(el) | |
| return True | |
| def getType(self): | |
| return list | |
| class DictProtocolParam(ProtocolParam): | |
| def check(self, data): | |
| if not isinstance(data,self.getType()): | |
| raise ProtocolException('Dict "%s..." %s not instance of %s' % (str(data)[0:20], type(data), self.getType())) | |
| return False | |
| keys = set(self.check_type.keys()) | |
| input_keys = data.keys() | |
| # check whether all keys are set | |
| # TODO: add required params | |
| if not keys.issubset(input_keys): | |
| raise ProtocolException('Dict "%s..." keys are missing: %s' % (str(data)[0:20], str(list(keys.difference(input_keys))))) | |
| for k,v in data.iteritems(): | |
| _type = type(self.check_type[k]) | |
| if self.is_param(self.check_type[k]): | |
| _type = self.check_type[k].getType() | |
| if not isinstance(v, _type): | |
| raise ProtocolException('Dict "%s..." key %s %s not instance of %s' % (str(data)[0:20], k, type(v), _type)) | |
| return False | |
| if self.is_param(self.check_type[k]): | |
| self.check_type[k].check(v) | |
| return True | |
| def getType(self): | |
| return dict | |
| class JsonProtocol(object): | |
| IN = None | |
| OUT = None | |
| @classmethod | |
| def check_input(self, input): | |
| if self.IN is None: | |
| raise NotImplementedError() | |
| return self.IN.check(input) | |
| @classmethod | |
| def check_output(self, output): | |
| if self.OUT is None: | |
| raise NotImplementedError() | |
| return self.OUT.check(output) | |
| class SendProtocol(JsonProtocol): | |
| IN = DictProtocolParam({ | |
| "videos":ListProtocolParam( | |
| DictProtocolParam({ | |
| 'val1':str(), | |
| 'val2':int(), | |
| 'val3':ListProtocolParam(str()) | |
| }) | |
| ) | |
| }) | |
| OUT = DictProtocolParam({ | |
| 'success':bool() | |
| }) | |
| if __name__ == '__main__': | |
| assert SendProtocol.check_input({"videos":[{'val1':'11','val2':11,'val3':["1","ds"]}]}) | |
| try: | |
| SendProtocol.check_input({"videos":[{'val1':'11','val2':11,'val3':[1,"ds"]}]}) | |
| except ProtocolException: | |
| assert True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment