00001 import string
00002 import re
00003
00004 from cdbVisCore import *
00005 from MyExceptions import *
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 def ValidateString(name,thestring,maxlength=-1,allowzerolength=False,regexprmask=""):
00028
00029 thestring = str(thestring).strip()
00030
00031
00032 if maxlength != -1:
00033 if len(thestring) > maxlength:
00034 raise StringError, str(name) + " : The length of the text " + str(len(thestring)) + " is longer than the defined maximum size (" + str(maxlength) + ")"
00035
00036
00037 if allowzerolength==False:
00038 if len(thestring) == 0:
00039 raise StringError, str(name) + ": Zero length text not allowed here."
00040
00041
00042 if regexprmask != "":
00043 rematch = re.compile(regexprmask)
00044 if rematch.match(thestring) != True:
00045 raise StringError, str(name) + ": The input given is not syntactically correct."
00046
00047
00048 return thestring
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 def ValidateNumber(name,thenumber,minvalue=None,maxvalue=None,allowdecimal=False):
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 if allowdecimal:
00075 if type(thenumber) != type(1) and type(thenumber) != type(1.0):
00076 raise NumberError, str(name) + ": Illegal input. Not a legal number. Integers and floats allowed only."
00077 else:
00078 if type(thenumber) != type(1):
00079 raise NumberError, str(name) + ": Illegal input. Not a integer. Integers allowed only."
00080
00081 if minvalue != None:
00082 if thenumber < minvalue:
00083 raise NumberError, str(name) + ": Illegal input. Smallest value allowed is " + str(minvalue) + "."
00084 if maxvalue != None:
00085 if thenumber > maxvalue:
00086 raise NumberError, str(name) + ": Illegal input. Biggest value allowed is " + str(maxvalue) + "."
00087
00088
00089 return True
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 def ValidateSystem(name,thesystem):
00102
00103 validsystems = allvalidsystems
00104
00105 if thesystem in validsystems:
00106 return thesystem
00107 else:
00108 if string.find(thesystem,",",0) != -1:
00109 thesystem = thesystem.split(",")
00110 for system in thesystem:
00111 if system not in validsystems:
00112 raise SystemNameError, str(name) + ": The given input is not a valid list of subsystems."
00113
00114 else:
00115 raise SystemNameError, str(name) + ": The given input is not a valid system name."
00116
00117