From c9d918157c145e3fe76d3dfb195c9d3f9a8fff12 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sun, 12 Jul 2015 11:57:57 +0530 Subject: [PATCH] transmission: Remove get-enabled from actions --- actions/transmission | 46 +++---------------------- plinth/modules/transmission/__init__.py | 17 ++++++--- plinth/modules/transmission/views.py | 8 ++--- 3 files changed, 19 insertions(+), 52 deletions(-) diff --git a/actions/transmission b/actions/transmission index 8ba8aeeeb..9ad6bced0 100755 --- a/actions/transmission +++ b/actions/transmission @@ -37,10 +37,6 @@ def parse_arguments(): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='subcommand', help='Sub command') - # Get whether service is enabled - subparsers.add_parser('get-enabled', - help='Get whether Transmission service is enabled') - # Enable service subparsers.add_parser('enable', help='Enable Transmission service') @@ -58,50 +54,16 @@ def parse_arguments(): return parser.parse_args() -def subcommand_get_enabled(_): - """Get whether Transmission service is enabled.""" - try: - with open(SERVICE_CONFIG, 'r') as file_handle: - for line in file_handle: - if line.startswith('ENABLE_DAEMON'): - value = line.split('=')[1].strip() - print('yes' if int(value) else 'no') - return - except IOError: - pass - - print('no') - - def subcommand_enable(_): """Start Transmission service.""" - set_service_enable(enable=True) - action_utils.service_start('transmission-daemon') - subprocess.call(['a2enconf', 'transmission-plinth']) - action_utils.service_reload('apache2') + action_utils.service_enable('transmission-daemon') + action_utils.webserver_enable('transmission-plinth') def subcommand_disable(_): """Stop Transmission service.""" - subprocess.call(['a2disconf', 'transmission-plinth']) - action_utils.service_reload('apache2') - action_utils.service_stop('transmission-daemon') - set_service_enable(enable=False) - - -def set_service_enable(enable): - """Enable/disable Transmission daemon; enable: boolean.""" - newline = 'ENABLE_DAEMON=1\n' if enable else 'ENABLE_DAEMON=0\n' - - with open(SERVICE_CONFIG, 'r') as file_handle: - lines = file_handle.readlines() - for index, line in enumerate(lines): - if line.startswith('ENABLE_DAEMON'): - lines[index] = newline - break - - with open(SERVICE_CONFIG, 'w') as file_handle: - file_handle.writelines(lines) + action_utils.webserver_disable('transmission-plinth') + action_utils.service_disable('transmission-daemon') def subcommand_merge_configuration(arguments): diff --git a/plinth/modules/transmission/__init__.py b/plinth/modules/transmission/__init__.py index 664874ff1..7a1b46916 100644 --- a/plinth/modules/transmission/__init__.py +++ b/plinth/modules/transmission/__init__.py @@ -22,6 +22,7 @@ Plinth module to configure Transmission server from gettext import gettext as _ from plinth import actions +from plinth import action_utils from plinth import cfg from plinth import service as service_module @@ -37,10 +38,18 @@ def init(): menu.add_urlname(_('BitTorrent (Transmission)'), 'glyphicon-save', 'transmission:index', 100) - output = actions.run('transmission', ['get-enabled']) - enabled = (output.strip() == 'yes') - global service service = service_module.Service( 'transmission', _('Transmission BitTorrent'), ['http', 'https'], - is_external=True, enabled=enabled) + is_external=True, enabled=is_enabled()) + + +def is_enabled(): + """Return whether the module is enabled.""" + return (action_utils.service_is_enabled('transmission-daemon') and + action_utils.webserver_is_enabled('transmission-plinth')) + + +def is_running(): + """Return whether the service is running.""" + return action_utils.service_is_running('transmission-daemon') diff --git a/plinth/modules/transmission/views.py b/plinth/modules/transmission/views.py index 11658d4bc..d02f22ce1 100644 --- a/plinth/modules/transmission/views.py +++ b/plinth/modules/transmission/views.py @@ -28,7 +28,6 @@ import socket from .forms import TransmissionForm from plinth import actions -from plinth import action_utils from plinth import package from plinth.modules import transmission @@ -67,15 +66,12 @@ def index(request): def get_status(): """Get the current settings from Transmission server.""" - output = actions.run('transmission', ['get-enabled']) - enabled = (output.strip() == 'yes') - configuration = open(TRANSMISSION_CONFIG, 'r').read() status = json.loads(configuration) status = {key.translate(str.maketrans({'-': '_'})): value for key, value in status.items()} - status['enabled'] = enabled - status['is_running'] = action_utils.service_is_running('transmission-daemon') + status['enabled'] = transmission.is_enabled() + status['is_running'] = transmission.is_running() status['hostname'] = socket.gethostname() return status