sys.stdout.seek

Here are the examples of the python api sys.stdout.seek taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

17 Examples 7

Example 1

Project: rebase-helper
Source File: test_utils.py
View license
    def test_get_message_yes(self):
        question = 'bla bla'
        answer = 'yes'

        self._setup_fake_IO(answer)
        inp = ConsoleHelper.get_message(question)
        sys.stdout.seek(0)

        assert sys.stdout.readline() == question + ' [Y/n]? '
        assert inp is True

Example 2

Project: rebase-helper
Source File: test_utils.py
View license
    def test_get_message_no(self):
        question = 'bla bla'
        answer = 'no'

        self._setup_fake_IO(answer)
        inp = ConsoleHelper.get_message(question)
        sys.stdout.seek(0)

        assert sys.stdout.readline() == question + ' [Y/n]? '
        assert inp is False

Example 3

Project: rebase-helper
Source File: test_utils.py
View license
    def test_get_message_yes_default_no(self):
        question = 'bla bla'
        answer = 'yes'

        self._setup_fake_IO(answer)
        inp = ConsoleHelper.get_message(question, default_yes=False)
        sys.stdout.seek(0)

        assert sys.stdout.readline() == question + ' [y/N]? '
        assert inp is True

Example 4

Project: rebase-helper
Source File: test_utils.py
View license
    def test_get_message_no_input_default_yes(self):
        question = 'bla bla'
        answer = '\n'

        self._setup_fake_IO(answer)
        inp = ConsoleHelper.get_message(question)
        sys.stdout.seek(0)

        assert sys.stdout.readline() == question + ' [Y/n]? '
        assert inp is True

Example 5

Project: rebase-helper
Source File: test_utils.py
View license
    def test_get_message_no_input_default_no(self):
        question = 'bla bla'
        answer = '\n'

        self._setup_fake_IO(answer)
        inp = ConsoleHelper.get_message(question, default_yes=False)
        sys.stdout.seek(0)

        assert sys.stdout.readline() == question + ' [y/N]? '
        assert inp is False

Example 6

Project: rebase-helper
Source File: test_utils.py
View license
    def test_get_message_any_input_default_yes(self):
        question = 'bla bla'
        answer = 'random input\ndsfdf'

        self._setup_fake_IO(answer)
        inp = ConsoleHelper.get_message(question, any_input=True)
        sys.stdout.seek(0)

        assert sys.stdout.readline() == question + ' '
        assert inp is True

Example 7

Project: rebase-helper
Source File: test_utils.py
View license
    def test_get_message_any_input_default_no(self):
        question = 'bla bla'
        answer = 'random input\n'

        self._setup_fake_IO(answer)
        inp = ConsoleHelper.get_message(question, default_yes=False, any_input=True)
        sys.stdout.seek(0)

        assert sys.stdout.readline() == question + ' '
        assert inp is False

Example 8

Project: cfn-pyplates
Source File: test_cli.py
View license
    def _generate(self, fail_on_error=True):
        return_code = cli.generate()
        sys.stdout.seek(0)
        out = '\n'.join([line for line in sys.stdout.readlines()])
        if return_code != 0:
            if fail_on_error:
                self.fail('generate failed, stdout dump follows:\n{0}'.format(
                    out)
                )
        return out

Example 9

Project: elastic-recheck
Source File: test_query.py
View license
    def test_query(self):
        with open('elastic_recheck/tests/unit/logstash/1284371.analysis') as f:
            expected_stdout = f.read()
        with mock.patch('elastic_recheck.results.SearchEngine.search') as \
                mock_search:
            with open('elastic_recheck/tests/unit/logstash/1284371.json') as f:
                jsonResponse = json.loads(f.read())
                mock_search.return_value = ResultSet(jsonResponse)
            query.query('elastic_recheck/tests/unit/queries/1284371.yaml')
            sys.stdout.seek(0)
            self.assertEqual(expected_stdout, sys.stdout.read())

Example 10

Project: gsutil
Source File: unit_testcase.py
View license
  def RunCommand(self, command_name, args=None, headers=None, debug=0,
                 return_stdout=False, return_stderr=False,
                 return_log_handler=False, cwd=None):
    """Method for calling gslib.command_runner.CommandRunner.

    Passes parallel_operations=False for all tests, optionally saving/returning
    stdout output. We run all tests multi-threaded, to exercise those more
    complicated code paths.
    TODO: Change to run with parallel_operations=True for all tests. At
    present when you do this it causes many test failures.

    Args:
      command_name: The name of the command being run.
      args: Command-line args (arg0 = actual arg, not command name ala bash).
      headers: Dictionary containing optional HTTP headers to pass to boto.
      debug: Debug level to pass in to boto connection (range 0..3).
      return_stdout: If True, will save and return stdout produced by command.
      return_stderr: If True, will save and return stderr produced by command.
      return_log_handler: If True, will return a MockLoggingHandler instance
           that was attached to the command's logger while running.
      cwd: The working directory that should be switched to before running the
           command. The working directory will be reset back to its original
           value after running the command. If not specified, the working
           directory is left unchanged.

    Returns:
      One or a tuple of requested return values, depending on whether
      return_stdout, return_stderr, and/or return_log_handler were specified.
    """
    args = args or []

    command_line = ' '.join([command_name] + args)
    if self.is_debugging:
      self.stderr_save.write('\nRunCommand of %s\n' % command_line)

    # Save and truncate stdout and stderr for the lifetime of RunCommand. This
    # way, we can return just the stdout and stderr that was output during the
    # RunNamedCommand call below.
    sys.stdout.seek(0)
    sys.stderr.seek(0)
    stdout = sys.stdout.read()
    stderr = sys.stderr.read()
    if stdout:
      self.accumulated_stdout.append(stdout)
    if stderr:
      self.accumulated_stderr.append(stderr)
    sys.stdout.seek(0)
    sys.stderr.seek(0)
    sys.stdout.truncate()
    sys.stderr.truncate()

    mock_log_handler = MockLoggingHandler()
    logging.getLogger(command_name).addHandler(mock_log_handler)

    try:
      with WorkingDirectory(cwd):
        self.command_runner.RunNamedCommand(
            command_name, args=args, headers=headers, debug=debug,
            parallel_operations=False, do_shutdown=False)
    finally:
      sys.stdout.seek(0)
      stdout = sys.stdout.read()
      sys.stderr.seek(0)
      stderr = sys.stderr.read()
      logging.getLogger(command_name).removeHandler(mock_log_handler)
      mock_log_handler.close()

      log_output = '\n'.join(
          '%s:\n  ' % level + '\n  '.join(records)
          for level, records in mock_log_handler.messages.iteritems()
          if records)
      if self.is_debugging and log_output:
        self.stderr_save.write(
            '==== logging RunCommand %s %s ====\n' % (self.id(), command_line))
        self.stderr_save.write(log_output)
        self.stderr_save.write('\n==== end logging ====\n')
      if self.is_debugging and stdout:
        self.stderr_save.write(
            '==== stdout RunCommand %s %s ====\n' % (self.id(), command_line))
        self.stderr_save.write(stdout)
        self.stderr_save.write('==== end stdout ====\n')
      if self.is_debugging and stderr:
        self.stderr_save.write(
            '==== stderr RunCommand %s %s ====\n' % (self.id(), command_line))
        self.stderr_save.write(stderr)
        self.stderr_save.write('==== end stderr ====\n')

      # Reset stdout and stderr files, so that we won't print them out again
      # in tearDown if debugging is enabled.
      sys.stdout.seek(0)
      sys.stderr.seek(0)
      sys.stdout.truncate()
      sys.stderr.truncate()

    to_return = []
    if return_stdout:
      to_return.append(stdout)
    if return_stderr:
      to_return.append(stderr)
    if return_log_handler:
      to_return.append(mock_log_handler)
    if len(to_return) == 1:
      return to_return[0]
    return tuple(to_return)

Example 11

Project: raspberry_pwn
Source File: testing.py
View license
def runCase(parse):
    retVal = True
    handled_exception = None
    unhandled_exception = None
    result = False
    console = ""

    try:
        result = start()
    except KeyboardInterrupt:
        pass
    except SqlmapBaseException, e:
        handled_exception = e
    except Exception, e:
        unhandled_exception = e
    finally:
        sys.stdout.seek(0)
        console = sys.stdout.read()
        LOGGER_HANDLER.stream = sys.stdout = sys.__stdout__

    if unhandled_exception:
        Failures.failedTraceBack = "unhandled exception: %s" % str(traceback.format_exc())
        retVal = None
    elif handled_exception:
        Failures.failedTraceBack = "handled exception: %s" % str(traceback.format_exc())
        retVal = None
    elif result is False:  # this means no SQL injection has been detected - if None, ignore
        retVal = False

    console = getUnicode(console, encoding=sys.stdin.encoding)

    if parse and retVal:
        with codecs.open(conf.dumper.getOutputFile(), "rb", UNICODE_ENCODING) as f:
            content = f.read()

        for item, parse_from_console_output in parse:
            parse_on = console if parse_from_console_output else content

            if item.startswith("r'") and item.endswith("'"):
                if not re.search(item[2:-1], parse_on, re.DOTALL):
                    retVal = None
                    Failures.failedItems.append(item)

            elif item not in parse_on:
                retVal = None
                Failures.failedItems.append(item)

        if Failures.failedItems:
            Failures.failedParseOn = console

    elif retVal is False:
        Failures.failedParseOn = console

    return retVal

Example 12

Project: golismero
Source File: testing.py
View license
def runCase(parse):
    global failedItem
    global failedParseOn
    global failedTraceBack

    retVal = True
    handled_exception = None
    unhandled_exception = None
    result = False
    console = ""

    try:
        result = start()
    except KeyboardInterrupt:
        pass
    except SqlmapBaseException, e:
        handled_exception = e
    except Exception, e:
        unhandled_exception = e
    finally:
        sys.stdout.seek(0)
        console = sys.stdout.read()
        LOGGER_HANDLER.stream = sys.stdout = sys.__stdout__

    if unhandled_exception:
        failedTraceBack = "unhandled exception: %s" % str(traceback.format_exc())
        retVal = None
    elif handled_exception:
        failedTraceBack = "handled exception: %s" % str(traceback.format_exc())
        retVal = None
    elif result is False:  # this means no SQL injection has been detected - if None, ignore
        retVal = False

    console = getUnicode(console, system=True)

    if parse and retVal:
        with codecs.open(conf.dumper.getOutputFile(), "rb", UNICODE_ENCODING) as f:
            content = f.read()

        for item, parse_from_console_output in parse:
            parse_on = console if parse_from_console_output else content

            if item.startswith("r'") and item.endswith("'"):
                if not re.search(item[2:-1], parse_on, re.DOTALL):
                    retVal = None
                    failedItem = item
                    break

            elif item not in parse_on:
                retVal = None
                failedItem = item
                break

        if failedItem is not None:
            failedParseOn = console

    elif retVal is False:
        failedParseOn = console

    return retVal

Example 13

Project: gsutil
Source File: unit_testcase.py
View license
  def tearDown(self):
    super(GsUtilUnitTestCase, self).tearDown()

    self.root_logger.handlers = self.log_handlers_save
    self.temp_log_handler.flush()
    self.temp_log_handler.close()
    self.log_handler_stream.seek(0)
    log_output = self.log_handler_stream.read()
    self.log_handler_stream.close()
    os.unlink(self.log_handler_file)

    sys.stdout.seek(0)
    sys.stderr.seek(0)
    stdout = sys.stdout.read()
    stderr = sys.stderr.read()
    stdout += ''.join(self.accumulated_stdout)
    stderr += ''.join(self.accumulated_stderr)
    sys.stdout.close()
    sys.stderr.close()
    sys.stdout = self.stdout_save
    sys.stderr = self.stderr_save
    os.unlink(self.stdout_file)
    os.unlink(self.stderr_file)

    if self.is_debugging and stdout:
      sys.stderr.write('==== stdout %s ====\n' % self.id())
      sys.stderr.write(stdout)
      sys.stderr.write('==== end stdout ====\n')
    if self.is_debugging and stderr:
      sys.stderr.write('==== stderr %s ====\n' % self.id())
      sys.stderr.write(stderr)
      sys.stderr.write('==== end stderr ====\n')
    if self.is_debugging and log_output:
      sys.stderr.write('==== log output %s ====\n' % self.id())
      sys.stderr.write(log_output)
      sys.stderr.write('==== end log output ====\n')

Example 14

View license
    def _output(self):
        sys.stdout.seek(0)
        return sys.stdout.read()

Example 15

View license
    def _output(self):
        sys.stdout.seek(0)
        return sys.stdout.read()

Example 16

Project: zookeeper-monitoring
Source File: test.py
View license
    def output(self):
        sys.stdout.seek(0)
        return sys.stdout.read()

Example 17

Project: zookeeper-monitoring
Source File: test.py
View license
    def output(self):
        sys.stdout.seek(0)
        return sys.stdout.read()