Rename pyTivoConfigurator from .py to .pyw -- keeps it from opening an unneeded termi...
[pyTivo.git] / Cheetah / Utils / VerifyType.py
blob6c52e09580d1dc3fc11fc95c24b736c8ccaa43dc
1 #!/usr/bin/env python
2 # $Id: VerifyType.py,v 1.4 2005/11/02 22:26:08 tavis_rudd Exp $
3 """Functions to verify an argument's type
5 Meta-Data
6 ================================================================================
7 Author: Mike Orr <iron@mso.oz.net>
8 License: This software is released for unlimited distribution under the
9 terms of the MIT license. See the LICENSE file.
10 Version: $Revision: 1.4 $
11 Start Date: 2001/11/07
12 Last Revision Date: $Date: 2005/11/02 22:26:08 $
13 """
14 __author__ = "Mike Orr <iron@mso.oz.net>"
15 __revision__ = "$Revision: 1.4 $"[11:-2]
17 ##################################################
18 ## DEPENDENCIES
20 import types # Used in VerifyTypeClass.
22 ##################################################
23 ## PRIVATE FUNCTIONS
25 def _errmsg(argname, ltd, errmsgExtra=''):
26 """Construct an error message.
28 argname, string, the argument name.
29 ltd, string, description of the legal types.
30 errmsgExtra, string, text to append to error mssage.
31 Returns: string, the error message.
32 """
33 if errmsgExtra:
34 errmsgExtra = '\n' + errmsgExtra
35 return "arg '%s' must be %s%s" % (argname, ltd, errmsgExtra)
38 ##################################################
39 ## TYPE VERIFICATION FUNCTIONS
41 def VerifyType(arg, argname, legalTypes, ltd, errmsgExtra=''):
42 """Verify the type of an argument.
44 arg, any, the argument.
45 argname, string, name of the argument.
46 legalTypes, list of type objects, the allowed types.
47 ltd, string, description of legal types (for error message).
48 errmsgExtra, string, text to append to error message.
49 Returns: None.
50 Exceptions: TypeError if 'arg' is the wrong type.
51 """
52 if type(arg) not in legalTypes:
53 m = _errmsg(argname, ltd, errmsgExtra)
54 raise TypeError(m)
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.
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)
76 # @@MO: Commented until we determine whether it's useful.
77 #def VerifyClass(arg, argname, klass, ltd):
78 # """Same, but allow *only* a subclass of the right class.
79 # """
80 # VerifyTypeClass(arg, argname, [types.ClassType], ltd, klass)
82 # vim: shiftwidth=4 tabstop=4 expandtab