8000 [3.8] bpo-27452: IDLE: Cleanup config.py code (GH-14577) by miss-islington · Pull Request #14802 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.8] bpo-27452: IDLE: Cleanup config.py code (GH-14577) #14802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
bpo-27452: IDLE: Cleanup config.py code (GH-14577)
(cherry picked from commit f8d4cc7)

Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
  • Loading branch information
csabella authored and miss-islington committed Jul 16, 2019
commit 86c9591d27ac49e3f44a21e3afcf52d31fa2a951
35 changes: 9 additions & 26 deletions Lib/idlelib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,11 @@ def IsEmpty(self):
self.RemoveEmptySections()
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):
"""Update user configuration file.

If self not empty after removing empty sections, write the file
to disk. Otherwise, remove the file from disk if it exists.

"""
fname = self.file
if fname:
Expand All @@ -145,8 +139,8 @@ def Save(self):
cfgFile = open(fname, 'w')
with cfgFile:
self.write(cfgFile)
else:
self.RemoveFile()
elif os.path.exists(self.file):
os.remove(self.file)

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

def CreateConfigHandlers(self):
"Populate default and user config parser dictionaries."
#build idle install path
if __name__ != '__main__': # we were imported
idleDir = os.path.dirname(__file__)
else: # we were exec'ed (for testing only)
idleDir = os.path.abspath(sys.path[0])
self.userdir = userDir = self.GetUserCfgDir()

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])
idledir = os.path.dirname(__file__)
self.userdir = userdir = self.GetUserCfgDir()
for cfg_type in self.config_types:
self.defaultCfg[cfg_type] = IdleConfParser(
os.path.join(idledir, f'config-{cfg_type}.def'))
self.userCfg[cfg_type] = IdleUserConfParser(
os.path.join(userdir, f'config-{cfg_type}.cfg'))

def GetUserCfgDir(self):
"""Return a filesystem directory for storing user config files.
Expand Down
13 changes: 0 additions & 13 deletions Lib/idlelib/idle_test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,6 @@ def test_is_empty(self):
self.assertFalse(parser.IsEmpty())
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):
with tempfile.TemporaryDirectory() as tdir:
path = os.path.join(tdir, 'test.cfg')
Expand Down
1 change: 1 addition & 0 deletions Misc/NEWS.d/next/IDLE/2019-07-03-22-47-44.bpo-27452.nePPLi.rst 6655
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cleanup ``config.py`` by inlining ``RemoveFile`` and simplifying the handling of ``file`` in ``CreateConfigHandlers``.
0