scistag.filestag.shared_archive.SharedArchive

class SharedArchive(source, identifier, cache=False)[source]

Bases: object

Defines a shared zip archive which can be used by multiple users, e.g. classes to provide shared data quickly from a compact archive once initialized.

Usage: SharedArchive.register(“sharedData.zip”, “sharedData”)

Then data can be loaded flexible via FileStag, independent of if it’s located in the web, in a zip archive or as simple local file: FileStag.load_file(“zip://@sharedData/testFile.zip”) FileStag.load_file(“local_file.txt”) FileStag.load_file(“https://www….”)

Note: Registered zip files have to add an @ in front of their identifier.

Initializer

Parameters
  • source (str | bytes) – The source, either a filename or a bytes object

  • identifier (str) – The identifier via which this object can be accessed

  • cache – Defines if this archive shall be cached in memory

Methods

check_in_zip_direct

Verifies if a file exists within a zip file

close

Closes the archive to unload and not having to wait for the gc

exists

Returns if the file exists

exists_at_source

Returns if given file exists

find_files

Lists all element from the archive matching given filter

is_loaded

Returns if a given zip file was registered

load_file

Loads a file by filename

load_file_from_zip_direct

Loads a file directly from a zip archive

read_file

Loads the data from given file to memory

register

Registers a new archive.

scan

Scans an archive for a given file mask to search for files of a specific type

unload

Unloads a zip file, e.g.

Attributes

__annotations__

__dict__

__doc__

__module__

__weakref__

list of weak references to the object (if defined)

access_lock

Access lock (for multi-threading)

archives

Dictionary of the loaded archives, identifier: SharedArchive

identifier

The archive's unique identifier

filename

The archive's filename (if loaded from a file), otherwise empty

classmethod _split_identifier_and_filename(identifier)[source]

Returns the registered zip archive and the filename within the archive

Parameters

identifier – The provided identifier

Returns

identifier or filename of zip file, filename within the archive

static check_in_zip_direct(zip_filename, filename)[source]

Verifies if a file exists within a zip file

Parameters
  • zip_filename – The name of the zip file

  • filename – The filename within the zip file

Return type

bool

Returns

Returns if the file exists

close()[source]

Closes the archive to unload and not having to wait for the gc

exists(name)[source]

Returns if the file exists

Parameters

name (str) – The file’s name

Return type

bool

Returns

True if it exists

classmethod exists_at_source(identifier, filename=None)[source]

Returns if given file exists

Parameters

identifier (str) – The archive identifier. Alternate: a full identifier in the form

zip://@identifier/filename :type filename: str | None :param filename: The name of the file to load. :rtype: bool :return: True if the file exists

find_files(name_filter='*')[source]

Lists all element from the archive matching given filter

Parameters

name_filter (str) – The filter

Return type

list[str]

Returns

The list of found elements

classmethod is_loaded(filename)[source]

Returns if a given zip file was registered

Parameters

filename – The zip file to be removed

Return type

bool

Returns

True if the archive exists

classmethod load_file(identifier, filename=None)[source]

Loads a file by filename

Parameters

identifier (str) – The archive identifier. Alternate: a full identifier in the form

zip://@identifier/filename or a path to a zipfile, e.g.

zip://filename.zip/filename

Parameters

filename (str | None) – The name of the file to load.

Return type

bytes | None

Returns

The data if the file could be found

static load_file_from_zip_direct(zip_filename, filename)[source]

Loads a file directly from a zip archive

Parameters
  • zip_filename – The name of the zip file

  • filename – The filename within the zip file

Return type

bytes | None

Returns

The file’s data if it could be found. None otherwise.

read_file(name)[source]

Loads the data from given file to memory

Parameters

name (str) – The name of the file to load

Return type

bytes | None

Returns

The file’s data. None if the file could not be found

classmethod register(source, identifier, cache=False)[source]

Registers a new archive.

Parameters
  • source (str | bytes) – The source, either a filename or a bytes object

  • identifier (str) – The identifier via which this object can be accessed

  • cache – Defines if this archive shall be cached in memory

Return type

SharedArchive

Returns

The archive

classmethod scan(identifier, name_filter='*', long_identifier=True)[source]

Scans an archive for a given file mask to search for files of a specific type

Parameters
  • identifier (str) – The archive identifier

  • name_filter (str) – The name mask to search for

  • long_identifier – Defines if the scan shall return long identifiers (zip://@identifier/filename) so the results can be used for FileStag.load_file). True by default.

Return type

list[str]

Returns

All file in given archive matching the mask

classmethod unload(filename=None, identifier=None)[source]

Unloads a zip file, e.g. if it’s uninstalled

Parameters
  • filename (str | None) – The zip file to be removed

  • identifier (str | None) – The identifier of the archive to unload

Return type

bool

Returns

True if an archive with given filename could be found and removed

access_lock = <RLock(None, 0)>

Access lock (for multi-threading)

archives: dict[str, 'SharedArchive'] = {'scistagessential': <scistag.filestag.shared_archive.SharedArchive object>}

Dictionary of the loaded archives, identifier: SharedArchive

filename

The archive’s filename (if loaded from a file), otherwise empty

identifier

The archive’s unique identifier