Huge refactor of the libstormgames library. It is hopefully mostly backwards compatible. Still lots of testing to do, and probably some fixes needed, but this is a good start.
This commit is contained in:
102
config.py
Normal file
102
config.py
Normal file
@ -0,0 +1,102 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Configuration management for Storm Games.
|
||||
|
||||
Provides functionality for:
|
||||
- Reading and writing configuration files
|
||||
- Global and local configuration handling
|
||||
"""
|
||||
|
||||
import configparser
|
||||
import os
|
||||
from xdg import BaseDirectory
|
||||
|
||||
class Config:
|
||||
"""Configuration management class for Storm Games."""
|
||||
|
||||
def __init__(self, game_title):
|
||||
"""Initialize configuration system for a game.
|
||||
|
||||
Args:
|
||||
game_title (str): Title of the game
|
||||
"""
|
||||
self.game_title = game_title
|
||||
self.global_path = os.path.join(BaseDirectory.xdg_config_home, "storm-games")
|
||||
self.game_path = os.path.join(self.global_path,
|
||||
str.lower(str.replace(game_title, " ", "-")))
|
||||
|
||||
# Create game directory if it doesn't exist
|
||||
if not os.path.exists(self.game_path):
|
||||
os.makedirs(self.game_path)
|
||||
|
||||
# Initialize config parsers
|
||||
self.local_config = configparser.ConfigParser()
|
||||
self.global_config = configparser.ConfigParser()
|
||||
|
||||
# Load existing configurations
|
||||
self.read_local_config()
|
||||
self.read_global_config()
|
||||
|
||||
def read_local_config(self):
|
||||
"""Read local configuration from file."""
|
||||
try:
|
||||
with open(os.path.join(self.game_path, "config.ini"), 'r') as configfile:
|
||||
self.local_config.read_file(configfile)
|
||||
except:
|
||||
pass
|
||||
|
||||
def read_global_config(self):
|
||||
"""Read global configuration from file."""
|
||||
try:
|
||||
with open(os.path.join(self.global_path, "config.ini"), 'r') as configfile:
|
||||
self.global_config.read_file(configfile)
|
||||
except:
|
||||
pass
|
||||
|
||||
def write_local_config(self):
|
||||
"""Write local configuration to file."""
|
||||
with open(os.path.join(self.game_path, "config.ini"), 'w') as configfile:
|
||||
self.local_config.write(configfile)
|
||||
|
||||
def write_global_config(self):
|
||||
"""Write global configuration to file."""
|
||||
with open(os.path.join(self.global_path, "config.ini"), 'w') as configfile:
|
||||
self.global_config.write(configfile)
|
||||
|
||||
# Global variables for backward compatibility
|
||||
localConfig = configparser.ConfigParser()
|
||||
globalConfig = configparser.ConfigParser()
|
||||
gamePath = ""
|
||||
globalPath = ""
|
||||
|
||||
def write_config(write_global=False):
|
||||
"""Write configuration to file.
|
||||
|
||||
Args:
|
||||
write_global (bool): If True, write to global config, otherwise local (default: False)
|
||||
"""
|
||||
if not write_global:
|
||||
with open(gamePath + "/config.ini", 'w') as configfile:
|
||||
localConfig.write(configfile)
|
||||
else:
|
||||
with open(globalPath + "/config.ini", 'w') as configfile:
|
||||
globalConfig.write(configfile)
|
||||
|
||||
def read_config(read_global=False):
|
||||
"""Read configuration from file.
|
||||
|
||||
Args:
|
||||
read_global (bool): If True, read global config, otherwise local (default: False)
|
||||
"""
|
||||
if not read_global:
|
||||
try:
|
||||
with open(gamePath + "/config.ini", 'r') as configfile:
|
||||
localConfig.read_file(configfile)
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
with open(globalPath + "/config.ini", 'r') as configfile:
|
||||
globalConfig.read_file(configfile)
|
||||
except:
|
||||
pass
|
Reference in New Issue
Block a user