Commit b277f39f authored by Stavros Sachtouris's avatar Stavros Sachtouris Committed by Giorgos Korfiatis
Browse files

Implement the "config" CLI commands

The commands are "config list | set | delete" and can be used to
manage the agkyra settings, which can also be modified by editing
the configuration file.
parent fe18edec
......@@ -13,62 +13,50 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ws4py.client import WebSocketBaseClient
import cmd
import sys
import logging
from agkyra.syncer import setup, syncer
from agkyra.syncer.pithos_client import PithosFileClient
from agkyra.syncer.localfs_client import LocalfsFileClient
from agkyra import config
LOG = logging.getLogger(__name__)
class AgkyraCLI(cmd.Cmd):
"""The CLI for """
class AgkyraCLI(cmd.Cmd, WebSocketBaseClient):
"""The CLI for Agkyra is connected to a protocol server"""
cnf = config.AgkyraConfig()
is_shell = False
def init(self):
"""initialize syncer"""
# Read settings
sync = self.cnf.get('global', 'default_sync')
LOG.info('Using sync: %s' % sync)
cloud = self.cnf.get_sync(sync, 'cloud')
url = self.cnf.get_cloud(cloud, 'url')
token = self.cnf.get_cloud(cloud, 'token')
container = self.cnf.get_sync(sync, 'container')
directory = self.cnf.get_sync(sync, 'directory')
# Prepare syncer settings
self.settings = setup.SyncerSettings(
sync, url, token, container, directory,
ignore_ssl=True)
LOG.info('Local: %s' % directory)
LOG.info('Remote: %s of %s' % (container, url))
# self.exclude = self.cnf.get_sync(sync, 'exclude')
# Init syncer
master = PithosFileClient(self.settings)
slave = LocalfsFileClient(self.settings)
self.syncer = syncer.FileSyncer(self.settings, master, slave)
def preloop(self):
"""This runs when the shell loads"""
print 'Loading Agkyra (sometimes this takes a while)'
if not self.is_shell:
self.is_shell = True
self.prompt = '\xe2\x9a\x93 '
self.init()
"""Prepare agkyra shell"""
self.prompt = '\xe2\x9a\x93 '
self.default('')
def precmd(self):
print 'PRE'
def postcmd(self):
print 'POST'
def do_config(self, line):
"""Commands for managing the agkyra settings
list [global|cloud|sync [setting]] List all or some settings
set <global|cloud|sync> <setting> <value> Set a setting
delete <global|cloud|sync> [setting] Delete a setting or group
"""
args = line.split(' ')
try:
method = getattr(self, 'config_' + args[0])
method(args[1:])
except AttributeError:
self.do_help('config')
def print_option(self, section, name, option):
"""Print a configuration option"""
section = '%s.%s' % (section, name) if name else section
value = self.cnf.get(section, option)
print ' %s: %s' % (option, value)
sys.stdout.write(' %s: %s\n' % (option, value))
def list_section(self, section, name):
"""list contents of a section"""
......@@ -94,18 +82,14 @@ class AgkyraCLI(cmd.Cmd):
for section in self.cnf.sections():
self.list_section_type(section)
def do_list(self, line):
"""List current settings (\"help list\" for details)
list global List all settings
list global <option> Get the value of this global option
list cloud List all clouds
list cloud <name> List all options of a cloud
list cloud <name> <option> Get the value of this cloud option
list sync List all syncs
list sync <name> List all options of a sync
list sync <name> <option> Get the value of this sync option
def config_list(self, args):
"""List (all or some) options
list List all options
list <global | cloud | sync> List global | cloud | sync options
list global OPTION Get global option
list <cloud | sync> NAME List options a cloud or sync
list <cloud | sync> NAME OPTION List an option from a cloud or sync
"""
args = line.split()
try:
{
0: self.list_sections,
......@@ -114,20 +98,24 @@ class AgkyraCLI(cmd.Cmd):
3: self.print_option
}[len(args)](*args)
except Exception as e:
sys.stderr.write('%s\n' % e)
cmd.Cmd.do_help(self, 'list')
LOG.debug('%s\n' % e)
sys.stderr.write(self.config_list.__doc__ + '\n')
def set_global_setting(self, section, option, value):
assert section in ('global'), 'Syntax error'
self.cnf.set(section, option, value)
def set_setting(self, section, name, option, value):
assert section in self.sections(), 'Syntax error'
assert section in self.cnf.sections(), 'Syntax error'
self.cnf.set('%s.%s' % (section, name), option, value)
def do_set(self, line):
"""Set a setting"""
args = line.split()
def config_set(self, args):
"""Set an option
set global OPTION VALUE Set a global option
set <cloud | sync> NAME OPTION VALUE Set an option on cloud or sync
Creates a sync or cloud, if it
does not exist
"""
try:
{
3: self.set_global_setting,
......@@ -135,66 +123,34 @@ class AgkyraCLI(cmd.Cmd):
}[len(args)](*args)
self.cnf.write()
except Exception as e:
sys.stderr.write('%s\n' % e)
cmd.Cmd.do_help(self, 'set')
def do_start(self, line):
"""Start syncing"""
if not getattr(self, '_syncer_initialized', False):
self.syncer.probe_and_sync_all()
self._syncer_initialized = True
if self.syncer.paused:
self.syncer.start_decide()
def do_pause(self, line):
"""Pause syncing"""
if not self.syncer.paused:
self.syncer.pause_decide()
def do_status(self, line):
"""Get current status (running/paused, progress)"""
print 'paused' if self.syncer.paused else 'running'
# def do_shell(self, line):
# """Run system, shell commands"""
# if getattr(self, 'is_shell'):
# os.system(line)
# else:
# try:
# self.prompt = '\xe2\x9a\x93 '
# self.is_shell = True
# finally:
# self.init()
# self.cmdloop()
def do_help(self, line):
"""List commands with \"help\" or detailed help with \"help cmd\""""
if not line:
self.default(line)
cmd.Cmd.do_help(self, line)
def do_quit(self, line):
"""Quit Agkyra shell"""
return True
def default(self, line):
"""print help"""
sys.stderr.write('Usage:\t%s <command> [args]\n\n' % self.prompt)
for arg in [c for c in self.get_names() if c.startswith('do_')]:
sys.stderr.write('%s\t' % arg[3:])
method = getattr(self, arg)
sys.stderr.write(method.__doc__.split('\n')[0] + '\n')
sys.stderr.write('\n')
def emptyline(self):
if not self.is_shell:
return self.default('')
def run_onecmd(self, argv):
self.prompt = argv[0]
self.init()
self.onecmd(' '.join(argv[1:]))
LOG.debug('%s\n' % e)
sys.stderr.write(self.config_set.__doc__ + '\n')
def delete_option(self, section, name, option):
"""Delete a config secttion (sync or cloud)"""
assert section in self.cnf.sections(), 'Syntax error'
if section == config.CLOUD_PREFIX:
self.cnf.remove_from_cloud(name, option)
elif section == config.SYNC_PREFIX:
self.cnf.remove_from_sync(name, option)
else:
self.cnf.remove_option('%s.%s' % (section, name), option)
def config_delete(self, args):
"""Delete an option
delete global OPTION Delete a global option
delete <cloud | sync> NAME Delete a sync or cloud
delete <cloud |sync> NAME OPTION Delete a sync or cloud option
"""
try:
{
2: self.cnf.remove_option,
3: self.delete_option
}[len(args)](*args)
self.cnf.write()
except Exception as e:
LOG.debug('%s\n' % e)
sys.stderr.write(self.config_delete.__doc__ + '\n')
# AgkyraCLI().run_onecmd(sys.argv)
......
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