mirror of
https://github.com/infinition/Bjorn.git
synced 2025-12-06 06:11:46 +00:00
Clone
1
shared.py
infinition edited this page 2024-07-06 01:02:56 +02:00
Table of Contents
- shared.py SharedData
- Initialization and Start of SharedData
- SharedData Class
- __init__ Method
- initialize_paths Method
- get_default_config Method
- setup_environment Method
- initialize_epd_display Method
- initialize_variables Method
- create_livestatusfile Method
- generate_actions_json Method
- initialize_csv Method
- load_config Method
- save_config Method
- load_fonts Method
- load_font Method
- load_images Method
- update_bjornstatus Method
- load_image Method
- update_image_randomizer Method
- wrap_text Method
- read_data Method
- write_data Method
- update_stats Method
- print Method
- Summary
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
SharedDataclass 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
SharedDataclass is created to initialize shared resources and data.
SharedData Class
__init__ Method
- Purpose: Initializes shared data and resources.
- Key Steps:
- Initialize Paths:
- Calls the
initialize_pathsmethod to set up directory and file paths.
- Calls the
- Initialize Variables:
- Sets initial values for various attributes such as
last_comment_time,default_config, andconfig.
- Sets initial values for various attributes such as
- Setup Environment:
- Calls the
setup_environmentmethod to configure necessary directories and files.
- Calls the
- Create Live Status File:
- Calls the
create_livestatusfilemethod to create a file for storing the current status of scans.
- Calls the
- Load Fonts and Images:
- Calls the
load_fontsandload_imagesmethods to load necessary fonts and images.
- Calls the
- Initialize Paths:
initialize_paths Method
- Purpose: Sets up directory and file paths used by the application.
- Key Steps:
- Initialize Directories:
- Sets up paths for various directories such as
configdir,datadir,actions_dir,webdir,resourcesdir, and subdirectories.
- Sets up paths for various directories such as
- Initialize Files:
- Sets up paths for various files such as
shared_config_json,actions_file,netkbfile,livestatusfile, and others.
- Sets up paths for various files such as
- Initialize Directories:
get_default_config Method
- Purpose: Provides the default configuration settings for the application.
- Key Steps:
- Return Default Config:
- Returns a dictionary containing default configuration values for various settings.
- Return Default Config:
setup_environment Method
- Purpose: Sets up the environment by creating necessary directories and files.
- Key Steps:
- Clear Screen:
- Clears the terminal screen.
- Load Configuration:
- Calls the
load_configmethod to load configuration settings.
- Calls the
- Generate Actions JSON:
- Calls the
generate_actions_jsonmethod to create the actions configuration file.
- Calls the
- Initialize CSV:
- Calls the
initialize_csvmethod to set up the network knowledge base CSV file.
- Calls the
- Initialize EPD Display:
- Calls the
initialize_epd_displaymethod to set up the e-paper display.
- Calls the
- Clear Screen:
initialize_epd_display Method
- Purpose: Initializes the e-paper display.
- Key Steps:
- Set Up Display:
- Sets up the e-paper display and its dimensions.
- Set Up Display:
initialize_variables Method
- Purpose: Sets initial values for various attributes used by the application.
- Key Steps:
- Initialize Flags and Counters:
- Initializes various flags and counters such as
should_exit,bluetooth_active,wifi_connected, and others.
- Initializes various flags and counters such as
- Initialize Flags and Counters:
create_livestatusfile Method
- Purpose: Creates a file to store the current status of network scans.
- Key Steps:
- Check File Existence:
- Checks if the file exists and creates it if not, writing initial headers and values.
- Check File Existence:
generate_actions_json Method
- Purpose: Creates the actions configuration file.
- Key Steps:
- Iterate Over Actions:
- Iterates over action modules to extract necessary attributes and generate the JSON configuration file.
- Iterate Over Actions:
initialize_csv Method
- Purpose: Sets up the network knowledge base CSV file with headers.
- Key Steps:
- Check File Existence:
- Checks if the CSV file exists and creates it if not, writing initial headers and values.
- Check File Existence:
load_config Method
- Purpose: Loads the configuration from the shared configuration JSON file.
- Key Steps:
- Load Configuration File:
- Loads the configuration file and updates attributes with the loaded values.
- Load Configuration File:
save_config Method
- Purpose: Saves the current configuration to the shared configuration JSON file.
- Key Steps:
- Create Configuration Directory:
- Creates the configuration directory if it doesn't exist.
- Write Configuration File:
- Writes the current configuration to the JSON file.
- Create Configuration Directory:
load_fonts Method
- Purpose: Loads the fonts required by the application.
- Key Steps:
- Load Individual Fonts:
- Calls the
load_fontmethod for each font file needed.
- Calls the
- Load Individual Fonts:
load_font Method
- Purpose: Loads a specific font.
- Key Steps:
- Load Font File:
- Loads a font file from the specified path and size.
- Load Font File:
load_images Method
- Purpose: Loads the images required for the e-paper display.
- Key Steps:
- Load Static Images:
- Loads individual static images from the specified directory.
- Load Image Series:
- Dynamically loads image series for different statuses from subdirectories.
- Load Static Images:
update_bjornstatus Method
- Purpose: Updates the status image displayed on the e-paper display.
- Key Steps:
- Get Status Image:
- Uses the current status to fetch the corresponding image for display.
- Get Status Image:
load_image Method
- Purpose: Loads an image from the specified path.
- Key Steps:
- Check Image Path:
- Checks if the image file exists and loads it if it does.
- Check Image Path:
update_image_randomizer Method
- Purpose: Updates the image randomizer and the image generation variable.
- Key Steps:
- Select Random Image:
- Selects a random image from the available images for the current status.
- Select Random Image:
wrap_text Method
- Purpose: Wraps text to fit within a specified width when rendered.
- Key Steps:
- Split Text into Lines:
- Splits the input text into lines that fit within the specified width.
- Split Text into Lines:
read_data Method
- Purpose: Reads data from the network knowledge base CSV file.
- Key Steps:
- Read CSV File:
- Reads the CSV file and returns the data as a list of dictionaries.
- Read CSV File:
write_data Method
- Purpose: Writes data to the network knowledge base CSV file.
- Key Steps:
- Merge New Data with Existing Data:
- Merges new data with existing data and writes the updated data back to the CSV file.
- Merge New Data with Existing Data:
update_stats Method
- Purpose: Updates the application statistics based on predefined formulas.
- Key Steps:
- Calculate Statistics:
- Calculates various statistics such as coin count and level number based on network knowledge base data.
- Calculate Statistics:
print Method
- Purpose: Prints a debug message if debug mode is enabled.
- Key Steps:
- Check Debug Mode:
- Checks if debug mode is enabled and prints the message if it is.
- Check Debug Mode:
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.