i2p: flake8 and yapf fixes

- Run isort and yapf.

- Better docstrings.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Sunil Mohan Adapa 2019-04-29 14:12:40 -07:00
parent 66b161b986
commit a73f002ed6
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
4 changed files with 57 additions and 64 deletions

View File

@ -44,10 +44,11 @@ def parse_arguments():
subparser.add_argument('--name', help='Name of the entry', required=True) subparser.add_argument('--name', help='Name of the entry', required=True)
subparser.add_argument('--url', help='URL of the entry', required=True) subparser.add_argument('--url', help='URL of the entry', required=True)
subparser = subparsers.add_parser( subparser = subparsers.add_parser('set-tunnel-property',
'set-tunnel-property', help='Modify configuration of a tunnel') help='Modify configuration of a tunnel')
subparser.add_argument('--name', help='Name of the tunnel', required=True) subparser.add_argument('--name', help='Name of the tunnel', required=True)
subparser.add_argument('--property', help='Property to modify', required=True) subparser.add_argument('--property', help='Property to modify',
required=True)
subparser.add_argument('--value', help='Value to assign', required=True) subparser.add_argument('--value', help='Value to assign', required=True)
subparsers.required = True subparsers.required = True
@ -67,10 +68,7 @@ def subcommand_disable(_):
def subcommand_set_tunnel_property(arguments): def subcommand_set_tunnel_property(arguments):
""" """Modify the configuration file for a certain tunnel."""
Modifies the configuration for a certain tunnel
"""
editor = TunnelEditor() editor = TunnelEditor()
editor \ editor \
.read_conf() \ .read_conf() \
@ -79,9 +77,7 @@ def subcommand_set_tunnel_property(arguments):
.write_conf() .write_conf()
print('Updated "{property}" of {filename} to {value}'.format( print('Updated "{property}" of {filename} to {value}'.format(
property=editor.calc_prop_path(arguments.property), property=editor.calc_prop_path(arguments.property),
filename=editor.conf_filename, filename=editor.conf_filename, value=arguments.value))
value=arguments.value
))
def subcommand_add_favorite(arguments): def subcommand_add_favorite(arguments):

View File

@ -24,6 +24,7 @@ from plinth import action_utils, actions, frontpage
from plinth import service as service_module from plinth import service as service_module
from plinth.menu import main_menu from plinth.menu import main_menu
from plinth.modules.users import register_group from plinth.modules.users import register_group
from .manifest import backup, clients from .manifest import backup, clients
version = 1 version = 1
@ -70,9 +71,7 @@ tunnels_to_manage = {
'Irc2P': 'i2p_irc-freedombox' 'Irc2P': 'i2p_irc-freedombox'
} }
service_ports = [ service_ports = ['http', 'https'] + list(tunnels_to_manage.values())
'http', 'https'
] + list(tunnels_to_manage.values())
def init(): def init():
@ -84,9 +83,9 @@ def init():
global service global service
setup_helper = globals()['setup_helper'] setup_helper = globals()['setup_helper']
if setup_helper.get_state() != 'needs-setup': if setup_helper.get_state() != 'needs-setup':
service = service_module.Service(managed_services[0], name, ports=service_ports, service = service_module.Service(
is_external=True, is_enabled=is_enabled, enable=enable, managed_services[0], name, ports=service_ports, is_external=True,
disable=disable, is_enabled=is_enabled, enable=enable, disable=disable,
is_running=is_running) is_running=is_running)
if is_enabled(): if is_enabled():
@ -108,20 +107,18 @@ def setup(helper, old_version=None):
]) ])
# Tunnels to all interfaces # Tunnels to all interfaces
for tunnel in tunnels_to_manage.keys(): for tunnel in tunnels_to_manage:
helper.call('post', actions.superuser_run, 'i2p', [ helper.call('post', actions.superuser_run, 'i2p', [
'set-tunnel-property', 'set-tunnel-property', '--name', tunnel, '--property', 'interface',
'--name', tunnel,
'--property', 'interface',
'--value', '0.0.0.0' '--value', '0.0.0.0'
]) ])
helper.call('post', disable) helper.call('post', disable)
helper.call('post', enable) helper.call('post', enable)
global service global service
if service is None: if service is None:
service = service_module.Service(managed_services[0], name, ports=service_ports, service = service_module.Service(
is_external=True, is_enabled=is_enabled, enable=enable, managed_services[0], name, ports=service_ports, is_external=True,
disable=disable, is_enabled=is_enabled, enable=enable, disable=disable,
is_running=is_running) is_running=is_running)
helper.call('post', service.notify_enabled, None, True) helper.call('post', service.notify_enabled, None, True)

View File

@ -13,6 +13,10 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
"""
Various helpers for the I2P app.
"""
import os import os
import re import re
@ -23,10 +27,11 @@ FILE_TUNNEL_CONF = os.path.join(I2P_CONF_DIR, 'i2ptunnel.config')
TUNNEL_IDX_REGEX = re.compile(r'tunnel.(\d+).name$') TUNNEL_IDX_REGEX = re.compile(r'tunnel.(\d+).name$')
class TunnelEditor(object): class TunnelEditor():
""" """Helper to edit I2P tunnel configuration file using augeas.
:type aug: augeas.Augeas :type aug: augeas.Augeas
""" """
def __init__(self, conf_filename=None, idx=None): def __init__(self, conf_filename=None, idx=None):
@ -36,62 +41,70 @@ class TunnelEditor(object):
@property @property
def lines(self): def lines(self):
"""Return lines from configuration file."""
if self.aug: if self.aug:
return self.aug.match('/files{}/*'.format(self.conf_filename)) return self.aug.match('/files{}/*'.format(self.conf_filename))
else:
return [] return []
def read_conf(self): def read_conf(self):
"""Return an instance of Augeaus for processing APT configuration.""" """Load an instance of Augeaus for processing APT configuration.
self.aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
augeas.Augeas.NO_MODL_AUTOLOAD) Chainable method.
"""
self.aug = augeas.Augeas(
flags=augeas.Augeas.NO_LOAD + augeas.Augeas.NO_MODL_AUTOLOAD)
self.aug.set('/augeas/load/Properties/lens', 'Properties.lns') self.aug.set('/augeas/load/Properties/lens', 'Properties.lns')
self.aug.set('/augeas/load/Properties/incl[last() + 1]', self.conf_filename) self.aug.set('/augeas/load/Properties/incl[last() + 1]',
self.conf_filename)
self.aug.load() self.aug.load()
return self return self
def write_conf(self): def write_conf(self):
"""Write changes to the configuration file to disk.
Chainable method.
"""
self.aug.save() self.aug.save()
return self return self
def set_tunnel_idx(self, name): def set_tunnel_idx(self, name):
"""Finds the index of the tunnel with the given name.
"""
Finds the index of the tunnel with the given name.
Chainable method. Chainable method.
:type name: basestring :type name: basestring
"""
"""
for prop in self.aug.match('/files{}/*'.format(self.conf_filename)): for prop in self.aug.match('/files{}/*'.format(self.conf_filename)):
match = TUNNEL_IDX_REGEX.search(prop) match = TUNNEL_IDX_REGEX.search(prop)
if match and self.aug.get(prop) == name: if match and self.aug.get(prop) == name:
self.idx = int(match.group(1)) self.idx = int(match.group(1))
return self return self
raise ValueError('No tunnel called {}'.format(name)) raise ValueError('No tunnel called {}'.format(name))
def calc_prop_path(self, tunnel_prop): def calc_prop_path(self, tunnel_prop):
""" """Calculates the property name as found in the properties files.
Calculates the property name as found in the properties files
:type tunnel_prop: str :type tunnel_prop: str
:rtype: basestring :rtype: basestring
""" """
calced_prop_path = '/files{filepath}/tunnel.{idx}.{tunnel_prop}'.format( calced_prop_path = '/files{filepath}/tunnel.{idx}.{tunnel_prop}'.format(
idx=self.idx, tunnel_prop=tunnel_prop, idx=self.idx, tunnel_prop=tunnel_prop, filepath=self.conf_filename)
filepath=self.conf_filename
)
return calced_prop_path return calced_prop_path
def set_tunnel_prop(self, tunnel_prop, value): def set_tunnel_prop(self, tunnel_prop, value):
""" """Updates a tunnel's property.
Updates a tunnel's property.
The idx has to be set and the property has to exist in the config file and belong to the tunnel's properties. The idx has to be set and the property has to exist in the config file
and belong to the tunnel's properties.
see calc_prop_path See calc_prop_path.
Chainable method. Chainable method.
@ -101,9 +114,11 @@ class TunnelEditor(object):
:type value: basestring | int :type value: basestring | int
:return: :return:
:rtype: :rtype:
""" """
if self.idx is None: if self.idx is None:
raise ValueError('Please init the tunnel index before calling this method') raise ValueError(
'Please init the tunnel index before calling this method')
calc_prop_path = self.calc_prop_path(tunnel_prop) calc_prop_path = self.calc_prop_path(tunnel_prop)
self.aug.set(calc_prop_path, value) self.aug.set(calc_prop_path, value)
@ -113,6 +128,7 @@ class TunnelEditor(object):
ret = self.aug.get(self.calc_prop_path(tunnel_prop)) ret = self.aug.get(self.calc_prop_path(tunnel_prop))
if ret is None: if ret is None:
raise KeyError('Unknown property {}'.format(tunnel_prop)) raise KeyError('Unknown property {}'.format(tunnel_prop))
return ret return ret
def __setitem__(self, tunnel_prop, value): def __setitem__(self, tunnel_prop, value):

View File

@ -1,16 +0,0 @@
# This file is part of FreedomBox.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#