How to use the atomicwrites.AtomicWriter function in atomicwrites

To help you get started, we’ve selected a few atomicwrites examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github darvid / reqwire / src / reqwire / cli.py View on Github external
src_dir = options['directory'] / options['source_dir']
    dest_dir = options['directory'] / options['build_dir']
    if not dest_dir.exists():
        dest_dir.mkdir()
    default_args = ['-r']
    if not tag:
        pattern = '*{}'.format(options['extension'])
        tag = (path.stem for path in src_dir.glob(pattern))
    for tag_name in tag:
        src = src_dir / ''.join((tag_name, options['extension']))
        dest = dest_dir / '{}.txt'.format(tag_name)
        console.info('building {}', click.format_filename(str(dest)))
        args = default_args[:]
        args += [str(src)]
        args += list(pip_compile_options)
        with atomicwrites.AtomicWriter(str(dest), 'w', True).open() as f:
            f.write(reqwire.scaffold.MODELINES_HEADER)
            with tempfile.NamedTemporaryFile() as temp_file:
                args += ['-o', temp_file.name]
                sh.pip_compile(*args, _out=f, _tty_out=False)
github commaai / openpilot / common / file_helpers.py View on Github external
def atomic_write_on_fs_tmp(path, **kwargs):
  """Creates an atomic writer using a temporary file in a temporary directory
     on the same filesystem as path.
  """
  # TODO(mgraczyk): This use of AtomicWriter relies on implementation details to set the temp
  #                 directory.
  writer = AtomicWriter(path, **kwargs)
  return writer._open(_get_fileobject_func(writer, get_tmpdir_on_same_filesystem(path)))
github commaai / openpilot / common / file_helpers.py View on Github external
def atomic_write_in_dir(path, **kwargs):
  """Creates an atomic writer using a temporary file in the same directory
     as the destination file.
  """
  writer = AtomicWriter(path, **kwargs)
  return writer._open(_get_fileobject_func(writer, os.path.dirname(path)))
github pimutils / todoman / todoman / model.py View on Github external
def _write_existing(self, path):
        original = self._read(path)
        vtodo = self.serialize(original)

        with open(path, 'rb') as f:
            cal = icalendar.Calendar.from_ical(f.read())
            for index, component in enumerate(cal.subcomponents):
                if component.get('uid', None) == self.todo.uid:
                    cal.subcomponents[index] = vtodo

        with AtomicWriter(path, overwrite=True).open() as f:
            f.write(cal.to_ical().decode("UTF-8"))
github python-adaptive / adaptive / adaptive / utils.py View on Github external
def save(fname, data, compress=True):
    fname = os.path.expanduser(fname)
    dirname = os.path.dirname(fname)
    if dirname:
        os.makedirs(dirname, exist_ok=True)

    blob = pickle.dumps(data, protocol=pickle.HIGHEST_PROTOCOL)
    if compress:
        blob = gzip.compress(blob)

    with AtomicWriter(fname, "wb", overwrite=True).open() as f:
        f.write(blob)
github jbms / beancount-import / beancount_import / journal_editor.py View on Github external
[]).append(entry)
    partially_booked_entries = []  # type: Entries
    empty_list = []  # type: Entries
    for entry in pre_booking_entries:
        meta = entry.meta
        post_booking_matches = post_booking_entries_by_meta.get(
            (meta.get('filename'), meta.get('lineno')), empty_list)
        if len(post_booking_matches) != 1:
            partially_booked_entries.append(entry)
            continue
        partially_booked_entries.append(
            _partially_book_entry(entry, post_booking_matches[0]))
    return partially_booked_entries


class _AtomicWriter(atomicwrites.AtomicWriter):
    """Wrapper that calls `os.stat` after close but before the rename."""

    def __init__(self, path, **kwargs):
        super().__init__(path, **kwargs)
        self.stat_result_after_close = None

    def sync(self, f):
        super().sync(f)
        f.close()
        self.stat_result_after_close = os.stat(f.name)


def _get_journal_contents(filename: str):
    with open(filename, 'r', encoding='utf-8') as f:
        return f.read()
github pimutils / todoman / todoman / model.py View on Github external
def _write_new(self, path):
        vtodo = self.serialize()

        c = icalendar.Calendar()
        c.add_component(vtodo)

        with AtomicWriter(path).open() as f:
            c.add('prodid', 'io.barrera.todoman')
            c.add('version', '2.0')
            f.write(c.to_ical().decode("UTF-8"))

        return vtodo

atomicwrites

Atomic file writes.

MIT
Latest version published 2 years ago

Package Health Score

64 / 100
Full package analysis

Similar packages