Skip to content

Commit 1b908a7

Browse files
author
gabriel pettier
committed
Allow using unicode configparser
First, try using configparser, so users of python2 configparser backport can use it. Second, add an as_unicode parameter to write, so it's possible to use codecs.open instead of open to save the file.
1 parent 0421a33 commit 1b908a7

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

kivy/config.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,10 @@
276276
__all__ = ('Config', 'ConfigParser')
277277

278278
try:
279-
from ConfigParser import ConfigParser as PythonConfigParser
279+
from configparser import ConfigParser as PythonConfigParser
280280
except ImportError:
281-
from configparser import RawConfigParser as PythonConfigParser
281+
from ConfigParser import ConfigParser as PythonConfigParser
282+
282283
from os import environ
283284
from os.path import exists
284285
from kivy import kivy_config_fn
@@ -287,6 +288,7 @@
287288
from kivy.utils import platform
288289
from kivy.compat import PY2, string_types
289290
from weakref import ref
291+
import codecs
290292

291293
_is_rpi = exists('/opt/vc/include/bcm_host.h')
292294

@@ -474,7 +476,7 @@ def adddefaultsection(self, section):
474476
return
475477
self.add_section(section)
476478

477-
def write(self):
479+
def write(self, as_unicode=False):
478480
'''Write the configuration to the last file opened using the
479481
:meth:`read` method.
480482
@@ -483,8 +485,12 @@ def write(self):
483485
if self.filename is None:
484486
return False
485487
try:
486-
with open(self.filename, 'w') as fd:
487-
PythonConfigParser.write(self, fd)
488+
if as_unicode:
489+
with codecs.open(self.filename, 'w', encoding='utf-8') as fd:
490+
PythonConfigParser.write(self, fd)
491+
else:
492+
with open(self.filename, 'w') as fd:
493+
PythonConfigParser.write(self, fd)
488494
except IOError:
489495
Logger.exception('Unable to write the config <%s>' % self.filename)
490496
return False

0 commit comments

Comments
 (0)