From 2359cbaadf0841be8162924cdcc446fcab66965e Mon Sep 17 00:00:00 2001 From: Sean Robinson Date: Sun, 13 Apr 2014 07:00:13 -0700 Subject: [PATCH] Change byte sequences to unicode sequences in ConfigManager There are three parts to this: 1) remove isinstance test in set_opt because SafeConfigParser already checks the type; 2) expand StringType to StringTypes in set_bool_opt; 3) use unicode built-in function. Signed-off-by: Sean Robinson --- wifiradar/config.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/wifiradar/config.py b/wifiradar/config.py index d87695e..25f8ed0 100644 --- a/wifiradar/config.py +++ b/wifiradar/config.py @@ -205,9 +205,7 @@ class ConfigManager(object, configparser.SafeConfigParser): will be turned into one before being added. Raises TypeError if 'value' is not a string. """ - section, option = str(section), str(option) - if not isinstance(value, StringType): - raise TypeError(_('value must be a string type')) + section, option = unicode(section), unicode(option) try: self.set(section, option, value) except configparser.NoSectionError: @@ -230,7 +228,7 @@ class ConfigManager(object, configparser.SafeConfigParser): raise ValueError(_('boolean value must be >= 0')) # use False = 0 and True = 1 to return index into tuple value = ('False', 'True')[value > 0] - elif isinstance(value, StringType): + elif isinstance(value, StringTypes): # convert to title case (i.e. capital first letter, only) value = value.title() if value not in ('False', 'True'): @@ -247,7 +245,7 @@ class ConfigManager(object, configparser.SafeConfigParser): """ if not isinstance(value, IntType): raise TypeError(_('value is not an integer')) - self.set_opt(section, option, str(value)) + self.set_opt(section, option, unicode(value)) def set_float_opt(self, section, option, value): """ Set 'option' to float 'value' in 'section'. @@ -257,7 +255,7 @@ class ConfigManager(object, configparser.SafeConfigParser): """ if not isinstance(value, (FloatType, IntType)): raise TypeError(_('value is not a float or integer')) - self.set_opt(section, option, str(float(value))) + self.set_opt(section, option, unicode(float(value))) def profiles(self): """ Return a list of the section names which denote AP profiles. @@ -318,7 +316,7 @@ class ConfigFileManager(ConfigManager): Copied from configparser and modified to write options in specific order. """ - self.set_opt('DEFAULT', 'auto_profile_order', str(self.auto_profile_order)) + self.set_opt('DEFAULT', 'auto_profile_order', unicode(self.auto_profile_order)) self.set_opt('DEFAULT', 'version', WIFI_RADAR_VERSION) (fd, tempfilename) = tempfile.mkstemp(prefix="wifi-radar.conf.") fp = os.fdopen(fd, "w") @@ -326,7 +324,7 @@ class ConfigFileManager(ConfigManager): if self._defaults: fp.write("[DEFAULT]\n") for key in sorted(self._defaults.keys()): - fp.write("%s = %s\n" % (key, str(self._defaults[key]).replace('\n','\n\t'))) + fp.write("%s = %s\n" % (key, unicode(self._defaults[key]).replace('\n','\n\t'))) fp.write("\n") # write other non-profile sections next for section in self._sections: @@ -335,7 +333,7 @@ class ConfigFileManager(ConfigManager): for key in sorted(self._sections[section].keys()): if key != "__name__": fp.write("%s = %s\n" % - (key, str(self._sections[section][key]).replace('\n', '\n\t'))) + (key, unicode(self._sections[section][key]).replace('\n', '\n\t'))) fp.write("\n") # write profile sections for section in self._sections: @@ -344,7 +342,7 @@ class ConfigFileManager(ConfigManager): for key in sorted(self._sections[section].keys()): if key != "__name__": fp.write("%s = %s\n" % - (key, str(self._sections[section][key]).replace('\n', '\n\t'))) + (key, unicode(self._sections[section][key]).replace('\n', '\n\t'))) fp.write("\n") fp.close() move(tempfilename, self.filename) -- 2.11.4.GIT