Ejemplo n.º 1
0
    def check_match(self, obj):
        try:
            tag = obj[self._tagName]
        except KeyError:
            if self._tagOpt:
                return
            else:
                raise tuf.FormatException("Missing tag %s on object" %
                                             self._tagName)
        except TypeError:
            raise tuf.FormatException("Got a %s, not a tagged object" %
                                         type(obj))
        if not isinstance(tag, basestring):
            raise tuf.FormatException("Expected a string for %s; got a %s" % (
                    self._tagName, type(tag)))
        try:
            subschema = self._tagvals[tag]
        except KeyError:
            if self._ignoreOthers:
                return
            else:
                raise tuf.FormatException("Unrecognized value %s for %s" % (
                        tag, self._tagName))

        subschema.check_match(obj)
Ejemplo n.º 2
0
 def check_match(self, obj):
     if isinstance(obj, bool) or not isinstance(obj, (int, long)):
         # We need to check for bool as a special case, since bool
         # is for historical reasons a subtype of int.
         raise tuf.FormatException("Got %r instead of an integer" % obj)
     elif not (self._lo <= obj <= self._hi):
         raise tuf.FormatException("%r not in range [%r,%r]"
                                      % (obj, self._lo, self._hi))
Ejemplo n.º 3
0
 def check_match(self, obj):
     if not isinstance(obj, (list, tuple)):
         raise tuf.FormatException("Expected %s; got %r"
                                      % (self._listName, obj))
     for item in obj:
         try:
             self._schema.check_match(item)
         except tuf.FormatException, e:
             raise tuf.FormatException("%s in %s" % (e, self._listName))
Ejemplo n.º 4
0
 def check_match(self, obj):
     if not isinstance(obj, (list, tuple)):
         raise tuf.FormatException("Expected %s; got %r"
                                      % (self._structName, obj))
     elif len(obj) < self._min:
         raise tuf.FormatException(
             "Too few fields in %s" % self._structName)
     elif len(obj) > len(self._subschemas) and not self._allowMore:
         raise tuf.FormatException(
             "Too many fields in %s" % self._structName)
     for item, schema in zip(obj, self._subschemas):
         schema.check_match(item)
Ejemplo n.º 5
0
    def check_match(self, obj):
        try:
            iter = obj.iteritems()
        except AttributeError:
            raise tuf.FormatException("Expected a dict; got %r" % obj)

        for k, v in iter:
            self._keySchema.check_match(k)
            self._valSchema.check_match(v)
Ejemplo n.º 6
0
    def check_match(self, obj):
        if not isinstance(obj, dict):
            raise tuf.FormatException("Wanted a %s; did not get a dict" %
                                         self._objname)

        for k, schema in self._required:
            try:
                item = obj[k]
            except KeyError:
                if not isinstance(schema, Opt):
                    raise tuf.FormatException("Missing key %s in %s"
                                                 % (k, self._objname))

            else:
                try:
                    schema.check_match(item)
                except tuf.FormatException, e:
                    raise tuf.FormatException("%s in %s.%s"
                                                 % (e, self._objname, k))
Ejemplo n.º 7
0
class ListOf(Schema):
    """
       Matches a homogenous list of some subschema.

       >>> s = ListOf(RE("(?:..)*"))
       >>> s.matches("hi")
       False
       >>> s.matches([])
       True
       >>> s.matches({})
       False
       >>> s.matches(["Hi", "this", "list", "is", "full", "of", "even", "strs"])
       True
       >>> s.matches(["This", "one", "is not"])
       False

       >>> s = ListOf(Int(), minCount=3, maxCount=10)
       >>> s.matches([3]*2)
       False
       >>> s.matches([3]*3)
       True
       >>> s.matches([3]*10)
       True
       >>> s.matches([3]*11)
       False
    """

    def __init__(self, schema, minCount=0, maxCount=sys.maxint, listName="list"):
        self._schema = schema
        self._minCount = minCount
        self._maxCount = maxCount
        self._listName = listName

    def check_match(self, obj):
        if not isinstance(obj, (list, tuple)):
            raise tuf.FormatException("Expected %s; got %r"
                                         % (self._listName, obj))
        for item in obj:
            try:
                self._schema.check_match(item)
            except tuf.FormatException, e:
                raise tuf.FormatException("%s in %s" % (e, self._listName))

        if not (self._minCount <= len(obj) <= self._maxCount):
            raise tuf.FormatException("Length of %s out of range"
                                         % self._listName)
Ejemplo n.º 8
0
 def check_match(self, obj):
     if self._str != obj:
         raise tuf.FormatException("Expected %r; got %r" % (self._str, obj))
Ejemplo n.º 9
0
 def check_match(self, obj):
     if not isinstance(obj, basestring) or not self._re.match(obj):
         raise tuf.FormatException("%r did not match %s"
                                      % (obj, self._reName))
Ejemplo n.º 10
0
 def check_match(self, obj):
     if self._base:
         self._base.check_match(obj)
     r = self._fn(obj)
     if r is False:
         raise tuf.FormatException("%s returned False" % self._fn)
Ejemplo n.º 11
0
 def check_match(self, obj):
     if not isinstance(obj, bool):
         raise tuf.FormatException("Got %r instead of a boolean" % obj)
Ejemplo n.º 12
0
    def check_match(self, obj):
        for m in self._subschemas:
            if m.matches(obj):
                return

        raise tuf.FormatException("Object matched no recognized alternative")
Ejemplo n.º 13
0
 def check_match(self, obj):
     if not isinstance(obj, basestring):
         raise tuf.FormatException("Expected a string; got %r" % obj)