-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_io.py
35 lines (28 loc) · 1.15 KB
/
config_io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# SPDX-FileCopyrightText: 2025 Meinhard Kissich
# SPDX-License-Identifier: MIT
import configparser
import streamlit as st
class ConfigIO:
config_path = "paper2go.ini"
config_defaults_path = "paper2go_default.ini"
@classmethod
def load_config(cls, defaults=False):
parser = configparser.ConfigParser()
parser.read(cls.config_path if not defaults else cls.config_defaults_path)
st.session_state["config"] = {section: dict(parser.items(section)) for section in parser.sections()}
if defaults:
cls.store_config()
@classmethod
def store_config(cls):
parser = configparser.ConfigParser()
for section in st.session_state["config"].keys():
parser.add_section(section)
for section in st.session_state["config"].keys():
section_dict = st.session_state["config"][section]
fields = section_dict.keys()
for field in fields:
value = section_dict[field]
parser.set(section, field, str(value))
if cls.config_path is not None:
with open(cls.config_path, 'w') as f:
parser.write(f)