Commit f8d4cc7d authored by Cheryl Sabella's avatar Cheryl Sabella Committed by Terry Jan Reedy

bpo-27452: IDLE: Cleanup config.py code (GH-14577)

parent f69d5c61
...@@ -123,17 +123,11 @@ class IdleUserConfParser(IdleConfParser): ...@@ -123,17 +123,11 @@ class IdleUserConfParser(IdleConfParser):
self.RemoveEmptySections() self.RemoveEmptySections()
return not self.sections() return not self.sections()
def RemoveFile(self):
"Remove user config file self.file from disk if it exists."
if os.path.exists(self.file):
os.remove(self.file)
def Save(self): def Save(self):
"""Update user configuration file. """Update user configuration file.
If self not empty after removing empty sections, write the file If self not empty after removing empty sections, write the file
to disk. Otherwise, remove the file from disk if it exists. to disk. Otherwise, remove the file from disk if it exists.
""" """
fname = self.file fname = self.file
if fname: if fname:
...@@ -145,8 +139,8 @@ class IdleUserConfParser(IdleConfParser): ...@@ -145,8 +139,8 @@ class IdleUserConfParser(IdleConfParser):
cfgFile = open(fname, 'w') cfgFile = open(fname, 'w')
with cfgFile: with cfgFile:
self.write(cfgFile) self.write(cfgFile)
else: elif os.path.exists(self.file):
self.RemoveFile() os.remove(self.file)
class IdleConf: class IdleConf:
"""Hold config parsers for all idle config files in singleton instance. """Hold config parsers for all idle config files in singleton instance.
...@@ -171,24 +165,13 @@ class IdleConf: ...@@ -171,24 +165,13 @@ class IdleConf:
def CreateConfigHandlers(self): def CreateConfigHandlers(self):
"Populate default and user config parser dictionaries." "Populate default and user config parser dictionaries."
#build idle install path idledir = os.path.dirname(__file__)
if __name__ != '__main__': # we were imported self.userdir = userdir = self.GetUserCfgDir()
idleDir = os.path.dirname(__file__) for cfg_type in self.config_types:
else: # we were exec'ed (for testing only) self.defaultCfg[cfg_type] = IdleConfParser(
idleDir = os.path.abspath(sys.path[0]) os.path.join(idledir, f'config-{cfg_type}.def'))
self.userdir = userDir = self.GetUserCfgDir() self.userCfg[cfg_type] = IdleUserConfParser(
os.path.join(userdir, f'config-{cfg_type}.cfg'))
defCfgFiles = {}
usrCfgFiles = {}
# TODO eliminate these temporaries by combining loops
for cfgType in self.config_types: #build config file names
defCfgFiles[cfgType] = os.path.join(
idleDir, 'config-' + cfgType + '.def')
usrCfgFiles[cfgType] = os.path.join(
userDir, 'config-' + cfgType + '.cfg')
for cfgType in self.config_types: #create config parsers
self.defaultCfg[cfgType] = IdleConfParser(defCfgFiles[cfgType])
self.userCfg[cfgType] = IdleUserConfParser(usrCfgFiles[cfgType])
def GetUserCfgDir(self): def GetUserCfgDir(self):
"""Return a filesystem directory for storing user config files. """Return a filesystem directory for storing user config files.
......
...@@ -159,19 +159,6 @@ class IdleUserConfParserTest(unittest.TestCase): ...@@ -159,19 +159,6 @@ class IdleUserConfParserTest(unittest.TestCase):
self.assertFalse(parser.IsEmpty()) self.assertFalse(parser.IsEmpty())
self.assertCountEqual(parser.sections(), ['Foo']) self.assertCountEqual(parser.sections(), ['Foo'])
def test_remove_file(self):
with tempfile.TemporaryDirectory() as tdir:
path = os.path.join(tdir, 'test.cfg')
parser = self.new_parser(path)
parser.RemoveFile() # Should not raise exception.
parser.AddSection('Foo')
parser.SetOption('Foo', 'bar', 'true')
parser.Save()
self.assertTrue(os.path.exists(path))
parser.RemoveFile()
self.assertFalse(os.path.exists(path))
def test_save(self): def test_save(self):
with tempfile.TemporaryDirectory() as tdir: with tempfile.TemporaryDirectory() as tdir:
path = os.path.join(tdir, 'test.cfg') path = os.path.join(tdir, 'test.cfg')
......
Cleanup ``config.py`` by inlining ``RemoveFile`` and simplifying the handling of ``file`` in ``CreateConfigHandlers``.
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment