Load a config.json file from the root of the littlefs flash filesystem. Signed-off-by: Matthias Blankertz <matthias@blankertz.org>
45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
# SPDX-License-Identifier: MIT
|
|
# Copyright (c) 2025 Matthias Blankertz <matthias@blankertz.org>
|
|
|
|
from errno import ENOENT
|
|
import json
|
|
import os
|
|
|
|
|
|
class Configuration:
|
|
DEFAULT_CONFIG = {
|
|
}
|
|
|
|
def __init__(self, config_path='/config.json'):
|
|
self.config_path = config_path
|
|
try:
|
|
with open(self.config_path, 'r') as conf_file:
|
|
self.config = json.load(conf_file)
|
|
except OSError as ex:
|
|
if ex.errno == ENOENT:
|
|
self.config = Configuration.DEFAULT_CONFIG
|
|
self._save()
|
|
else:
|
|
raise
|
|
except ValueError as ex:
|
|
print(f"Warning: Could not load configuration {self.config_path}:\n{ex}")
|
|
self._move_config_to_backup()
|
|
self.config = Configuration.DEFAULT_CONFIG
|
|
|
|
def _move_config_to_backup(self):
|
|
# Remove old backup
|
|
try:
|
|
os.remove(self.config_path + '.bup')
|
|
os.rename(self.config_path, self.config_path + '.bup')
|
|
except OSError as ex:
|
|
if ex.errno != ENOENT:
|
|
raise
|
|
os.sync()
|
|
|
|
def _save(self):
|
|
with open(self.config_path + '.new', 'w') as conf_file:
|
|
json.dump(self.config, conf_file)
|
|
self._move_config_to_backup()
|
|
os.rename(self.config_path + '.new', self.config_path)
|
|
os.sync()
|