Skip to content

Commit

Permalink
Allow quoted config values
Browse files Browse the repository at this point in the history
  • Loading branch information
Radek Novacek committed Jun 23, 2016
1 parent dd5ef65 commit d0a7be6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
32 changes: 27 additions & 5 deletions tests/test_config.py
Expand Up @@ -465,8 +465,8 @@ def testFilterHostOld(self):
self.assertEqual(manager.configs[0].filter_hosts, ['12345'])

def testFilterHostNew(self):
with open(os.path.join(self.config_dir, "test1.conf"), "w") as f:
f.write("""
with open(os.path.join(self.config_dir, "test1.conf"), "w") as f:
f.write("""
[test1]
type=esx
server=1.2.3.4
Expand All @@ -476,9 +476,31 @@ def testFilterHostNew(self):
env=staging
filter_hosts=12345
""")
manager = ConfigManager(self.logger, self.config_dir)
self.assertEqual(len(manager.configs), 1)
self.assertEqual(manager.configs[0].filter_hosts, ['12345'])
manager = ConfigManager(self.logger, self.config_dir)
self.assertEqual(len(manager.configs), 1)
self.assertEqual(manager.configs[0].filter_hosts, ['12345'])

def testQuotesInConfig(self):
with open(os.path.join(self.config_dir, "test1.conf"), "w") as f:
f.write("""
[test1]
type=esx
server="1.2.3.4"
username='admin'
password=p"asswor'd
owner=" root "
env='"staging"'
""")
manager = ConfigManager(self.logger, self.config_dir)
self.assertEqual(len(manager.configs), 1)
config = manager.configs[0]
self.assertEqual(config.name, "test1")
self.assertEqual(config.type, "esx")
self.assertEqual(config.server, "1.2.3.4")
self.assertEqual(config.username, "admin")
self.assertEqual(config.password, "p\"asswor'd")
self.assertEqual(config.owner, " root ")
self.assertEqual(config.env, '"staging"')


class TestGeneralConfig(TestBase):
Expand Down
12 changes: 11 additions & 1 deletion virtwho/config.py
Expand Up @@ -341,6 +341,16 @@ def type(self):
return self._type


class StripQuotesConfigParser(SafeConfigParser):
def get(self, section, option):
# Don't call super, SafeConfigParser is not inherited from object
value = SafeConfigParser.get(self, section, option)
for quote in ('"', "'"):
if value.startswith(quote) and value.endswith(quote):
return value.strip(quote)
return value


class ConfigManager(object):
def __init__(self, logger, config_dir=None, defaults=None):
if not defaults:
Expand All @@ -353,7 +363,7 @@ def __init__(self, logger, config_dir=None, defaults=None):
self._defaults = defaults
if config_dir is None:
config_dir = VIRTWHO_CONF_DIR
parser = SafeConfigParser()
parser = StripQuotesConfigParser()
self._configs = []
self.logger = logger
try:
Expand Down

0 comments on commit d0a7be6

Please sign in to comment.