From 389eb8969c7d8da26a5edca6ed4379ff7a5cfb73 Mon Sep 17 00:00:00 2001 From: Jim Miller Date: Wed, 8 Aug 2018 14:16:41 -0500 Subject: [PATCH] Make INI order tool py2/py3. --- ini-order.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/ini-order.py b/ini-order.py index be65928b..7c2cde75 100644 --- a/ini-order.py +++ b/ini-order.py @@ -1,16 +1,31 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright 2018, Jim Miller + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import re import sys +from io import open # so py2.7 has open with encoding param. argv = sys.argv[1:] -# infile = argv[0] -# outfile = argv[1] - sections = {} cursectname = "" cursectlines = [] -with open(argv[0],"r") as infile: +with open(argv[0],"r", encoding="utf8") as infile: for line in infile: if re.match(r"^\[([^\]]+)\]$",line): sections[cursectname] = cursectlines @@ -40,8 +55,8 @@ leadsects = [ followsects = [ ] -with open(argv[1],"w") as outfile: - kl = sections.keys() +with open(argv[1],"w", encoding="utf8") as outfile: + kl = list(sections.keys()) kl.sort() for k in leadsects: outfile.write("".join(sections[k]))