diff --git a/src/bmconfigparser.py b/src/bmconfigparser.py index 1ee64e94..877a3953 100644 --- a/src/bmconfigparser.py +++ b/src/bmconfigparser.py @@ -7,7 +7,6 @@ import shutil import os from datetime import datetime -import state from singleton import Singleton BMConfigDefaults = { @@ -99,7 +98,10 @@ class BMConfigParser(ConfigParser.SafeConfigParser): lambda x: x.startswith('BM-'), BMConfigParser().sections()) def read(self, filenames): + """Read config from file or list of files""" ConfigParser.ConfigParser.read(self, filenames) + if isinstance(filenames, str): + self._src = filenames for section in self.sections(): for option in self.options(section): try: @@ -116,14 +118,16 @@ class BMConfigParser(ConfigParser.SafeConfigParser): except ConfigParser.InterpolationError: continue - def save(self): - fileName = os.path.join(state.appdata, 'keys.dat') - fileNameBak = '.'.join([ - fileName, datetime.now().strftime("%Y%j%H%M%S%f"), 'bak']) + def save(self, filename=None): + """Save config to filename or to file from which it was read""" + if not filename: + filename = self._src + filename_bak = '.'.join([ + filename, datetime.now().strftime("%Y%j%H%M%S%f"), 'bak']) # create a backup copy to prevent the accidental loss due to # the disk write failure try: - shutil.copyfile(fileName, fileNameBak) + shutil.copyfile(filename, filename_bak) # The backup succeeded. fileNameExisted = True except (IOError, Exception): @@ -131,11 +135,11 @@ class BMConfigParser(ConfigParser.SafeConfigParser): # didn't exist before. fileNameExisted = False # write the file - with open(fileName, 'wb') as configfile: + with open(filename, 'wb') as configfile: self.write(configfile) # delete the backup if fileNameExisted: - os.remove(fileNameBak) + os.remove(filename_bak) def validate(self, section, option, value): try: diff --git a/src/tests/test_process.py b/src/tests/test_process.py index 73a6e493..4100514e 100644 --- a/src/tests/test_process.py +++ b/src/tests/test_process.py @@ -11,6 +11,8 @@ import unittest import psutil +from pybitmessage.bmconfigparser import BMConfigParser + def put_signal_file(path, filename): """Creates file, presence of which is a signal about some event.""" @@ -28,12 +30,28 @@ class TestProcessProto(unittest.TestCase): 'keys.dat', 'debug.log', 'messages.dat', 'knownnodes.dat', '.api_started', 'unittest.lock' ) + _settings = None @classmethod def setUpClass(cls): """Setup environment and start pybitmessage""" cls.home = os.environ['BITMESSAGE_HOME'] = tempfile.gettempdir() put_signal_file(cls.home, 'unittest.lock') + cls._start_process() + # let pybitmessage generate default config + # and update it with settings configured for test case if any + if cls._settings: + cls._stop_process(10) + config = BMConfigParser() + config.read(os.path.join(cls.home, 'keys.dat')) + cls._cleanup_files() + for setting in cls._settings.iteritems(): + config.set('bitmessagesettings', *setting) + config.save() + cls._start_process() + + @classmethod + def _start_process(cls): subprocess.call(cls._process_cmd) # nosec time.sleep(5) cls.pid = int(cls._get_readline('singleton.lock'))