Python cStringIO.StringIO() Examples

The following are 30 code examples of cStringIO.StringIO(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cStringIO , or try the search function .
Example #1
Source File: urllib2.py    From jawfish with MIT License 6 votes vote down vote up
def open_local_file(self, req):
        host = req.get_host()
        file = req.get_selector()
        localfile = url2pathname(file)
        stats = os.stat(localfile)
        size = stats[stat.ST_SIZE]
        modified = rfc822.formatdate(stats[stat.ST_MTIME])
        mtype = mimetypes.guess_type(file)[0]
        stats = os.stat(localfile)
        headers = mimetools.Message(StringIO(
            'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
            (mtype or 'text/plain', size, modified)))
        if host:
            host, port = splitport(host)
        if not host or \
           (not port and socket.gethostbyname(host) in self.get_names()):
            return addinfourl(open(localfile, 'rb'),
                              headers, 'file:'+file)
        raise URLError('file not on local host') 
Example #2
Source File: blob_upload_test.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def test_filename_too_large(self):
    """Test that exception is raised if the filename is too large."""
    self.now().AndReturn(datetime.datetime(2008, 11, 12, 10, 40))
    self.mox.ReplayAll()

    filename = 'a' * blob_upload._MAX_STRING_NAME_LENGTH + '.txt'

    form = FakeForm({
        'field1': FakeForm(name='field1',
                           file=StringIO.StringIO('a'),
                           type='image/png',
                           type_options={'a': 'b', 'x': 'y'},
                           filename=filename,
                           headers={}),
        })

    self.assertRaisesRegexp(
        webob.exc.HTTPClientError,
        'The filename exceeds the maximum allowed length of 500.',
        self.handler.store_and_build_forward_message,
        form, '================1234==')

    self.mox.VerifyAll() 
Example #3
Source File: blob_upload_test.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def test_content_type_too_large(self):
    """Test that exception is raised if the content-type is too large."""
    self.now().AndReturn(datetime.datetime(2008, 11, 12, 10, 40))
    self.mox.ReplayAll()

    content_type = 'text/' + 'a' * blob_upload._MAX_STRING_NAME_LENGTH

    form = FakeForm({
        'field1': FakeForm(name='field1',
                           file=StringIO.StringIO('a'),
                           type=content_type,
                           type_options={'a': 'b', 'x': 'y'},
                           filename='foobar.txt',
                           headers={}),
        })

    self.assertRaisesRegexp(
        webob.exc.HTTPClientError,
        'The Content-Type exceeds the maximum allowed length of 500.',
        self.handler.store_and_build_forward_message,
        form, '================1234==')

    self.mox.VerifyAll() 
Example #4
Source File: htmlvisitor.py    From me-ica with GNU Lesser General Public License v2.1 6 votes vote down vote up
def convert_flow(self, flow):
        """Convert the flow into XHTML and write it into the internal file."""
        # first write the normal HTML into a buffer
        orig_file = self._file
        html_file = StringIO.StringIO()
        self._file = NewlineWriteFile(html_file)
        super(HiNetXHTMLVisitor, self).convert_flow(flow)
        self._file = orig_file
        # now convert it to XHTML
        html_code = html_file.getvalue()
        html_code = html_code.replace('<br>', '<br />')
        html_code = html_code.replace('&nbsp;', '&#160;')
        self._file.write(html_code)


## Helper functions ## 
Example #5
Source File: fake_recipe_deps.py    From recipes-py with Apache License 2.0 6 votes vote down vote up
def _get_suite(buf, default, indent='  '):
  """This is a helper function to extract a python code suite from a StringIO
  object.

  This will dedent the buffer's content, split it into lines, and then re-indent
  it by the amount indicated.

  Args:
    * buf (StringIO) - The buffer to take the content from.
    * default (str) - The content to use if `buf` is empty.
    * indent (str) - The string to prepend to all de-indented lines.

  Returns the transformed string.
  """
  value = textwrap.dedent(buf.getvalue() or default).strip('\n')
  return '\n'.join(indent + l for l in value.splitlines()) 
Example #6
Source File: __init__.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def emit(events, stream=None, Dumper=Dumper,
        canonical=None, indent=None, width=None,
        allow_unicode=None, line_break=None):
    """
    Emit YAML parsing events into a stream.
    If stream is None, return the produced string instead.
    """
    getvalue = None
    if stream is None:
        from StringIO import StringIO
        stream = StringIO()
        getvalue = stream.getvalue
    dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
            allow_unicode=allow_unicode, line_break=line_break)
    try:
        for event in events:
            dumper.emit(event)
    finally:
        dumper.dispose()
    if getvalue:
        return getvalue() 
Example #7
Source File: blob_download_test.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def create_blob(self):
    """Create a blob in the datastore and on disk.

    Returns:
      BlobKey of new blob.
    """
    contents = 'a blob'
    blob_key = blobstore.BlobKey('blob-key-1')
    self.blob_storage.StoreBlob(blob_key, cStringIO.StringIO(contents))
    entity = datastore.Entity(blobstore.BLOB_INFO_KIND,
                              name=str(blob_key),
                              namespace='')
    entity['content_type'] = 'image/png'
    entity['creation'] = datetime.datetime(1999, 10, 10, 8, 42, 0)
    entity['filename'] = 'largeblob.png'
    entity['size'] = len(contents)
    datastore.Put(entity)

    return blob_key 
Example #8
Source File: blob_download_test.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def create_blob(self):
    """Create a GS object in the datastore and on disk.

    Overrides the superclass create_blob method.

    Returns:
      The BlobKey of the new object."
    """
    data = 'a blob'
    filename = '/gs/some_bucket/some_object'
    blob_store_key = base64.urlsafe_b64encode(filename)
    self.blob_storage.StoreBlob(blob_store_key, cStringIO.StringIO(data))

    blob_key = blobstore.create_gs_key(filename)
    entity = datastore.Entity(file_service_stub.GS_INFO_KIND,
                              name=blob_key,
                              namespace='')
    entity['content_type'] = 'image/png'
    entity['filename'] = 'largeblob.png'
    entity['size'] = len(data)
    entity['storage_key'] = blob_store_key
    datastore.Put(entity)

    return blob_key 
Example #9
Source File: dev_appserver_blobstore.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def EndRedirect(self, dispatched_output, original_output):
      """Handle the end of upload complete notification.

      Makes sure the application upload handler returned an appropriate status
      code.
      """
      response = dev_appserver.RewriteResponse(dispatched_output)
      logging.info('Upload handler returned %d', response.status_code)
      outfile = cStringIO.StringIO()
      outfile.write('Status: %s\n' % response.status_code)

      if response.body and len(response.body.read()) > 0:
        response.body.seek(0)
        outfile.write(response.body.read())
      else:
        outfile.write(''.join(response.headers.headers))

      outfile.seek(0)
      dev_appserver.URLDispatcher.EndRedirect(self,
                                              outfile,
                                              original_output) 
Example #10
Source File: base.py    From ripe-atlas-tools with GNU General Public License v3.0 6 votes vote down vote up
def capture_sys_output(use_fake_tty=False):
    """
    Wrap a block with this, and it'll capture standard out and standard error
    into handy variables:

      with capture_sys_output() as (stdout, stderr):
          self.cmd.run()

    More info: https://stackoverflow.com/questions/18651705/
    """

    capture_out, capture_err = StringIO(), StringIO()
    current_out, current_err = sys.stdout, sys.stderr
    current_in = sys.stdin
    try:
        if use_fake_tty:
            sys.stdin = FakeTTY(current_in)
        sys.stdout, sys.stderr = capture_out, capture_err
        yield capture_out, capture_err
    finally:
        sys.stdout, sys.stderr = current_out, current_err
        sys.stdin = current_in 
Example #11
Source File: cloudpickle.py    From pywren-ibm-cloud with Apache License 2.0 6 votes vote down vote up
def dumps(obj, protocol=None):
    """Serialize obj as a string of bytes allocated in memory

    protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to
    pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed
    between processes running the same Python version.

    Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure
    compatibility with older versions of Python.
    """
    file = StringIO()
    try:
        cp = CloudPickler(file, protocol=protocol)
        cp.dump(obj)
        return file.getvalue()
    finally:
        file.close()


# including pickles unloading functions in this namespace 
Example #12
Source File: conanfile.py    From conan-center-index with MIT License 6 votes vote down vote up
def _run_python_script(self, script):
        """
        execute python one-liner script and return its output
        :param script: string containing python script to be executed
        :return: output of the python script execution, or None, if script has failed
        """
        output = StringIO()
        command = '"%s" -c "%s"' % (self._python_executable, script)
        self.output.info('running %s' % command)
        try:
            self.run(command=command, output=output)
        except ConanException:
            self.output.info("(failed)")
            return None
        output = output.getvalue().strip()
        self.output.info(output)
        return output if output != "None" else None 
Example #13
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def EndRedirect(self, dispatched_output, original_output):
    """Process the end of an internal redirect.

    This method is called after all subsequent dispatch requests have finished.
    By default the output from the dispatched process is copied to the original.

    This will not be called on dispatchers that do not return an internal
    redirect.

    Args:
      dispatched_output: StringIO buffer containing the results from the
       dispatched
      original_output: The original output file.

    Returns:
      None if request handling is complete.
      A new AppServerRequest instance if internal redirect is required.
    """
    original_output.write(dispatched_output.read()) 
Example #14
Source File: blob_upload_test.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def test_store_and_build_forward_message_bad_mimes(self):
    """Test upload with no headers provided."""
    for unused_mime in range(len(BAD_MIMES)):
      # Should not require actual time value upon failure.
      self.now()

    self.mox.ReplayAll()

    for mime_type in BAD_MIMES:
      form = FakeForm({'field1': FakeForm(name='field1',
                                          file=StringIO.StringIO('file1'),
                                          type=mime_type,
                                          type_options={},
                                          filename='file',
                                          headers={}),
                      })

      self.assertRaisesRegexp(
          webob.exc.HTTPClientError,
          'Incorrectly formatted MIME type: %s' % mime_type,
          self.handler.store_and_build_forward_message,
          form,
          '================1234==')

    self.mox.VerifyAll() 
Example #15
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def __init__(self, response_file=None, **kwds):
    """Initializer.

    Args:
      response_file: A file-like object that contains the full response
        generated by the user application request handler.  If present
        the headers and body are set from this value, although the values
        may be further overridden by the keyword parameters.
      kwds: All keywords are mapped to attributes of AppServerResponse.
    """
    self.status_code = 200
    self.status_message = 'Good to go'
    self.large_response = False

    if response_file:
      self.SetResponse(response_file)
    else:
      self.headers = mimetools.Message(cStringIO.StringIO())
      self.body = None

    for name, value in kwds.iteritems():
      setattr(self, name, value) 
Example #16
Source File: test_cmd_line.py    From waveconverter with MIT License 6 votes vote down vote up
def setUp(self):
        self.verbose = False

        # we will then turn it back on for unittest output
        saved_stdout = sys.stdout
        if not self.verbose:
            sys.stdout = cStringIO.StringIO()

        os.chdir('../src')
        os.system("./waveconverter.py -p 4 -b ../input_files/weather_s100k.bb > ../test/tmp/tmp_output.txt")
        os.chdir('../test')

        # now read back the info contained in the temp file
        self.outputString = open("./tmp/tmp_output.txt", 'r').read()
 
        # turn stdout back on
        if not self.verbose:
            sys.stdout = saved_stdout
        
        pass
        

    # grep through the output to confirm that the expected transmissions are there 
Example #17
Source File: testing.py    From recruit with Apache License 2.0 6 votes vote down vote up
def make_input_stream(input, charset):
    # Is already an input stream.
    if hasattr(input, 'read'):
        if PY2:
            return input
        rv = _find_binary_reader(input)
        if rv is not None:
            return rv
        raise TypeError('Could not find binary reader for input stream.')

    if input is None:
        input = b''
    elif not isinstance(input, bytes):
        input = input.encode(charset)
    if PY2:
        return StringIO(input)
    return io.BytesIO(input) 
Example #18
Source File: __init__.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def emit(events, stream=None, Dumper=Dumper,
        canonical=None, indent=None, width=None,
        allow_unicode=None, line_break=None):
    """
    Emit YAML parsing events into a stream.
    If stream is None, return the produced string instead.
    """
    getvalue = None
    if stream is None:
        from StringIO import StringIO
        stream = StringIO()
        getvalue = stream.getvalue
    dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
            allow_unicode=allow_unicode, line_break=line_break)
    try:
        for event in events:
            dumper.emit(event)
    finally:
        dumper.dispose()
    if getvalue:
        return getvalue() 
Example #19
Source File: appengine_rpc_httplib2.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def RaiseHttpError(url, response_info, response_body, extra_msg=''):
  """Raise a urllib2.HTTPError based on an httplib2 response tuple."""
  if response_body is not None:
    stream = cStringIO.StringIO()
    stream.write(response_body)
    stream.seek(0)
  else:
    stream = None
  if not extra_msg:
    msg = response_info.reason
  else:
    msg = response_info.reason + ' ' + extra_msg
  raise urllib2.HTTPError(url, response_info.status, msg, response_info, stream) 
Example #20
Source File: blob_upload_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def execute_blob_test(self, blob_content, expected_result,
                        base64_encoding=False):
    """Execute a basic blob insertion."""
    expected_key = blobstore.BlobKey('expectedkey')
    expected_creation = datetime.datetime(2008, 11, 12)

    self.generate_blob_key().AndReturn(expected_key)

    self.mox.ReplayAll()
    self.handler.store_blob(content_type='image/png; a="b"; m="n"',
                            filename='stuff.png',
                            md5_hash=hashlib.md5(),
                            blob_file=StringIO.StringIO(blob_content),
                            creation=expected_creation,
                            base64_encoding=base64_encoding)

    self.assertEquals(expected_result,
                      self.storage.OpenBlob(expected_key).read())

    blob_info = blobstore.get(expected_key)
    self.assertFalse(blob_info is None)
    self.assertEquals(('image/png', {'a': 'b', 'm': 'n'}),
                      cgi.parse_header(blob_info.content_type))
    self.assertEquals(expected_creation, blob_info.creation)
    self.assertEquals('stuff.png', blob_info.filename)
    self.assertEquals(len(expected_result), blob_info.size)

    self.mox.VerifyAll() 
Example #21
Source File: blob_upload_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def test_store_and_build_forward_message_with_gs_bucket(self):
    """Test the high-level method to store a blob and build a MIME message."""
    self.generate_blob_key().AndReturn(blobstore.BlobKey('item1'))

    self.now().AndReturn(datetime.datetime(2008, 11, 12, 10, 40))

    self.mox.ReplayAll()

    form = FakeForm({
        'field1': FakeForm(name='field1',
                           file=StringIO.StringIO('file1'),
                           type='image/png',
                           type_options={'a': 'b', 'x': 'y'},
                           filename='stuff.png',
                           headers={'h1': 'v1',
                                    'h2': 'v2',
                                   }),
        })

    content_type, content_text = self.handler.store_and_build_forward_message(
        form, '================1234==', bucket_name='my-test-bucket')

    self.mox.VerifyAll()

    self.assertEqual(EXPECTED_GENERATED_CONTENT_TYPE_WITH_BUCKET, content_type)
    self.assertMessageEqual(EXPECTED_GENERATED_MIME_MESSAGE_WITH_BUCKET,
                            content_text)

    blob1 = blobstore.get('item1')
    self.assertEquals('stuff.png', blob1.filename) 
Example #22
Source File: xmpp_request_handler.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def get_boundary_and_content(self):
    """Returns the message boundary and entire content body for the form."""
    boundary = '----=' + ''.join(random.choice(string.letters + string.digits)
                                 for _ in range(25))
    s = cStringIO.StringIO()

    for name, value, sub_type in self._data:
      s.write('--%s\r\n' % boundary)
      s.write('Content-Type: text/%s; charset="UTF-8"\r\n' % sub_type)
      s.write('Content-Disposition: form-data; name="%s"\r\n' % name)
      s.write('\r\n')
      s.write('%s\r\n' % value.encode('utf-8'))
    s.write('--%s--\r\n' % boundary)
    return boundary, s.getvalue() 
Example #23
Source File: dev_appserver.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def Dispatch(self,
               request,
               outfile,
               base_env_dict=None):
    """Dispatches the Python CGI."""
    request_size = GetRequestSize(request, base_env_dict, outfile)
    if request_size is None:
      return


    memory_file = cStringIO.StringIO()
    CopyStreamPart(request.infile, memory_file, request_size)
    memory_file.seek(0)

    before_level = logging.root.level
    try:
      env = {}


      if self._config.env_variables:
        env.update(self._config.env_variables)
      if base_env_dict:
        env.update(base_env_dict)
      cgi_path = self._path_adjuster.AdjustPath(request.path)
      env.update(self._setup_env(cgi_path,
                                 request.relative_url,
                                 request.headers,
                                 memory_file))
      self._exec_cgi(self._config,
                     self._root_path,
                     request.path,
                     cgi_path,
                     env,
                     memory_file,
                     outfile,
                     self._module_dict)
    finally:
      logging.root.level = before_level 
Example #24
Source File: dev_appserver.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def __init__(self, method, relative_url, headers, body):
    payload = cStringIO.StringIO()
    payload.write('%s %s HTTP/1.1\r\n' % (method, relative_url))
    payload.write('Content-Length: %d\r\n' % len(body))
    for key, value in headers:
      payload.write('%s: %s\r\n' % (key, value))
    payload.write('\r\n')
    payload.write(body)
    self.rfile = cStringIO.StringIO(payload.getvalue())
    self.wfile = StringIO.StringIO()
    self.wfile_close = self.wfile.close
    self.wfile.close = self.connection_done 
Example #25
Source File: test_utils.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def build_request(path, body='', http_headers=None):
  """Build an ApiRequest for the given path and body.

  Args:
    path: A string containing the URL for the proposed request.
    body: A string containing the body of the proposed request.
    http_headers: A list of (header, value) headers to add to the request.

  Returns:
    An ApiRequest object built based on the incoming parameters.
  """
  (unused_scheme, unused_netloc, path, query,
   unused_fragment) = urlparse.urlsplit(path)
  env = {'SERVER_PORT': 42, 'REQUEST_METHOD': 'GET',
         'SERVER_NAME': 'localhost', 'HTTP_CONTENT_TYPE': 'application/json',
         'PATH_INFO': path, 'wsgi.input': cStringIO.StringIO(body)}
  if query:
    env['QUERY_STRING'] = query

  if http_headers:
    for header, value in http_headers:
      header = 'HTTP_%s' % header.upper().replace('-', '_')
      env[header] = value

  cgi_request = api_request.ApiRequest(env)
  return cgi_request 
Example #26
Source File: api_request_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def test_source_ip(self):
    body = '{}'
    path = '/_ah/api/guestbook/v1/greetings'
    env = {'SERVER_PORT': 42, 'REQUEST_METHOD': 'GET',
           'SERVER_NAME': 'localhost', 'HTTP_CONTENT_TYPE': 'application/json',
           'PATH_INFO': path, 'wsgi.input': cStringIO.StringIO(body)}

    request = api_request.ApiRequest(env)
    self.assertEqual(request.source_ip, None)

    env['REMOTE_ADDR'] = '1.2.3.4'
    request = api_request.ApiRequest(env)
    self.assertEqual(request.source_ip, '1.2.3.4') 
Example #27
Source File: test_optionfile.py    From ServerlessCrawler-VancouverRealState with MIT License 5 votes vote down vote up
def test_string(self):
        parser = Parser()
        if PY2:
            parser.readfp(StringIO(_cfg_file))
        else:
            parser.read_file(StringIO(_cfg_file))
        self.assertEqual(parser.get("default", "string"), "foo")
        self.assertEqual(parser.get("default", "quoted"), "bar")
        self.assertEqual(parser.get("default", "single_quoted"), "foobar") 
Example #28
Source File: server.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def build_request_environ(self, method, relative_url, headers, body,
                            source_ip, port, fake_login=False):
    if isinstance(body, unicode):
      body = body.encode('ascii')

    url = urlparse.urlsplit(relative_url)
    if port != 80:
      host = '%s:%s' % (self.host, port)
    else:
      host = self.host
    environ = {constants.FAKE_IS_ADMIN_HEADER: '1',
               'CONTENT_LENGTH': str(len(body)),
               'PATH_INFO': url.path,
               'QUERY_STRING': url.query,
               'REQUEST_METHOD': method,
               'REMOTE_ADDR': source_ip,
               'SERVER_NAME': self.host,
               'SERVER_PORT': str(port),
               'SERVER_PROTOCOL': 'HTTP/1.1',
               'wsgi.version': (1, 0),
               'wsgi.url_scheme': 'http',
               'wsgi.errors': cStringIO.StringIO(),
               'wsgi.multithread': True,
               'wsgi.multiprocess': True,
               'wsgi.input': cStringIO.StringIO(body)}
    if fake_login:
      environ[constants.FAKE_LOGGED_IN_HEADER] = '1'
    util.put_headers_in_environ(headers, environ)
    environ['HTTP_HOST'] = host
    return environ 
Example #29
Source File: mox_test.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def testSpecialTypes(self):
    """Verify that IsA can handle objects like cStringIO.StringIO."""
    isA = mox.IsA(cStringIO.StringIO())
    stringIO = cStringIO.StringIO()
    self.assert_(isA == stringIO) 
Example #30
Source File: create_dataset.py    From cat-bbs with MIT License 5 votes vote down vote up
def compress_img(img, method):
    """Compress an image (numpy array) using the provided image compression
    method."""
    img_compressed_buffer = StringIO.StringIO()
    im = misc.toimage(img)
    im.save(img_compressed_buffer, format=method)
    img_compressed = img_compressed_buffer.getvalue()
    img_compressed_buffer.close()
    return img_compressed