Example #1
0
 def _build(self, obj, stream, context):
     if not isinstance(obj, collections.Sequence):
         raise RangeError("expected sequence type, found %s" % type(obj))
     if len(obj) < self.mincount or len(obj) > self.maxcount:
         raise RangeError("expected %d to %d, found %d" %
                          (self.mincount, self.maxcount, len(obj)))
     cnt = 0
     try:
         if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
             for subobj in obj:
                 if isinstance(obj, bytes):
                     subobj = bchr(subobj)
                 self.subcon._build(subobj, stream, context.__copy__())
                 cnt += 1
         else:
             for subobj in obj:
                 if isinstance(obj, bytes):
                     subobj = bchr(subobj)
                 self.subcon._build(subobj, stream, context)
                 cnt += 1
     except ConstructError:
         if cnt < self.mincount:
             raise RangeError(
                 "expected %d to %d, found %d" %
                 (self.mincount, self.maxcount, len(obj)),
                 sys.exc_info()[1])
Example #2
0
 def _build(self, obj, stream, context):
     if len(obj) < self.mincount or len(obj) > self.maxcout:
         raise RangeError("expected %d to %d, found %d" %
             (self.mincount, self.maxcout, len(obj)))
     cnt = 0
     try:
         if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
             for subobj in obj:
                 if isinstance(obj, bytes):
                     subobj = bchr(subobj)
                 self.subcon._build(subobj, stream, context.__copy__())
                 cnt += 1
         else:
             for subobj in obj:
                 if isinstance(obj, bytes):
                     subobj = bchr(subobj)
                 self.subcon._build(subobj, stream, context)
                 cnt += 1
     except ConstructError:
         if cnt < self.mincount:
             raise RangeError("expected %d to %d, found %d" %
                 (self.mincount, self.maxcout, len(obj)), sys.exc_info()[1])
Example #3
0
 def _build(self, obj, stream, context):
     if len(obj) < self.mincount or len(obj) > self.maxcout:
         raise RangeError("expected %d to %d, found %d" %
             (self.mincount, self.maxcout, len(obj)))
     cnt = 0
     try:
         if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
             for subobj in obj:
                 if isinstance(obj, bytes):
                     subobj = bchr(subobj)
                 self.subcon._build(subobj, stream, context.__copy__())
                 cnt += 1
         else:
             for subobj in obj:
                 if isinstance(obj, bytes):
                     subobj = bchr(subobj)
                 self.subcon._build(subobj, stream, context)
                 cnt += 1
     except ConstructError as ex:
         if cnt < self.mincount:
             raise RangeError("expected %d to %d, found %d" %
                 (self.mincount, self.maxcout, len(obj)), ex)
Example #4
0
 def _build(self, obj, stream, context):
     self.start_quote._build(None, stream, context)
     if self.encoding:
         obj = obj.encode(self.encoding)
     for ch in obj:
         ch = bchr(ch)
         if ch == self.esc_char:
             self.char._build(self.esc_char, stream, context)
         elif ch == self.end_quote:
             if self.esc_char is None:
                 raise QuotedStringError("found ending quote in data, "
                     "but no escape char defined", ch)
             else:
                 self.char._build(self.esc_char, stream, context)
         self.char._build(ch, stream, context)
     self.char._build(self.end_quote, stream, context)
Example #5
0
 def _build(self, obj, stream, context):
     terminated = False
     if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
         for subobj in obj:
             self.subcon._build(subobj, stream, context.__copy__())
             if self.predicate(subobj, context):
                 terminated = True
                 break
     else:
         for subobj in obj:
             subobj = bchr(subobj)
             self.subcon._build(subobj, stream, context.__copy__())
             if self.predicate(subobj, context):
                 terminated = True
                 break
     if not terminated:
         raise ArrayError("missing terminator")
Example #6
0
 def _build(self, obj, stream, context):
     _write_stream(stream, self.length, bchr(obj) if isinstance(obj, int) else obj)
Example #7
0
 def _build(self, obj, stream, context):
     _write_stream(stream, self.length, bchr(obj) if isinstance(obj, int) else obj)
Example #8
0
class IdentifierAdapter(Adapter):
    """
    Adapter for programmatic identifiers

    Parameters:
    * subcon - the subcon to adapt
    """
    def _encode(self, obj, context):
        return obj[0], obj[1:]
    def _decode(self, obj, context):
        return obj[0] + b"".join(obj[1])


_default_identifier_headset = set()
for c in b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_":
    _default_identifier_headset.add(bchr(c))

_default_identifier_tailset = set()
for c in b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_":
    _default_identifier_tailset.add(bchr(c))


def Identifier(name,
               headset=_default_identifier_headset,
               tailset=_default_identifier_tailset
    ):
    """a programmatic identifier (symbol). must start with a char of headset,
    followed by a sequence of tailset characters
    * name - the name of the field
    * headset - charset for the first character. default is A-Z, a-z, and _
    * tailset - charset for the tail. default is A-Z, a-z, 0-9 and _