Python StringIO.StringIO() Examples

The following are 30 code examples for showing how to use StringIO.StringIO(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module StringIO , or try the search function .

Example 1
Project: CAMISIM   Author: CAMI-challenge   File: loggingwrapper.py    License: Apache License 2.0 6 votes vote down vote up
def add_log_stream(self, stream=sys.stderr, level=logging.INFO):
		"""
		Add a stream where messages are outputted to.

		@param stream: stderr/stdout or a file stream
		@type stream: file | FileIO | StringIO
		@param level: minimum level of messages to be logged
		@type level: int | long

		@return: None
		@rtype: None
		"""
		assert self.is_stream(stream)
		# assert isinstance(stream, (file, io.FileIO))
		assert level in self._levelNames

		err_handler = logging.StreamHandler(stream)
		err_handler.setFormatter(self.message_formatter)
		err_handler.setLevel(level)
		self._logger.addHandler(err_handler) 
Example 2
Project: CAMISIM   Author: CAMI-challenge   File: metadatatable.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, separator="\t", logfile=None, verbose=True):
		"""
			Handle tab separated files

			@attention:

			@param separator: default character assumed to separate values in a file
			@type separator: str | unicode
			@param logfile: file handler or file path to a log file
			@type logfile: file | io.FileIO | StringIO.StringIO | basestring
			@param verbose: Not verbose means that only warnings and errors will be past to stream
			@type verbose: bool

			@return: None
			@rtype: None
		"""
		assert logfile is None or isinstance(logfile, basestring) or self.is_stream(logfile)
		assert isinstance(separator, basestring), "separator must be string"
		assert isinstance(verbose, bool), "verbose must be true or false"
		super(MetadataTable, self).__init__(label="MetadataReader", logfile=logfile, verbose=verbose)

		self._number_of_rows = 0
		self._meta_table = {}
		self._separator = separator
		self._list_of_column_names = [] 
Example 3
Project: bioservices   Author: cokelaer   File: mappers.py    License: GNU General Public License v3.0 6 votes vote down vote up
def get_data_from_biodbnet(self, df_hgnc):
        """keys are unique Gene names
        
        input is made of the df based on HGNC data web services

        uniprot accession are duplicated sometimes. If som this is actually the
        iprimary accession entry and all secondary ones.


        e.g. ,
        
        ABHD11 >>>> Q8N723;Q8NFV2;Q8NFV3;Q6PJU0;Q8NFV4;H7BYM8;Q8N722;Q9HBS8 ABHDB_HUMAN Alpha/beta hydrolase domain-containing protein 11
        correspond actually to the primary one : Q8NFV4

        """
        b = biodbnet.BioDBNet()
        res2 = b.db2db("Gene Symbol", ["HGNC ID", "UniProt Accession", "UniProt Entry Name", "UniProt Protein Name", "KEGG Gene ID", "Ensembl Gene ID"], 
                res.keys()[0:2000])

        import pandas as pd
        import StringIO
        c = pd.read_csv(StringIO.StringIO(res2), delimiter="\t", index_col="Gene Symbol")
        return c 
Example 4
Project: alibuild   Author: alisw   File: test_build.py    License: GNU General Public License v3.0 6 votes vote down vote up
def dummy_open(x, mode="r"):
  if mode == "r":
    known = {
      "/sw/BUILD/27ce49698e818e8efb56b6eff6dd785e503df341/defaults-release/.build_succeeded": (0, StringIO("0")),
      "/sw/BUILD/3e90b4e08bad439fa5f25282480d1adb9efb0c0d/zlib/.build_succeeded": (0, StringIO("0")),
      "/sw/BUILD/%s/ROOT/.build_succeeded" % TEST_ROOT_BUILD_HASH: (0, StringIO("0")),
      "/sw/osx_x86-64/defaults-release/v1-1/.build-hash": (1, StringIO("27ce49698e818e8efb56b6eff6dd785e503df341")),
      "/sw/osx_x86-64/zlib/v1.2.3-1/.build-hash": (1, StringIO("3e90b4e08bad439fa5f25282480d1adb9efb0c0d")),
      "/sw/osx_x86-64/ROOT/v6-08-30-1/.build-hash": (1, StringIO(TEST_ROOT_BUILD_HASH))
    }
    if not x in known:
      return DEFAULT
    threshold, result = known[x]
    if threshold > TIMES_ASKED.get(x, 0):
      result = None
    TIMES_ASKED[x] = TIMES_ASKED.get(x, 0) + 1
    if not result:
      raise IOError
    return result
  return StringIO() 
Example 5
Project: mx   Author: graalvm   File: mx_ide_eclipse.py    License: GNU General Public License v2.0 6 votes vote down vote up
def _copy_eclipse_settings(p, files=None):
    processors = p.annotation_processors()

    settingsDir = join(p.dir, ".settings")
    mx.ensure_dir_exists(settingsDir)

    for name, sources in p.eclipse_settings_sources().items():
        out = StringIO()
        print('# GENERATED -- DO NOT EDIT', file=out)
        for source in sources:
            print('# Source:', source, file=out)
            with open(source) as f:
                print(f.read(), file=out)
        if p.javaCompliance:
            jc = p.javaCompliance if p.javaCompliance.value < _max_Eclipse_JavaExecutionEnvironment else mx.JavaCompliance(_max_Eclipse_JavaExecutionEnvironment)
            content = out.getvalue().replace('${javaCompliance}', str(jc))
        else:
            content = out.getvalue()
        if processors:
            content = content.replace('org.eclipse.jdt.core.compiler.processAnnotations=disabled', 'org.eclipse.jdt.core.compiler.processAnnotations=enabled')
        mx.update_file(join(settingsDir, name), content)
        if files:
            files.append(join(settingsDir, name)) 
Example 6
Project: mx   Author: graalvm   File: select_jdk.py    License: GNU General Public License v2.0 6 votes vote down vote up
def get_shell_commands(args, jdk, extra_jdks):
    setvar_format = get_setvar_format(args.shell)
    shell_commands = StringIO()
    print(setvar_format % ('JAVA_HOME', jdk), file=shell_commands)
    if extra_jdks:
        print(setvar_format % ('EXTRA_JAVA_HOMES', os.pathsep.join(extra_jdks)), file=shell_commands)
    path = os.environ.get('PATH').split(os.pathsep)
    if path:
        jdk_bin = join(jdk, 'bin')
        old_java_home = os.environ.get('JAVA_HOME')
        replace = join(old_java_home, 'bin') if old_java_home else None
        if replace in path:
            path = [e if e != replace else jdk_bin for e in path]
        else:
            path = [jdk_bin] + path
        print(setvar_format % ('PATH', get_PATH_sep(args.shell).join(path)), file=shell_commands)
    return shell_commands.getvalue().strip() 
Example 7
Project: EXOSIMS   Author: dsavransky   File: test_PlanetPopulation.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_str(self):
        """
        Test __str__ method, for full coverage and check that all modules have required attributes.
        """
        atts_list = ['PlanetPhysicalModel', 'arange', 'erange', 'Irange', 'Orange', 'wrange', 'prange',
                     'Rprange', 'Mprange', 'rrange', 'scaleOrbits', 'constrainOrbits', 'eta']
        for mod in self.allmods:
            with RedirectStreams(stdout=self.dev_null):
                obj = mod(**self.spec)
            original_stdout = sys.stdout
            sys.stdout = StringIO()
            # call __str__ method
            result = obj.__str__()
            # examine what was printed
            contents = sys.stdout.getvalue()
            self.assertEqual(type(contents), type(''))
            # attributes from ICD
            for att in atts_list:
                self.assertIn(att,contents,'{} missing for {}'.format(att,mod.__name__))
            sys.stdout.close()
            # it also returns a string, which is not necessary
            self.assertEqual(type(result), type(''))
            # put stdout back
            sys.stdout = original_stdout 
Example 8
Project: EXOSIMS   Author: dsavransky   File: test_Completeness.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_str(self):
        """
        Test __str__ method, for full coverage and check that all modules have required attributes.
        """
        atts_list = ['PlanetPopulation', 'PlanetPhysicalModel', 'dMagLim', 'minComp']

        for mod in self.allmods:
            with RedirectStreams(stdout=self.dev_null):
                obj = mod(**copy.deepcopy(self.spec))
            original_stdout = sys.stdout
            sys.stdout = StringIO()
            # call __str__ method
            result = obj.__str__()
            # examine what was printed
            contents = sys.stdout.getvalue()
            self.assertEqual(type(contents), type(''))
            # attributes from ICD
            for att in atts_list:
                self.assertIn(att,contents,'{} missing for {}'.format(att,mod.__name__))
            sys.stdout.close()
            # it also returns a string, which is not necessary
            self.assertEqual(type(result), type(''))
            # put stdout back
            sys.stdout = original_stdout 
Example 9
def test_str(self):
        r"""Test __str__ method, for full coverage."""
        tlist = self.fixture
        # replace stdout and keep a reference
        original_stdout = sys.stdout
        sys.stdout = StringIO()
        # call __str__ method
        result = tlist.__str__()
        # examine what was printed
        contents = sys.stdout.getvalue()
        self.assertEqual(type(contents), type(''))
        self.assertIn('PlanetPhysicalModel', contents)
        self.assertIn('PlanetPopulation', contents)
        self.assertIn('Completeness', contents)
        sys.stdout.close()
        # it also returns a string, which is not necessary
        self.assertEqual(type(result), type(''))
        # put stdout back
        sys.stdout = original_stdout

    # @unittest.skip("Skipping populate_target_list.") 
Example 10
Project: EXOSIMS   Author: dsavransky   File: test_PlanetPhysicalModel.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_str(self):
        """
        Test __str__ method, for full coverage and check that all modules have required attributes.
        """

        for mod in self.allmods:
            with RedirectStreams(stdout=self.dev_null):
                obj = mod()
            original_stdout = sys.stdout
            sys.stdout = StringIO()
            # call __str__ method
            result = obj.__str__()
            # examine what was printed
            contents = sys.stdout.getvalue()
            self.assertEqual(type(contents), type(''))
            # attributes from ICD
            self.assertIn('_outspec', contents,'_outspec missing for %s'%mod.__name__)
            sys.stdout.close()
            # it also returns a string, which is not necessary
            self.assertEqual(type(result), type(''))
            # put stdout back
            sys.stdout = original_stdout 
Example 11
Project: django-river   Author: javrasya   File: test__migrations.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_shouldCreateAllMigrations(self):
        for f in os.listdir("river/migrations"):
            if f != "__init__.py" and f != "__pycache__" and not f.endswith(".pyc"):
                open(os.path.join("river/tests/volatile/river/", f), 'wb').write(open(os.path.join("river/migrations", f), 'rb').read())

        self.migrations_before = list(filter(lambda f: f.endswith('.py') and f != '__init__.py', os.listdir('river/tests/volatile/river/')))

        out = StringIO()
        sys.stout = out

        call_command('makemigrations', 'river', stdout=out)

        self.migrations_after = list(filter(lambda f: f.endswith('.py') and f != '__init__.py', os.listdir('river/tests/volatile/river/')))

        assert_that(out.getvalue(), equal_to("No changes detected in app 'river'\n"))
        assert_that(self.migrations_after, has_length(len(self.migrations_before))) 
Example 12
Project: pywren-ibm-cloud   Author: pywren   File: cloudpickle.py    License: 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 13
Project: jawfish   Author: war-and-code   File: urllib2.py    License: 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 14
Project: me-ica   Author: ME-ICA   File: test_fileholders.py    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_same_file_as():
    fh = FileHolder('a_fname')
    assert_true(fh.same_file_as(fh))
    fh2 = FileHolder('a_test')
    assert_false(fh.same_file_as(fh2))
    sio0 = StringIO()
    fh3 = FileHolder('a_fname', sio0)
    fh4 = FileHolder('a_fname', sio0)
    assert_true(fh3.same_file_as(fh4))
    assert_false(fh3.same_file_as(fh))
    fh5 = FileHolder(fileobj=sio0)
    fh6 = FileHolder(fileobj=sio0)
    assert_true(fh5.same_file_as(fh6))
    # Not if the filename is the same
    assert_false(fh5.same_file_as(fh3))
    # pos doesn't matter
    fh4_again = FileHolder('a_fname', sio0, pos=4)
    assert_true(fh3.same_file_as(fh4_again)) 
Example 15
Project: misp42splunk   Author: remg427   File: __init__.py    License: 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 16
Project: misp42splunk   Author: remg427   File: __init__.py    License: 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 17
Project: convseg   Author: chqiwang   File: server.py    License: MIT License 5 votes vote down vote up
def tag(self, sentences):
        sentences = self.preprocess(sentences)
        sf = StringIO.StringIO()
        sf.write(sentences)
        sf.seek(0)
        data = TASK.read_raw_file_all(sf)
        output = self.model.tag_all(data, self.batch_size)
        return TASK.create_output(*output) 
Example 18
Project: MPContribs   Author: materialsproject   File: webui.py    License: MIT License 5 votes vote down vote up
def view(identifier=None, cid_short=None):
    mpfile = read_mpfile_to_view()
    if mpfile is None:
        return render_template("home.html", alert="Choose an MPFile!", session=session)
    fmt = session["options"][0]
    try:
        mpfile_stringio = StringIO(mpfile)
        if identifier is None or cid_short is None:
            response = Response(
                stream_with_context(
                    stream_template(
                        "index.html",
                        session=session,
                        content=process_mpfile(mpfile_stringio, fmt=fmt),
                    )
                )
            )
            response.headers["X-Accel-Buffering"] = "no"
            return response
        else:
            ids = [identifier, cid_short]
            iterator = process_mpfile(mpfile_stringio, fmt=fmt, ids=ids)
            for it in iterator:
                if isinstance(it, list):
                    d = jsonify(it)
            return d
    except Exception:
        pass 
Example 19
Project: L.E.S.M.A   Author: NatanaelAntonioli   File: L.E.S.M.A. - Fabrica de Noobs Speedtest.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, response):
		# response doesn't support tell() and read(), required by
		# GzipFile
		if not gzip:
			raise SpeedtestHTTPError('HTTP response body is gzip encoded, '
									 'but gzip support is not available')
		IO = BytesIO or StringIO
		self.io = IO()
		while 1:
			chunk = response.read(1024)
			if len(chunk) == 0:
				break
			self.io.write(chunk)
		self.io.seek(0)
		gzip.GzipFile.__init__(self, mode='rb', fileobj=self.io) 
Example 20
Project: L.E.S.M.A   Author: NatanaelAntonioli   File: L.E.S.M.A. - Fabrica de Noobs Speedtest.py    License: Apache License 2.0 5 votes vote down vote up
def _create_data(self):
		chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		multiplier = int(round(int(self.length) / 36.0))
		IO = BytesIO or StringIO
		self._data = IO(
			('content1=%s' %
			 (chars * multiplier)[0:int(self.length) - 9]
			 ).encode()
		) 
Example 21
Project: L.E.S.M.A   Author: NatanaelAntonioli   File: L.E.S.M.A. - Fabrica de Noobs Speedtest.py    License: Apache License 2.0 5 votes vote down vote up
def csv(self, delimiter=','):
		"""Return data in CSV format"""

		data = self.dict()
		out = StringIO()
		writer = csv.writer(out, delimiter=delimiter, lineterminator='')
		writer.writerow([data['server']['id'], data['server']['sponsor'],
						 data['server']['name'], data['timestamp'],
						 data['server']['d'], data['ping'], data['download'],
						 data['upload']])
		return out.getvalue() 
Example 22
Project: CAMISIM   Author: CAMI-challenge   File: fastaanonymizer.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, logfile=None, verbose=True, debug=False, seed=None, tmp_dir=None):
		"""
			Anonymize fasta sequences

			@attention: 'shuf' is used which loads everything into memory!

			@param logfile: file handler or file path to a log file
			@type logfile: file | io.FileIO | StringIO.StringIO | str | unicode
			@param verbose: Not verbose means that only warnings and errors will be past to stream
			@type verbose: bool
			@param debug: more output and files are kept, manual clean up required
			@type debug: bool
			@param seed: The seed written to the random_source file used by the 'shuf' command
			@type seed: long | int | float | str | unicode
			@param tmp_dir: directory for temporary files, like the random_source file for 'shuf'
			@type tmp_dir: str | unicode

			@return: None
			@rtype: None
		"""
		assert isinstance(verbose, bool)
		assert isinstance(debug, bool)
		assert seed is None or isinstance(seed, (long, int, float, basestring))
		assert tmp_dir is None or isinstance(tmp_dir, basestring)
		if tmp_dir is not None:
			assert self.validate_dir(tmp_dir)
		else:
			tmp_dir = tempfile.gettempdir()
		self._tmp_dir = tmp_dir
		super(FastaAnonymizer, self).__init__(logfile, verbose, debug, label="FastaAnonymizer")

		if seed is not None:
			random.seed(seed)

		script_dir = os.path.dirname(self.get_full_path(__file__))
		self._anonymizer = os.path.join(script_dir, "anonymizer.py")
		self._fastastreamer = os.path.join(script_dir, "fastastreamer.py")
		assert self.validate_file(self._anonymizer)
		assert self.validate_file(self._fastastreamer) 
Example 23
Project: CAMISIM   Author: CAMI-challenge   File: fastastreamer.py    License: Apache License 2.0 5 votes vote down vote up
def stream_directory(self, directory, out_stream=sys.stdout, file_format="fastq", extension="fq", paired=False):
		"""
			Stream sequences consecutively

			@attention:

			@param directory: A directory
			@type directory: basestring
			@param out_stream: A stream the output will be written to.
			@type out_stream: file | io.FileIO | StringIO.StringIO
			@param file_format: Fasta format of input and output. Either 'fasta' or 'fastq'.
			@type file_format: basestring
			@param extension: file extension to be filtered for
			@type extension: basestring
			@param paired: sequences are streamed interweaved from a pair of files if True, else consecutively
			@type paired: bool

			@return: None
			@rtype: None
		"""
		assert isinstance(directory, basestring)
		directory = FastaStreamer.get_full_path(directory)
		assert self.validate_dir(directory)
		assert self.is_stream(out_stream)
		assert isinstance(file_format, basestring)
		file_format = file_format.lower()
		assert file_format in self._legal_formats
		assert extension is None or isinstance(extension, basestring)

		list_of_file = self.get_files_in_directory(directory, extension=extension)
		if not paired:
			self.consecutive_stream(list_of_file, out_stream=out_stream, file_format=file_format)
		else:
			self.interweave_stream(list_of_file, out_stream=out_stream, file_format=file_format, extension=extension) 
Example 24
Project: CAMISIM   Author: CAMI-challenge   File: fastastreamer.py    License: Apache License 2.0 5 votes vote down vote up
def stream_file(self, file_path, out_stream=sys.stdout, file_format="fastq", paired=False):
		"""
			Stream sequences consecutively

			@attention:

			@param file_path: A file path
			@type file_path: basestring
			@param out_stream: A stream the output will be written to.
			@type out_stream: file | io.FileIO | StringIO.StringIO
			@param file_format: Fasta format of input and output. Either 'fasta' or 'fastq'.
			@type file_format: basestring
			@param paired: sequences are streamed as pair, else one by one
			@type paired: bool

			@return: None
			@rtype: None
		"""
		assert isinstance(file_path, basestring)
		file_path = FastaStreamer.get_full_path(file_path)
		assert self.validate_file(file_path)
		assert self.is_stream(out_stream)
		assert isinstance(file_format, basestring)
		file_format = file_format.lower()
		assert file_format in self._legal_formats

		self.consecutive_stream(file_path, out_stream=out_stream, file_format=file_format, paired=paired) 
Example 25
Project: CAMISIM   Author: CAMI-challenge   File: loggingwrapper.py    License: Apache License 2.0 5 votes vote down vote up
def is_stream(stream):
		return isinstance(stream, (file, io.FileIO, StringIO.StringIO)) or stream.__class__ is StringIO.StringIO 
Example 26
Project: CAMISIM   Author: CAMI-challenge   File: loggingwrapper.py    License: Apache License 2.0 5 votes vote down vote up
def set_log_file(self, log_file, mode='a', level=logging.INFO):
		"""
		Add a stream where messages are outputted to.

		@attention: file stream will only be closed if a file path is given!

		@param log_file: file stream or file path of logfile
		@type log_file: file | FileIO | StringIO | basestring
		@param mode: opening mode for logfile, if a file path is given
		@type mode: basestring
		@param level: minimum level of messages to be logged
		@type level: int or long

		@return: None
		@rtype: None
		"""
		assert isinstance(log_file, basestring) or self.is_stream(log_file)
		assert level in self._levelNames

		if LoggingWrapper._map_logfile_handler[self._label] is not None:
			self._logger.removeHandler(LoggingWrapper._map_logfile_handler[self._label])
			LoggingWrapper._map_logfile_handler[self._label].close()
			LoggingWrapper._map_logfile_handler[self._label] = None

		if self.is_stream(log_file):
			self.add_log_stream(stream=log_file, level=level)
			return

		try:
			err_handler_file = logging.FileHandler(log_file, mode)
			err_handler_file.setFormatter(self.message_formatter)
			err_handler_file.setLevel(level)
			self._logger.addHandler(err_handler_file)
			LoggingWrapper._map_logfile_handler[self._label] = err_handler_file
		except Exception:
			sys.stderr.write("[LoggingWrapper] Could not open '{}' for logging\n".format(log_file))
			return 
Example 27
Project: CAMISIM   Author: CAMI-challenge   File: loggingwrapper.py    License: Apache License 2.0 5 votes vote down vote up
def is_stream(stream):
		"""
		Test for streams

		@param stream: Any kind of stream type
		@type stream: file | io.FileIO | StringIO.StringIO

		@return: True if stream
		@rtype: bool
		"""
		return isinstance(stream, (file, io.FileIO, StringIO.StringIO)) or stream.__class__ is StringIO.StringIO 
Example 28
Project: CAMISIM   Author: CAMI-challenge   File: compress.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, default_compression="gz", label="Compress", logfile=None, verbose=True, debug=False):
        """
        Constructor

        @attention:

        @param default_compression: default compression used for files
        @type default_compression: str | unicode
        @param logfile: file handler or file path to a log file
        @type logfile: file | io.FileIO | StringIO.StringIO | basestring
        @param verbose: Not verbose means that only warnings and errors will be past to stream
        @type verbose: bool
        @param debug: Display debug messages
        @type debug: bool

        @return: None
        @rtype: None
        """
        assert logfile is None or isinstance(logfile, basestring) or self.is_stream(logfile)
        assert isinstance(default_compression, basestring), "separator must be string"
        assert isinstance(verbose, bool), "verbose must be true or false"
        assert default_compression.lower() in self._open, "Unknown compression: '{}'".format(default_compression)

        super(Compress, self).__init__(label=label, logfile=logfile, verbose=verbose, debug=debug)

        # self._logger = LoggingWrapper(self._label, verbose=verbose)
        # if logfile is not None:
        #     self._logger.set_log_file(logfile)

        self._default_compression = default_compression 
Example 29
Project: CAMISIM   Author: CAMI-challenge   File: sequencevalidator.py    License: Apache License 2.0 5 votes vote down vote up
def _validate_file_start(self, stream_handle, file_format):
		"""
			Validate that a stream with sequences starts with the correct character

			@param stream_handle: Any kind of stream type
			@type stream_handle: file | io.FileIO | StringIO.StringIO
			@param file_format: Format of the file at the file_path provided. Valid: 'fasta', 'fastq'
			@type file_format: str | unicode

			@return: True if the first character is correct
			@rtype: bool
		"""
		assert self.is_stream(stream_handle)
		assert isinstance(file_format, basestring)
		file_format = file_format.lower()
		assert file_format in self._formats

		sequence_indicator = self._sequence_indicators[file_format]

		head = stream_handle.read(1)
		stream_handle.seek(0)
		if not head:
			return False
		if not head.startswith(sequence_indicator):
			return False
		return True 
Example 30
Project: ToonRooter   Author: martenjacobs   File: rooter.py    License: MIT License 5 votes vote down vote up
def create_payload_tar(self):
        tar_path = tempfile.mkstemp(suffix=".tar.gz")[1]
        ssh_key = self._ssh_pubkey_data
        with tarfile.open(tar_path, "w:gz") as tar:
            tar.add('payload/', arcname='payload')

            ssh_key_str = StringIO.StringIO(ssh_key)

            info = tarfile.TarInfo(name="payload/id_rsa.pub")
            info.size=len(ssh_key)

            tar.addfile(tarinfo=info, fileobj=StringIO.StringIO(ssh_key))
        return tar_path