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') '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('--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.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.add_argument('--icon', help='URL to icon', required=False)
subparser = subparsers.add_parser('set-tunnel-property', subparser = subparsers.add_parser('set-tunnel-property',
@ -92,12 +93,8 @@ def subcommand_add_favorite(arguments):
url = arguments.url url = arguments.url
editor = RouterEditor() editor = RouterEditor()
editor.read_conf().add_favorite( editor.read_conf().add_favorite(arguments.name, url, arguments.description,
arguments.name, arguments.icon).write_conf()
url,
arguments.description,
arguments.icon
).write_conf()
print('Added {} to favorites'.format(url)) 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.menu import main_menu
from plinth.modules.i2p.resources import FAVORITES from plinth.modules.i2p.resources import FAVORITES
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
@ -97,8 +98,10 @@ def setup(helper, old_version=None):
for fav in FAVORITES: for fav in FAVORITES:
args = [ args = [
'add-favorite', 'add-favorite',
'--name', fav.get('name'), '--name',
'--url', fav.get('url'), fav.get('name'),
'--url',
fav.get('url'),
] ]
if 'icon' in fav: if 'icon' in fav:
args.extend(['--icon', fav.get('icon')]) args.extend(['--icon', fav.get('icon')])

View File

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

View File

@ -14,6 +14,9 @@
# 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/>.
# #
"""
Application manifest for I2P.
"""
from django.utils.translation import ugettext_lazy as _ 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 # 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/>.
# #
"""
Pre-defined list of favorites for I2P and some additional favorites.
"""
DEFAULT_FAVORITES = [ DEFAULT_FAVORITES = [
{ {

View File

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