New python object: FceuxConfigParser. This will be used by gfceux to map different things in the config.
This commit is contained in:
parent
9da46b197c
commit
1e7a87f370
|
@ -5,51 +5,78 @@
|
|||
import re
|
||||
import os
|
||||
|
||||
class ConfigParser:
|
||||
class FceuxConfigParser:
|
||||
def __init__(self, filename):
|
||||
try:
|
||||
f = open(filename, "rw");
|
||||
f = open(filename, "r+")
|
||||
except:
|
||||
print "Can't open config."
|
||||
|
||||
self.f = f
|
||||
self.fn = filename
|
||||
|
||||
def _open(self):
|
||||
try:
|
||||
self.f = open(self.fn, "r+")
|
||||
return 1
|
||||
except:
|
||||
print "Can't open config."
|
||||
return 0
|
||||
|
||||
def _close(self):
|
||||
self.f.close()
|
||||
|
||||
|
||||
|
||||
def readKey(self, keyname):
|
||||
self.f.seek(0)
|
||||
# do some lines
|
||||
self._open()
|
||||
# do some lines
|
||||
while 1:
|
||||
line = self.f.readline()
|
||||
if line == 0:
|
||||
break
|
||||
if re.search(keyname, line):
|
||||
self._close()
|
||||
return line
|
||||
|
||||
# key not found
|
||||
self._close()
|
||||
return 0
|
||||
|
||||
def writeKey(self, keyname, value):
|
||||
self.f.seek(0)
|
||||
self._open()
|
||||
|
||||
cursor = 0
|
||||
# find the key
|
||||
while 1:
|
||||
line = self.f.readline()
|
||||
if line == 0:
|
||||
key = self.f.read(keyname.__len__())
|
||||
if key == "":
|
||||
return 0
|
||||
if re.search(keyname, line):
|
||||
cursor = self.f.tell()
|
||||
if key == keyname:
|
||||
print key
|
||||
break
|
||||
|
||||
newline = line.split('=')[0] + '=' + str(value)
|
||||
self.f.seek(os.SEEK_CUR, -line.split('=')[0].__len__)
|
||||
else:
|
||||
self.f.readline()
|
||||
# move back until we find a =
|
||||
#while self.f.read(1) != '=':
|
||||
# self.f.seek(1, -2)
|
||||
|
||||
|
||||
|
||||
print self.f.read(3)
|
||||
|
||||
self.f.write(value)
|
||||
|
||||
self._close()
|
||||
|
||||
|
||||
def close(self):
|
||||
self.f.close()
|
||||
print "hey"
|
||||
|
||||
|
||||
cp = ConfigParser("/home/lukas/.fceultra/fceu.cfg")
|
||||
cp.writeKey("Fullscreen", 1)
|
||||
#print cp.readKey("Fullscreen")
|
||||
cp = FceuxConfigParser("/home/lukas/.fceux/fceux.cfg")
|
||||
cp.writeKey("SDL.Fullscreen", "1")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue