ForceVariableDialog return a python value instead of string
authorlaurent
Mon, 07 Dec 2009 21:36:38 +0100
changeset 475 9ca03fdff80f
parent 474 a07115f79c27
child 476 2e09d2e86a35
ForceVariableDialog return a python value instead of string
dialogs/ForceVariableDialog.py
--- a/dialogs/ForceVariableDialog.py	Mon Dec 07 21:26:36 2009 +0100
+++ b/dialogs/ForceVariableDialog.py	Mon Dec 07 21:36:38 2009 +0100
@@ -33,39 +33,35 @@
                      "D" : ["DINT", "UDINT", "REAL", "DWORD"],
                      "L" : ["LINT", "ULINT", "LREAL", "LWORD"]} 
 
-def checkbool(v):
-    return v in ["TRUE", "FALSE"]
+def gen_get_function(f):
+    def get_function(v):
+        try:
+            return f(v)
+        except:
+            return None
+    return get_function
 
-def gen_check_function(f):
-    def check_function(v):
-        try:
-            f(v)
-            return True
-        except:
-            return False
-    return check_function
+getinteger = gen_check_function(int)
+getfloat = gen_check_function(float)
+getstring = gen_check_function(str)
 
-checkinteger = gen_check_function(int)
-checkfloat = gen_check_function(float)
-checkstring = gen_check_function(str)
-
-CheckTypeValue = {"BOOL": lambda x: x in ["TRUE", "FALSE"],
-                  "SINT": checkinteger,
-                  "INT": checkinteger,
-                  "DINT": checkinteger,
-                  "LINT": checkinteger,
-                  "USINT": checkinteger,
-                  "UINT": checkinteger,
-                  "UDINT": checkinteger,
-                  "ULINT": checkinteger,
-                  "BYTE": checkinteger,
-                  "WORD": checkinteger,
-                  "DWORD": checkinteger,
-                  "LWORD": checkinteger,
-                  "REAL": checkfloat,
-                  "LREAL": checkfloat,
-                  "STRING": checkstring,
-                  "WSTRING": checkstring,}
+GetTypeValue = {"BOOL": lambda x: {"TRUE": True, "FALSE": False}.get(x, None),
+                "SINT": getinteger,
+                "INT": getinteger,
+                "DINT": getinteger,
+                "LINT": getinteger,
+                "USINT": getinteger,
+                "UINT": getinteger,
+                "UDINT": getinteger,
+                "ULINT": getinteger,
+                "BYTE": getinteger,
+                "WORD": getinteger,
+                "DWORD": getinteger,
+                "LWORD": getinteger,
+                "REAL": getfloat,
+                "LREAL": getfloat,
+                "STRING": getstring,
+                "WSTRING": getstring}
 
 
 class ForceVariableDialog(wx.TextEntryDialog):
@@ -96,7 +92,7 @@
             message = wx.MessageDialog(self, _("You must type a value!"), _("Error"), wx.OK|wx.ICON_ERROR)
             message.ShowModal()
             message.Destroy()
-        elif not CheckTypeValue[self.IEC_Type](value):
+        elif GetTypeValue[self.IEC_Type](value) is None:
             message = wx.MessageDialog(self, _("Invalid value \"%s\" for \"%s\" variable!")%(value, self.IEC_Type), _("Error"), wx.OK|wx.ICON_ERROR)
             message.ShowModal()
             message.Destroy()
@@ -104,4 +100,4 @@
             self.EndModal(wx.ID_OK)
 
     def GetValue(self):
-        return self.GetSizer().GetItem(1).GetWindow().GetValue()
+        return GetTypeValue[self.IEC_Type](self.GetSizer().GetItem(1).GetWindow().GetValue())