From 7057da734d1a5eb9c01faedf5aa7891bf27d0f54 Mon Sep 17 00:00:00 2001 From: Ari Lamstein Date: Fri, 28 Feb 2025 10:42:51 -0800 Subject: [PATCH 1/2] Update sort-ini.py script to work with Python 3. Also run on script. --- config/sort-ini.py | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/config/sort-ini.py b/config/sort-ini.py index 98976d3..353a11d 100755 --- a/config/sort-ini.py +++ b/config/sort-ini.py @@ -1,46 +1,50 @@ #!/usr/bin/env python import sys -import ConfigParser +import configparser as ConfigParser if len(sys.argv) > 1: filename = sys.argv[1] else: - filename = 'config.ini' - + filename = "config.ini" + oconfig = ConfigParser.RawConfigParser() oconfig.read(filename) # This part will destroy the configuration if there's a crash while # writing the output. We're in an GIT-controlled directory, so # I didn't care enough to fix this. -with open(filename, 'wb') as fd: +with open(filename, "wb") as fd: # Copy of write() code that sorts output by section if oconfig._defaults: fd.write("[%s]\n" % DEFAULTSECT) - for (key, value) in oconfig._defaults.items(): - fd.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t'))) + for key, value in oconfig._defaults.items(): + fd.write("%s = %s\n" % (key, str(value).replace("\n", "\n\t"))) fd.write("\n") - + result = {} for section in sorted(oconfig._sections): - if section == 'Planet': - fd.write("[%s]\n" % section) - for (key, value) in oconfig._sections[section].items(): + if section == "Planet": + fd.write(b"[%s]\n" % section.encode("utf-8")) + for key, value in oconfig._sections[section].items(): if key != "__name__": - if section == 'Planet': - fd.write("%s = %s\n" % - (key, str(value).replace('\n', '\n\t'))) + if section == "Planet": + fd.write( + b"%s = %s\n" + % ( + key.encode("utf-8"), + str(value).replace("\n", "\n\t").encode("utf-8"), + ) + ) else: - result[value.replace('"', '')] = section - if section == 'Planet': - fd.write("\n") - + result[value.replace('"', "")] = section + if section == "Planet": + fd.write("\n".encode("utf-8")) + for key, value in sorted(result.items()): - fd.write("[%s]\n" % value) + fd.write(b"[%s]\n" % value.encode("utf-8")) name = key if "'" in key: name = '"%s"' % key - fd.write("name = %s\n" % name) - fd.write("\n") - + fd.write(b"name = %s\n" % name.encode("utf-8")) + fd.write(b"\n") From 6075cd9bed23da289aa76878017654d34987d936 Mon Sep 17 00:00:00 2001 From: Ari Lamstein Date: Mon, 3 Mar 2025 12:56:20 -0800 Subject: [PATCH 2/2] If you open the file as just 'w' with an encoding of utf-8 then a lot of the changes are unnecessary. Revert changes made by black. --- config/sort-ini.py | 48 +++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/config/sort-ini.py b/config/sort-ini.py index 353a11d..e2db9ff 100755 --- a/config/sort-ini.py +++ b/config/sort-ini.py @@ -1,50 +1,46 @@ #!/usr/bin/env python import sys -import configparser as ConfigParser +import configparser if len(sys.argv) > 1: filename = sys.argv[1] else: - filename = "config.ini" - -oconfig = ConfigParser.RawConfigParser() + filename = 'config.ini' + +oconfig = configparser.RawConfigParser() oconfig.read(filename) # This part will destroy the configuration if there's a crash while # writing the output. We're in an GIT-controlled directory, so # I didn't care enough to fix this. -with open(filename, "wb") as fd: +with open(filename, 'w', encoding='utf-8') as fd: # Copy of write() code that sorts output by section if oconfig._defaults: fd.write("[%s]\n" % DEFAULTSECT) - for key, value in oconfig._defaults.items(): - fd.write("%s = %s\n" % (key, str(value).replace("\n", "\n\t"))) + for (key, value) in oconfig._defaults.items(): + fd.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t'))) fd.write("\n") - + result = {} for section in sorted(oconfig._sections): - if section == "Planet": - fd.write(b"[%s]\n" % section.encode("utf-8")) - for key, value in oconfig._sections[section].items(): + if section == 'Planet': + fd.write("[%s]\n" % section) + for (key, value) in oconfig._sections[section].items(): if key != "__name__": - if section == "Planet": - fd.write( - b"%s = %s\n" - % ( - key.encode("utf-8"), - str(value).replace("\n", "\n\t").encode("utf-8"), - ) - ) + if section == 'Planet': + fd.write("%s = %s\n" % + (key, str(value).replace('\n', '\n\t'))) else: - result[value.replace('"', "")] = section - if section == "Planet": - fd.write("\n".encode("utf-8")) - + result[value.replace('"', '')] = section + if section == 'Planet': + fd.write("\n") + for key, value in sorted(result.items()): - fd.write(b"[%s]\n" % value.encode("utf-8")) + fd.write("[%s]\n" % value) name = key if "'" in key: name = '"%s"' % key - fd.write(b"name = %s\n" % name.encode("utf-8")) - fd.write(b"\n") + fd.write("name = %s\n" % name) + fd.write("\n") +