1773c11dd51fb4b0bf97fd56b47225c8d6acdfe7
[mevemon] / package / src / validation.py
1 """ This module contains all our input validation functions """
2 from constants import KEY_SIZE
3
4 class ValidationError(StandardError):
5     """ Exception that is raised if input validation fails
6     """
7     def __init__(self, message):
8         StandardError.__init__(self)
9         self.message = message
10
11     def __str__(self):
12         return repr(self.message)
13
14
15
16 def validate_api_key(api_key):
17     """ Validates an EVE api key. throws ValidationError exception if the
18         format is invalid.
19     """
20     #TODO: anything else we can do to validate the api key?
21     
22     if len(api_key) != KEY_SIZE:
23         raise ValidationError("API Key must be %s characters" % KEY_SIZE)
24     elif not api_key.isalnum():
25         raise ValidationError("API Key must only contain alphanumeric " +\
26                               "characters")
27
28
29 def validate_uid(uid):
30     """ Validates an EVE Online uid, throws ValidationError exception if the
31         format is invalid.
32     """
33     #TODO: anything else we can do to validate the uid?
34
35     if not uid.isdigit():
36         raise ValidationError("UID must be a number")
37     if len(uid) < 1:
38         raise ValidationError("Missing UID")