Commit d6a8b9c1 authored by Laurent Bachelier's avatar Laurent Bachelier

Add get/set config CLI commands

parent 51575f2b
......@@ -52,11 +52,63 @@ class ListConfigCmd(Command):
except GetConfigError, e:
print >>sys.stderr, 'Error: %s' % e
return 1
for sectionkey, section in config.data.iteritems():
if args.section is None or sectionkey == args.section:
for key, value in section.iteritems():
print "%s.%s=%s" % (sectionkey, key, value)
class GetConfigCmd(Command):
DESCRIPTION = 'Get the value of a configuration entry'
@staticmethod
def configure_parser(parser):
parser.add_argument('section_key', metavar='SECTION.KEY')
def cmd(self, args):
try:
config = ConfigCmdParent.get_config(self.storage, args.config_info)
except GetConfigError, e:
print >>sys.stderr, 'Error: %s' % e
return 1
if '.' not in args.section_key:
print >>sys.stderr, 'You must provide a section and a key (SECTION.KEY).'
return 1
section, key = args.section_key.split('.')
if not config.data[section].has_key(key):
print >>sys.stderr, 'Error: %s is undefined.' % args.section_key
return 2
print config.data[section][key]
class SetConfigCmd(Command):
DESCRIPTION = 'Set the value of a configuration entry'
@staticmethod
def configure_parser(parser):
parser.add_argument('section_key', metavar='SECTION.KEY')
parser.add_argument('value')
def cmd(self, args):
try:
config = ConfigCmdParent.get_config(self.storage, args.config_info)
except GetConfigError, e:
print >>sys.stderr, 'Error: %s' % e
return 1
if '.' not in args.section_key:
print >>sys.stderr, 'You must provide a section and a key (SECTION.KEY).'
return 1
section, key = args.section_key.split('.')
config.data[section][key] = args.value
config.save()
class GetConfigError(Exception):
pass
......@@ -93,5 +145,7 @@ class ConfigCmdParent(CommandParent):
class ConfigPlugin(Plugin):
def init(self):
self.register_cli_command('config', ConfigCmdParent)
self.register_cli_command('config', 'get', GetConfigCmd)
self.register_cli_command('config', 'list', ListConfigCmd)
self.register_cli_command('config', 'resolve', ResolveCmd)
self.register_cli_command('config', 'list', ListConfigCmd)
self.register_cli_command('config', 'set', SetConfigCmd)
......@@ -18,17 +18,26 @@ class BaseCLITest(TestCase):
if self.root:
shutil.rmtree(self.root)
def beginCapture(self):
def beginCapture(self, with_stderr = False):
self.stdout = sys.stdout
# begin capture
sys.stdout = StringIO()
if with_stderr:
self.stderr = sys.stderr
sys.stderr = sys.stdout
elif not hasattr(self, 'stderr'):
self.stderr = None
def endCapture(self):
captured = sys.stdout
captured = sys.stdout.getvalue()
# end capture
sys.stdout = self.stdout
self.stdout = None
return captured.getvalue()
if self.stdout is not None:
sys.stdout = self.stdout
self.stdout = None
if self.stderr is not None:
sys.stderr = self.stderr
self.stderr = None
return captured
def test_init(self):
self.beginCapture()
......
......@@ -28,17 +28,26 @@ class ConfigCLITest(TestCase):
if self.root:
shutil.rmtree(self.root)
def beginCapture(self):
def beginCapture(self, with_stderr = False):
self.stdout = sys.stdout
# begin capture
sys.stdout = StringIO()
if with_stderr:
self.stderr = sys.stderr
sys.stderr = sys.stdout
elif not hasattr(self, 'stderr'):
self.stderr = None
def endCapture(self):
captured = sys.stdout
captured = sys.stdout.getvalue()
# end capture
sys.stdout = self.stdout
self.stdout = None
return captured.getvalue()
if self.stdout is not None:
sys.stdout = self.stdout
self.stdout = None
if self.stderr is not None:
sys.stderr = self.stderr
self.stderr = None
return captured
def test_resolve(self):
for p in (os.path.join(self.root, 'penguins'), os.path.join(self.root, 'penguins'+os.path.sep)):
......@@ -100,3 +109,28 @@ class ConfigCLITest(TestCase):
output = self.endCapture()
assert len(output)
self.assertRaises(ValueError, output.split('\n').index, 'info.realname=Penguin')
def test_getAndSet(self):
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', '-g', 'list']) in (0, None)
output = self.endCapture()
assert len(output) == 0
self.beginCapture(True)
assert self.app.main(['ass2m_test', 'config', '-g', 'get', 'lol.cat']) == 2
output = self.endCapture()
assert output.strip() == 'Error: lol.cat is undefined.'
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', '-g', 'set', 'lol.cat', '42']) in (0, None)
output = self.endCapture()
assert len(output) == 0
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', '-g', 'get', 'lol.cat']) in (0, None)
output = self.endCapture()
assert output.strip() == '42'
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', '-g', 'list']) in (0, None)
output = self.endCapture()
assert len(output.strip().split('\n')) == 1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment