コード例 #1
0
ファイル: schema.py プロジェクト: Stamped/Stamped
 def _setType(self, requiredType):
     allowed = [basestring, bool, int, long, float, dict, list, datetime]
     if requiredType in allowed:
         return requiredType
     msg = "Invalid Type (%s)" % requiredType
     logs.warning(msg)
     raise SchemaTypeError(msg)
コード例 #2
0
ファイル: schema.py プロジェクト: Stamped/Stamped
 def __setattr__(self, name, value):
     # Will work for mangled private variables too
     if name[:1] == '_':
         object.__setattr__(self, name, value)
     
     elif isinstance(value, SchemaElement):
         try:
             self._elements[name] = value
             self._elements[name]._name = name
             # DEMO: create weakref
             self._elements[name]._parent = weakref.ref(self)
         except:
             msg = "Cannot Add Element (%s)" % name
             logs.warning(msg)
             raise SchemaKeyError(msg)
     
     else:
         if name in self._elements:
             self._elements[name].setElement(name, value)
         else:
             try:
                 if len(self._contents(name)) == 1:
                     for k, v in self._elements.iteritems():
                         if isinstance(v, Schema) and 1 == len(v._contents(name)):
                             v[name] = value
                             v.setIsSet(True)
                     
                     return
             except:
                 pass
             
             msg = "Cannot Set Element (%s)" % name
             #logs.warning(msg)
             raise SchemaKeyError(msg)
コード例 #3
0
ファイル: schema.py プロジェクト: Stamped/Stamped
 def exportSchema(self, schema):
     try:
         schema.importData(self.value, overflow=True)
         return schema
     except:
         msg = "Conversion failed (Define in subclass?)"
         logs.warning(msg)
         raise SchemaValidationError(msg)
コード例 #4
0
ファイル: schema.py プロジェクト: Stamped/Stamped
 def validate(self):
     if len(self._elements) == 0 and self._required == True:
         msg = "Required Schema Empty (%s)" % self._name
         logs.warning(msg)
         raise SchemaValidationError(msg)
     
     for k, v in self._elements.iteritems():
         if v.isSet:
             v.validate()
コード例 #5
0
ファイル: utils.py プロジェクト: Stamped/Stamped
def printException():
    """
        Simple debug utility to print a stack trace.
    """
    #traceback.print_exc()
    
    #traceback.print_exception(exc_type, exc_value, exc_traceback,
    #                          limit=8, file=sys.stderr)
    logs.warning(getFormattedException())
コード例 #6
0
ファイル: schema.py プロジェクト: Stamped/Stamped
 def __contains__(self, item):
     ret = self._contents(item)
     
     if len(ret) == 1:
         return ret[0]._isSet
     elif len(ret) == 0:
         return False
     else:
         msg = "Multiple Keys Exist (%s)" % item
         logs.warning(msg)
         raise SchemaKeyError(msg)
コード例 #7
0
ファイル: schema.py プロジェクト: Stamped/Stamped
    def validate(self):
        if len(self._data) == 0 and self._required == True:
            msg = "Required List Empty (%s)" % self._name
            logs.warning(msg)
            raise SchemaValidationError(msg)

        for item in self._data:
            if not isinstance(item, SchemaElement):
                msg = "Invalid List Element (%s)" % self._element
                logs.warning(msg)
                raise SchemaTypeError(msg)
            item.validate()
コード例 #8
0
ファイル: schema.py プロジェクト: Stamped/Stamped
 def validate(self):
     if self.value == None and self._required == True:
         msg = "Required field empty (%s)" % self._name
         logs.warning(msg)
         raise SchemaValidationError(msg)
     
     if self._data != None \
         and not isinstance(self._data, self._requiredType):
         msg = "Incorrect type (%s)" % self._name
         logs.warning(msg)
         raise SchemaKeyError(msg)
     
     return True
コード例 #9
0
ファイル: schema.py プロジェクト: Stamped/Stamped
    def setElement(self, name, value):
        try:
            msg = "Set Element Failed (%s)" % name

            # Convert empty strings
            ### TODO: Do we want this functionality?
            # if value == '':
            #     value = None

            if value == None and self._default != None:
                value = self._default

            # Type checking
            if value != None and not isinstance(value, self._requiredType):

                if isinstance(value, dict):
                    msg = "Cannot set dictionary as value (%s)" % name
                    logs.warning(msg)
                    raise SchemaTypeError(msg)
                elif isinstance(value, list):
                    msg = "Cannot set list as value (%s)" % value
                    logs.warning(msg)
                    raise SchemaTypeError(msg)
                elif self._requiredType == bool:
                    b = str(value).lower()
                    if b == 'true' or b == '1':
                        value = True
                    elif b == 'false' or b == '0':
                        value = False
                elif self._requiredType == basestring:
                    value = str(value)
                elif self._requiredType == float:
                    value = float(value)
                elif self._requiredType == int:
                    value = int(value)

                if not isinstance(value, self._requiredType):
                    msg = "Incorrect type (%s)" % name
                    logs.warning(msg)
                    raise SchemaKeyError(msg)
            
            # Case
            if self._case:
                if self._case == 'upper':
                    value = str(value).upper()
                elif self._case == 'lower':
                    value = str(value).lower()
            
            if self._normalize:
                value = normalize(value)
            
            self._name  = name
            self._data  = value
            self.setIsSet(True)
            self.validate()
        except:
            logs.warning(msg)
            raise
コード例 #10
0
ファイル: schema.py プロジェクト: Stamped/Stamped
 def exportSparse(self):
     ret = {}
     for k, v in self._elements.iteritems():
         if isinstance(v, Schema):
             data = v.exportSparse()
             if len(data) > 0:
                 ret[k] = data
         elif isinstance(v, SchemaList):
             if len(v) > 0:
                 ret[k] = v.value
         elif isinstance(v, SchemaElement):
             # if v.isSet == True:
             if v.value != None:
                 ret[k] = v.value
         else:
             msg = "Unrecognized Element (%s)" % k
             logs.warning(msg)
             raise SchemaTypeError(msg)
     return ret
コード例 #11
0
ファイル: schema.py プロジェクト: Stamped/Stamped
    def _import(self, item):
        element = self._element
        if isinstance(item, SchemaElement):
            return item

        elif isinstance(element, Schema) or isinstance(element, SchemaList):
            newSchemaItem = copy.deepcopy(element)
            newSchemaItem._overflow = self._overflow
            newSchemaItem.importData(item)
            return newSchemaItem

        elif isinstance(element, SchemaElement):
            newSchemaElement = copy.deepcopy(element)
            newSchemaElement.setElement('e', item)
            return newSchemaElement

        else:
            msg = "Invalid List Element (%s)" % element
            logs.warning(msg)
            raise SchemaTypeError(msg)
コード例 #12
0
ファイル: schema.py プロジェクト: Stamped/Stamped
    def importData(self, data):
        # Make sure there's something to import
        if data == None or len(data) == 0:
            return

        # Use delimiter if set and if data not already a list
        if self._delimiter != None and isinstance(data, basestring):
            try:
                data = data.split(self._delimiter)
            except:
                msg = "Invalid Delimiter for Data (%s)" % data
                logs.warning(msg)
                raise SchemaValidationError(msg)

        # Ensure that data is a valid list
        if not isinstance(data, list):
            msg = "Incorrect List Input (%s)" % data
            logs.warning(msg)
            raise SchemaTypeError(msg)

        data = copy.copy(data)

        # Ensure that element is set properly
        if not isinstance(self._element, SchemaElement):
            msg = "Invalid List Element (%s)" % self._element
            logs.warning(msg)
            raise SchemaTypeError(msg)
        element = self._element

        # Append data
        for item in data:
            self.append(item)

        self.setIsSet(True)
コード例 #13
0
ファイル: schema.py プロジェクト: Stamped/Stamped
 def __getattr__(self, name):
     if name[:1] == '_':
         return SchemaElement.__getattr__(self, name)
     
     def _returnOutput(item):
         if isinstance(item, Schema) or isinstance(item, SchemaList):
             item._parent = weakref.ref(self)
             return item
         return item.value
     
     if name in self._elements:
         return _returnOutput(self._elements[name])
     
     try:
         result = self._contents(name)
         if len(result) != 1:
             raise
         return _returnOutput(result[0])
     except:
         msg = "Cannot Get Element (%s)" % name
         logs.warning(msg)
         raise SchemaKeyError(msg)
コード例 #14
0
ファイル: utils.py プロジェクト: Stamped/Stamped
def sendEmail(msg, **kwargs):
    if not validate_email(msg['to']):
        msg = "Invalid email address"
        logs.warning(msg)
        raise Exception(msg)
    
    format = kwargs.pop('format', 'text')
    
    try:
        ses = boto.connect_ses(keys.aws.AWS_ACCESS_KEY_ID, keys.aws.AWS_SECRET_KEY)
        ses.send_email(msg['from'], msg['subject'], msg['body'], msg['to'], format=format)
    except Exception as e:
        logs.warning('EMAIL FAILED: %s' % msg)
        logs.warning(e)
    
    return True
コード例 #15
0
ファイル: schema.py プロジェクト: Stamped/Stamped
 def _importData(self, data, **kwargs):
     # Wipe all contents if not set in data
     clear = kwargs.pop('clear', True)
     
     if isinstance(data, Schema):
         data = data.value
     
     if not isinstance(data, dict) and data != None:
         msg = "Invalid Type (data=%s) (type=%s)" % (data, type(data))
         logs.warning(msg)
         logs.warning("Schema: %s" % self)
         raise SchemaTypeError(msg)
     
     ret = {}
     data = copy.copy(data)
     derivatives = []
     
     for k, v in self._elements.iteritems():
         item  = None
         isSet = False
         
         if data != None and k in data:
             item  = data.pop(k)
             isSet = True
         
         # Dictionary or List
         if isinstance(v, Schema) or isinstance(v, SchemaList):
             v._overflow = self._overflow
             
             if item == None:
                 if v._required == True:
                     msg = "Missing Nested Element (%s)" % k
                     logs.warning(msg)
                     raise SchemaValidationError(msg)
             
             if clear:
                 v.setElement(k, item)
             else:
                 v.importData(item)
         
         # Value
         elif isinstance(v, SchemaElement):
             if isSet:
                 v.setElement(k, item)
             elif v._derivedFrom != None:
                 # Wait until everything else has been set
                 derivatives.append(v)
             elif clear:
                 v._clearElement()
             else:
                 v.validate()
         
         else:
             msg = "Unrecognized Element (%s)" % k
             logs.warning(msg)
             raise SchemaTypeError(msg)
     
     # Attempt to set any derivative values
     for element in derivatives:
         try:
             inputValue = self._elements[element._derivedFrom].value
             element.setElement(element._name, \
                                 element._derivedFn(inputValue))
         except:
             msg = "Unable to derive (%s)" % element._name
             logs.warning(msg)
             raise SchemaValidationError(msg)
     
     # Fail if excess data exists
     if data != None and len(data) > 0 and self._overflow == False:
         msg = "Unknown Field: %s" % data
         logs.warning(msg)
         raise SchemaValidationError(msg)
     
     self.setIsSet(True)