scistag.filestag.file_stag.FileStag

class FileStag[source]

Bases: object

Helper class to load data from a variety of sources such as local files, registered archives of the web

Methods

copy

Copies a file from given source to given target location

delete

Deletes a file

exists

Verifies if a file exists

is_simple

Returns if the file path points to a simple file on disk which does not require loading it to memory

load

Loads a file by filename from a local file, a registered web archive or the web

load_json

Loads a json dictionary from a given file source

load_text

Loads a text file from a given file source

save

Saves data to a file

save_json

Saves json data to a file target

save_text

Saves text data to a file

Attributes

__dict__

__doc__

__module__

__weakref__

list of weak references to the object (if defined)

classmethod copy(source, target, create_dir=False, **params)[source]

Copies a file from given source to given target location

Parameters
  • source (str) – The source location

  • target (str) – The target location

  • create_dir (bool) – Defines if a directory of the target shall be created if needed

  • params – The parameters to be passed to the source loader, e.g. max_cache_age etc.

Return type

bool

Returns

True on success

classmethod delete(target, **params)[source]

Deletes a file

Parameters
  • target (str) – The file’s target name, see load_file() for the supported protocols.

  • params – The advanced storage parameters, depending on the type of storage, such as timeout_s for file’s stored via network.

Return type

bool

Returns

True on success

classmethod exists(filename, **params)[source]

Verifies if a file exists

Parameters
  • filename (str) – The file’s source such as a local filename or URL. See FileNameType

  • params – Advanced parameters, protocol dependent

Return type

bool

Returns

True if the file exists

classmethod is_simple(filename)[source]

Returns if the file path points to a simple file on disk which does not require loading it to memory

Parameters

filename (FileSourceTypes | FileTargetTypes) – The file’s source such as a local filename or URL. See FileNameType

Return type

bool

Returns

True if it is a normal, local file

classmethod load(source, **params)[source]

Loads a file by filename from a local file, a registered web archive or the web

Parameters
  • source (FileSourceTypes) – The file’s source such as a local filename or URL. See FileNameType

  • params – Advanced loading params passed to the file loader such as timeout_s or max_cache_age for files from the web.

Return type

bytes | None

Returns

The data if the file could be found

classmethod load_json(source, encoding='utf-8', **params)[source]

Loads a json dictionary from a given file source

Parameters
  • source (FileSourceTypes) – The file’s source, see load_file().

  • encoding (str) – The text encoding. utf-8 by default

  • params – The advanced loading parameters, file source dependent, e.g. timeout_s for a timeout from file’s from the web.

Return type

dict | None

Returns

The file’s content

classmethod load_text(source, encoding='utf-8', **params)[source]

Loads a text file from a given file source

Parameters
  • source (FileSourceTypes) – The file’s source, see load_file().

  • encoding (str) – The text encoding. utf-8 by default

  • params – The advanced loading parameters, file source dependent, e.g. timeout_s for a timeout from file’s from the web.

Return type

str | None

Returns

The file’s content

classmethod save(target, data, **params)[source]

Saves data to a file

Parameters
  • target (str) – The file’s target name, see load_file().

  • data (bytes) – The data to be stored

  • params – The advanced storage parameters, depending on the type of storage, such as timeout_s for file’s stored via network.

Return type

bool

Returns

True on success

classmethod save_json(target, data, indent=None, encoding='utf-8', **params)[source]

Saves json data to a file target

Parameters
  • target (FileTargetTypes) – The file’s target. See FileNameType

  • data (dict) – The dictionary to be stored

  • indent (int | None) – The json indenting. None by default

  • encoding (str) – The encoding to use. utf-8 by default.

  • params – The advanced storage parameters, depending on the type of storage, such as timeout_s for file’s stored via network.

Return type

bool

Returns

True on success

classmethod save_text(target, text, encoding='utf-8', **params)[source]

Saves text data to a file

Parameters
  • target (str) – The file’s target, see save_file().

  • text (str) – The text to be stored

  • encoding (str) – The encoding to use. utf-8 by default.

  • params – The advanced storage parameters, depending on the type of storage, such as timeout_s for file’s stored via network.

Return type

bool

Returns

True on success