zensols.install package#

Submodules#

zensols.install.download#

Inheritance diagram of zensols.install.download

Downloads files with a progress bar.

Inspired by this thread.

class zensols.install.download.AuthenticationDownloader(use_progress_bar=True, skip_if_exists=True, mkdir=True, buffer_size=10485760, tqdm_params=<factory>, user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0', user=None, password=None, check_certificate=False)[source]#

Bases: Downloader

Download by first authenticating by login with the server.

__init__(use_progress_bar=True, skip_if_exists=True, mkdir=True, buffer_size=10485760, tqdm_params=<factory>, user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0', user=None, password=None, check_certificate=False)#
check_certificate: bool = False#

Whether to check the server’s certification for validity.

password: str = None#

The password for the authentication.

user: str = None#

The user for the authentication.

class zensols.install.download.Downloader(use_progress_bar=True, skip_if_exists=True, mkdir=True, buffer_size=10485760, tqdm_params=<factory>, user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0')[source]#

Bases: object

A utility class to download a file and (optionally) display a progress bar as it downloads.

DEFAULT_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0'#
__init__(use_progress_bar=True, skip_if_exists=True, mkdir=True, buffer_size=10485760, tqdm_params=<factory>, user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0')#
buffer_size: int = 10485760#

The size in bytes of the read buffer with a default of 10MB.

download(url, output_path)[source]#
mkdir: bool = True#

Recursively create directories for the target path if they do not already exist.

skip_if_exists: bool = True#

Skip download if the file exists as the target path.

tqdm_params: Dict[str, Any]#

Parameters given to tqdm for the progress bar when downloading.

use_progress_bar: bool = True#

Whether or not to render the progress bar as the file downloads.

user_agent: str = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0'#

The user agent header used for the file request.

zensols.install.installer#

Inheritance diagram of zensols.install.installer

API to download, uncompress and cache files such as binary models.

class zensols.install.installer.Installer(resources, package_resource=None, base_directory=None, sub_directory=None, downloader=<factory>)[source]#

Bases: Dictable

Downloads files from the internet and optionally extracts them.

The files are extracted to either base_directory or a path resolved from the home directory with name (i.e. ~/.cache/zensols/someappname). If the ~/.cache directory does not yet exist, it will base the installs in the home directory per the DEFAULT_BASE_DIRECTORIES attribute. Finally, the sub_directory is also added to the path if set.

Instances of this class are resource path iterable and indexable by name.

See:

Resource

DEFAULT_BASE_DIRECTORIES = ('~/.cache', '~/', '/tmp')#

Contains a list of directories to look as the default base when base_directory is not given.

See:

base_directory

See:

package_resource

__init__(resources, package_resource=None, base_directory=None, sub_directory=None, downloader=<factory>)#
base_directory: Path = None#

The directory to base relative resource paths. If this is not set, then this attribute is set from package_resource on initialization.

See:

package_resource

See:

DEFAULT_BASE_DIRECTORIES

property by_name: Dict[str, Resource]#

All resources as a dict with keys as their respective names.

downloader: Downloader#

Used to download the file from the Internet.

get_path(resource, compressed=False)[source]#

Return the path where a resource is installed.

Parameters:
  • resource (Resource) – the resource to find

  • compressed (bool) – if True, return the path where its compressed file (if any) lives

Return type:

Path

Returns:

the path of the resource

get_singleton_path(compressed=False)[source]#

Return the path of resource, which is expected to be the only one.

Parameters:

compressed (bool) – if True, return the path where its compressed file (if any) lives

Raises:

InstallError if the number of resources length isn’t 1

Return type:

Path

Returns:

the resource’s path

install()[source]#

Download and install all resources.

Return type:

List[Status]

Returns:

a list of statuses for each resource downloaded

package_resource: Union[str, PackageResource] = None#

Package resource (i.e. zensols.someappname). This field is converted to a package if given as a string during post initialization. This is used to set base_directory using the package name from the home directory if given. Otherwise, base_directory is used. One must be set.

property paths_by_name: Dict[str, Path]#

All resource paths as a dict with keys as their respective names.

resources: Tuple[Resource]#

The list of resources to install and track.

sub_directory: Path = None#

A path that is added to base_directory or the path referred by package_resource if set. Setting this is useful to allow for more directory structure in the installation (see class docs). This is only useful when using it with package_resource.

write(depth=0, writer=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]#

Write this instance as either a Writable or as a Dictable. If class attribute _DICTABLE_WRITABLE_DESCENDANTS is set as True, then use the write() method on children instead of writing the generated dictionary. Otherwise, write this instance by first creating a dict recursively using asdict(), then formatting the output.

If the attribute _DICTABLE_WRITE_EXCLUDES is set, those attributes are removed from what is written in the write() method.

Note that this attribute will need to be set in all descendants in the instance hierarchy since writing the object instance graph is done recursively.

Parameters:
  • depth (int) – the starting indentation depth

  • writer (TextIOBase) – the writer to dump the content of this writable

class zensols.install.installer.Status(resource, downloaded_path, target_path, uncompressed)[source]#

Bases: Dictable

Tells of what was installed and how.

__init__(resource, downloaded_path, target_path, uncompressed)#
downloaded_path: Path#

The path where resource was downloaded, or None if it wasn’t downloaded.

resource: Resource#

The resource that might have been installed.

target_path: Path#

Where the resource was installed and/or downloaded on the file system.

uncompressed: bool#

Whether or not the resource was uncompressed.

zensols.install.resource#

Inheritance diagram of zensols.install.resource

Installable resource.

class zensols.install.resource.FileSystemUpdate(dry_run)[source]#

Bases: Dictable

A command (GoF pattern) to udpate the file system after a resource has decompressed a file. First experiment with ListUpdate, then find the corresponding command with dry_run turned on, then turn it off once you’ve validated its doing the right thing.

Path fields (i.e. ListUpdate.path) are formatted with the dictionary version of FileSystemUpdateContext and also a target property with the uncompressed path.

__init__(dry_run)#
dry_run: bool#

If True don’t do anything, just act like it.

abstract invoke(context)[source]#
class zensols.install.resource.FileSystemUpdateContext(resource, check_path, target)[source]#

Bases: Dictable

The context given to a FileSystemUpdate.

__init__(resource, check_path, target)#
check_path: Path#

The installer relative Resource.check_path.

resource: Resource#

The Resource that created this context and updating the file system.

target: Path#

The installer relative target path from Resource.

class zensols.install.resource.ListUpdate(dry_run, path)[source]#

Bases: FileSystemUpdate

Lists the contents of path.

__init__(dry_run, path)#
invoke(context)[source]#
path: str#

A file or directory to list.

class zensols.install.resource.MoveUpdate(dry_run, source, target)[source]#

Bases: FileSystemUpdate

Move file(s) from source to target.

__init__(dry_run, source, target)#
invoke(context)[source]#
source: str#

The source glob (i.e. {target}/../*) from where to move the files.

target: str#

The target directory where the files end up.

class zensols.install.resource.RemoveUpdate(dry_run, paths)[source]#

Bases: FileSystemUpdate

Remove/clean up files to help keep the file system “clean”.

__init__(dry_run, paths)#
invoke(context)[source]#
paths: Sequence[str]#

The list of path formatted files to remove. For example {target}/../__MACOSX.

class zensols.install.resource.Resource(url, name=None, remote_name=None, is_compressed=None, rename=True, check_path=None, sub_path=None, clean_up=True, updates=())[source]#

Bases: Dictable

A resource that is installed by downloading from the Internet and then optionally uncompressed. Once the file is downloaded, it is only uncompressed if it is an archive file. This is determined by the file extension.

__init__(url, name=None, remote_name=None, is_compressed=None, rename=True, check_path=None, sub_path=None, clean_up=True, updates=())#
check_path: str = None#

The file to check for existance before doing uncompressing.

clean_up: bool = True#

Whether or not to remove the downloaded compressed after finished.

property compressed_name: str#

The file name with the extension and used to uncompress. If the resource isn’t compressed, just the name is returned.

get_file_name(compressed=False)[source]#

Return the path where a resource is installed.

Parameters:

compressed (bool) – if True, return the path where its compressed file (if any) lives

Return type:

str

Returns:

the path of the resource

is_compressed: bool = None#

Whether or not the file is compressed. If this isn’t set, it is derived from the file name.

name: str = None#

Used for local file naming.

remote_name: str = None#

The name of extracted file (or root directory if a compressed file) after being downloaded. If this isn’t set, it is taken from the file name portion of the path of the URL.

rename: bool = True#

If True then rename the directory to the name.

sub_path: Path = None#

The path to a file in the compressed file after it is extracted. This is only used to obtain the file name in get_file_name() when used to locate the uncompressed resource file.

uncompress(path=None, out_dir=None)[source]#

Uncompress the file.

Parameters:
  • path (Path) – the file to uncompress

  • out_dir (Path) – where the uncompressed files are extracted

Return type:

bool

updates: Sequence[FileSystemUpdate] = ()#

The file system updates to apply after the file has been decompressed.

url: str#

The URL that locates the file to install.

Module contents#

exception zensols.install.InstallError[source]#

Bases: APIError

Raised for issues while downloading or installing files.

__module__ = 'zensols.install'#