from buildbot.process import remotecommand
from buildbot.process import results


class WasherTaskCommand(remotecommand.RemoteCommand):
    def __init__(self, task_name=None, task_args=None):

        if task_name is None:
            task_name = "main"

        if task_args is None:
            task_args = {}

        remotecommand.RemoteCommand.__init__(
            self,
            "washertask",
            {"task_name": task_name,
             "task_args": task_args},
            decodeRC={results.SUCCESS: results.SUCCESS,
                      results.FAILURE: results.FAILURE,
                      results.EXCEPTION: results.EXCEPTION,
                      results.WARNINGS: results.WARNINGS})

    def remoteUpdate(self, update):
        for name, value in update.get("progress", {}).items():
            self.step.setProgress(name, value)

        if "summary" in update:
            self.step.description = update["summary"]
            self.step.updateSummary()

        if "createlog" in update:
            logname = update["createlog"]
            if logname not in self.step.logfiles:
                logfile = self.step.addLogForRemoteCommands(logname)
                self.step.logfiles[logname] = logfile
                self.useLog(logfile, False, logname)

        return super().remoteUpdate(update)
