From ef493ae243b9bd854bae1360665bd5f790a0f7b5 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Fri, 6 Jun 2014 18:21:54 +0530 Subject: [PATCH] Use new-style classes everywhere --- filedict.py | 9 ++++++--- logger.py | 3 ++- menu.py | 2 +- model.py | 2 ++ modules/apps/apps.py | 5 +++-- modules/config/config.py | 5 ++--- modules/diagnostics/diagnostics.py | 14 ++++++++------ modules/expert_mode/expert_mode.py | 5 +++-- modules/firewall/firewall.py | 4 ++-- modules/first_boot/first_boot.py | 4 ++-- modules/help/help.py | 13 ++++++++----- modules/lib/auth_page.py | 4 ++-- modules/lib/user_store.py | 3 +++ modules/owncloud/owncloud.py | 5 +++-- modules/packages/packages.py | 5 +++-- modules/pagekite/pagekite.py | 8 ++++---- modules/santiago/santiago.py | 30 ++++++++++++++++-------------- modules/system/system.py | 10 +++++----- modules/tor/tor.py | 8 +++++--- modules/users/users.py | 14 ++++++++------ modules/xmpp/xmpp.py | 19 +++++++++++-------- plugin_mount.py | 22 ++++++++++++++-------- tests/actions_test.py | 1 - tests/auth_test.py | 4 ++-- tests/user_store_test.py | 4 ++-- 25 files changed, 117 insertions(+), 86 deletions(-) diff --git a/filedict.py b/filedict.py index 94f779759..955e0e09f 100644 --- a/filedict.py +++ b/filedict.py @@ -10,12 +10,15 @@ import json import UserDict import sqlite3 + -class DefaultArg: +class DefaultArg(object): pass -class Solutions: + +class Solutions(object): Sqlite3 = 0 + class FileDict(UserDict.DictMixin): "A dictionary that stores its data persistantly in a file" @@ -134,7 +137,7 @@ class FileDict(UserDict.DictMixin): def batch(self): return self._Batch(self) - class _Batch: + class _Batch(object): def __init__(self, d): self.__d = d diff --git a/logger.py b/logger.py index 6fd8171fa..9a753948e 100644 --- a/logger.py +++ b/logger.py @@ -6,7 +6,8 @@ cherrypy.log.error_file = cfg.status_log_file cherrypy.log.access_file = cfg.access_log_file cherrypy.log.screen = False -class Logger(): + +class Logger(object): """By convention, log levels are DEBUG, INFO, WARNING, ERROR and CRITICAL.""" def log(self, msg, level="DEBUG"): try: diff --git a/menu.py b/menu.py index 84b26c19d..0092f4561 100644 --- a/menu.py +++ b/menu.py @@ -3,7 +3,7 @@ import cherrypy import cfg -class Menu(): +class Menu(object): """One menu item.""" def __init__(self, label="", icon="", url="#", order=50): """label is the text that is displayed on the menu. diff --git a/model.py b/model.py index 6ad424e23..f92952221 100644 --- a/model.py +++ b/model.py @@ -3,6 +3,8 @@ class User(dict): is a bcrypt hash of the password), salt, groups, and an email address. They can be blank or None, but the keys must exist. """ def __init__(self, dict=None): + super(User, self).__init__() + for key in ['username', 'name', 'passphrase', 'salt', 'email']: self[key] = '' for key in ['groups']: diff --git a/modules/apps/apps.py b/modules/apps/apps.py index 05bb7fad3..1487a72c5 100644 --- a/modules/apps/apps.py +++ b/modules/apps/apps.py @@ -6,8 +6,9 @@ import util class Apps(PagePlugin): - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(Apps, self).__init__() + self.register_page("apps") self.menu = cfg.main_menu.add_item("Apps", "icon-download-alt", "/apps", 80) self.menu.add_item("Chat", "icon-comment", "/../jwchat", 30) diff --git a/modules/config/config.py b/modules/config/config.py index bda9dd2de..ad9e4eb91 100644 --- a/modules/config/config.py +++ b/modules/config/config.py @@ -91,9 +91,8 @@ and must not be greater than 63 characters in length.'), class Configuration(PagePlugin): """System configuration page""" - def __init__(self, *args, **kwargs): - del args # Unused - del kwargs # Unused + def __init__(self): + super(Configuration, self).__init__() self.register_page('sys.config') diff --git a/modules/diagnostics/diagnostics.py b/modules/diagnostics/diagnostics.py index e82de7d11..f3db5672c 100644 --- a/modules/diagnostics/diagnostics.py +++ b/modules/diagnostics/diagnostics.py @@ -28,10 +28,11 @@ import cfg import util -class diagnostics(PagePlugin): +class Diagnostics(PagePlugin): order = 30 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(Diagnostics, self).__init__() + self.register_page("sys.diagnostics") cfg.html_root.sys.menu.add_item("Diagnostics", "icon-screenshot", "/sys/diagnostics", 30) @@ -41,10 +42,11 @@ class diagnostics(PagePlugin): return util.render_template(template='diagnostics', title=_('System Diagnostics')) -class test(PagePlugin): +class Test(PagePlugin): order = 31 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(Test, self).__init__() + self.register_page("sys.diagnostics.test") @cherrypy.expose diff --git a/modules/expert_mode/expert_mode.py b/modules/expert_mode/expert_mode.py index 50c2811e6..49c78e297 100644 --- a/modules/expert_mode/expert_mode.py +++ b/modules/expert_mode/expert_mode.py @@ -22,8 +22,9 @@ class Experts(PagePlugin): """Expert forms page""" order = 60 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(Experts, self).__init__() + self.register_page('sys.expert') cfg.html_root.sys.menu.add_item(_('Expert Mode'), 'icon-cog', diff --git a/modules/firewall/firewall.py b/modules/firewall/firewall.py index ebdbf67a7..6d5214fde 100644 --- a/modules/firewall/firewall.py +++ b/modules/firewall/firewall.py @@ -34,8 +34,8 @@ class Firewall(PagePlugin): """Firewall menu entry and introduction page""" order = 40 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(Firewall, self).__init__() self.register_page('sys.firewall') cfg.html_root.sys.menu.add_item(_('Firewall'), 'icon-flag', diff --git a/modules/first_boot/first_boot.py b/modules/first_boot/first_boot.py index 7bcf98e27..204b65ac4 100644 --- a/modules/first_boot/first_boot.py +++ b/modules/first_boot/first_boot.py @@ -69,8 +69,8 @@ FreedomBox!')) class FirstBoot(PagePlugin): """First boot wizard""" - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(FirstBoot, self).__init__() # this is the url this page will hang off of (/firstboot) self.register_page('firstboot') diff --git a/modules/help/help.py b/modules/help/help.py index 927f9e6c6..939fed89e 100644 --- a/modules/help/help.py +++ b/modules/help/help.py @@ -8,15 +8,17 @@ import util class Help(PagePlugin): order = 20 # order of running init in PagePlugins - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + + def __init__(self): + super(Help, self).__init__() + self.register_page("help") self.menu = cfg.main_menu.add_item(_("Documentation"), "icon-book", "/help", 101) self.menu.add_item(_("Where to Get Help"), "icon-search", "/help/index", 5) self.menu.add_item(_("Developer's Manual"), "icon-info-sign", "/help/view/plinth", 10) self.menu.add_item(_("FAQ"), "icon-question-sign", "/help/view/faq", 20) self.menu.add_item(_("%s Wiki" % cfg.box_name), "icon-pencil", "http://wiki.debian.org/FreedomBox", 30) - self.menu.add_item(_("About"), "icon-star", "/help/about", 100) + self.menu.add_item(_("About"), "icon-star", "/help/about", 100) @cherrypy.expose def index(self): @@ -31,8 +33,9 @@ class Help(PagePlugin): class View(PagePlugin): - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(View, self).__init__() + self.register_page("help.view") @cherrypy.expose diff --git a/modules/lib/auth_page.py b/modules/lib/auth_page.py index 1d9b49ca3..2b2ab6369 100644 --- a/modules/lib/auth_page.py +++ b/modules/lib/auth_page.py @@ -36,8 +36,8 @@ class LoginForm(forms.Form): # pylint: disable-msg=W0232 class AuthController(PagePlugin): """Login and logout pages""" - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(AuthController, self).__init__() self.register_page('auth') diff --git a/modules/lib/user_store.py b/modules/lib/user_store.py index 41d13f947..e6c416fb3 100644 --- a/modules/lib/user_store.py +++ b/modules/lib/user_store.py @@ -4,8 +4,11 @@ from model import User from plugin_mount import UserStoreModule from withsqlite.withsqlite import sqlite_db + class UserStore(UserStoreModule, sqlite_db): def __init__(self): + super(UserStore, self).__init__() + self.db_file = cfg.user_db sqlite_db.__init__(self, self.db_file, autocommit=True, check_same_thread=False) self.__enter__() diff --git a/modules/owncloud/owncloud.py b/modules/owncloud/owncloud.py index 535ba019b..34c8554e7 100644 --- a/modules/owncloud/owncloud.py +++ b/modules/owncloud/owncloud.py @@ -22,8 +22,9 @@ class OwnCloud(PagePlugin): """ownCloud configuration page""" order = 90 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(OwnCloud, self).__init__() + self.register_page('apps.owncloud') cfg.html_root.apps.menu.add_item('Owncloud', 'icon-picture', diff --git a/modules/packages/packages.py b/modules/packages/packages.py index 89fe69676..d6f589238 100644 --- a/modules/packages/packages.py +++ b/modules/packages/packages.py @@ -48,8 +48,9 @@ class Packages(PagePlugin): """Package page""" order = 20 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(Packages, self).__init__() + self.register_page('sys.packages') cfg.html_root.sys.menu.add_item('Package Manager', 'icon-gift', diff --git a/modules/pagekite/pagekite.py b/modules/pagekite/pagekite.py index cc7791a2d..57037708e 100644 --- a/modules/pagekite/pagekite.py +++ b/modules/pagekite/pagekite.py @@ -35,8 +35,8 @@ class PageKite(PagePlugin): """PageKite menu entry and introduction page""" order = 60 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(PageKite, self).__init__() self.register_page("apps.pagekite") cfg.html_root.apps.menu.add_item( @@ -109,8 +109,8 @@ class Configure(PagePlugin): # pylint: disable-msg=C0103 """Main configuration form""" order = 65 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(Configure, self).__init__() self.register_page("apps.pagekite.configure") diff --git a/modules/santiago/santiago.py b/modules/santiago/santiago.py index b052daab7..e9339fe55 100644 --- a/modules/santiago/santiago.py +++ b/modules/santiago/santiago.py @@ -23,14 +23,15 @@ santiago_port = 52854 # return True class Santiago(PagePlugin): - order = 90 # order of running init in PagePlugins - def __init__(self, *args, **kwargs): + order = 90 # order of running init in PagePlugins + def __init__(self): + super(Santiago, self).__init__() self.register_page("santiago") self.santiago_address = self.get_santiago_address() #TODO: multiple santiago ports #set a listener on the santiago address - def get_santiago_address(self): + def get_santiago_address(self): if 'santiago' in cfg.users['admin'] and 'address' in cfg.users['admin']['santiago']: return cfg.users['admin']['santiago']['address'] else: @@ -54,11 +55,11 @@ class Santiago(PagePlugin): print "Need to add these two lines to /etc/torrc:\n%s" % hidden_service_config return "" - def check_for_hidden_service(self): + def check_for_hidden_service(self): pass - @cherrypy.expose - def index(self, *args, **kw): + @cherrypy.expose + def index(self, *args, **kw): """ A request is a dict with some required keys: @@ -111,12 +112,13 @@ class Santiago(PagePlugin): ## Plinth page to config santiago class santiago(PagePlugin): - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) - self.menu = cfg.html_root.privacy.menu.add_item("Santiago", "icon-leaf", "/privacy/santiago", 10) - self.register_page("privacy.santiago") + def __init__(self): + super(Santiago, self).__init__(self) - @cherrypy.expose - @require() - def index(self): - return "Santiago's config goes here." + self.menu = cfg.html_root.privacy.menu.add_item("Santiago", "icon-leaf", "/privacy/santiago", 10) + self.register_page("privacy.santiago") + + @cherrypy.expose + @require() + def index(self): + return "Santiago's config goes here." diff --git a/modules/system/system.py b/modules/system/system.py index cfdf92515..1896e00c2 100644 --- a/modules/system/system.py +++ b/modules/system/system.py @@ -4,16 +4,16 @@ from plugin_mount import PagePlugin import cfg import util -sys_dir = "modules/installed/sys" - class Sys(PagePlugin): order = 10 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + + def __init__(self): + super(Sys, self).__init__() + self.register_page("sys") self.menu = cfg.main_menu.add_item(_("System"), "icon-cog", "/sys", 100) - self.menu.add_item(_("Users and Groups"), "icon-user", "/sys/users", 15) + self.menu.add_item(_("Users and Groups"), "icon-user", "/sys/users", 15) @cherrypy.expose def index(self): diff --git a/modules/tor/tor.py b/modules/tor/tor.py index 9e64240f8..f564ed6ad 100644 --- a/modules/tor/tor.py +++ b/modules/tor/tor.py @@ -28,10 +28,12 @@ import cfg import util -class tor(PagePlugin): +class Tor(PagePlugin): order = 60 # order of running init in PagePlugins - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + + def __init__(self): + super(Tor, self).__init__() + self.register_page("apps.tor") cfg.html_root.apps.menu.add_item("Tor", "icon-eye-close", "/apps/tor", 30) diff --git a/modules/users/users.py b/modules/users/users.py index 5722271b7..bdcd7f044 100644 --- a/modules/users/users.py +++ b/modules/users/users.py @@ -11,8 +11,10 @@ import util class Users(PagePlugin): order = 20 # order of running init in PagePlugins - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + + def __init__(self): + super(Users, self).__init__() + self.register_page("sys.users") @staticmethod @@ -52,8 +54,8 @@ class UserAdd(PagePlugin): """Add user page""" order = 30 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(UserAdd, self).__init__() self.register_page('sys.users.add') @@ -112,8 +114,8 @@ class UserEdit(PagePlugin): """User edit page""" order = 35 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(UserEdit, self).__init__() self.register_page('sys.users.edit') diff --git a/modules/xmpp/xmpp.py b/modules/xmpp/xmpp.py index ab80118c2..ff9d354fb 100644 --- a/modules/xmpp/xmpp.py +++ b/modules/xmpp/xmpp.py @@ -20,11 +20,12 @@ class XMPP(PagePlugin): """XMPP Page""" order = 60 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(XMPP, self).__init__() + self.register_page('apps.xmpp') cfg.html_root.apps.menu.add_item('XMPP', 'icon-comment', - '/apps/xmpp', 40) + '/apps/xmpp', 40) self.client_service = service.Service( 'xmpp-client', _('Chat Server - client connections'), @@ -66,9 +67,10 @@ class Configure(PagePlugin): """Configuration page""" order = 65 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) - self.register_page("apps.xmpp.configure") + def __init__(self): + super(Configure, self).__init__() + + self.register_page('apps.xmpp.configure') @cherrypy.expose @require() @@ -148,8 +150,9 @@ class Register(PagePlugin): """User registration page""" order = 65 - def __init__(self, *args, **kwargs): - PagePlugin.__init__(self, *args, **kwargs) + def __init__(self): + super(Register, self).__init__() + self.register_page('apps.xmpp.register') @cherrypy.expose diff --git a/plugin_mount.py b/plugin_mount.py index 38ac1e313..0b815338c 100644 --- a/plugin_mount.py +++ b/plugin_mount.py @@ -18,10 +18,14 @@ class PluginMount(type): def get_plugins(cls, *args, **kwargs): return cls.init_plugins(*args, **kwargs) -class MultiplePluginViolation: + +class MultiplePluginViolation(Exception): + """Multiple plugins found for a type where only one is expected""" pass + class PluginMountSingular(PluginMount): + """Plugin mounter that allows only one plugin of this meta type""" def __init__(cls, name, bases, attrs): if not hasattr(cls, 'plugins'): cls.plugins = [] @@ -29,7 +33,7 @@ class PluginMountSingular(PluginMount): if len(cls.plugins) > 0: raise MultiplePluginViolation cls.plugins.append(cls) - + def _setattr_deep(obj, path, value): """If path is 'x.y.z' or ['x', 'y', 'z'] then perform obj.x.y.z = value""" @@ -42,7 +46,7 @@ def _setattr_deep(obj, path, value): setattr(obj, path[-1], value) -class PagePlugin: +class PagePlugin(object): """ Mount point for page plugins. Page plugins provide display pages in the interface (one menu item, for example). @@ -53,18 +57,20 @@ class PagePlugin: order = 50 __metaclass__ = PluginMount - def __init__(self, *args, **kwargs): + + def __init__(self): """If cfg.html_root is none, then this is the html_root.""" if not cfg.html_root: cfg.log('Setting html root to %s' % self.__class__.__name__) cfg.html_root = self - + def register_page(self, url): + """Add a page to the page tree structure""" cfg.log.info("Registering page: %s" % url) _setattr_deep(cfg.html_root, url, self) -class UserStoreModule: +class UserStoreModule(object): """ Mount Point for plugins that will manage the user backend storage, where we keep a hash for each user. @@ -79,5 +85,5 @@ class UserStoreModule: compatibility with third party software. A future version of Plinth is likely to require LDAP. """ - __metaclass__ = PluginMountSingular # singular because we can only use one user store at a time - + # Singular because we can only use one user store at a time + __metaclass__ = PluginMountSingular diff --git a/tests/actions_test.py b/tests/actions_test.py index c78fdce72..09fbae144 100644 --- a/tests/actions_test.py +++ b/tests/actions_test.py @@ -5,7 +5,6 @@ from actions import superuser_run, run import os import shlex import subprocess -import sys import unittest class TestPrivileged(unittest.TestCase): diff --git a/tests/auth_test.py b/tests/auth_test.py index 6ea5d0252..08806748b 100644 --- a/tests/auth_test.py +++ b/tests/auth_test.py @@ -1,18 +1,18 @@ #! /usr/bin/env python # -*- mode: python; mode: auto-fill; fill-column: 80 -*- -import user_store, auth +import auth from logger import Logger import cfg import unittest import cherrypy import plugin_mount import os -from model import User cfg.log = Logger() cherrypy.log.access_file = None + class Auth(unittest.TestCase): """Test check_credentials function of auth to confirm it works as expected""" diff --git a/tests/user_store_test.py b/tests/user_store_test.py index e6a3e8433..def0c0c93 100644 --- a/tests/user_store_test.py +++ b/tests/user_store_test.py @@ -1,7 +1,6 @@ #! /usr/bin/env python # -*- mode: python; mode: auto-fill; fill-column: 80 -*- -import user_store from logger import Logger import cfg import unittest @@ -13,6 +12,7 @@ cfg.log = Logger() cherrypy.log.access_file = None + class UserStore(unittest.TestCase): """Test each function of user_store to confirm they work as expected""" @@ -83,4 +83,4 @@ class UserStore(unittest.TestCase): return user if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main()