From a0a536b3a35acdf4d5cdae5bd832102ae0559ad8 Mon Sep 17 00:00:00 2001 From: Sean Robinson Date: Sat, 5 Apr 2014 10:37:08 -0700 Subject: [PATCH] Add config.copy function and its tests This function produces a non-ConfigFileManager copy of configuration information which can be passed to other parts of WiFi Radar (e.g. ConnectionManager and the UI). Signed-off-by: Sean Robinson --- test/unit/config.py | 28 ++++++++++++++++++++++++++-- wifiradar/config.py | 14 ++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/test/unit/config.py b/test/unit/config.py index 6c04e41..f07a7eb 100644 --- a/test/unit/config.py +++ b/test/unit/config.py @@ -30,8 +30,8 @@ import unittest import mock -from wifiradar.config import (make_section_name, ConfigManager, - ConfigFileManager) +from wifiradar.config import (copy_configuration, make_section_name, + ConfigManager, ConfigFileManager) from wifiradar.misc import NoDeviceError @@ -210,6 +210,30 @@ class TestConfigManager(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/config.py b/wifiradar/config.py index b9cd8e7..bc50e15 100644 --- a/wifiradar/config.py +++ b/wifiradar/config.py @@ -50,6 +50,20 @@ import wifiradar.misc as misc 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 'essid' and 'bssid' to make a config file section name. 'essid' and 'bssid' are strings. -- 2.11.4.GIT