From db1e415c80928490210ce275760dc3cce32a0003 Mon Sep 17 00:00:00 2001 From: Sean Robinson Date: Sat, 26 Apr 2014 15:41:12 -0700 Subject: [PATCH] Move copy_configuration into ConfigManager The idea here is that a ConfigManager is going to know more about its internals, so it should generate any copies. Signed-off-by: Sean Robinson --- test/unit/config.py | 50 +++++++++++++++++++++++++------------------------- wifiradar/__init__.py | 7 +++---- wifiradar/config.py | 25 +++++++++++-------------- 3 files changed, 39 insertions(+), 43 deletions(-) diff --git a/test/unit/config.py b/test/unit/config.py index d12f090..8b80835 100644 --- a/test/unit/config.py +++ b/test/unit/config.py @@ -28,7 +28,7 @@ import unittest import mock -from wifiradar.config import (copy_configuration, make_section_name, +from wifiradar.config import (make_section_name, ConfigManager, ConfigFileManager) from wifiradar.misc import PYVERSION, NoDeviceError @@ -231,6 +231,30 @@ class TestConfigManager(unittest.TestCase): self.assertEqual(self.config.items(section), items_orig) self.assertEqual(self.config.items('SECT03'), items_orig) + def test_copy(self): + """ Test copy method. """ + # Copy just the DEFAULT section. + defaults = [('eggs', 'spam'), ('ham', 'spam and spam')] + orig = ConfigManager(defaults=dict(defaults)) + copy = orig.copy() + self.assertEqual(copy.items('DEFAULT'), orig.items('DEFAULT')) + # Copy multiple sections with profiles. + items = dict((('eggs', 'spam'), ('ham', 'spam and spam'))) + orig = ConfigManager() + orig.set_section('SECT01', items) + orig.set_section('SECT02', items) + orig.set_section('WinterPalace:00:00:00:00:00:00', items) + orig.set_section('SummerKingdom:', items) + copy = orig.copy(profiles=True) + for section in orig.sections(): + self.assertEqual(copy.items(section), orig.items(section)) + # Copy multiple sections without profiles. + copy = orig.copy(profiles=False) + orig.remove_section('WinterPalace:00:00:00:00:00:00') + orig.remove_section('SummerKingdom:') + for section in orig.sections(): + self.assertEqual(copy.items(section), orig.items(section)) + class TestConfigFileManager(unittest.TestCase): def setUp(self): @@ -328,30 +352,6 @@ class TestConfigFileManager(unittest.TestCase): class TestConfigFunctions(unittest.TestCase): - def test_copy(self): - """ Test copy function. """ - # Copy just the DEFAULT section. - defaults = [('eggs', 'spam'), ('ham', 'spam and spam')] - orig = ConfigManager(dict(defaults)) - copy = copy_configuration(orig) - self.assertEqual(copy.defaults(), orig.defaults()) - # Copy multiple sections with profiles. - items = dict((('eggs', 'spam'), ('ham', 'spam and spam'))) - orig = ConfigManager() - orig.set_section('SECT01', items) - orig.set_section('SECT02', items) - orig.set_section('WinterPalace:00:00:00:00:00:00', items) - orig.set_section('SummerKingdom:', items) - copy = copy_configuration(orig, profiles=True) - for section in orig.sections(): - self.assertEqual(copy.items(section), orig.items(section)) - # Copy multiple sections without profiles. - copy = copy_configuration(orig) - orig.remove_section('WinterPalace:00:00:00:00:00:00') - orig.remove_section('SummerKingdom:') - for section in orig.sections(): - self.assertEqual(copy.items(section), orig.items(section)) - def test_make_section_name(self): """ Test make_section_name function. """ # Check single AP profiles. diff --git a/wifiradar/__init__.py b/wifiradar/__init__.py index 4041726..65f60f3 100644 --- a/wifiradar/__init__.py +++ b/wifiradar/__init__.py @@ -38,8 +38,7 @@ import logging.handlers from multiprocessing import Pipe, Process from threading import Thread -from wifiradar.config import (copy_configuration, make_section_name, - ConfigManager) +from wifiradar.config import (make_section_name, ConfigManager) from wifiradar.connections import ConnectionManager, scanner from wifiradar.pubsub import Dispatcher, Message from wifiradar.misc import _, PYVERSION, PipeError, get_new_profile @@ -88,7 +87,7 @@ class Main(object): try: if self.config.get_opt_as_bool('GENERAL', 'new_file'): self.config.remove_option('GENERAL', 'new_file') - config_copy = copy_configuration(self.config) + config_copy = self.config.copy() self.msg_pipe.send(Message('PREFS-EDIT', config_copy)) except NoOptionError: pass @@ -230,7 +229,7 @@ class Main(object): def _preferences_edit_request(self): """ Pass a :class:`ConfigManager` to the UI for editing. """ - config_copy = copy_configuration(self.config) + config_copy = self.config.copy() self.msg_pipe.send(Message('PREFS-EDIT', config_copy)) def _preferences_update(self, config): diff --git a/wifiradar/config.py b/wifiradar/config.py index a109e2b..7ffb4d4 100644 --- a/wifiradar/config.py +++ b/wifiradar/config.py @@ -52,20 +52,6 @@ if PYVERSION < 3: logger = logging.getLogger(__name__) -def copy_configuration(original, profiles=False): - """ Return a :class:`ConfigManager` copy of :dat:`original`. If - :data:`profiles` is False (the default), the copy does not include - the known AP profiles. - """ - config_copy = ConfigManager() - config_copy._defaults = original._defaults.copy() - config_copy._sections = original._sections.copy() - # If not needed, remove the profiles from the new copy. - if not profiles: - for section in config_copy.profiles(): - config_copy.remove_section(section) - return config_copy - def make_section_name(essid, bssid): """ Returns the combined :data:`essid` and :data:`bssid` to make a config file section name. :data:`essid` and :data:`bssid` are @@ -392,6 +378,17 @@ class ConfigManager(object): auto_profile_order_copy.remove(ap) self.auto_profile_order = auto_profile_order_copy + def copy(self, profiles=True): + """ Return a copy of the :class:`ConfigManager` object. + """ + config_copy = ConfigManager() + config_copy._dict = self._dict_type(self._dict) + # If not needed, remove the profiles from the new copy. + if not profiles: + for section in config_copy.profiles(): + config_copy.remove_section(section) + return config_copy + @property def auto_profile_order(self): """ Get the profile order as a list. -- 2.11.4.GIT