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('--url', help='URL of the entry', required=True)
subparser = subparsers.add_parser(
'set-tunnel-property', help='Modify configuration of a tunnel')
subparser = subparsers.add_parser('set-tunnel-property',
help='Modify configuration of a tunnel')
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)
subparsers.required = True
@ -67,10 +68,7 @@ def subcommand_disable(_):
def subcommand_set_tunnel_property(arguments):
"""
Modifies the configuration for a certain tunnel
"""
"""Modify the configuration file for a certain tunnel."""
editor = TunnelEditor()
editor \
.read_conf() \
@ -79,9 +77,7 @@ def subcommand_set_tunnel_property(arguments):
.write_conf()
print('Updated "{property}" of {filename} to {value}'.format(
property=editor.calc_prop_path(arguments.property),
filename=editor.conf_filename,
value=arguments.value
))
filename=editor.conf_filename, value=arguments.value))
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.menu import main_menu
from plinth.modules.users import register_group
from .manifest import backup, clients
version = 1
@ -70,9 +71,7 @@ tunnels_to_manage = {
'Irc2P': 'i2p_irc-freedombox'
}
service_ports = [
'http', 'https'
] + list(tunnels_to_manage.values())
service_ports = ['http', 'https'] + list(tunnels_to_manage.values())
def init():
@ -84,10 +83,10 @@ def init():
global service
setup_helper = globals()['setup_helper']
if setup_helper.get_state() != 'needs-setup':
service = service_module.Service(managed_services[0], name, ports=service_ports,
is_external=True, is_enabled=is_enabled, enable=enable,
disable=disable,
is_running=is_running)
service = service_module.Service(
managed_services[0], name, ports=service_ports, is_external=True,
is_enabled=is_enabled, enable=enable, disable=disable,
is_running=is_running)
if is_enabled():
add_shortcut()
@ -108,21 +107,19 @@ def setup(helper, old_version=None):
])
# Tunnels to all interfaces
for tunnel in tunnels_to_manage.keys():
for tunnel in tunnels_to_manage:
helper.call('post', actions.superuser_run, 'i2p', [
'set-tunnel-property',
'--name', tunnel,
'--property', 'interface',
'set-tunnel-property', '--name', tunnel, '--property', 'interface',
'--value', '0.0.0.0'
])
helper.call('post', disable)
helper.call('post', enable)
global service
if service is None:
service = service_module.Service(managed_services[0], name, ports=service_ports,
is_external=True, is_enabled=is_enabled, enable=enable,
disable=disable,
is_running=is_running)
service = service_module.Service(
managed_services[0], name, ports=service_ports, is_external=True,
is_enabled=is_enabled, enable=enable, disable=disable,
is_running=is_running)
helper.call('post', service.notify_enabled, None, True)
helper.call('post', add_shortcut)

View File

@ -13,6 +13,10 @@
# 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/>.
#
"""
Various helpers for the I2P app.
"""
import os
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$')
class TunnelEditor(object):
"""
class TunnelEditor():
"""Helper to edit I2P tunnel configuration file using augeas.
:type aug: augeas.Augeas
"""
def __init__(self, conf_filename=None, idx=None):
@ -36,62 +41,70 @@ class TunnelEditor(object):
@property
def lines(self):
"""Return lines from configuration file."""
if self.aug:
return self.aug.match('/files{}/*'.format(self.conf_filename))
else:
return []
return []
def read_conf(self):
"""Return an instance of Augeaus for processing APT configuration."""
self.aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
augeas.Augeas.NO_MODL_AUTOLOAD)
"""Load an instance of Augeaus for processing APT configuration.
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/incl[last() + 1]', self.conf_filename)
self.aug.set('/augeas/load/Properties/incl[last() + 1]',
self.conf_filename)
self.aug.load()
return self
def write_conf(self):
"""Write changes to the configuration file to disk.
Chainable method.
"""
self.aug.save()
return self
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.
:type name: basestring
"""
"""
for prop in self.aug.match('/files{}/*'.format(self.conf_filename)):
match = TUNNEL_IDX_REGEX.search(prop)
if match and self.aug.get(prop) == name:
self.idx = int(match.group(1))
return self
raise ValueError('No tunnel called {}'.format(name))
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
:rtype: basestring
"""
calced_prop_path = '/files{filepath}/tunnel.{idx}.{tunnel_prop}'.format(
idx=self.idx, tunnel_prop=tunnel_prop,
filepath=self.conf_filename
)
idx=self.idx, tunnel_prop=tunnel_prop, filepath=self.conf_filename)
return calced_prop_path
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.
@ -101,9 +114,11 @@ class TunnelEditor(object):
:type value: basestring | int
:return:
:rtype:
"""
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)
self.aug.set(calc_prop_path, value)
@ -113,6 +128,7 @@ class TunnelEditor(object):
ret = self.aug.get(self.calc_prop_path(tunnel_prop))
if ret is None:
raise KeyError('Unknown property {}'.format(tunnel_prop))
return ret
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/>.
#