Commit 51575f2b authored by Laurent Bachelier's avatar Laurent Bachelier

Ability to select the config type for every 'config' command

parent e3d3e2b1
......@@ -66,6 +66,9 @@ class File(IObject):
self.perms = {}
IObject.__init__(self, storage)
def __str__(self):
return self.path
def set_all_perms(self, perms):
self.perms['all'] = perms
......
......@@ -21,53 +21,77 @@ import os
import sys
from ass2m.plugin import Plugin
from ass2m.cmd import Command
from ass2m.cmd import Command, CommandParent
__all__ = ['ConfigPlugin']
class ResolveCmd(Command):
DESCRIPTION = 'Get the storage name of a path'
@staticmethod
def configure_parser(parser):
parser.add_argument('path', nargs='+')
DESCRIPTION = 'Get the absolute path of the configuration file'
def cmd(self, args):
for path in args.path:
if not os.path.exists(path):
print >>sys.stderr, 'Error: Path "%s" does not exist.' % path
continue
f = self.storage.get_file_from_realpath(os.path.realpath(path))
if not f:
print >>sys.stderr, 'Error: Path "%s" is not in working directory.' % path
continue
print '%s => %s' % (f.path, os.path.join(self.storage.path, f._get_confname()))
try:
config = ConfigCmdParent.get_config(self.storage, args.config_info)
except GetConfigError, e:
print >>sys.stderr, 'Error: %s' % e
return 1
print '%s => %s' % (config, os.path.join(self.storage.path, config._get_confname()))
class ListConfigCmd(Command):
DESCRIPTION = 'Get all the values of the global config'
DESCRIPTION = 'Get all the values of a configuration'
@staticmethod
def configure_parser(parser):
parser.add_argument('section', nargs='?')
def cmd(self, args):
config = self.storage.get_config()
self._print_config(config.data, args.section)
def _print_config(self, data, filter_section = None):
for sectionkey, section in data.iteritems():
if filter_section is None or sectionkey == filter_section:
try:
config = ConfigCmdParent.get_config(self.storage, args.config_info)
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 GetConfigError(Exception):
pass
class ConfigCmdParent(CommandParent):
DESCRIPTION = 'Interact with the stored configurations'
@staticmethod
def configure_parser(parser):
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-g', '--global', help='Use global configuration',
action='store_const', const=('global', None), dest='config_info')
group.add_argument('-f', '--file', help='Use a file\'s configuration',
type=lambda v: ('file', v), metavar='FILEPATH', dest='config_info')
group.add_argument('-u', '--user', help='Use an user\'s configuration',
type=lambda v: ('user', v), metavar='USERNAME', dest='config_info')
@staticmethod
def get_config(storage, config_info):
config_type, config_name = config_info
if config_type == 'global':
return storage.get_config()
elif config_type == 'file':
f = storage.get_file_from_realpath(os.path.realpath(config_name))
if not f:
raise GetConfigError('Path "%s" is not in working directory %s.' % (config_name, storage.root))
return f
elif config_type == 'user':
u = storage.get_user(config_name)
if not u or not u.exists:
raise GetConfigError('User "%s" does not exist.' % config_name)
return u
class ConfigPlugin(Plugin):
def init(self):
self.register_cli_command('config', 'Interact with the stored configurations')
self.register_cli_command('config', ConfigCmdParent)
self.register_cli_command('config', 'resolve', ResolveCmd)
self.register_cli_command('config', 'global', 'Interact with the global configuration')
self.register_cli_command('config', 'global', 'list', ListConfigCmd)
self.register_cli_command('config', 'list', ListConfigCmd)
......@@ -30,6 +30,9 @@ __all__ = ['Storage']
class GlobalConfig(IObject):
def __str__(self):
return '<global>'
def _get_confname(self):
return 'config'
......
from unittest import TestCase
from ass2m.cli import CLI
from ass2m.storage import Storage
from ass2m.users import User
from tempfile import mkdtemp
import os
import shutil
......@@ -10,12 +11,19 @@ from StringIO import StringIO
class ConfigCLITest(TestCase):
def setUp(self):
self.root = mkdtemp(prefix='ass2m_test_root')
Storage.create(self.root)
storage = Storage.create(self.root)
self.app = CLI(self.root)
os.mkdir(os.path.join(self.root, 'penguins'))
with open(os.path.join(self.root, 'penguins', 'gentoo'), 'w') as f:
f.write('HELLO')
user = User(storage, 'penguin')
user.realname = 'Penguin'
user.password = 'monkey1'
user.key = 'fabf37d746da8a45df63489f642b3813'
user.save()
def tearDown(self):
if self.root:
shutil.rmtree(self.root)
......@@ -35,25 +43,60 @@ class ConfigCLITest(TestCase):
def test_resolve(self):
for p in (os.path.join(self.root, 'penguins'), os.path.join(self.root, 'penguins'+os.path.sep)):
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', 'resolve', p]) in (0, None)
assert self.app.main(['ass2m_test', 'config', '-f', p, 'resolve']) in (0, None)
output = self.endCapture()
assert output.startswith('/penguins => ')
assert output.strip().endswith('/7fe0243160cdd4fdb87ab2ce1ecf9fef501cd1a5')
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', 'resolve', os.path.join(self.root, 'penguins', 'gentoo')]) in (0, None)
assert self.app.main(['ass2m_test', 'config', '-f', os.path.join(self.root, 'penguins', 'gentoo'), 'resolve']) in (0, None)
output = self.endCapture()
assert output.startswith('/penguins/gentoo => ')
assert output.strip().endswith('/090ec30f855081df160d60cbfe635c399b5ff523')
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', 'resolve', self.root]) in (0, None)
assert self.app.main(['ass2m_test', 'config', '-f', self.root, 'resolve']) in (0, None)
output = self.endCapture()
assert output.startswith(' => ')
assert output.strip().endswith('/da39a3ee5e6b4b0d3255bfef95601890afd80709')
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', '-g', 'resolve']) in (0, None)
output = self.endCapture()
assert output.startswith('<global> => ')
assert output.strip().endswith('/config')
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', '-u', 'penguin', 'resolve']) in (0, None)
output = self.endCapture()
assert output.startswith('penguin => ')
assert output.strip().endswith('/users/penguin')
def test_configList(self):
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', 'global', 'list']) in (0, None)
assert self.app.main(['ass2m_test', 'config', '-g', 'list']) in (0, None)
output = self.endCapture()
assert len(output) == 0
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', '-f', self.root, 'list']) in (0, None)
output = self.endCapture()
assert len(output) # it has the default perms
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', '-u', 'penguin', 'list']) in (0, None)
output = self.endCapture() # no ValueError
assert len(output)
def test_filterList(self):
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', '-u', 'penguin', 'list']) in (0, None)
output = self.endCapture()
assert len(output)
output.split('\n').index('info.realname=Penguin') # no ValueError
self.beginCapture()
assert self.app.main(['ass2m_test', 'config', '-u', 'penguin', 'list', 'auth']) in (0, None)
output = self.endCapture()
assert len(output)
self.assertRaises(ValueError, output.split('\n').index, 'info.realname=Penguin')
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