# Copyright (C) 2015 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
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 """
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()
self.default('')
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)
def list_section(self, section, name):
"""list contents of a section"""
content = dict(self.cnf.items(section))
if section in 'global' and name:
self.print_option(section, '', name)
else:
if name:
content = content[name]
for option in content.keys():
self.print_option(section, name, option)
def list_section_type(self, section):
"""print the contents of a configuration section"""
names = ['', ] if section in ('global', ) else self.cnf.keys(section)
assert names, 'Section %s not found' % section
for name in names:
print section, name
self.list_section(section, name)
def list_sections(self):
"""List all configuration sections"""
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