C:/mesDocs/QT_confDbVis_canvas/Validation.py

Go to the documentation of this file.
00001 import string   #string module
00002 import re       #regular expressions module
00003 
00004 from cdbVisCore import *
00005 from MyExceptions import *
00006 
00007 ##
00008 #  This module is a control/validation module with all the different validations on both user 
00009 # input and database input for all different data types. To prevent from trying to add "illegal"
00010 # data to the database.
00011 # 
00012 
00013 ##
00014 #  Validates string input to the given conditions.
00015 #     
00016 #       Parameters:
00017 #       @name - to be used in exception handling, identification that will be used to the output
00018 #               to the user so he/she will understand what/where went wrong.
00019 #       @thestring - string to be validated
00020 #       @maxlength - the max length of the string, -1 means no limit
00021 #       @allowzerolength - if the string can have length of zero; boolean true or false
00022 #       @regexprmask - a python regular expression mask, if the string needs to have a special syntax 
00023 #       
00024 #       !Return:
00025 #       If the string satisfy the given conditions it is returned, else and exception is thrown.
00026 #     
00027 def ValidateString(name,thestring,maxlength=-1,allowzerolength=False,regexprmask=""):
00028 
00029     thestring = str(thestring).strip() #strip leading and trailing white space
00030 
00031     #Length test
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     #Zero length test
00037     if allowzerolength==False:
00038         if len(thestring) == 0:
00039             raise StringError, str(name) + ": Zero length text not allowed here."
00040 
00041     #Mask test
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     #Our string went through all given tests
00048     return thestring
00049 
00050 ##
00051 #  Validates a number with the given constraints.
00052 #     
00053 #       Input:
00054 #       @minvalue - the smallest value allowed
00055 #       @maxvalue - the biggest value allowed
00056 #       @allowdecimal - whether decimal (float) numbers are allowed or not
00057 #       
00058 #       !Return:
00059 #       True is returned if validation was successful, else we throw an exception.
00060 #     
00061 def ValidateNumber(name,thenumber,minvalue=None,maxvalue=None,allowdecimal=False):
00062 
00063 
00064     #Check if we have a number and not a string, boolean etc.
00065     #if allowdecimal == False:
00066     #matchstring = "r'^\-?\d+$'" #negative or positive integer
00067     #else:
00068     #matchstring = "r'^\-?\d+(\.\d+)?$'" #negative or positive decimal or integer
00069 
00070     #rematch = re.compile(matchstring)
00071     #if rematch.match(thenumber) != True:
00072     #return False
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     #Yeah, this is the number we want
00089     return True
00090 
00091 ##
00092 #  Test if the systemname is valid, we have 12 different names to test against;
00093 #         set in the cdbVisCore. Case-sensitive.
00094 #       
00095 #       Parameters:
00096 #       @thesystem - name of the system
00097 #       
00098 #       !Return:
00099 #       The name of the system itself if legal, or an exception is thrown otherwise
00100 #     
00101 def ValidateSystem(name,thesystem):
00102 
00103     validsystems = allvalidsystems #allvalidsystems is set in cdbVisCore module
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 

Generated on Fri Aug 31 11:11:17 2007 for CDBVis by  doxygen 1.5.3