Ejemplo n.º 1
0
 def _AsciiHexEncode(self, input):  # also based on piddlePDF
     "Helper function used by images"
     output = StringIO()
     for char in input:
         output.write('%02x' % ord(char))
     output.reset()
     return output.read()
Ejemplo n.º 2
0
 def _AsciiHexEncode(self, input):  # also based on piddlePDF
     "Helper function used by images"
     output = StringIO()
     for char in input:
         output.write('%02x' % ord(char))
     output.reset()
     return output.read()
Ejemplo n.º 3
0
def _AsciiHexEncode(input):
  """This is a verbose encoding used for binary data within
    a PDF file.  One byte binary becomes two bytes of ASCII."""
  "Helper function used by images"
  output = StringIO()
  for char in input:
    output.write('%02x' % ord(char))
  output.write('>')
  output.reset()
  return output.read()
Ejemplo n.º 4
0
def _AsciiHexEncode(input):
    """This is a verbose encoding used for binary data within
    a PDF file.  One byte binary becomes two bytes of ASCII."""
    "Helper function used by images"
    output = StringIO()
    for char in input:
        output.write('%02x' % ord(char))
    output.write('>')
    output.reset()
    return output.read()
Ejemplo n.º 5
0
def _AsciiHexDecode(input):
  "Not used except to provide a test of the preceding"
  #strip out all whitespace
  stripped = string.join(string.split(input), '')
  assert stripped[-1] == '>', 'Invalid terminator for Ascii Hex Stream'
  stripped = stripped[:-1]  #chop off terminator
  assert len(stripped) % 2 == 0, 'Ascii Hex stream has odd number of bytes'
  i = 0
  output = StringIO()
  while i < len(stripped):
    twobytes = stripped[i:i + 2]
    output.write(chr(eval('0x' + twobytes)))
    i = i + 2
  output.reset()
  return output.read()
Ejemplo n.º 6
0
def _AsciiHexDecode(input):
    "Not used except to provide a test of the preceding"
    #strip out all whitespace
    stripped = string.join(string.split(input),'')
    assert stripped[-1] == '>', 'Invalid terminator for Ascii Hex Stream'
    stripped = stripped[:-1]  #chop off terminator
    assert len(stripped) % 2 == 0, 'Ascii Hex stream has odd number of bytes'
    i = 0
    output = StringIO()
    while i < len(stripped):
        twobytes = stripped[i:i+2]
        output.write(chr(eval('0x'+twobytes)))
        i = i + 2
    output.reset()
    return output.read()
Ejemplo n.º 7
0
    def clean(self, data, initial=None):
        """
        Checks that the file-upload field data contains a valid image (GIF, JPG,
        PNG, possibly others -- whatever the Python Imaging Library supports).
        """
        f = super(ImageField, self).clean(data, initial)
        if f is None:
            return None
        elif not data and initial:
            return initial
        from PIL import Image

        # We need to get a file object for PIL. We might have a path or we might
        # have to read the data into memory.
        if hasattr(data, 'temporary_file_path'):
            file = data.temporary_file_path()
        else:
            if hasattr(data, 'read'):
                file = StringIO(data.read())
            else:
                file = StringIO(data['content'])

        try:
            # load() is the only method that can spot a truncated JPEG,
            #  but it cannot be called sanely after verify()
            trial_image = Image.open(file)
            trial_image.load()

            # Since we're about to use the file again we have to reset the
            # file object if possible.
            if hasattr(file, 'reset'):
                file.reset()

            # verify() is the only method that can spot a corrupt PNG,
            #  but it must be called immediately after the constructor
            trial_image = Image.open(file)
            trial_image.verify()
        except ImportError: 
            # Under PyPy, it is possible to import PIL. However, the underlying
            # _imaging C module isn't available, so an ImportError will be 
            # raised. Catch and re-raise. 
            raise
        except Exception: # Python Imaging Library doesn't recognize it as an image
            raise ValidationError(self.error_messages['invalid_image'])
        if hasattr(f, 'seek') and isinstance(f.seek, collections.Callable):
            f.seek(0)
        return f
Ejemplo n.º 8
0
    def clean(self, data, initial=None):
        """
        Checks that the file-upload field data contains a valid image (GIF, JPG,
        PNG, possibly others -- whatever the Python Imaging Library supports).
        """
        f = super(ImageField, self).clean(data, initial)
        if f is None:
            return None
        elif not data and initial:
            return initial
        from PIL import Image

        # We need to get a file object for PIL. We might have a path or we might
        # have to read the data into memory.
        if hasattr(data, 'temporary_file_path'):
            file = data.temporary_file_path()
        else:
            if hasattr(data, 'read'):
                file = StringIO(data.read())
            else:
                file = StringIO(data['content'])

        try:
            # load() is the only method that can spot a truncated JPEG,
            #  but it cannot be called sanely after verify()
            trial_image = Image.open(file)
            trial_image.load()

            # Since we're about to use the file again we have to reset the
            # file object if possible.
            if hasattr(file, 'reset'):
                file.reset()

            # verify() is the only method that can spot a corrupt PNG,
            #  but it must be called immediately after the constructor
            trial_image = Image.open(file)
            trial_image.verify()
        except ImportError:
            # Under PyPy, it is possible to import PIL. However, the underlying
            # _imaging C module isn't available, so an ImportError will be
            # raised. Catch and re-raise.
            raise
        except Exception:  # Python Imaging Library doesn't recognize it as an image
            raise ValidationError(self.error_messages['invalid_image'])
        if hasattr(f, 'seek') and isinstance(f.seek, collections.Callable):
            f.seek(0)
        return f
Ejemplo n.º 9
0
def getPDF(request):
    """Returns PDF as a binary stream."""

    if 'q' in request.GET:
            
        rml = getRML(request.GET['q'])  
    
        buf = StringIO()
        
        #create the pdf
        rml2pdf.go(rml, outputFileName=buf)
        buf.reset()
        pdfData = buf.read()
        
        #send the response
        response = HttpResponse(mimetype='application/pdf')
        response.write(pdfData)
        response['Content-Disposition'] = 'attachment; filename=output.pdf'
        return response
Ejemplo n.º 10
0
class StringIO(object):
    def __init__(self, stringio=None):
        self.encoding = None
        self.stringio_object = stringio
        if self.stringio_object is None:
            self.stringio_object = WrappedStringIO()

    def close(self):
        return self.stringio_object.close()

    def closed(self, x):
        return self.stringio_object.closed(x)

    def flush(self):
        return self.stringio_object.flush()

    def getvalue(self, use_pos=None):
        return self.stringio_object.getvalue(use_pos)

    def isatty(self):
        return self.stringio_object.isatty()

    def __next__(self):
        return next(self.stringio_object)

    def read(self, s=None):
        return self.stringio_object.read(s)

    def readline(self):
        return self.stringio_object.readline()

    def readlines(self):
        return self.stringio_object.readlines()

    def reset(self):
        return self.stringio_object.reset()

    def seek(self, position):
        return self.stringio_object.seek(position)

    def softspace(self, x, base=None):
        return self.stringio_object.softspace(x, base)

    def tell(self):
        return self.stringio_object.tell()

    def truncate(self):
        return self.stringio_object.truncate()

    def write(self, s):
        return self.stringio_object.write(s)

    def writelines(self, sequence_of_strings):
        return self.stringio_object.writelines(sequence_of_strings)
Ejemplo n.º 11
0
class TestPrefilterFrontEnd(PrefilterFrontEnd):
    
    input_prompt_template = string.Template('')
    output_prompt_template = string.Template('')
    banner = ''

    def __init__(self):
        self.out = StringIO()
        PrefilterFrontEnd.__init__(self)
        # Some more code for isolation (yeah, crazy)
        self._on_enter()
        self.out.flush()
        self.out.reset()
        self.out.truncate()

    def write(self, string, *args, **kwargs):
       self.out.write(string) 

    def _on_enter(self):
        self.input_buffer += '\n'
        PrefilterFrontEnd._on_enter(self)
Ejemplo n.º 12
0
class ISAPIInputWrapper:
    # Based on ModPythonInputWrapper in mp_wsgi_handler.py
    def __init__(self, ecb):
        self._in = StringIO()
        self._ecb = ecb
        if self._ecb.AvailableBytes > 0:
            data = self._ecb.AvailableData
            # Check if more data from client than what is in ecb.AvailableData
            excess = self._ecb.TotalBytes - self._ecb.AvailableBytes
            if excess > 0:
                extra = self._ecb.ReadClient(excess)
                data = data + extra
            self._in.write(data)
        # rewind to start
        self._in.seek(0)

    def __next__(self):
        return next(self._in)

    def read(self, size=-1):
        return self._in.read(size)

    def readline(self, size=-1):
        return self._in.readline(size)

    def readlines(self, hint=-1):
        return self._in.readlines()

    def reset(self):
        self._in.reset()

    def seek(self, *args, **kwargs):
        self._in.seek(*args, **kwargs)

    def tell(self):
        return self._in.tell()

    def __iter__(self):
        return iter(self._in.readlines())
Ejemplo n.º 13
0
class StreamCheck:
    def __init__(self):
        global streamno
        self.no = streamno
        streamno += 1
        self.buffer = StringIO()
        self.next_len, self.next_func = 1, self.read_header_len

    def read_header_len(self, s):
        if ord(s) != len(protocol_name):
            print(self.no, 'BAD HEADER LENGTH')
        return len(protocol_name), self.read_header

    def read_header(self, s):
        if s != protocol_name:
            print(self.no, 'BAD HEADER')
        return 8, self.read_reserved

    def read_reserved(self, s):
        return 20, self.read_download_id

    def read_download_id(self, s):
        if DEBUG:
            print(self.no, 'download ID ' + tohex(s))
        return 20, self.read_peer_id

    def read_peer_id(self, s):
        if DEBUG:
            print(self.no, 'peer ID' + make_readable(s))
        return 4, self.read_len

    def read_len(self, s):
        l = toint(s)
        if l > 2**23:
            print(self.no, 'BAD LENGTH: ' + str(l) + ' (' + s + ')')
        return l, self.read_message

    def read_message(self, s):
        if not s:
            return 4, self.read_len
        m = s[0]
        if ord(m) > 8:
            print(self.no, 'BAD MESSAGE: ' + str(ord(m)))
        if m == Connecter.REQUEST:
            if len(s) != 13:
                print(self.no, 'BAD REQUEST SIZE: ' + str(len(s)))
                return 4, self.read_len
            index = toint(s[1:5])
            begin = toint(s[5:9])
            length = toint(s[9:])
            print(
                self.no, 'Request: ' + str(index) + ': ' + str(begin) + '-' +
                str(begin) + '+' + str(length))
        elif m == Connecter.CANCEL:
            if len(s) != 13:
                print(self.no, 'BAD CANCEL SIZE: ' + str(len(s)))
                return 4, self.read_len
            index = toint(s[1:5])
            begin = toint(s[5:9])
            length = toint(s[9:])
            print(
                self.no, 'Cancel: ' + str(index) + ': ' + str(begin) + '-' +
                str(begin) + '+' + str(length))
        elif m == Connecter.PIECE:
            index = toint(s[1:5])
            begin = toint(s[5:9])
            length = len(s) - 9
            print(
                self.no, 'Piece: ' + str(index) + ': ' + str(begin) + '-' +
                str(begin) + '+' + str(length))
        else:
            print(self.no,
                  'Message ' + str(ord(m)) + ' (length ' + str(len(s)) + ')')
        return 4, self.read_len

    def write(self, s):
        while True:
            i = self.next_len - self.buffer.tell()
            if i > len(s):
                self.buffer.write(s)
                return
            self.buffer.write(s[:i])
            s = s[i:]
            m = self.buffer.getvalue()
            self.buffer.reset()
            self.buffer.truncate()
            x = self.next_func(m)
            self.next_len, self.next_func = x
Ejemplo n.º 14
0
    def _proxy(self, url, params=None, method=None, body=None, headers=None):
        # get query string

        params = dict(self.request.params) if params is None else params
        parsed_url = urlparse(url)
        all_params = parse_qs(parsed_url.query)

        for p in all_params:
            all_params[p] = ",".join(all_params[p])
        all_params.update(params)
        params_encoded = {}

        query_string = urlencode(all_params)

        if parsed_url.port is None:
            url = "%s://%s%s?%s" % (
                parsed_url.scheme, parsed_url.hostname,
                parsed_url.path, query_string
            )
        else:  # pragma: nocover
            url = "%s://%s:%i%s?%s" % (
                parsed_url.scheme, parsed_url.hostname, parsed_url.port,
                parsed_url.path, query_string
            )

        log.info("Send query to URL:\n%s." % url)

        if method is None:
            method = self.request.method

        # forward request to target (without Host Header)
        http = httplib2.Http()
        if headers is None:  # pragma: nocover
            headers = dict(self.request.headers)
            if parsed_url.hostname != "localhost":
                headers.pop("Host")

        headers["Cache-Control"] = "no-cache"
  
        # Other problematic headers
        for header in [
            "Content-Length",
            "Content-Location",
            "Content-Encoding",
        ]:  # pragma: no cover
            if header in headers:
                del headers[header]

        if method in ["POST", "PUT"] and body is None:  # pragma: nocover
            body = StringIO(self.request.body)

        try:
            if method in ["POST", "PUT"]:
                resp, content = http.request(
                    url, method=method, body=body, headers=headers
                )
            else:
                resp, content = http.request(
                    url, method=method, headers=headers
                )
        except Exception as e:  # pragma: nocover
            log.error(
                "Error '%s' while getting the URL:\n%s\nMethode: %s." %
                (sys.exc_info()[0], url, method)
            )

            log.error(
                "--- With headers ---\n%s" %
                "\n".join(["%s: %s" % h for h in headers.items()])
            )

            log.error("--- Exception ---")
            log.exception(e)

            if method in ["POST", "PUT"]:
                log.error("--- With body ---")
                if hasattr(body, "read"):
                    body.reset()
                    log.error(body.read())
                else:
                    log.error(body)

            raise HTTPBadGateway("See logs for detail")

        if resp.status < 200 or resp.status >= 300:  # pragma: no cover
            log.error(
                "Error '%s' in response of URL:\n%s." %
                (resp.reason, url)
            )

            log.error("Status: %i" % resp.status)
            log.error("Method: %s" % method)

            log.error(
                "--- With headers ---\n%s" %
                "\n".join(["%s: %s" % h for h in headers.items()])
            )

            if method == "POST":
                log.error("--- Query with body ---")
                if hasattr(body, "read"):
                    body.reset()
                    log.error(body.read())
                else:
                    log.error(body)

            log.error("--- Return content ---")
            log.error(content)

            raise HTTPInternalServerError("See logs for details")

        return resp, content
Ejemplo n.º 15
0
class NatCheck:
    def __init__(self, resultfunc, downloadid, peerid, ip, port, rawserver):
        self.resultfunc = resultfunc
        self.downloadid = downloadid
        self.peerid = peerid
        self.ip = ip
        self.port = port
        self.closed = False
        self.buffer = StringIO()
        self.next_len = 1
        self.next_func = self.read_header_len
        try:
            self.connection = rawserver.start_connection((ip, port), self)
            self.connection.write(chr(len(protocol_name)) + protocol_name +
                (chr(0) * 8) + downloadid)
        except socketerror:
            self.answer(False)
        except IOError:
            self.answer(False)

    def answer(self, result):
        self.closed = True
        try:
            self.connection.close()
        except AttributeError:
            pass
        self.resultfunc(result, self.downloadid, self.peerid, self.ip, self.port)

    def read_header_len(self, s):
        if ord(s) != len(protocol_name):
            return None
        return len(protocol_name), self.read_header

    def read_header(self, s):
        if s != protocol_name:
            return None
        return 8, self.read_reserved

    def read_reserved(self, s):
        return 20, self.read_download_id

    def read_download_id(self, s):
        if s != self.downloadid:
            return None
        return 20, self.read_peer_id

    def read_peer_id(self, s):
        if s != self.peerid:
            return None
        self.answer(True)
        return None

    def data_came_in(self, connection, s):
        while True:
            if self.closed:
                return
            i = self.next_len - self.buffer.tell()
            if i > len(s):
                self.buffer.write(s)
                return
            self.buffer.write(s[:i])
            s = s[i:]
            m = self.buffer.getvalue()
            self.buffer.reset()
            self.buffer.truncate()
            x = self.next_func(m)
            if x is None:
                if not self.closed:
                    self.answer(False)
                return
            self.next_len, self.next_func = x

    def connection_lost(self, connection):
        if not self.closed:
            self.closed = True
            self.resultfunc(False, self.downloadid, self.peerid, self.ip, self.port)

    def connection_flushed(self, connection):
        pass
Ejemplo n.º 16
0
def _AsciiBase85Decode(input):
    """This is not used - Acrobat Reader decodes for you - but a round
    trip is essential for testing."""
    outstream = StringIO()
    #strip all whitespace
    stripped = string.join(string.split(input),'')
    #check end
    assert stripped[-2:] == '~>', 'Invalid terminator for Ascii Base 85 Stream'
    stripped = stripped[:-2]  #chop off terminator

    #may have 'z' in it which complicates matters - expand them
    stripped = string.replace(stripped,'z','!!!!!')
    # special rules apply if not a multiple of five bytes.  
    whole_word_count, remainder_size = divmod(len(stripped), 5)
    #print '%d words, %d leftover' % (whole_word_count, remainder_size)
    assert remainder_size != 1, 'invalid Ascii 85 stream!'
    cut = 5 * whole_word_count
    body, lastbit = stripped[0:cut], stripped[cut:]
    
    for i in range(whole_word_count):
        offset = i*5
        c1 = ord(body[offset]) - 33
        c2 = ord(body[offset+1]) - 33
        c3 = ord(body[offset+2]) - 33
        c4 = ord(body[offset+3]) - 33
        c5 = ord(body[offset+4]) - 33

        num = ((85**4) * c1) + ((85**3) * c2) + ((85**2) * c3) + (85*c4) + c5    

        temp, b4 = divmod(num,256)
        temp, b3 = divmod(temp,256)
        b1, b2 = divmod(temp, 256)

        assert  num == 16777216 * b1 + 65536 * b2 + 256 * b3 + b4, 'dodgy code!'
        outstream.write(chr(b1))
        outstream.write(chr(b2))
        outstream.write(chr(b3))
        outstream.write(chr(b4))
        
    #decode however many bytes we have as usual
    if remainder_size > 0:
        while len(lastbit) < 5:
            lastbit = lastbit + '!'
        c1 = ord(lastbit[0]) - 33
        c2 = ord(lastbit[1]) - 33
        c3 = ord(lastbit[2]) - 33
        c4 = ord(lastbit[3]) - 33
        c5 = ord(lastbit[4]) - 33
        num = ((85**4) * c1) + ((85**3) * c2) + ((85**2) * c3) + (85*c4) + c5    
        temp, b4 = divmod(num,256)
        temp, b3 = divmod(temp,256)
        b1, b2 = divmod(temp, 256)
        assert  num == 16777216 * b1 + 65536 * b2 + 256 * b3 + b4, 'dodgy code!'
        #print 'decoding: %d %d %d %d %d -> %d -> %d %d %d %d' % (
        #    c1,c2,c3,c4,c5,num,b1,b2,b3,b4)

        #the last character needs 1 adding; the encoding loses
        #data by rounding the number to x bytes, and when
        #divided repeatedly we get one less
        if remainder_size == 2:
            lastword = chr(b1+1)
        elif remainder_size == 3:
            lastword = chr(b1) + chr(b2+1)
        elif remainder_size == 4:
            lastword = chr(b1) + chr(b2) + chr(b3+1)
        outstream.write(lastword)

    #terminator code for ascii 85    
    outstream.reset()
    return outstream.read()
Ejemplo n.º 17
0
def _AsciiBase85Encode(input):
    """This is a compact encoding used for binary data within
    a PDF file.  Four bytes of binary data become five bytes of
    ASCII.  This is the default method used for encoding images."""
    outstream = StringIO()
    # special rules apply if not a multiple of four bytes.  
    whole_word_count, remainder_size = divmod(len(input), 4)
    cut = 4 * whole_word_count
    body, lastbit = input[0:cut], input[cut:]
    
    for i in range(whole_word_count):
        offset = i*4
        b1 = ord(body[offset])
        b2 = ord(body[offset+1])
        b3 = ord(body[offset+2])
        b4 = ord(body[offset+3])
    
        num = 16777216 * b1 + 65536 * b2 + 256 * b3 + b4

        if num == 0:
            #special case
            outstream.write('z')
        else:
            #solve for five base-85 numbers                            
            temp, c5 = divmod(num, 85)
            temp, c4 = divmod(temp, 85)
            temp, c3 = divmod(temp, 85)
            c1, c2 = divmod(temp, 85)
            assert ((85**4) * c1) + ((85**3) * c2) + ((85**2) * c3) + (85*c4) + c5 == num, 'dodgy code!'
            outstream.write(chr(c1+33))
            outstream.write(chr(c2+33))
            outstream.write(chr(c3+33))
            outstream.write(chr(c4+33))
            outstream.write(chr(c5+33))

    # now we do the final bit at the end.  I repeated this separately as
    # the loop above is the time-critical part of a script, whereas this
    # happens only once at the end.

    #encode however many bytes we have as usual
    if remainder_size > 0:
        while len(lastbit) < 4:
            lastbit = lastbit + '\000'
        b1 = ord(lastbit[0])
        b2 = ord(lastbit[1])
        b3 = ord(lastbit[2])
        b4 = ord(lastbit[3])

        num = 16777216 * b1 + 65536 * b2 + 256 * b3 + b4

        #solve for c1..c5
        temp, c5 = divmod(num, 85)
        temp, c4 = divmod(temp, 85)
        temp, c3 = divmod(temp, 85)
        c1, c2 = divmod(temp, 85)

        #print 'encoding: %d %d %d %d -> %d -> %d %d %d %d %d' % (
        #    b1,b2,b3,b4,num,c1,c2,c3,c4,c5)
        lastword = chr(c1+33) + chr(c2+33) + chr(c3+33) + chr(c4+33) + chr(c5+33)
        #write out most of the bytes.
        outstream.write(lastword[0:remainder_size + 1])

    #terminator code for ascii 85    
    outstream.write('~>')
    outstream.reset()
    return outstream.read()
Ejemplo n.º 18
0
class Connection:
    def __init__(self, Encoder, connection, id, ext_handshake=False):
        self.Encoder = Encoder
        self.connection = connection
        self.connecter = Encoder.connecter
        self.id = id
        self.readable_id = make_readable(id)
        self.locally_initiated = (id != None)
        self.complete = False
        self.keepalive = lambda: None
        self.closed = False
        self.buffer = StringIO()
        if self.locally_initiated:
            incompletecounter.increment()
        if self.locally_initiated or ext_handshake:
            self.connection.write(chr(len(protocol_name)) + protocol_name + 
                option_pattern + self.Encoder.download_id)
        if ext_handshake:
            self.Encoder.connecter.external_connection_made += 1
            self.connection.write(self.Encoder.my_id)
            self.next_len, self.next_func = 20, self.read_peer_id
        else:
            self.next_len, self.next_func = 1, self.read_header_len
        self.Encoder.raw_server.add_task(self._auto_close, 15)

    def get_ip(self, real=False):
        return self.connection.get_ip(real)

    def get_id(self):
        return self.id

    def get_readable_id(self):
        return self.readable_id

    def is_locally_initiated(self):
        return self.locally_initiated

    def is_flushed(self):
        return self.connection.is_flushed()

    def read_header_len(self, s):
        if ord(s) != len(protocol_name):
            return None
        return len(protocol_name), self.read_header

    def read_header(self, s):
        if s != protocol_name:
            return None
        return 8, self.read_reserved

    def read_reserved(self, s):
        return 20, self.read_download_id

    def read_download_id(self, s):
        if s != self.Encoder.download_id:
            return None
        if not self.locally_initiated:
            self.Encoder.connecter.external_connection_made += 1
            self.connection.write(chr(len(protocol_name)) + protocol_name + 
                option_pattern + self.Encoder.download_id + self.Encoder.my_id)
        return 20, self.read_peer_id

    def read_peer_id(self, s):
        if not self.id:
            self.id = s
            self.readable_id = make_readable(s)
        else:
            if s != self.id:
                return None
        self.complete = self.Encoder.got_id(self)
        if not self.complete:
            return None
        if self.locally_initiated:
            self.connection.write(self.Encoder.my_id)
            incompletecounter.decrement()
        c = self.Encoder.connecter.connection_made(self)
        self.keepalive = c.send_keepalive
        return 4, self.read_len

    def read_len(self, s):
        l = toint(s)
        if l > self.Encoder.max_len:
            return None
        return l, self.read_message

    def read_message(self, s):
        if s != '':
            self.connecter.got_message(self, s)
        return 4, self.read_len

    def read_dead(self, s):
        return None

    def _auto_close(self):
        if not self.complete:
            self.close()

    def close(self):
        if not self.closed:
            self.connection.close()
            self.sever()

    def sever(self):
        self.closed = True
        del self.Encoder.connections[self.connection]
        if self.complete:
            self.connecter.connection_lost(self)
        elif self.locally_initiated:
            incompletecounter.decrement()

    def send_message_raw(self, message):
        if not self.closed:
            self.connection.write(message)

    def data_came_in(self, connection, s):
        self.Encoder.measurefunc(len(s))
        while True:
            if self.closed:
                return
            i = self.next_len - self.buffer.tell()
            if i > len(s):
                self.buffer.write(s)
                return
            self.buffer.write(s[:i])
            s = s[i:]
            m = self.buffer.getvalue()
            self.buffer.reset()
            self.buffer.truncate()
            try:
                x = self.next_func(m)
            except:
                self.next_len, self.next_func = 1, self.read_dead
                raise
            if x is None:
                self.close()
                return
            self.next_len, self.next_func = x

    def connection_flushed(self, connection):
        if self.complete:
            self.connecter.connection_flushed(self)

    def connection_lost(self, connection):
        if connection in self.Encoder.connections:
            self.sever()
Ejemplo n.º 19
0
 def embedFigure(self, fig, attr={}):  # pyplot figure
     imgPointer = StringIO()
     fig.savefig(imgPointer, format='png')
     imgPointer.reset()
     data = imgPointer.read()
     return self.embedImage(data, attr)
Ejemplo n.º 20
0
def _AsciiBase85Encode(input):
  """This is a compact encoding used for binary data within
    a PDF file.  Four bytes of binary data become five bytes of
    ASCII.  This is the default method used for encoding images."""
  outstream = StringIO()
  # special rules apply if not a multiple of four bytes.
  whole_word_count, remainder_size = divmod(len(input), 4)
  cut = 4 * whole_word_count
  body, lastbit = input[0:cut], input[cut:]

  for i in range(whole_word_count):
    offset = i * 4
    b1 = ord(body[offset])
    b2 = ord(body[offset + 1])
    b3 = ord(body[offset + 2])
    b4 = ord(body[offset + 3])

    num = 16777216 * b1 + 65536 * b2 + 256 * b3 + b4

    if num == 0:
      #special case
      outstream.write('z')
    else:
      #solve for five base-85 numbers
      temp, c5 = divmod(num, 85)
      temp, c4 = divmod(temp, 85)
      temp, c3 = divmod(temp, 85)
      c1, c2 = divmod(temp, 85)
      assert ((85**4) * c1) + ((85**3) * c2) + ((85**2) * c3) + (85 * c4) + c5 == num, 'dodgy code!'
      outstream.write(chr(c1 + 33))
      outstream.write(chr(c2 + 33))
      outstream.write(chr(c3 + 33))
      outstream.write(chr(c4 + 33))
      outstream.write(chr(c5 + 33))

  # now we do the final bit at the end.  I repeated this separately as
  # the loop above is the time-critical part of a script, whereas this
  # happens only once at the end.

  #encode however many bytes we have as usual
  if remainder_size > 0:
    while len(lastbit) < 4:
      lastbit = lastbit + '\000'
    b1 = ord(lastbit[0])
    b2 = ord(lastbit[1])
    b3 = ord(lastbit[2])
    b4 = ord(lastbit[3])

    num = 16777216 * b1 + 65536 * b2 + 256 * b3 + b4

    #solve for c1..c5
    temp, c5 = divmod(num, 85)
    temp, c4 = divmod(temp, 85)
    temp, c3 = divmod(temp, 85)
    c1, c2 = divmod(temp, 85)

    #print 'encoding: %d %d %d %d -> %d -> %d %d %d %d %d' % (
    #    b1,b2,b3,b4,num,c1,c2,c3,c4,c5)
    lastword = chr(c1 + 33) + chr(c2 + 33) + chr(c3 + 33) + chr(c4 + 33) + chr(c5 + 33)
    #write out most of the bytes.
    outstream.write(lastword[0:remainder_size + 1])

  #terminator code for ascii 85
  outstream.write('~>')
  outstream.reset()
  return outstream.read()
Ejemplo n.º 21
0
def _AsciiBase85Decode(input):
  """This is not used - Acrobat Reader decodes for you - but a round
    trip is essential for testing."""
  outstream = StringIO()
  #strip all whitespace
  stripped = string.join(string.split(input), '')
  #check end
  assert stripped[-2:] == '~>', 'Invalid terminator for Ascii Base 85 Stream'
  stripped = stripped[:-2]  #chop off terminator

  #may have 'z' in it which complicates matters - expand them
  stripped = string.replace(stripped, 'z', '!!!!!')
  # special rules apply if not a multiple of five bytes.
  whole_word_count, remainder_size = divmod(len(stripped), 5)
  #print '%d words, %d leftover' % (whole_word_count, remainder_size)
  assert remainder_size != 1, 'invalid Ascii 85 stream!'
  cut = 5 * whole_word_count
  body, lastbit = stripped[0:cut], stripped[cut:]

  for i in range(whole_word_count):
    offset = i * 5
    c1 = ord(body[offset]) - 33
    c2 = ord(body[offset + 1]) - 33
    c3 = ord(body[offset + 2]) - 33
    c4 = ord(body[offset + 3]) - 33
    c5 = ord(body[offset + 4]) - 33

    num = ((85**4) * c1) + ((85**3) * c2) + ((85**2) * c3) + (85 * c4) + c5

    temp, b4 = divmod(num, 256)
    temp, b3 = divmod(temp, 256)
    b1, b2 = divmod(temp, 256)

    assert num == 16777216 * b1 + 65536 * b2 + 256 * b3 + b4, 'dodgy code!'
    outstream.write(chr(b1))
    outstream.write(chr(b2))
    outstream.write(chr(b3))
    outstream.write(chr(b4))

  #decode however many bytes we have as usual
  if remainder_size > 0:
    while len(lastbit) < 5:
      lastbit = lastbit + '!'
    c1 = ord(lastbit[0]) - 33
    c2 = ord(lastbit[1]) - 33
    c3 = ord(lastbit[2]) - 33
    c4 = ord(lastbit[3]) - 33
    c5 = ord(lastbit[4]) - 33
    num = ((85**4) * c1) + ((85**3) * c2) + ((85**2) * c3) + (85 * c4) + c5
    temp, b4 = divmod(num, 256)
    temp, b3 = divmod(temp, 256)
    b1, b2 = divmod(temp, 256)
    assert num == 16777216 * b1 + 65536 * b2 + 256 * b3 + b4, 'dodgy code!'
    #print 'decoding: %d %d %d %d %d -> %d -> %d %d %d %d' % (
    #    c1,c2,c3,c4,c5,num,b1,b2,b3,b4)

    #the last character needs 1 adding; the encoding loses
    #data by rounding the number to x bytes, and when
    #divided repeatedly we get one less
    if remainder_size == 2:
      lastword = chr(b1 + 1)
    elif remainder_size == 3:
      lastword = chr(b1) + chr(b2 + 1)
    elif remainder_size == 4:
      lastword = chr(b1) + chr(b2) + chr(b3 + 1)
    outstream.write(lastword)

  #terminator code for ascii 85
  outstream.reset()
  return outstream.read()
Ejemplo n.º 22
0
class OutputTrap(object):
    """ Object which can trap text sent to stdout and stderr.
    """

    def __init__(self, out=None, err=None):
        # Filelike objects to store stdout/stderr text.
        if out is None:
            self.out = StringIO()
        else:
            self.out = out
        if err is None:
            self.err = StringIO()
        else:
            self.err = err

        # Boolean to check if the stdout/stderr hook is set.
        self.out_set = False
        self.err_set = False

    @property
    def out_text(self):
        """ Return the text currently in the stdout buffer.
        """
        return self.out.getvalue()

    @property
    def err_text(self):
        """ Return the text currently in the stderr buffer.
        """
        return self.err.getvalue()

    def set(self):
        """ Set the hooks.
        """

        if sys.stdout is not self.out:
            self._out_save = sys.stdout
            sys.stdout = self.out
            self.out_set = True

        if sys.stderr is not self.err:
            self._err_save = sys.stderr
            sys.stderr = self.err
            self.err_set = True

    def unset(self):
        """ Remove the hooks.
        """

        if self.out_set:
            sys.stdout = self._out_save
        self.out_set = False

        if self.err_set:
            sys.stderr = self._err_save
        self.err_set = False

    def clear(self):
        """ Clear out the buffers.
        """

        self.out.reset()
        self.out.truncate()

        self.err.reset()
        self.err.truncate()

    def add_to_message(self, message):
        """ Add the text from stdout and stderr to the message from the
        interpreter to its listeners.

        Parameters
        ----------
        message : dict
        """

        out_text = self.out_text
        if out_text:
            message['stdout'] = out_text

        err_text = self.err_text
        if err_text:
            message['stderr'] = err_text
Ejemplo n.º 23
0
class NewSocketHandler:     # hand a new socket off where it belongs
    def __init__(self, multihandler, connection):
        self.multihandler = multihandler
        self.connection = connection
        connection.set_handler(self)
        self.closed = False
        self.buffer = StringIO()
        self.complete = False
        self.next_len, self.next_func = 1, self.read_header_len
        self.multihandler.rawserver.add_task(self._auto_close, 15)

    def _auto_close(self):
        if not self.complete:
            self.close()
        
    def close(self):
        if not self.closed:
            self.connection.close()
            self.closed = True

        
#   header format:
#        connection.write(chr(len(protocol_name)) + protocol_name + 
#            (chr(0) * 8) + self.encrypter.download_id + self.encrypter.my_id)

    # copied from Encrypter and modified
    
    def read_header_len(self, s):
        l = ord(s)
        return l, self.read_header

    def read_header(self, s):
        self.protocol = s
        return 8, self.read_reserved

    def read_reserved(self, s):
        self.options = s
        return 20, self.read_download_id

    def read_download_id(self, s):
        if s in self.multihandler.singlerawservers:
            if self.multihandler.singlerawservers[s].protocol == self.protocol:
                return True
        return None

    def read_dead(self, s):
        return None

    def data_came_in(self, garbage, s):
        while True:
            if self.closed:
                return
            i = self.next_len - self.buffer.tell()
            if i > len(s):
                self.buffer.write(s)
                return
            self.buffer.write(s[:i])
            s = s[i:]
            m = self.buffer.getvalue()
            self.buffer.reset()
            self.buffer.truncate()
            try:
                x = self.next_func(m)
            except:
                self.next_len, self.next_func = 1, self.read_dead
                raise
            if x is None:
                self.close()
                return
            if x == True:       # ready to process
                self.multihandler.singlerawservers[m]._external_connection_made(
                        self.connection, self.options, s)
                self.complete = True
                return
            self.next_len, self.next_func = x

    def connection_flushed(self, ss):
        pass

    def connection_lost(self, ss):
        self.closed = True