import json
import api

class WWWDomain(api.API):

    def __init__(self, auth_handler):
        self.url = auth_handler.url
        self.sessid = auth_handler.sessid
        self.func = 'wwwdomain.edit'
        self.out = 'json'
        self._clear_params()

    def _clear_params(self):
        try:
            self.params.clear()
        except (NameError,AttributeError):
            pass
        self.params = {
            'auth' : self.sessid,
            'out'  : 'json',
            'func' : self.func,
        }

    def list(self, domain=None):
        """List all www domains. If domains is used, list details about this one."""
        self._clear_params()
        if domain:
            self.params['elid'] = domain
        else:
            self.params['func'] = 'wwwdomain'
        data = self.process_api(self.url, self.params)
        out = json.load(data)
        try:
            return out['elem']
        except KeyError:
            return out

    def add(self, domain,  owner, admin, ip, **kwargs):
        """Add a new wwwdomain to configuration. If a DNS server is configurated, API adds
        domain there too."""
        self._clear_params()
        self.params['sok']    = 'yes'
        self.params['domain'] = domain
        self.params['owner']  = owner
        self.params['admin']  = admin
        self.params['ip']     = ip
        for key in kwargs:
            self.params[key] = kwargs[key]

        data = self.process_api(self.url, self.params)
        out = json.load(data)
        return out


@@ -1511,28 +1511,1029 @@
 d(data)%0A        return out%0A%0A
+    def delete(self, domain, **kwatgs):%0A        %22%22%22Delete one or list of domains.%22%22%22%0A        self._clear_params()%0A        self.params%5B'func'%5D = 'wwwdomain.delete'%0A%0A        if type(domain) is list:%0A            self.params%5B'elid'%5D = ', '.join(domain)%0A        else:%0A            self.params%5B'elid'%5D = domain%0A%0A        data = self.process_api(self.url, self.params)%0A        out = json.load(data)%0A        return out%0A%0A    def delete_confirm(self, domain, **kwargs):%0A        raise NotImplementedError%0A%0A    def plain(self, domain, **kwargs):%0A        %22%22%22View apache/nginx configs%22%22%22%0A        self._clear_params()%0A        self.params%5B'func'%5D = 'wwwdomain.plain'%0A        self.params%5B'elid'%5D = domain%0A%0A        data = self.process_api(self.url, self.params)%0A        out = json.load(data)%0A        return out%0A%0A    def enable(self, domain, **kwargs):%0A        %22%22%22Enable domain.%22%22%22%0A        raise NotImplementedError%0A%0A    def disable(self, domain, **kwargs):%0A        %22%22%22Disable domain.%22%22%22%0A        raise NotImplementedError%0A

add delete and plain function