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
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, "w") as fp:
            json.dump(mockSettings, fp)
            raise Exception()

    with atomic_writer(p, "w") 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
def retrieve_download(dl):
    """Saves a download to a temporary file and returns path.

    .. versionadded: 1.37

    Args:
        url (unicode): URL to .alfredworkflow file in GitHub repo

    Returns:
        unicode: path to downloaded file

    """
    if not match_workflow(dl.filename):
        raise ValueError("attachment not a workflow: " + dl.filename)

    path = os.path.join(tempfile.gettempdir(), dl.filename)
    wf().logger.debug("downloading update from " "%r to %r ...", dl.url, path)

    r = requests.get(dl.url)
    r.raise_for_status()

    with atomic_writer(path, "wb") as file_obj:
        file_obj.write(r.content)

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

    assert len(os.listdir(tempdir)) == 1
    assert os.path.exists(p)
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)
Exemple #6
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 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 process_queue(self):
        """Generate thumbnails for queued files."""
        logger.info('process queue')

        if not self.has_queue:
            logger.info("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
                    self.download_image(line)
                    with atomic_writer(self._queue_path, 'wb') as fp:
                        pass
                        fp.write('')
    def save_queue(self):
        """Save queued files."""
        if not self._queue:
            return

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

        logger.info('self.cachedir=%s', self.cachedir)

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

        self._queue = []
Exemple #10
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
                    queue.append(line)
            with atomic_writer(self._queue_path, 'wb') as fp:
                fp.write('')

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

        return succeeded
 def write():
     with atomic_writer(p, "w") as fp:
         json.dump(mockSettings, fp)
         raise Exception()
 def write():
     with atomic_writer(p, "w") as fp:
         json.dump(DEFAULT_SETTINGS, fp)
         raise Exception()
 def write():
     with atomic_writer(p, "w"):
         raise Exception()
 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()