Skip to content

opener

ngtools.opener

General-purpose byte stream opener.

Classes:

Name Description
open

General-purpose stream opener.

chain

Chain multiple file objects.

Functions:

Name Description
stringify_path

Ensure that the input is a str or a file-like object.

parse_protocols

Parse protocols out of a url.

fsopen

Open a file with fsspec (authentify if needed).

filesystem

Return the fsspec filesystem corresponding to a protocol or URI.

exists

Check that the file or directory pointed by a URI exists.

linc_auth_opt

Create fsspec authentication options to access lincbrain data.

read_json

Read local or remote JSON file.

write_json

Write local or remote JSON file.

update_json

Update local or remote JSON file.

parse_protocols

parse_protocols(*args, **kwargs)

Bases: parsed_protocols

Parse ngtools uri.

path property

path

Alias for url.

with_part

with_part(**kwargs)

Replace parts.

with_layer

with_layer(layer)

Replace layer.

with_format

with_format(format)

Replace format.

with_stream

with_stream(stream)

Replace stream.

with_url

with_url(url)

Replace url.

with_path

with_path(path)

Replace path.

chain

chain(*files, **kwargs)

Chain multiple file objects.

read

read(n=None, *a, **k)

Read characters ("t") or byes ("b").

readline

readline(n=None, *a, **k)

Read lines ("t").

readinto

readinto(buf, *a, **k)

Read bytes into an existing buffer.

write

write(*a, **k)

Write characters ("t") or bytes ("b").

writeline

writeline(*a, **k)

Write lines.

peek

peek(*a, **k)

Read the first bytes without moving the cursor.

seek

seek(*a, **k)

Move the cursor.

tell

tell(*a, **k)

Return the cursor position.

open

open(fileobj, mode='rb', compression=None, open=True)

General-purpose stream opener.

Handles:

  • paths (local or url)
  • opened file-objects
  • anything that fsspec handles
  • compressed streams

Open a file from a path or url or an opened file object.

Parameters:

Name Type Description Default
fileobj path or url or file - object

Input file

required
mode str

Opening mode

'rb'
compression (zip, bz2, gzip, lzma, xz, infer)

Compression mode. If 'infer', guess from magic number (if mode 'r') or filename (if mode 'w').

'zip'
auth dict

Authentication options to pass to fsspec.

required

Returns:

Type Description
fileobj

Opened file

fileobj instance-attribute

fileobj = stringify_path(fileobj)

The input path or file-like object

fileobjs instance-attribute

fileobjs = []

Additional file-like objects that may be created upon opening.

mode instance-attribute

mode = mode

Opening mode.

compression instance-attribute

compression = compression

Compression mode.

open

open()

Open the file.

close

close()

Close the file object.

read

read(*a, **k)

Read characters ("t") or byes ("b").

readline

readline(*a, **k)

Read lines ("t").

readinto

readinto(*a, **k)

Read bytes into an existing buffer.

write

write(*a, **k)

Write characters ("t") or bytes ("b").

writeline

writeline(*a, **k)

Write lines.

peek

peek(*a, **k)

Read the first bytes without moving the cursor.

seek

seek(*a, **k)

Move the cursor.

tell

tell(*a, **k)

Return the cursor position.

readable

readable()

Is stream readable.

writable

writable()

Is stream readable.

seekable

seekable()

Is stream readable.

async_open

Bases: open

Async version of open.

open async

open()

Open the file.

close async

close()

Close the file object.

read async

read(*a, **k)

Read characters ("t") or byes ("b").

readline async

readline(*a, **k)

Read lines ("t").

readinto async

readinto(*a, **k)

Read bytes into an existing buffer.

write async

write(*a, **k)

Write characters ("t") or bytes ("b").

writeline async

writeline(*a, **k)

Write lines.

peek async

peek(*a, **k)

Read the first bytes without moving the cursor.

seek async

seek(*a, **k)

Move the cursor.

tell async

tell(*a, **k)

Return the cursor position.

readable

readable()

Is stream readable.

writable

writable()

Is stream readable.

seekable

seekable()

Is stream readable.

stringify_path

stringify_path(filename)

Ensure that the input is a str or a file-like object.

linc_auth_opt

linc_auth_opt(token=None)

Create fsspec authentication options to access lincbrain data.

These options should only be used when accessing data behind neuroglancer.lincbrain.org.

Parameters:

Name Type Description Default
token str

Your LINCBRAIN_API_KEY

None

Returns:

Name Type Description
opt dict

options to pass to fsspec's HTTPFileSystem.

dandi_auth_opt

dandi_auth_opt(token=None, instance='dandi')

Create fsspec authentication options to access the dandi api.

These options should only be used when accessing data behind dandi://.

Parameters:

Name Type Description Default
token str

Your DANDI_API_KEY or LINCBRAIN_API_KEY

None
instance (dandi, linc)
"dandi"

Returns:

Name Type Description
opt dict

options to pass to fsspec's HTTPFileSystem.

filesystem

filesystem(protocol, **opt)

Return the filesystem corresponding to a protocol or URI.

exists

exists(uri, **opt)

Check that the file or directory pointed by a URI exists.

async_exists async

async_exists(uri, **opt)

Check that the file or directory pointed by a URI exists.

read_json

read_json(fileobj, *args, **kwargs)

Read local or remote JSON file.

write_json

write_json(obj, fileobj, *args, **kwargs)

Write local or remote JSON file.

update_json

update_json(obj, fileobj, *args, **kwargs)

Read local or remote JSON file.

fsopen

fsopen(uri, mode='rb', compression=None)

Open a file with fsspec (authentify if needed).

fsopen_async async

fsopen_async(uri, mode='rb', compression=None)

Open a file with fsspec (authentify if needed).