Trying to better follow DRY
[ejpi] / src / history.py
index 999a0be..dcd04fa 100644 (file)
@@ -1,10 +1,10 @@
 #!/usr/bin/env python
 
 
+import re
 import weakref
-import warnings
 
-from libraries.recipes import algorithms
+from util import algorithms
 import operation
 
 
@@ -15,6 +15,15 @@ __BASE_MAPPINGS = {
 }
 
 
+_VARIABLE_VALIDATION_RE = re.compile("^[a-zA-Z0-9]+$")
+
+
+def validate_variable_name(variableName):
+       match = _VARIABLE_VALIDATION_RE.match(variableName)
+       if match is None:
+               raise RuntimeError("Invalid characters in '%s'" % variableName)
+
+
 def parse_number(userInput):
        try:
                base = __BASE_MAPPINGS.get(userInput[0:2], 10)
@@ -102,6 +111,7 @@ class RpnCalcHistory(object):
 
        def __init__(self, history, entry, errorReporting, constants, operations):
                self.history = history
+               self.history._parse_value = self._parse_value
                self.__entry = weakref.ref(entry)
 
                self.__errorReporter = errorReporting
@@ -127,12 +137,6 @@ class RpnCalcHistory(object):
                self.__entry().clear()
 
        def push_entry(self):
-               """
-               @todo Add operation duplication.  If value is empty, peek at the top
-                       item.  If it has children, grab the last one, push it and reapply the
-                       operation.  If there are no children then just duplicate the item
-               """
-
                value = self.__entry().get_value()
 
                valueNode = None
@@ -150,7 +154,7 @@ class RpnCalcHistory(object):
                        node = self._apply_operation(Node)
                        return node
                except StandardError, e:
-                       self.errorReporter.push_exception(e)
+                       self.errorReporter.push_exception()
                        return None
 
        def serialize_stack(self):
@@ -159,7 +163,6 @@ class RpnCalcHistory(object):
                        for stackNode in self.history
                )
                serialized = list(serialized)
-               serialized.reverse()
                return serialized
 
        def deserialize_stack(self, data):
@@ -184,6 +187,7 @@ class RpnCalcHistory(object):
                except KeyError:
                        pass
 
+               validate_variable_name(userInput)
                return operation.Variable(userInput)
 
        def _apply_operation(self, Node):