i2p: Minor flake8 and yapf fixes

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Sunil Mohan Adapa 2019-04-29 16:35:15 -07:00
parent dca91da571
commit 97ed7fe144
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
6 changed files with 57 additions and 35 deletions

View File

@ -43,7 +43,8 @@ def parse_arguments():
'add-favorite', help='Add an eepsite to the list of favorites')
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('--description', help='short description', required=False)
subparser.add_argument('--description', help='Short description',
required=False)
subparser.add_argument('--icon', help='URL to icon', required=False)
subparser = subparsers.add_parser('set-tunnel-property',
@ -92,12 +93,8 @@ def subcommand_add_favorite(arguments):
url = arguments.url
editor = RouterEditor()
editor.read_conf().add_favorite(
arguments.name,
url,
arguments.description,
arguments.icon
).write_conf()
editor.read_conf().add_favorite(arguments.name, url, arguments.description,
arguments.icon).write_conf()
print('Added {} to favorites'.format(url))

View File

@ -25,6 +25,7 @@ from plinth import service as service_module
from plinth.menu import main_menu
from plinth.modules.i2p.resources import FAVORITES
from plinth.modules.users import register_group
from .manifest import backup, clients
version = 1
@ -97,8 +98,10 @@ def setup(helper, old_version=None):
for fav in FAVORITES:
args = [
'add-favorite',
'--name', fav.get('name'),
'--url', fav.get('url'),
'--name',
fav.get('name'),
'--url',
fav.get('url'),
]
if 'icon' in fav:
args.extend(['--icon', fav.get('icon')])

View File

@ -17,9 +17,9 @@
Various helpers for the I2P app.
"""
from collections import OrderedDict
import os
import re
from collections import OrderedDict
import augeas
@ -137,10 +137,11 @@ class TunnelEditor():
self.aug.set(self.calc_prop_path(tunnel_prop), value)
class RouterEditor(object):
"""
class RouterEditor():
"""Helper to edit I2P router configuration file using augeas.
:type aug: augeas.Augeas
"""
FAVORITE_PROP = 'routerconsole.favorites'
@ -151,44 +152,58 @@ class RouterEditor(object):
self.aug = None
def read_conf(self):
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
@property
def favorite_property(self):
return '/files{filename}/{prop}'.format(filename=self.conf_filename, prop=self.FAVORITE_PROP)
"""Return the favourites property from configuration file."""
return '/files{filename}/{prop}'.format(filename=self.conf_filename,
prop=self.FAVORITE_PROP)
def add_favorite(self, name, url, description=None, icon=None):
"""
Adds a favorite to the router.config
"""Add a favorite to the router configuration file.
Favorites are in a single string and separated by ','.
none of the incoming params can therefore use commas.
I2P replaces the commas by dots.
Favorites are in a single string and separated by ','. none of the
incoming params can therefore use commas. I2P replaces the commas by
dots.
That's ok for the name and description,
but not for the url and icon
That's ok for the name and description, but not for the url and icon.
:type name: basestring
:type url: basestring
:type description: basestring
:type icon: basestring
"""
if not description:
description = ''
if not icon:
icon = '/themes/console/images/eepsite.png'
if ',' in url:
raise ValueError('URL cannot contain commas')
if ',' in icon:
raise ValueError('Icon cannot contain commas')
@ -198,13 +213,12 @@ class RouterEditor(object):
prop = self.favorite_property
favorites = self.aug.get(prop) or ''
new_favorite = '{name},{description},{url},{icon},'.format(
name=name, description=description, url=url,
icon=icon
)
name=name, description=description, url=url, icon=icon)
self.aug.set(prop, favorites + new_favorite)
return self
def get_favorites(self):
"""Return list of favorites."""
favs_string = self.aug.get(self.favorite_property) or ''
favs_split = favs_string.split(',')
@ -218,15 +232,13 @@ class RouterEditor(object):
raise SyntaxError("Invalid number of fields in favorite line")
favs = OrderedDict()
i = 0
while i < favs_len:
next_idx = i + self.FAVORITE_TUPLE_SIZE
t = favs_split[i:next_idx]
name, description, url, icon = t
for index in range(0, favs_len, self.FAVORITE_TUPLE_SIZE):
next_index = index + self.FAVORITE_TUPLE_SIZE
name, description, url, icon = favs_split[index:next_index]
favs[url] = {
"name": name,
"description": description,
"icon": icon
'name': name,
'description': description,
'icon': icon
}
i = next_idx
return favs

View File

@ -14,6 +14,9 @@
# 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/>.
#
"""
Application manifest for I2P.
"""
from django.utils.translation import ugettext_lazy as _

View File

@ -14,6 +14,9 @@
# 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/>.
#
"""
Pre-defined list of favorites for I2P and some additional favorites.
"""
DEFAULT_FAVORITES = [
{

View File

@ -14,6 +14,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/>.
#
"""
Common parts for all I2P tests.
"""
from pathlib import Path
DATA_DIR = Path(__file__).parent / 'data'