documented and cleaned up the python fceux config parser

This commit is contained in:
punkrockguy318 2008-12-05 17:37:01 +00:00
parent ffaa490eb1
commit c063fdaf99
2 changed files with 34 additions and 20 deletions

View File

@ -1,4 +1,5 @@
================= version 2.0.4 ( yet to be released) ============ ================= version 2.0.4 ( yet to be released) ============
* Documented and cleaned fceux config parser
* Udated about box UI * Udated about box UI
* Updated UI, explains joystick support isn't complete yet in the GUI * Updated UI, explains joystick support isn't complete yet in the GUI
* Cleaned up glade file to silence warnings * Cleaned up glade file to silence warnings

View File

@ -1,7 +1,11 @@
#!/usr/bin/env python #!/usr/bin/env python
#TODO: fix writing # config_parse.py
#TODO # This module handles the reading and writing of keys to the fceux
# config file.
#
# Lukas Sabota
# Licensed under the GPL
import re import re
import os import os
@ -10,18 +14,24 @@ class FceuxConfigParser:
def __init__(self, filename): def __init__(self, filename):
self.fn = filename self.fn = filename
def _open(self): def _open(self, mode):
try: try:
self.f = open(self.fn, "r") self.f = open(self.fn, mode)
return 1 return 1
except: 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 return 0
def readKey(self, keyname): def readKey(self, keyname):
self._open() """ readKey()
reads a key from the configfile and returns the value
"""
self._open("r")
# do some lines # do some lines
while 1: while 1:
line = self.f.readline() line = self.f.readline()
@ -33,10 +43,22 @@ class FceuxConfigParser:
# key not found # key not found
self.f.close() 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): def writeKey(self, keyname, value):
self._open() """ writeKey()
modifies an existing key in the config file.
"""
self._open("r")
buf = "" buf = ""
@ -46,7 +68,7 @@ class FceuxConfigParser:
data = self.f.read(keyname.__len__()) data = self.f.read(keyname.__len__())
buf += data buf += data
if data == "": if data == "":
return 0 return None
if data == keyname: if data == keyname:
break break
else: else:
@ -72,16 +94,7 @@ class FceuxConfigParser:
self.f.close() self.f.close()
# write the buffer to the config file # write the buffer to the config file
self.f = open(self.fn, 'w') self.f._open('w')
self.f.write(buf) self.f.write(buf)
self.f.close() self.f.close()