Indentation updates

- No spaces at the beginning and end of a single line comment. (python code does this)
- Double space after a statement in a multi-line comment. (PEP-8)
- Enforce 79 character limit. (PEP-8)
- Empty line after a block ends make code more readable.
- Empty line at the end of a multi line comment. (python code seems to do this)
This commit is contained in:
Sunil Mohan Adapa 2014-08-17 19:45:00 +05:30
parent 4387c28495
commit 5e25a432c3
4 changed files with 33 additions and 18 deletions

View File

@ -32,7 +32,8 @@ Actions run commands with this contract (version 1.1):
C. Only one action can be called at a time. C. Only one action can be called at a time.
This prevents us from appending multiple (unexpected) actions to the call. This prevents us from appending multiple (unexpected) actions to the
call.
$ action="echo '$options'; echo 'oops'" $ action="echo '$options'; echo 'oops'"
$ options="hi" $ options="hi"
@ -47,12 +48,12 @@ Actions run commands with this contract (version 1.1):
$ $action # oops, the file system is gone! $ $action # oops, the file system is gone!
Arguments that fail this validation won't, but probably should, raise a Arguments that fail this validation won't, but probably should, raise a
ValueError. They don't because sanitizing this case is significantly ValueError. They don't because sanitizing this case is significantly
easier than detecting if it occurs. easier than detecting if it occurs.
The options list is coerced into a space-separated string before being The options list is coerced into a space-separated string before being
shell-escaped. Option lists including shell escape characters may need to shell-escaped. Option lists including shell escape characters may need
be unescaped on the receiving end. to be unescaped on the receiving end.
E. Actions must exist in the actions directory. E. Actions must exist in the actions directory.
@ -86,26 +87,31 @@ LOGGER = logging.getLogger(__name__)
def run(action, options=None, async=False): def run(action, options=None, async=False):
"""Safely run a specific action as the current user. """Safely run a specific action as the current user.
See actions._run for more information. See actions._run for more information.
""" """
return _run(action, options, async, False) return _run(action, options, async, False)
def superuser_run(action, options=None, async=False): def superuser_run(action, options=None, async=False):
"""Safely run a specific action as root. """Safely run a specific action as root.
See actions._run for more information. See actions._run for more information.
""" """
return _run(action, options, async, True) return _run(action, options, async, True)
def _run(action, options=None, async=False, run_as_root=False): def _run(action, options=None, async=False, run_as_root=False):
"""Safely run a specific action as a normal user or root. """Safely run a specific action as a normal user or root.
actions are pulled from the actions directory.
Actions are pulled from the actions directory.
- options are added to the action command. - options are added to the action command.
- async: run asynchronously or wait for the command to complete. - async: run asynchronously or wait for the command to complete.
- run_as_root: execute the command through sudo. - run_as_root: execute the command through sudo.
"""
"""
if options is None: if options is None:
options = [] options = []
@ -124,7 +130,8 @@ def _run(action, options=None, async=False, run_as_root=False):
cmd = [cmd] cmd = [cmd]
# contract: 3C, 3D: don't allow users to insert escape characters in options # contract: 3C, 3D: don't allow users to insert escape characters in
# options
if options: if options:
if not hasattr(options, "__iter__"): if not hasattr(options, "__iter__"):
options = [options] options = [options]

View File

@ -15,13 +15,16 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
""" Plinth project specific errors """ """
Project specific errors
"""
class PlinthError(Exception): class PlinthError(Exception):
"""Base class for all Plinth specific errors."""
pass pass
class ActionError(PlinthError): class ActionError(PlinthError):
""" Use this error for exceptions when executing an action """ """Use this error for exceptions when executing an action."""
pass pass

19
menu.py
View File

@ -19,8 +19,8 @@ class Menu(object):
orders, but feel free to disregard that. If you need more orders, but feel free to disregard that. If you need more
granularity, don't bother renumbering things. Feel free to granularity, don't bother renumbering things. Feel free to
use fractional orders. use fractional orders.
"""
"""
self.label = label self.label = label
self.icon = icon self.icon = icon
self.url = url self.url = url
@ -31,11 +31,12 @@ class Menu(object):
self.items = [] self.items = []
def get(self, urlname, url_args=None, url_kwargs=None): def get(self, urlname, url_args=None, url_kwargs=None):
"""Return a menu item with given URL name""" """Return a menu item with given URL name."""
url = reverse(urlname, args=url_args, kwargs=url_kwargs) url = reverse(urlname, args=url_args, kwargs=url_kwargs)
for item in self.items: for item in self.items:
if item.url == url: if item.url == url:
return item return item
raise KeyError('Menu item not found') raise KeyError('Menu item not found')
def sort_items(self): def sort_items(self):
@ -44,14 +45,18 @@ class Menu(object):
def add_urlname(self, label, icon, urlname, order=50, url_args=None, def add_urlname(self, label, icon, urlname, order=50, url_args=None,
url_kwargs=None): url_kwargs=None):
""" Add a named URL to the menu (via add_item) """Add a named URL to the menu (via add_item).
url_args and url_kwargs will be passed on to url reverse """
url_args and url_kwargs will be passed on to Django reverse().
"""
url = reverse(urlname, args=url_args, kwargs=url_kwargs) url = reverse(urlname, args=url_args, kwargs=url_kwargs)
return self.add_item(label, icon, url, order) return self.add_item(label, icon, url, order)
def add_item(self, label, icon, url, order=50): def add_item(self, label, icon, url, order=50):
"""This method creates a menu item with the parameters, adds """Create a new menu item with given parameters, add it to this menu and
that menu item to this menu, and returns the item. return it.
""" """
item = Menu(label=label, icon=icon, url=url, order=order) item = Menu(label=label, icon=icon, url=url, order=order)
self.items.append(item) self.items.append(item)
@ -59,7 +64,7 @@ class Menu(object):
return item return item
def active_item(self, request): def active_item(self, request):
"""Return the first active item (e.g. submenu) that is found""" """Return the first active item (e.g. submenu) that is found."""
for item in self.items: for item in self.items:
if request.path.startswith(item.url): if request.path.startswith(item.url):
return item return item

View File

@ -10,8 +10,8 @@ def init():
"""Initialize the Help module""" """Initialize the Help module"""
menu = cfg.main_menu.add_urlname(_('Documentation'), 'icon-book', menu = cfg.main_menu.add_urlname(_('Documentation'), 'icon-book',
'help:index', 101) 'help:index', 101)
menu.add_urlname(_("Where to Get Help"), "icon-search", menu.add_urlname(_('Where to Get Help'), 'icon-search',
"help:index_explicit", 5) 'help:index_explicit', 5)
menu.add_urlname(_('Developer\'s Manual'), 'icon-info-sign', menu.add_urlname(_('Developer\'s Manual'), 'icon-info-sign',
'help:helppage', 10, url_args=('plinth',)) 'help:helppage', 10, url_args=('plinth',))
menu.add_urlname(_('FAQ'), 'icon-question-sign', 'help:helppage', 20, menu.add_urlname(_('FAQ'), 'icon-question-sign', 'help:helppage', 20,