def handle_file(self, file, provider):
    data = json.loads(open(file).read())
    print(('Using provider: ' + self.provider))
    try:
        acme_version = (2 if ('acme-v02' in data[self.provider]['Account']['Registration']['uri']) else 1)
    except TypeError:
        if ('DomainsCertificate' in data):
            acme_version = 1
        else:
            acme_version = 2
    if (acme_version == 1):
        certs = data['DomainsCertificate']['Certs']
    elif (acme_version == 2):
        certs = data[self.provider]['Certificates']
    print((('Certificate storage contains ' + str(len(certs))) + ' certificates'))
    for c in certs:
        if (acme_version == 1):
            name = c['Certificate']['Domain']
            privatekey = c['Certificate']['PrivateKey']
            fullchain = c['Certificate']['Certificate']
            sans = c['Domains']['SANs']
        elif (acme_version == 2):
            name = c['domain']['main']
            privatekey = c['key']
            fullchain = c['certificate']
            sans = (c['domain']['sans'] if ('sans' in c['domain'].keys()) else None)
        privatekey = b64decode(privatekey).decode('utf-8')
        fullchain = b64decode(fullchain).decode('utf-8')
        start = fullchain.find('-----BEGIN CERTIFICATE-----', 1)
        cert = fullchain[0:start]
        chain = fullchain[start:]
        directory = (('certs/' + name) + '/')
        try:
            os.makedirs(directory)
        except OSError as error:
            if (error.errno != errno.EEXIST):
                raise
        with open((directory + 'privkey.pem'), 'w') as f:
            f.write(privatekey)
        with open((directory + 'cert.pem'), 'w') as f:
            f.write(cert)
        with open((directory + 'chain.pem'), 'w') as f:
            f.write(chain)
        with open((directory + 'fullchain.pem'), 'w') as f:
            f.write(fullchain)
        directory = 'certs_flat/'
        with open(((directory + name) + '.key'), 'w') as f:
            f.write(privatekey)
        with open(((directory + name) + '.crt'), 'w') as f:
            f.write(fullchain)
        with open(((directory + name) + '.chain.pem'), 'w') as f:
            f.write(chain)
        if sans:
            for name in sans:
                with open(((directory + name) + '.key'), 'w') as f:
                    f.write(privatekey)
                with open(((directory + name) + '.crt'), 'w') as f:
                    f.write(fullchain)
                with open(((directory + name) + '.chain.pem'), 'w') as f:
                    f.write(chain)
        print((('Extracted certificate for: ' + name) + ((', ' + ', '.join(sans)) if sans else '')))
def handle_file(self, file, provider):
    data = json.loads(open(file).read())
    print(('Using provider: ' + self.provider))
    try:
        acme_version = (2 if ('acme-v02' in data[self.provider]['Account']['Registration']['uri']) else 1)
    except TypeError:
        if ('DomainsCertificate' in data):
            acme_version = 1
        else:
            acme_version = 2
    if (acme_version == 1):
        certs = data['DomainsCertificate']['Certs']
    elif (acme_version == 2):
        certs = data[self.provider]['Certificates']
    print((('Certificate storage contains ' + str(len(certs))) + ' certificates'))
    for c in certs:
        if (acme_version == 1):
            name = c['Certificate']['Domain']
            privatekey = c['Certificate']['PrivateKey']
            fullchain = c['Certificate']['Certificate']
            sans = c['Domains']['SANs']
        elif (acme_version == 2):
            name = c['domain']['main']
            privatekey = c['key']
            fullchain = c['certificate']
            sans = (c['domain']['sans'] if ('sans' in c['domain'].keys()) else None)
        privatekey = b64decode(privatekey).decode('utf-8')
        fullchain = b64decode(fullchain).decode('utf-8')
        start = fullchain.find('-----BEGIN CERTIFICATE-----', 1)
        cert = fullchain[0:start]
        chain = fullchain[start:]
        directory = (('certs/' + name) + '/')
        try:
            os.makedirs(directory)
        except OSError as error:
            if (error.errno != errno.EEXIST):
                raise
        with open((directory + 'privkey.pem'), 'w') as f:
            f.write(privatekey)
        with open((directory + 'cert.pem'), 'w') as f:
            f.write(cert)
        with open((directory + 'chain.pem'), 'w') as f:
            f.write(chain)
        with open((directory + 'fullchain.pem'), 'w') as f:
            f.write(fullchain)
        directory = 'certs_flat/'
        with open(((directory + name) + '.key'), 'w') as f:
            f.write(privatekey)
        with open(((directory + name) + '.crt'), 'w') as f:
            f.write(fullchain)
        with open(((directory + name) + '.chain.pem'), 'w') as f:
            f.write(chain)
        if sans:
            for name in sans:
                with open(((directory + name) + '.key'), 'w') as f:
                    f.write(privatekey)
                with open(((directory + name) + '.crt'), 'w') as f:
                    f.write(fullchain)
                with open(((directory + name) + '.chain.pem'), 'w') as f:
                    f.write(chain)
        print((('Extracted certificate for: ' + name) + ((', ' + ', '.join(sans)) if sans else )))