def test_failed_without_overwriting(tempdir):
    """AtomicWriter: Exception after writing won't overwrite the old file"""
    p = _settings(tempdir)
    mockSettings = {}

    def write():
        with atomic_writer(p, 'wb') as fp:
            json.dump(mockSettings, fp)
            raise Exception()

    with atomic_writer(p, 'wb') as fp:
        json.dump(DEFAULT_SETTINGS, fp)

    assert len(os.listdir(tempdir)) == 1
    assert os.path.exists(p)

    with pytest.raises(Exception):
        write()

    assert len(os.listdir(tempdir)) == 1
    assert os.path.exists(p)

    with open(p, 'rb') as fp:
        real_settings = json.load(fp)

    assert DEFAULT_SETTINGS == real_settings
Esempio n. 2
0
    def process_queue(self):
        """Generate thumbnails for queued files."""
        if not self.has_queue:
            log.debug('Thumbnail queue empty.')
            return

        queue = []
        with LockFile(self._queue_path):
            with open(self._queue_path) as fp:
                for line in fp:
                    line = line.strip()
                    if not line:
                        continue
                    if not os.path.exists(line):
                        log.debug('File does not exist : %r', line)
                        continue
                    queue.append(line)
            # Clear queue file
            with atomic_writer(self._queue_path, 'wb') as fp:
                fp.write('')

        succeeded = True
        for i, img_path in enumerate(queue):
            log.debug('Generating thumbnail {}/{} ...', i + 1, len(queue))
            if not self.generate_thumbnail(img_path):
                succeeded = False

        return succeeded
def test_write_file_succeed(tempdir):
    """Succeed, no temp file left"""
    p = _settings(tempdir)
    with atomic_writer(p, 'wb') as fp:
        json.dump(DEFAULT_SETTINGS, fp)

    assert len(os.listdir(tempdir)) == 1
    assert os.path.exists(p)
Esempio n. 4
0
    def save_queue(self):
        """Save queued files."""
        if not self._queue:
            return

        text = []
        for p in self._queue:
            if isinstance(p, unicode):
                p = p.encode('utf-8')
            text.append(p)
            log.debug('Queued for thumbnail generation : %r', p)

        text = b'\n'.join(text)
        with LockFile(self._queue_path):
            with atomic_writer(self._queue_path, 'ab') as fp:
                fp.write(b'{}\n'.format(text))

        self._queue = []
 def write():
     with atomic_writer(p, 'wb') as fp:
         json.dump(mockSettings, fp)
         raise Exception()
 def write():
     with atomic_writer(p, 'wb') as fp:
         json.dump(DEFAULT_SETTINGS, fp)
         raise Exception()
 def write():
     with atomic_writer(p, 'wb'):
         raise Exception()