Esempio n. 1
0
    def __init__(self, filename, mode='r', force_overwrite=True):
        self._open = False
        self._file = None
        self._topology = None
        self._positions = None
        self._mode = mode
        self._last_topology = None

        if mode == 'r':
            PDBTrajectoryFile._loadNameReplacementTables()

            if _is_url(filename):
                self._file = urlopen(filename)
                if filename.lower().endswith('.gz'):
                    if six.PY3:
                        self._file = gzip.GzipFile(fileobj=self._file)
                    else:
                        self._file = gzip.GzipFile(fileobj=six.StringIO(
                            self._file.read()))
                if six.PY3:
                    self._file = six.StringIO(self._file.read().decode('utf-8'))
            else:
                self._file = open_maybe_zipped(filename, 'r')

            self._read_models()
        elif mode == 'w':
            self._header_written = False
            self._footer_written = False
            self._file = open_maybe_zipped(filename, 'w', force_overwrite)
        else:
            raise ValueError("invalid mode: %s" % mode)

        self._open = True
Esempio n. 2
0
    def __init__(self, filename, mode='r', force_overwrite=True):
        """Open a xyz file for reading/writing. """
        self._is_open = False
        self._filename = filename
        self._mode = mode
        self._frame_index = 0
        # track which line we're on. this is not essential, but its useful
        # when reporting errors to the user to say what line it occured on.
        self._line_counter = 0

        if mode == 'r':
            self._fh = open_maybe_zipped(filename, 'r')
            self._is_open = True
        elif mode == 'w':
            self._fh = open_maybe_zipped(filename, 'w', force_overwrite)
            self._is_open = True
        else:
            raise ValueError('mode must be one of "r" or "w". '
                             'you supplied "{0}"'.format(mode))
Esempio n. 3
0
    def __init__(self, filename, mode='r', force_overwrite=True):
        """Open a xyz file for reading/writing. """
        self._is_open = False
        self._filename = filename
        self._mode = mode
        self._frame_index = 0
        # track which line we're on. this is not essential, but its useful
        # when reporting errors to the user to say what line it occured on.
        self._line_counter = 0

        if mode == 'r':
            self._fh = open_maybe_zipped(filename, 'r')
            self._is_open = True
        elif mode == 'w':
            self._fh = open_maybe_zipped(filename, 'w', force_overwrite)
            self._is_open = True
        else:
            raise ValueError('mode must be one of "r" or "w". '
                             'you supplied "{0}"'.format(mode))
Esempio n. 4
0
 def test_write_bz2(self):
     fn = os.path.join(self.tmpdir, 'write.bz2')
     with open_maybe_zipped(fn, 'w') as f:
         f.write(u'COOKIE')
     with bz2.BZ2File(fn, 'r') as f:
         eq(f.read().decode('utf-8'), u'COOKIE')
Esempio n. 5
0
 def test_read_bz2(self):
     fn = os.path.join(self.tmpdir, 'read.bz2')
     with bz2.BZ2File(fn, 'w') as f:
         f.write('COOKIE'.encode('utf-8'))
     eq(open_maybe_zipped(fn, 'r').read(), u'COOKIE')
Esempio n. 6
0
 def test_write_gz(self):
     fn = os.path.join(self.tmpdir, 'write.gz')
     with open_maybe_zipped(fn, 'w') as f:
         f.write(u'COOKIE')
     with gzip.GzipFile(fn, 'r') as f:
         eq(f.read().decode('utf-8'), u'COOKIE')
Esempio n. 7
0
 def test_read_gz(self):
     fn = os.path.join(self.tmpdir, 'read.gz')
     with gzip.GzipFile(fn, 'w') as f:
         f.write('COOKIE'.encode('utf-8'))
     eq(open_maybe_zipped(fn, 'r').read(), u'COOKIE')
Esempio n. 8
0
def test_read_gz(tmpdir):
    fn = '{}/read.gz'.format(tmpdir)
    with gzip.GzipFile(fn, 'w') as f:
        f.write('COOKIE'.encode('utf-8'))
    eq(open_maybe_zipped(fn, 'r').read(), u'COOKIE')
Esempio n. 9
0
def test_write_bz2(tmpdir):
    fn = '{}/write.bz2'.format(tmpdir)
    with open_maybe_zipped(fn, 'w') as f:
        f.write(u'COOKIE')
    with bz2.BZ2File(fn, 'r') as f:
        eq(f.read().decode('utf-8'), u'COOKIE')
Esempio n. 10
0
def test_read_bz2(tmpdir):
    fn = '{}/read.bz2'.format(tmpdir)
    with bz2.BZ2File(fn, 'w') as f:
        f.write('COOKIE'.encode('utf-8'))
    eq(open_maybe_zipped(fn, 'r').read(), u'COOKIE')
Esempio n. 11
0
def test_write_gz(tmpdir):
    fn = '{}/write.gz'.format(tmpdir)
    with open_maybe_zipped(fn, 'w') as f:
        f.write(u'COOKIE')
    with gzip.GzipFile(fn, 'r') as f:
        eq(f.read().decode('utf-8'), u'COOKIE')