/usr/share/hatari/hatariui/config.py is in hatari 1.7.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | #!/usr/bin/env python
#
# Class and helper functions for handling (Hatari) INI style
# configuration files: loading, saving, setting/getting variables,
# mapping them to sections, listing changes
#
# Copyright (C) 2008-2012 by Eero Tamminen
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import os
# mapping from Hatari config variable name to type id (Bool, Int, String)
from conftypes import conftypes
# ------------------------------------------------------
# Helper functions for type safe Hatari configuration variable access.
# Map booleans, integers and strings to Python types, and back to strings.
def value_to_text(key, value):
"value_to_text(key, value) -> text, convert Python type to string"
assert(key in conftypes)
valtype = type(value)
if valtype == bool:
assert(conftypes[key] == "Bool")
if value:
text = "TRUE"
else:
text = "FALSE"
elif valtype == int:
assert(conftypes[key] == "Int")
text = str(value)
else:
assert(conftypes[key] == "String")
if value == None:
text = ""
else:
text = value
return text
def text_to_value(text):
"text_to_value(text) -> value, convert INI file values to real types"
# bool?
upper = text.upper()
if upper == "FALSE":
value = False
elif upper == "TRUE":
value = True
else:
try:
# integer?
value = int(text)
except ValueError:
# string
value = text
return value
# ------------------------------------------------------
# Handle INI style configuration files as used by Hatari
class ConfigStore:
def __init__(self, userconfdir, defaults = {}, miss_is_error = True):
"ConfigStore(userconfdir, fgfile[,defaults,miss_is_error])"
self.defaults = defaults
self.userpath = self._get_full_userpath(userconfdir)
self.miss_is_error = miss_is_error
def _get_full_userpath(self, leafdir):
"get_userpath(leafdir) -> config file default save path from HOME, CWD or their subdir"
# user's hatari.cfg can be in home or current work dir,
# current dir is used only if $HOME fails
for path in (os.getenv("HOME"), os.getenv("HOMEPATH"), os.getcwd()):
if path and os.path.exists(path) and os.path.isdir(path):
if leafdir:
hpath = "%s%c%s" % (path, os.path.sep, leafdir)
if os.path.exists(hpath) and os.path.isdir(hpath):
return hpath
return path
return None
def get_filepath(self, filename):
"get_filepath(filename) -> return correct full path to config file"
# user config has preference over system one
for path in (self.userpath, os.getenv("HATARI_SYSTEM_CONFDIR")):
if path:
file = "%s%c%s" % (path, os.path.sep, filename)
if os.path.isfile(file):
return file
# writing needs path name although it's missing for reading
return "%s%c%s" % (self.userpath, os.path.sep, filename)
def load(self, path):
"load(path) -> load given configuration file"
if os.path.isfile(path):
sections = self._read(path)
if sections:
self.sections = sections
else:
print("ERROR: configuration file loading failed!")
return
else:
print("WARNING: configuration file missing!")
if self.defaults:
print("-> using dummy 'defaults'.")
self.sections = self.defaults
self.path = path
self.cfgfile = os.path.basename(path)
self.original = self.get_checkpoint()
self.changed = False
def is_loaded(self):
"is_loaded() -> True if configuration loading succeeded"
if self.sections:
return True
return False
def get_path(self):
"get_path() -> configuration file path"
return self.path
def _read(self, path):
"_read(path) -> (all keys, section2key mappings)"
print("Reading configuration file '%s'..." % path)
config = open(path, "r")
if not config:
return ({}, {})
name = "[_orphans_]"
seckeys = {}
sections = {}
for line in config.readlines():
line = line.strip()
if not line or line[0] == '#':
continue
if line[0] == '[':
if line in sections:
print("WARNING: section '%s' twice in configuration" % line)
if seckeys:
sections[name] = seckeys
seckeys = {}
name = line
continue
if line.find('=') < 0:
print("WARNING: line without key=value pair:\n%s" % line)
continue
key, text = [string.strip() for string in line.split('=')]
seckeys[key] = text_to_value(text)
if seckeys:
sections[name] = seckeys
return sections
def get_checkpoint(self):
"get_checkpoint() -> checkpoint, get the state of variables at this point"
checkpoint = {}
for section in self.sections.keys():
checkpoint[section] = self.sections[section].copy()
return checkpoint
def get_checkpoint_changes(self, checkpoint):
"get_checkpoint_changes() -> list of (key, value) pairs for later changes"
changed = []
if not self.changed:
return changed
for section in self.sections.keys():
if section not in checkpoint:
for key, value in self.sections[section].items():
changed.append((key, value))
continue
for key, value in self.sections[section].items():
if (key not in checkpoint[section] or
value != checkpoint[section][key]):
text = value_to_text(key, value)
changed.append(("%s.%s" % (section, key), text))
return changed
def revert_to_checkpoint(self, checkpoint):
"revert_to_checkpoint(checkpoint), revert to given checkpoint"
self.sections = checkpoint
def get(self, section, key):
return self.sections[section][key]
def set(self, section, key, value):
"set(section,key,value), set given key to given section"
if section not in self.sections:
if self.miss_is_error:
raise AttributeError("no section '%s'" % section)
self.sections[section] = {}
if key not in self.sections[section]:
if self.miss_is_error:
raise AttributeError("key '%s' not in section '%s'" % (key, section))
self.sections[section][key] = value
self.changed = True
elif self.sections[section][key] != value:
self.changed = True
self.sections[section][key] = value
def is_changed(self):
"is_changed() -> True if current configuration is changed"
return self.changed
def get_changes(self):
"get_changes(), return (key, value) list for each changed config option"
return self.get_checkpoint_changes(self.original)
def write(self, fileobj):
"write(fileobj), write current configuration to given file object"
sections = list(self.sections.keys())
sections.sort()
for name in sections:
fileobj.write("%s\n" % name)
keys = list(self.sections[name].keys())
keys.sort()
for key in keys:
value = value_to_text(key, self.sections[name][key])
fileobj.write("%s = %s\n" % (key, value))
fileobj.write("\n")
def save(self):
"save() -> path, if configuration changed, save it"
if not self.changed:
print("No configuration changes to save, skipping")
return None
fileobj = None
if self.path:
try:
fileobj = open(self.path, "w")
except:
pass
if not fileobj:
print("WARNING: non-existing/writable configuration file, creating a new one...")
if not os.path.exists(self.userpath):
os.makedirs(self.userpath)
self.path = "%s%c%s" % (self.userpath, os.path.sep, self.cfgfile)
fileobj = open(self.path, "w")
if not fileobj:
print("ERROR: opening '%s' for saving failed" % self.path)
return None
self.write(fileobj)
print("Saved configuration file:", self.path)
self.changed = False
return self.path
def save_as(self, path):
"save_as(path) -> path, save configuration to given file and select it"
assert(path)
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
self.path = path
self.changed = True
return self.save()
def save_tmp(self, path):
"save_tmp(path) -> path, save configuration to given file without selecting it"
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
fileobj = open(path, "w")
if not fileobj:
print("ERROR: opening '%s' for saving failed" % path)
return None
self.write(fileobj)
print("Saved temporary configuration file:", path)
return path
|