flask_file_share package

Module for initializing the FlaskFileShare package.

Usage:

>> import flask_file_share import as ffs

>> client = ffs.Client()

Subpackages

Submodules

File: main.py

Main module for running the File Sharing Flask web application.

Functions:

main(): Main function to run the web application.

Usage:

The web application is run by executing the main function in this module.

Example

python main.py

flask_file_share.app.build_web_app_parser(parser: ArgumentParser) ArgumentParser

Build Web App parser.

Parameters:

parser (argparse.ArgumentParser) – The ArgumentParser object.

Returns:

The modified ArgumentParser object.

Return type:

argparse.ArgumentParser

flask_file_share.app.main(args: Namespace)
flask_file_share.app.parse_args() Namespace

Parse CLI arguments.

Returns:

The parsed command-line arguments.

Return type:

argparse.Namespace

File: cli.py

A command-line interface (CLI) for interacting with the File Sharing Flask application.

Functions:
  • client_session_manager(host: str, username: str, password: str) -> Generator[FileSharingClient, None, None]:

    Context manager for managing the client session.

  • handle_login(*args, **kwargs): Handle login command.

  • handle_logout(*args, **kwargs): Handle logout command.

  • handle_list(args, file_sharing_client: FileSharingClient): Handle list command.

  • handle_upload(args, file_sharing_client: FileSharingClient): Handle upload command.

  • handle_download(args, file_sharing_client: FileSharingClient): Handle download command.

  • handle_downloadl(args, file_sharing_client: FileSharingClient): Handle downloadl command.

  • build_cli_parser(parser): Build CLI parser.

  • parse_args(): Parse CLI arguments.

  • main(args): Main function to execute CLI commands.

Usage:

python cli.py <cli_command> [-H HOST] [-u USERNAME] [-p PASSWORD] [-f FILE] [-o OUTPUT] [-n NBFILES] [-r ORDER]

param cli_command:

str Command to execute (login, logout, list, upload, download, downloadl).

param -H:

str, optional Host server URL. Default is http://localhost:5000.

param –host:

str, optional Host server URL. Default is http://localhost:5000.

param -u:

str, optional Username for authentication.

param –username:

str, optional Username for authentication.

param -p:

str, optional Password for authentication.

param –password:

str, optional Password for authentication.

param -f:

str, optional File to upload or download.

param –file:

str, optional File to upload or download.

param -o:

str, optional Directory or file path to save the downloaded file.

param –output:

str, optional Directory or file path to save the downloaded file.

param -n:

int, optional Number of files to list or download.

param –nbfiles:

int, optional Number of files to list or download.

param -r:

str, optional Order of files to list or download (asc or desc).

param –order:

str, optional Order of files to list or download (asc or desc).

Example

python cli.py list -u myusername -p mypassword -n 5

flask_file_share.cli.build_cli_parser(parser: ArgumentParser) ArgumentParser

Build CLI parser.

Parameters:

parser (argparse.ArgumentParser) – The ArgumentParser object.

Returns:

The modified ArgumentParser object.

Return type:

argparse.ArgumentParser

flask_file_share.cli.client_session_manager(host: str, username: str, password: str) Generator[FileSharingClient, None, None]

Context manager for managing the client session.

Parameters:
  • host (str) – The host server URL.

  • username (str) – The username for authentication.

  • password (str) – The password for authentication.

Yields:

FileSharingClient – An instance of the FileSharingClient.

flask_file_share.cli.handle_download(args, file_sharing_client: FileSharingClient)

Handle download command.

Parameters:
  • args (argparse.Namespace) – The parsed command-line arguments.

  • file_sharing_client (FileSharingClient) – An instance of the FileSharingClient.

flask_file_share.cli.handle_downloadl(args, file_sharing_client: FileSharingClient)

Handle downloadl command.

Parameters:
  • args (argparse.Namespace) – The parsed command-line arguments.

  • file_sharing_client (FileSharingClient) – An instance of the FileSharingClient.

flask_file_share.cli.handle_list(args, file_sharing_client: FileSharingClient)

Handle list command.

Parameters:
  • args (argparse.Namespace) – The parsed command-line arguments.

  • file_sharing_client (FileSharingClient) – An instance of the FileSharingClient.

flask_file_share.cli.handle_login(args, file_sharing_client: FileSharingClient)

Handle login command.

flask_file_share.cli.handle_logout(args, file_sharing_client: FileSharingClient)

Handle logout command.

flask_file_share.cli.handle_upload(args, file_sharing_client: FileSharingClient)

Handle upload command.

Parameters:
  • args (argparse.Namespace) – The parsed command-line arguments.

  • file_sharing_client (FileSharingClient) – An instance of the FileSharingClient.

flask_file_share.cli.main(args: Namespace)

Main function to execute CLI commands.

Parameters:

args (argparse.Namespace) – The parsed command-line arguments.

flask_file_share.cli.parse_args() Namespace

Parse CLI arguments.

Returns:

The parsed command-line arguments.

Return type:

argparse.Namespace

File: client.py

Python client for interacting with the File Sharing Flask application.

Class:

FileSharingClient: A client class for interacting with the File Sharing Flask application.

Functions:

main(): Main function to execute CLI commands.

Usage:

import flask_file_share as ffs

# Initialize the client client = ffs.Client(username=”your_username”, password=”your_password”, base_url=”http://localhost:5000”)

# Example usage: client.login() client.upload_file(“path/to/your/file.txt”) client.list_files() client.download_file(“filename.txt”, “path/to/save”) client.download_last_n_files(5, “path/to/save”) client.logout()

class flask_file_share.client.FileSharingClient(username: str, password: str, base_url: str)

Bases: object

__init__(username: str, password: str, base_url: str)

Initialize the FileSharingClient instance.

Parameters:
  • username (str) – The username for authentication.

  • password (str) – The password for authentication.

  • base_url (str) – The base URL of the server.

download_file(filename: str, folder_path: str | Path, save_filename: str = '')

Download a file from the server.

Parameters:
  • filename (str) – Name of the file to download.

  • folder_path (Union[str, Path]) – Path to the folder to save the downloaded file.

  • save_filename (str, optional) – Name to save the downloaded file as.

download_last_n_files(nb_files: int, folder_path: str | Path, filename: str | None = None)

Download the last N files from the server.

Parameters:
  • nb_files (int) – Number of files to download.

  • folder_path (Union[str, Path]) – Path to the folder to save the downloaded files.

  • filename (str, optional) – Name to save the downloaded file as.

list_files(nb_files: int = 10, order: str = 'desc')

List files on the server.

Parameters:
  • nb_files (int, optional) – Number of files to list.

  • order (str, optional) – Order of listing files.

login()

Log in to the server.

logout()

Log out from the server.

upload_file(file_path: str)

Upload a file to the server.

Parameters:

file_path (str) – Path to the file to upload.

flask_file_share.client.main()

Main function to execute CLI commands.

File: main.py

CLI module for interacting with the FlaskFileShare application.

Functions:
create_parser() -> argparse.ArgumentParser:

Create an ArgumentParser for the CLI.

main():

Main function to execute the CLI.

Usage:

python main.py server python main.py cli <cli_command> [-H HOST] [-u USERNAME] [-p PASSWORD] [-f FILE] [-o OUTPUT] [-n NBFILES] [-r ORDER]

param server:

Run the web server.

param cli:

Run the CLI tool.

param -H:

Host server URL. Default is http://localhost:5000.

param –host:

Host server URL. Default is http://localhost:5000.

param -u:

Username for authentication.

param –username:

Username for authentication.

param -p:

Password for authentication.

param –password:

Password for authentication.

param -f:

File to upload or download.

param –file:

File to upload or download.

param -o:

Directory or file path to save the downloaded file.

param –output:

Directory or file path to save the downloaded file.

param -n:

Number of files to list or download.

param –nbfiles:

Number of files to list or download.

param -r:

Order of files to list or download (asc or desc).

param –order:

Order of files to list or download (asc or desc).

Example

python main.py server python main.py cli list -u myusername -p mypassword -n 5

flask_file_share.main.create_parser() ArgumentParser

Create an ArgumentParser for the CLI.

Returns:

argparse.ArgumentParser:

The ArgumentParser object for the CLI.

flask_file_share.main.main()

Main function to execute the CLI.

File: settings.py

Settings for the File Sharing Flask application.

Description:

This file contains configuration settings for the File Sharing Flask application. It defines constants such as DEBUG mode, base directory, upload folder, data file path, static folder, templates folder, allowed hosts, and session lifetime. It also loads environment variables from a .env file if it exists.

Usage:

The settings are used throughout the application to configure various aspects such as file paths, session lifetime, and debugging mode.

- DEBUG

A boolean flag indicating whether the application is running in debug mode.

Type:

bool

- USERNAME_STR

The environment variable name for the username.

Type:

str

- PASSWORD_STR

The environment variable name for the password.

Type:

str

- SECRET_KEY_STR

The environment variable name for the secret key.

Type:

str

- USERNAME

The username used for authentication.

Type:

str

- PASSWORD

The password used for authentication.

Type:

str

- SECRET_KEY

The secret key used for encryption.

Type:

str

- BASE_DIR

The base directory path of the application.

Type:

Path

- UPLOADS_DATA_DIR

The directory path for storing uploaded data.

Type:

Path

- UPLOAD_FOLDER

The directory path for storing uploaded files.

Type:

Path

- DATA_FILE

The path to the JSON file used for storing data.

Type:

Path

- STATIC_FOLDER

The directory path for static files.

Type:

Path

- TEMPLATES_FOLDER

The directory path for template files.

Type:

Path

- ALLOWED_HOSTS

A list of allowed hostnames or IP addresses.

Type:

List[str]

- PERMANENT_SESSION_LIFETIME_MINUTES

The session timeout duration in minutes.

Type:

float