#!/usr/bin/env python
+import re
import weakref
-import warnings
-from libraries.recipes import algorithms
+from util import algorithms
import operation
}
+_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)
raise ValueError('Cannot parse "%s" as a number' % userInput)
-class ErrorReporting(object):
-
- def push_message(self, message):
- raise NotImplementedError
-
- def push_exception(self, exception):
- self.push_message(exception.message)
- warnings.warn(exception, stacklevel=3)
-
- def pop_message(self):
- raise NotImplementedError
-
-
-class ErrorIgnore(ErrorReporting):
-
- def push_message(self, message):
- pass
-
- def pop_message(self):
- pass
-
-
-class ErrorWarning(ErrorReporting):
-
- def push_message(self, message):
- warnings.warn(message, stacklevel=2)
-
- def pop_message(self):
- pass
-
-
class AbstractHistory(object):
"""
Is it just me or is this class name begging for some jokes?
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
self.__serialRenderer = operation.render_number()
@property
- def errorReporter(self):
- return self.__errorReporter
-
- @property
def OPERATIONS(self):
return self.__operations
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
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):
for stackNode in self.history
)
serialized = list(serialized)
- serialized.reverse()
return serialized
def deserialize_stack(self, data):
except KeyError:
pass
+ validate_variable_name(userInput)
return operation.Variable(userInput)
def _apply_operation(self, Node):