diff --git a/actions/i2p b/actions/i2p index c170aadf0..2ee857510 100755 --- a/actions/i2p +++ b/actions/i2p @@ -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)) diff --git a/plinth/modules/i2p/__init__.py b/plinth/modules/i2p/__init__.py index 8cfc59e58..a0334e14b 100644 --- a/plinth/modules/i2p/__init__.py +++ b/plinth/modules/i2p/__init__.py @@ -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')]) diff --git a/plinth/modules/i2p/helpers.py b/plinth/modules/i2p/helpers.py index 883db10b8..6374fc9f2 100644 --- a/plinth/modules/i2p/helpers.py +++ b/plinth/modules/i2p/helpers.py @@ -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 diff --git a/plinth/modules/i2p/manifest.py b/plinth/modules/i2p/manifest.py index c65bdebe7..472b9e016 100644 --- a/plinth/modules/i2p/manifest.py +++ b/plinth/modules/i2p/manifest.py @@ -14,6 +14,9 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # +""" +Application manifest for I2P. +""" from django.utils.translation import ugettext_lazy as _ diff --git a/plinth/modules/i2p/resources.py b/plinth/modules/i2p/resources.py index 1efe5783b..b6b9df7fd 100644 --- a/plinth/modules/i2p/resources.py +++ b/plinth/modules/i2p/resources.py @@ -14,6 +14,9 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # +""" +Pre-defined list of favorites for I2P and some additional favorites. +""" DEFAULT_FAVORITES = [ { diff --git a/plinth/modules/i2p/tests/__init__.py b/plinth/modules/i2p/tests/__init__.py index 5d1e441e2..1bf534884 100644 --- a/plinth/modules/i2p/tests/__init__.py +++ b/plinth/modules/i2p/tests/__init__.py @@ -14,6 +14,10 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # +""" +Common parts for all I2P tests. +""" + from pathlib import Path DATA_DIR = Path(__file__).parent / 'data'