#!/usr/bin/python
from Tkinter import *
from p_ggen import thanUnicode
from thanwidstrans import T
##############################################################################
##############################################################################
class ThanValidator:
"Common validation code."
def __init__(self, title=""):
"Just set error messages to no error."
self.thanClearErr()
def thanValidate(self, v):
"Validate the value v and return the correct value."
return v
def thanGet(self, v):
"Try to return a correct value without validation."
return v
def thanSetErr(self, i, t):
"Set error messages to given error."
self.thanIerror = i
self.thanTerror = t
def thanClearErr(self):
"Set error messages to no error."
self.thanIerror = 0
self.thanTerror = ""
def thanGetErr(self):
"Return the text error message."
return self.thanTerror
def thanGetIerr(self):
"Return the code of the error message."
return self.thanIerror
class ThanValUni(ThanValidator):
"Validate unicode text."
def thanValidate(self, v):
"Validate the value v and return the correct value."
return thanUnicode(v)
def thanGet(self, v):
"Try to return the correct value."
return thanUnicode(v)
class ThanValBlank(ThanValUni):
"Validate that (unicode) text is non blank."
def thanValidate(self, v):
"Validate the value v and return the correct value."
v = ThanValUni.thanValidate(self, v)
if len(v.strip()) > 0: return v
self.thanSetErr(1, "Non blank unicode string was expected.")
return None
##############################################################################
##############################################################################
class ThanValFloat(ThanValidator):
"Get and validate a float number."
def num(self, a): return float(a)
tnumexp = "A float was expected."
tnumbounds = "bounds should be a tuple of float values."
tnumbet = "A float was expected between"
def __init__(self, vmin=None, vmax=None):
ThanValidator.__init__(self)
self.thanBounds = vmin, vmax
if self.thanBounds != (None, None):
try: self.thanBounds = [self.num(self.thanBounds[i]) for i in (0,1)]
except ValueError, IndexError: raise ValueError, self.tnumbounds
def thanValidate(self, v):
try: v = self.num(v)
except ValueError:
self.thanSetErr(1, self.tnumexp)
return None
if self.thanBounds != (None, None):
if v < self.thanBounds[0] or v > self.thanBounds[1]:
v = [str(self.thanBounds[i]) for i in (0,1)]
v.insert(0, self.tnumbet)
self.thanSetErr(1, " ".join(v))
return None
self.thanClearErr()
return v
def thanGet(self, v):
try: v = self.num(v)
except: pass
return v
class ThanValFloatFortran(ThanValFloat):
"Get and validate a float number and checks for Fortran idiosyngracies."
def thanValidate(self, v):
"Check if exponenet is with D instead of E."
v1 = ThanValFloat.thanValidate(self, v)
print "fortran:", v1
if v1 != None: return v1
try: v+""; v.replace
except: return None # Not stringlike enough
print "TRYING FORTRAN:'", v
v = v.replace("d", "e")
v = v.replace("D", "e")
print "after:", v
return ThanValFloat.thanValidate(self, v)
##############################################################################
##############################################################################
class ThanValInt(ThanValFloat, Entry):
"Get and validate an integer number."
def num(self, a):
return int(a)
tnumexp = "An integer was expected."
tnumbounds = "bounds should be a tuple of integer values."
tnumbet = "An integer was expected between"
##############################################################################
##############################################################################
if __name__ == "__main__" and 1:
def validates():
print "a=", b.thanValidate(a.thanGet()),
print b.thanGetErr(), b.thanGetIerr()
root = Tk()
from p_gtkwid import ThanEntry
a = ThanEntry(root, bg="green")
b = ThanValInt(0, 1)
a.grid()
but = Button(root, text="Validate", command=validates)
but.grid()
root.mainloop()
|