1 | # $Id: VerifyType.py,v 1.4 2005/11/02 22:26:08 tavis_rudd Exp $ |
---|
2 | """Functions to verify an argument's type |
---|
3 | |
---|
4 | Meta-Data |
---|
5 | ================================================================================ |
---|
6 | Author: Mike Orr <iron@mso.oz.net> |
---|
7 | License: This software is released for unlimited distribution under the |
---|
8 | terms of the MIT license. See the LICENSE file. |
---|
9 | Version: $Revision: 1.4 $ |
---|
10 | Start Date: 2001/11/07 |
---|
11 | Last Revision Date: $Date: 2005/11/02 22:26:08 $ |
---|
12 | """ |
---|
13 | __author__ = "Mike Orr <iron@mso.oz.net>" |
---|
14 | __revision__ = "$Revision: 1.4 $"[11:-2] |
---|
15 | |
---|
16 | ################################################## |
---|
17 | ## DEPENDENCIES |
---|
18 | |
---|
19 | import types # Used in VerifyTypeClass. |
---|
20 | |
---|
21 | ################################################## |
---|
22 | ## PRIVATE FUNCTIONS |
---|
23 | |
---|
24 | def _errmsg(argname, ltd, errmsgExtra=''): |
---|
25 | """Construct an error message. |
---|
26 | |
---|
27 | argname, string, the argument name. |
---|
28 | ltd, string, description of the legal types. |
---|
29 | errmsgExtra, string, text to append to error mssage. |
---|
30 | Returns: string, the error message. |
---|
31 | """ |
---|
32 | if errmsgExtra: |
---|
33 | errmsgExtra = '\n' + errmsgExtra |
---|
34 | return "arg '%s' must be %s%s" % (argname, ltd, errmsgExtra) |
---|
35 | |
---|
36 | |
---|
37 | ################################################## |
---|
38 | ## TYPE VERIFICATION FUNCTIONS |
---|
39 | |
---|
40 | def VerifyType(arg, argname, legalTypes, ltd, errmsgExtra=''): |
---|
41 | """Verify the type of an argument. |
---|
42 | |
---|
43 | arg, any, the argument. |
---|
44 | argname, string, name of the argument. |
---|
45 | legalTypes, list of type objects, the allowed types. |
---|
46 | ltd, string, description of legal types (for error message). |
---|
47 | errmsgExtra, string, text to append to error message. |
---|
48 | Returns: None. |
---|
49 | Exceptions: TypeError if 'arg' is the wrong type. |
---|
50 | """ |
---|
51 | if type(arg) not in legalTypes: |
---|
52 | m = _errmsg(argname, ltd, errmsgExtra) |
---|
53 | raise TypeError(m) |
---|
54 | return True |
---|
55 | |
---|
56 | |
---|
57 | def VerifyTypeClass(arg, argname, legalTypes, ltd, klass, errmsgExtra=''): |
---|
58 | """Same, but if it's a class, verify it's a subclass of the right class. |
---|
59 | |
---|
60 | arg, any, the argument. |
---|
61 | argname, string, name of the argument. |
---|
62 | legalTypes, list of type objects, the allowed types. |
---|
63 | ltd, string, description of legal types (for error message). |
---|
64 | klass, class, the parent class. |
---|
65 | errmsgExtra, string, text to append to the error message. |
---|
66 | Returns: None. |
---|
67 | Exceptions: TypeError if 'arg' is the wrong type. |
---|
68 | """ |
---|
69 | VerifyType(arg, argname, legalTypes, ltd, errmsgExtra) |
---|
70 | # If no exception, the arg is a legal type. |
---|
71 | if type(arg) == types.ClassType and not issubclass(arg, klass): |
---|
72 | # Must test for "is class type" to avoid TypeError from issubclass(). |
---|
73 | m = _errmsg(argname, ltd, errmsgExtra) |
---|
74 | raise TypeError(m) |
---|
75 | return True |
---|
76 | |
---|
77 | # @@MO: Commented until we determine whether it's useful. |
---|
78 | #def VerifyClass(arg, argname, klass, ltd): |
---|
79 | # """Same, but allow *only* a subclass of the right class. |
---|
80 | # """ |
---|
81 | # VerifyTypeClass(arg, argname, [types.ClassType], ltd, klass) |
---|
82 | |
---|
83 | # vim: shiftwidth=4 tabstop=4 expandtab |
---|