Allow multiline parameters in configparser (#949)
* FIX: Exception.message does not exist in Python3 * FIX: Allow multiline configfile parameters * Added configparser tests * Reasonable file ending for test files
This commit is contained in:
parent
edf2942010
commit
9500f4db01
@ -33,9 +33,8 @@ class ParseError(Exception):
|
||||
msg = "Error parsing string at line %d:\n" % self.lineNum
|
||||
else:
|
||||
msg = "Error parsing config file '%s' at line %d:\n" % (self.fileName, self.lineNum)
|
||||
msg += "%s\n%s" % (self.line, self.message)
|
||||
msg += "%s\n%s" % (self.line, Exception.__str__(self))
|
||||
return msg
|
||||
#raise Exception()
|
||||
|
||||
|
||||
def writeConfigFile(data, fname):
|
||||
@ -93,13 +92,14 @@ def genString(data, indent=''):
|
||||
s += indent + sk + ':\n'
|
||||
s += genString(data[k], indent + ' ')
|
||||
else:
|
||||
s += indent + sk + ': ' + repr(data[k]) + '\n'
|
||||
s += indent + sk + ': ' + repr(data[k]).replace("\n", "\\\n") + '\n'
|
||||
return s
|
||||
|
||||
def parseString(lines, start=0):
|
||||
|
||||
data = OrderedDict()
|
||||
if isinstance(lines, basestring):
|
||||
lines = lines.replace("\\\n", "")
|
||||
lines = lines.split('\n')
|
||||
lines = [l for l in lines if re.search(r'\S', l) and not re.match(r'\s*#', l)] ## remove empty lines
|
||||
|
||||
|
36
pyqtgraph/tests/test_configparser.py
Normal file
36
pyqtgraph/tests/test_configparser.py
Normal file
@ -0,0 +1,36 @@
|
||||
from pyqtgraph import configfile
|
||||
import numpy as np
|
||||
import tempfile, os
|
||||
|
||||
def test_longArrays():
|
||||
"""
|
||||
Test config saving and loading of long arrays.
|
||||
"""
|
||||
tmp = tempfile.mktemp(".cfg")
|
||||
|
||||
arr = np.arange(20)
|
||||
configfile.writeConfigFile({'arr':arr}, tmp)
|
||||
config = configfile.readConfigFile(tmp)
|
||||
|
||||
assert all(config['arr'] == arr)
|
||||
|
||||
os.remove(tmp)
|
||||
|
||||
def test_multipleParameters():
|
||||
"""
|
||||
Test config saving and loading of multiple parameters.
|
||||
"""
|
||||
tmp = tempfile.mktemp(".cfg")
|
||||
|
||||
par1 = [1,2,3]
|
||||
par2 = "Test"
|
||||
par3 = {'a':3,'b':'c'}
|
||||
|
||||
configfile.writeConfigFile({'par1':par1, 'par2':par2, 'par3':par3}, tmp)
|
||||
config = configfile.readConfigFile(tmp)
|
||||
|
||||
assert config['par1'] == par1
|
||||
assert config['par2'] == par2
|
||||
assert config['par3'] == par3
|
||||
|
||||
os.remove(tmp)
|
Loading…
x
Reference in New Issue
Block a user