From 0404dd070ceda03937aad3116c76955e513373ca Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Sun, 21 Dec 2014 21:06:21 +0100 Subject: python3 compatibility code for i3status tmp config generation --- py3status/__init__.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/py3status/__init__.py b/py3status/__init__.py index 04a5d06..9a9a484 100755 --- a/py3status/__init__.py +++ b/py3status/__init__.py @@ -293,6 +293,16 @@ class I3status(Thread): ordered.append(j) return ordered + @staticmethod + def write_in_tmpfile(text, tmpfile): + """ + Write the given text in the given tmpfile in python2 and python3. + """ + try: + tmpfile.write(text) + except TypeError: + tmpfile.write(str.encode(text)) + def write_tmp_i3status_config(self, tmpfile): """ Given a temporary file descriptor, write a valid i3status config file @@ -304,13 +314,19 @@ class I3status(Thread): elif section_name == 'order': for module_name in conf: if self.valid_config_param(module_name): - tmpfile.write('order += "%s"\n' % module_name) - tmpfile.write('\n') + self.write_in_tmpfile( + 'order += "%s"\n' % module_name, + tmpfile + ) + self.write_in_tmpfile('\n', tmpfile) elif self.valid_config_param(section_name): - tmpfile.write('%s {\n' % section_name) + self.write_in_tmpfile('%s {\n' % section_name, tmpfile) for key, value in conf.items(): - tmpfile.write(' %s = "%s"\n' % (key, value)) - tmpfile.write('}\n\n') + self.write_in_tmpfile( + ' %s = "%s"\n' % (key, value), + tmpfile + ) + self.write_in_tmpfile('}\n\n', tmpfile) tmpfile.flush() def run(self): -- cgit v1.3