Override static member in DaemonRunner

143 views Asked by At

I'm trying to override the DaemonRunner in the python standard daemon process library (found here https://pypi.python.org/pypi/python-daemon/)

The DaemonRunner responds to command line arguments for start, stop, and restart, but I want to add a fourth option for status.

The class I want to override looks something like this:

class DaemonRunner(object):
    def _start(self):
        ...etc

    action_funcs = {'start': _start}

I've tried to override it like this:

class StatusDaemonRunner(DaemonRunner):
    def _status(self):
        ...

    DaemonRunner.action_funcs['status'] = _status

This works to some extent, but the problem is that every instance of DaemonRunner now have the new behaviour. Is it possible to override it without modifying every instance of DaemonRunner?

1

There are 1 answers

2
simonemainardi On BEST ANSWER

I would override action_functs to make it a non-static member of class StatusDaemonRunner(DaemonRunner).

In terms of code I would do:

class StatusDaemonRunner(runner.DaemonRunner):
    def __init__(self, app):
        self.action_funcs = runner.DaemonRunner.action_funcs.copy()
        self.action_funcs['status'] = StatusDaemonRunner._status
        super(StatusDaemonRunner, self).__init__(app)
    def _status(self):
        pass  # do your stuff

Indeed, if we look at the getter in the implementation of DaemonRunner (here) we can see that it acess the attribute using self

    def _get_action_func(self):
    """ Return the function for the specified action.

        Raises ``DaemonRunnerInvalidActionError`` if the action is
        unknown.

        """
    try:
        func = self.action_funcs[self.action]
    except KeyError:
        raise DaemonRunnerInvalidActionError(
            u"Unknown action: %(action)r" % vars(self))
    return func

Hence the previous code should do the trick.