Skip to content

shared.py

infinition edited this page Jul 5, 2024 · 1 revision

shared.py SharedData

This document describes the detailed step-by-step process of how the SharedData class works, including the specific methods, classes, and functions used at each step.

Initialization and Start of SharedData

Importing Modules

  • The SharedData class imports several modules, including standard libraries (os, sys, json, importlib, random, time, csv, logging) and custom modules (Image, ImageFont, epd2in13_V2, Logger).

Creating SharedData Instance

  • An instance of the SharedData class is created to initialize shared resources and data.

SharedData Class

__init__ Method

  • Purpose: Initializes shared data and resources.
  • Key Steps:
    1. Initialize Paths:
      • Calls the initialize_paths method to set up directory and file paths.
    2. Initialize Variables:
      • Sets initial values for various attributes such as last_comment_time, default_config, and config.
    3. Setup Environment:
      • Calls the setup_environment method to configure necessary directories and files.
    4. Create Live Status File:
      • Calls the create_livestatusfile method to create a file for storing the current status of scans.
    5. Load Fonts and Images:
      • Calls the load_fonts and load_images methods to load necessary fonts and images.

initialize_paths Method

  • Purpose: Sets up directory and file paths used by the application.
  • Key Steps:
    1. Initialize Directories:
      • Sets up paths for various directories such as configdir, datadir, actions_dir, webdir, resourcesdir, and subdirectories.
    2. Initialize Files:
      • Sets up paths for various files such as shared_config_json, actions_file, netkbfile, livestatusfile, and others.

get_default_config Method

  • Purpose: Provides the default configuration settings for the application.
  • Key Steps:
    1. Return Default Config:
      • Returns a dictionary containing default configuration values for various settings.

setup_environment Method

  • Purpose: Sets up the environment by creating necessary directories and files.
  • Key Steps:
    1. Clear Screen:
      • Clears the terminal screen.
    2. Load Configuration:
      • Calls the load_config method to load configuration settings.
    3. Generate Actions JSON:
      • Calls the generate_actions_json method to create the actions configuration file.
    4. Initialize CSV:
      • Calls the initialize_csv method to set up the network knowledge base CSV file.
    5. Initialize EPD Display:
      • Calls the initialize_epd_display method to set up the e-paper display.

initialize_epd_display Method

  • Purpose: Initializes the e-paper display.
  • Key Steps:
    1. Set Up Display:
      • Sets up the e-paper display and its dimensions.

initialize_variables Method

  • Purpose: Sets initial values for various attributes used by the application.
  • Key Steps:
    1. Initialize Flags and Counters:
      • Initializes various flags and counters such as should_exit, bluetooth_active, wifi_connected, and others.

create_livestatusfile Method

  • Purpose: Creates a file to store the current status of network scans.
  • Key Steps:
    1. Check File Existence:
      • Checks if the file exists and creates it if not, writing initial headers and values.

generate_actions_json Method

  • Purpose: Creates the actions configuration file.
  • Key Steps:
    1. Iterate Over Actions:
      • Iterates over action modules to extract necessary attributes and generate the JSON configuration file.

initialize_csv Method

  • Purpose: Sets up the network knowledge base CSV file with headers.
  • Key Steps:
    1. Check File Existence:
      • Checks if the CSV file exists and creates it if not, writing initial headers and values.

load_config Method

  • Purpose: Loads the configuration from the shared configuration JSON file.
  • Key Steps:
    1. Load Configuration File:
      • Loads the configuration file and updates attributes with the loaded values.

save_config Method

  • Purpose: Saves the current configuration to the shared configuration JSON file.
  • Key Steps:
    1. Create Configuration Directory:
      • Creates the configuration directory if it doesn't exist.
    2. Write Configuration File:
      • Writes the current configuration to the JSON file.

load_fonts Method

  • Purpose: Loads the fonts required by the application.
  • Key Steps:
    1. Load Individual Fonts:
      • Calls the load_font method for each font file needed.

load_font Method

  • Purpose: Loads a specific font.
  • Key Steps:
    1. Load Font File:
      • Loads a font file from the specified path and size.

load_images Method

  • Purpose: Loads the images required for the e-paper display.
  • Key Steps:
    1. Load Static Images:
      • Loads individual static images from the specified directory.
    2. Load Image Series:
      • Dynamically loads image series for different statuses from subdirectories.

update_bjornstatus Method

  • Purpose: Updates the status image displayed on the e-paper display.
  • Key Steps:
    1. Get Status Image:
      • Uses the current status to fetch the corresponding image for display.

load_image Method

  • Purpose: Loads an image from the specified path.
  • Key Steps:
    1. Check Image Path:
      • Checks if the image file exists and loads it if it does.

update_image_randomizer Method

  • Purpose: Updates the image randomizer and the image generation variable.
  • Key Steps:
    1. Select Random Image:
      • Selects a random image from the available images for the current status.

wrap_text Method

  • Purpose: Wraps text to fit within a specified width when rendered.
  • Key Steps:
    1. Split Text into Lines:
      • Splits the input text into lines that fit within the specified width.

read_data Method

  • Purpose: Reads data from the network knowledge base CSV file.
  • Key Steps:
    1. Read CSV File:
      • Reads the CSV file and returns the data as a list of dictionaries.

write_data Method

  • Purpose: Writes data to the network knowledge base CSV file.
  • Key Steps:
    1. Merge New Data with Existing Data:
      • Merges new data with existing data and writes the updated data back to the CSV file.

update_stats Method

  • Purpose: Updates the application statistics based on predefined formulas.
  • Key Steps:
    1. Calculate Statistics:
      • Calculates various statistics such as coin count and level number based on network knowledge base data.

print Method

  • Purpose: Prints a debug message if debug mode is enabled.
  • Key Steps:
    1. Check Debug Mode:
      • Checks if debug mode is enabled and prints the message if it is.

Summary

The SharedData class is responsible for managing shared resources and data for different modules in the Bjorn project. It handles the initialization and configuration of paths, logging, fonts, and images, sets up the environment, creates necessary directories and files, and manages the loading and saving of configuration settings. The class also provides utility functions for reading and writing data to CSV files, updating statistics, and wrapping text for display purposes.