From c063fdaf99b59ca9dc294a4da0d5097d07e9d42b Mon Sep 17 00:00:00 2001 From: punkrockguy318 Date: Fri, 5 Dec 2008 17:37:01 +0000 Subject: [PATCH] documented and cleaned up the python fceux config parser --- ChangeLog | 1 + config_parse.py | 53 ++++++++++++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/ChangeLog b/ChangeLog index ce395083..07b94651 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ ================= version 2.0.4 ( yet to be released) ============ +* Documented and cleaned fceux config parser * Udated about box UI * Updated UI, explains joystick support isn't complete yet in the GUI * Cleaned up glade file to silence warnings diff --git a/config_parse.py b/config_parse.py index 4fae7eaf..5c5c29f6 100755 --- a/config_parse.py +++ b/config_parse.py @@ -1,7 +1,11 @@ #!/usr/bin/env python -#TODO: fix writing -#TODO +# config_parse.py +# This module handles the reading and writing of keys to the fceux +# config file. +# +# Lukas Sabota +# Licensed under the GPL import re import os @@ -10,18 +14,24 @@ class FceuxConfigParser: def __init__(self, filename): self.fn = filename - def _open(self): + def _open(self, mode): try: - self.f = open(self.fn, "r") + self.f = open(self.fn, mode) return 1 except: - print "Can't open config." + if mode == "r": + print "Can't open config for reading." + else: + print "Couldn't write to the config." return 0 def readKey(self, keyname): - self._open() + """ readKey() + reads a key from the configfile and returns the value + """ + self._open("r") # do some lines while 1: line = self.f.readline() @@ -33,10 +43,22 @@ class FceuxConfigParser: # key not found self.f.close() - return 0 - + return None + + def addKey(self, keyname, value=""): + """ addKey() + adds a key with a (optional) value to the config file. + """ + + self._open("a") + self.f.write(keyname + " = " + str(value)) + self.f.close() + def writeKey(self, keyname, value): - self._open() + """ writeKey() + modifies an existing key in the config file. + """ + self._open("r") buf = "" @@ -46,7 +68,7 @@ class FceuxConfigParser: data = self.f.read(keyname.__len__()) buf += data if data == "": - return 0 + return None if data == keyname: break else: @@ -72,16 +94,7 @@ class FceuxConfigParser: self.f.close() # write the buffer to the config file - self.f = open(self.fn, 'w') + self.f._open('w') self.f.write(buf) self.f.close() - - - - - - - - -