Load the images for the e-paper display, scaled for display size.
(self)
| 1628 | logger.error(f"Unexpected error in initialize_csv: {e}") |
| 1629 | |
| 1630 | |
| 1631 | def load_config(self): |
| 1632 | """Load the configuration from the shared configuration JSON file.""" |
| 1633 | try: |
| 1634 | logger.info("Loading configuration...") |
| 1635 | if os.path.exists(self.shared_config_json): |
| 1636 | # Check if file is empty before attempting to parse |
| 1637 | if os.path.getsize(self.shared_config_json) == 0: |
| 1638 | logger.warning("Configuration file is empty, creating new one with default values...") |
| 1639 | self.save_config() |
| 1640 | return |
| 1641 | with open(self.shared_config_json, 'r') as f: |
| 1642 | config = json.load(f) |
| 1643 | config = self._normalize_config_keys(config) |
| 1644 | self.config.update(config) |
| 1645 | self.config = self._normalize_config_keys(self.config) |
| 1646 | for key, value in self.config.items(): |
| 1647 | setattr(self, key, value) |
| 1648 | self._remove_legacy_attributes() |
| 1649 | else: |
| 1650 | logger.warning("Configuration file not found, creating new one with default values...") |
| 1651 | self.save_config() |
| 1652 | self.load_config() |
| 1653 | time.sleep(2) |
| 1654 | except json.JSONDecodeError as e: |
| 1655 | logger.error(f"Configuration file is corrupted or invalid JSON: {e}") |
| 1656 | logger.warning("Recreating configuration file with default values...") |
| 1657 | self.save_config() |
| 1658 | except FileNotFoundError: |
| 1659 | logger.error("Error loading configuration: File not found.") |
| 1660 | self.save_config() |
| 1661 | |
| 1662 | def save_config(self): |
| 1663 | """Save the configuration to the shared configuration JSON file. |
| 1664 | |
| 1665 | Holds _config_lock so concurrent writers to self.config can't change |
| 1666 | the dict mid-serialization (would raise "dictionary changed size |
| 1667 | during iteration"). Serializes a shallow snapshot to keep the lock |
| 1668 | window short. |
| 1669 | """ |
| 1670 | logger.info("Saving configuration...") |
| 1671 | try: |
| 1672 | if not os.path.exists(self.configdir): |
| 1673 | os.makedirs(self.configdir) |
| 1674 | logger.info(f"Created configuration directory at {self.configdir}") |
| 1675 | try: |
| 1676 | with self._config_lock: |
| 1677 | self.config = self._normalize_config_keys(self.config) |
| 1678 | snapshot = dict(self.config) |
| 1679 | with open(self.shared_config_json, 'w') as f: |
| 1680 | json.dump(snapshot, f, indent=4) |
| 1681 | logger.info(f"Configuration saved to {self.shared_config_json}") |
| 1682 | except IOError as e: |
| 1683 | logger.error(f"Error writing to configuration file: {e}") |
| 1684 | except Exception as e: |
| 1685 | logger.error(f"Unexpected error while writing to configuration file: {e}") |
| 1686 | except OSError as e: |
| 1687 | logger.error(f"OS error while creating configuration directory: {e}") |
no test coverage detected