8000 bpo-27452: IDLE: Cleanup config.py code (GH-14577) · python/cpython@f8d4cc7 · GitHub
[go: up one dir, main page]

Skip to content

Commit f8d4cc7

Browse files
csabellaterryjreedy
authored andcommitted
bpo-27452: IDLE: Cleanup config.py code (GH-14577)
1 parent f69d5c6 commit f8d4cc7

File tree

3 files changed

+10
-39
lines changed

3 files changed

+10
-39
lines changed

Lib/idlelib/config.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,11 @@ def IsEmpty(self):
123123
self.RemoveEmptySections()
124124
return not self.sections()
125125

126-
def RemoveFile(self):
127-
"Remove user config file self.file from disk if it exists."
128-
if os.path.exists(self.file):
129-
os.remove(self.file)
130-
131126
def Save(self):
132127
"""Update user configuration file.
133128
134129
If self not empty after removing empty sections, write the file
135130
to disk. Otherwise, remove the file from disk if it exists.
136-
137131
"""
138132
fname = self.file
139133
if fname:
@@ -145,8 +139,8 @@ def Save(self):
145139
cfgFile = open(fname, 'w')
146140
with cfgFile:
147141
self.write(cfgFile)
148-
else:
149-
self.RemoveFile()
142+
elif os.path.exists(self.file):
143+
os.remove(self.file)
150144

151145
class IdleConf:
152146
"""Hold config parsers for all idle config files in singleton instance.
@@ -171,24 +165,13 @@ def __init__(self, _utest=False):
171165

172166
def CreateConfigHandlers(self):
173167
"Populate default and user config parser dictionaries."
174-
#build idle install path
175-
if __name__ != '__main__': # we were imported
176-
idleDir = os.path.dirname(__file__)
177-
else: # we were exec'ed (for testing only)
178-
idleDir = os.path.abspath(sys.path[0])
179-
self.userdir = userDir = self.GetUserCfgDir()
180-
181-
defCfgFiles = {}
182-
usrCfgFiles = {}
183-
# TODO eliminate these temporaries by combining loops
184-
for cfgType in self.config_types: #build config file names
185-
defCfgFiles[cfgType] = os.path.join(
186-
idleDir, 'config-' + cfgType + '.def')
187-
usrCfgFiles[cfgType] = os.path.join(
188-
userDir, 'config-' + cfgType + '.cfg')
189-
for cfgType in self.config_types: #create config parsers
190-
self.defaultCfg[cfgType] = IdleConfParser(defCfgFiles[cfgType])
191-
self.userCfg[cfgType] = IdleUserConfParser(usrCfgFiles[cfgType])
168+
idledir = os.path.dirname(__file__)
169+
self.userdir = userdir = self.GetUserCfgDir()
170+
for cfg_type in self.config_types:
171+
self.defaultCfg[cfg_type] = IdleConfParser(
172+
os.path.join(idledir, f'config-{cfg_type}.def'))
173+
self.userCfg[cfg_type] = IdleUserConfParser(
174+
os.path.join(userdir, f'config-{cfg_type}.cfg'))
192175

193176
def GetUserCfgDir(self):
194177
"""Return a filesystem directory for storing user config files.

Lib/idlelib/idle_test/test_config.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,6 @@ def test_is_empty(self):
159159
self.assertFalse(parser.IsEmpty())
160160
self.assertCountEqual(parser.sections(), ['Foo'])
161161

162-
def test_remove_file(self):
163-
with tempfile.TemporaryDirectory() as tdir:
164-
path = os.path.join(tdir, 'test.cfg')
165-
parser = self.new_parser(path)
166-
parser.RemoveFile() # Should not raise exception.
167-
168-
parser.AddSection('Foo')
169-
parser.SetOption('Foo', 'bar', 'true')
170-
parser.Save()
171-
self.assertTrue(os.path.exists(path))
172-
parser.RemoveFile()
173-
self.assertFalse(os.path.exists(path))
174-
175162
def test_save(self):
176163
with tempfile.TemporaryDirectory() as tdir:
177164
path = os.path.join(tdir, 'test.cfg')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cleanup ``config.py`` by inlining ``RemoveFile`` and simplifying the handling of ``file`` in ``CreateConfigHandlers``.

0 commit comments

Comments
 (0)
0