diff --git a/digitalocean/Account.py b/digitalocean/Account.py index bbb4278b..4faf657a 100644 --- a/digitalocean/Account.py +++ b/digitalocean/Account.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from .baseapi import BaseAPI + class Account(BaseAPI): def __init__(self, *args, **kwargs): self.droplet_limit = None @@ -25,7 +26,7 @@ def load(self): account = data['account'] for attr in account.keys(): - setattr(self,attr,account[attr]) + setattr(self, attr, account[attr]) def __str__(self): - return "%s" % self.email \ No newline at end of file + return "%s" % self.email diff --git a/digitalocean/Action.py b/digitalocean/Action.py index 81245b37..02141ddb 100644 --- a/digitalocean/Action.py +++ b/digitalocean/Action.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from .baseapi import BaseAPI + class Action(BaseAPI): def __init__(self, *args, **kwargs): self.id = None @@ -32,7 +33,7 @@ def load_directly(self): action = action[u'action'] # Loading attributes for attr in action.keys(): - setattr(self,attr,action[attr]) + setattr(self, attr, action[attr]) def load(self): action = self.get_data( @@ -45,7 +46,7 @@ def load(self): action = action[u'action'] # Loading attributes for attr in action.keys(): - setattr(self,attr,action[attr]) + setattr(self, attr, action[attr]) def __str__(self): - return "%s %s [%s]" % (self.id, self.type, self.status) \ No newline at end of file + return "%s %s [%s]" % (self.id, self.type, self.status) diff --git a/digitalocean/Domain.py b/digitalocean/Domain.py index 1b1c15f5..5c34847d 100644 --- a/digitalocean/Domain.py +++ b/digitalocean/Domain.py @@ -3,6 +3,7 @@ from .Record import Record from .baseapi import BaseAPI + class Domain(BaseAPI): def __init__(self, *args, **kwargs): self.name = None @@ -27,7 +28,7 @@ def load(self): domain = domains['domain'] for attr in domain.keys(): - setattr(self,attr,domain[attr]) + setattr(self, attr, domain[attr]) def destroy(self): """ @@ -62,7 +63,7 @@ def create_new_domain_record(self, *args, **kwargs): "data": kwargs.get("data", None) } - # Optional Args + #  Optional Args if kwargs.get("priority", None): data['priority'] = kwargs.get("priority", None) @@ -84,9 +85,9 @@ def create(self): """ # URL https://api.digitalocean.com/v2/domains data = { - "name": self.name, - "ip_address": self.ip_address, - } + "name": self.name, + "ip_address": self.ip_address, + } domain = self.get_data( "domains", @@ -115,4 +116,4 @@ def get_records(self): return records def __str__(self): - return "%s" % self.name \ No newline at end of file + return "%s" % self.name diff --git a/digitalocean/Droplet.py b/digitalocean/Droplet.py index ae0ab320..bc186a5a 100644 --- a/digitalocean/Droplet.py +++ b/digitalocean/Droplet.py @@ -1,21 +1,24 @@ # -*- coding: utf-8 -*- -import requests from .Action import Action from .Image import Image from .Kernel import Kernel from .baseapi import BaseAPI, Error from .SSHKey import SSHKey + class DropletError(Error): """Base exception class for this module""" pass + class BadKernelObject(DropletError): pass + class BadSSHKeyFormat(DropletError): pass + class Droplet(BaseAPI): """"Droplet managment @@ -124,7 +127,7 @@ def load(self): droplet = droplets['droplet'] for attr in droplet.keys(): - setattr(self,attr,droplet[attr]) + setattr(self, attr, droplet[attr]) for net in self.networks['v4']: if net['type'] == 'private': @@ -228,7 +231,7 @@ def restore(self, image_id): return self.get_data( "droplets/%s/actions/" % self.id, type="POST", - params={"type":"restore", "image": image_id} + params={"type": "restore", "image": image_id} ) def rebuild(self, image_id=None): @@ -315,7 +318,7 @@ def change_kernel(self, kernel): return self.get_data( "droplets/%s/actions/" % self.id, type="POST", - params={'type' : 'change_kernel', 'kernel': kernel.id} + params={'type': 'change_kernel', 'kernel': kernel.id} ) def __get_ssh_keys_id(self): @@ -327,7 +330,7 @@ def __get_ssh_keys_id(self): ssh_keys_id = list() for ssh_key in self.ssh_keys: if type(ssh_key) in [int, long]: - ssh_keys_id.append( int(ssh_key) ) + ssh_keys_id.append(int(ssh_key)) elif type(ssh_key) == SSHKey: ssh_keys_id.append(ssh_key.id) @@ -337,7 +340,7 @@ def __get_ssh_keys_id(self): key.token = self.token results = key.load_by_pub_key(ssh_key) - if results == None: + if results is None: key.public_key = ssh_key key.name = "SSH Key %s" % self.name key.create() @@ -346,7 +349,9 @@ def __get_ssh_keys_id(self): ssh_keys_id.append(key.id) else: - raise BadSSHKeyFormat("Droplet.ssh_keys should be a list of IDs or public keys") + raise BadSSHKeyFormat( + "Droplet.ssh_keys should be a list of IDs or public keys" + ) return ssh_keys_id @@ -358,19 +363,19 @@ def create(self, *args, **kwargs): assigned to the object. """ for attr in kwargs.keys(): - setattr(self,attr,kwargs[attr]) + setattr(self, attr, kwargs[attr]) # Provide backwards compatibility if not self.size_slug and self.size: self.size_slug = self.size data = { - "name": self.name, - "size": self.size_slug, - "image": self.image, - "region": self.region, - "ssh_keys[]": self.__get_ssh_keys_id(), - } + "name": self.name, + "size": self.size_slug, + "image": self.image, + "region": self.region, + "ssh_keys[]": self.__get_ssh_keys_id(), + } if self.backups: data['backups'] = True @@ -463,7 +468,7 @@ def get_kernel_available(self): if not url: break data = self.get_data(url) - except KeyError: # No links. + except KeyError: # No links. break return kernels diff --git a/digitalocean/Image.py b/digitalocean/Image.py index 024f9c31..d06a63de 100644 --- a/digitalocean/Image.py +++ b/digitalocean/Image.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- -import requests from .baseapi import BaseAPI + class Image(BaseAPI): def __init__(self, *args, **kwargs): self.id = None @@ -28,9 +28,9 @@ def load(self): data = self.get_data("images/%s" % self.id) image_dict = data['image'] - #Setting the attribute values + # Setting the attribute values for attr in image_dict.keys(): - setattr(self,attr,image_dict[attr]) + setattr(self, attr, image_dict[attr]) return self @@ -64,4 +64,4 @@ def rename(self, new_name): ) def __str__(self): - return "%s %s %s" % (self.id, self.name, self.distribution) \ No newline at end of file + return "%s %s %s" % (self.id, self.name, self.distribution) diff --git a/digitalocean/Kernel.py b/digitalocean/Kernel.py index d109e923..1453cc31 100644 --- a/digitalocean/Kernel.py +++ b/digitalocean/Kernel.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from .baseapi import BaseAPI + class Kernel(BaseAPI): def __init__(self, *args, **kwargs): self.name = "" @@ -9,4 +10,4 @@ def __init__(self, *args, **kwargs): super(Kernel, self).__init__(*args, **kwargs) def __str__(self): - return "%s %s" % (self.name, self.version) \ No newline at end of file + return "%s %s" % (self.name, self.version) diff --git a/digitalocean/Manager.py b/digitalocean/Manager.py index 67b1a69c..26c4de31 100644 --- a/digitalocean/Manager.py +++ b/digitalocean/Manager.py @@ -45,7 +45,7 @@ def __deal_with_pagination(self, url, data, params): values.append(value) data = {} data[key] = values - except KeyError: # No pages. + except KeyError: # No pages. pass return data diff --git a/digitalocean/Metadata.py b/digitalocean/Metadata.py index fa0bf580..c550998b 100644 --- a/digitalocean/Metadata.py +++ b/digitalocean/Metadata.py @@ -7,6 +7,7 @@ from .baseapi import BaseAPI + class Metadata(BaseAPI): """ Metadata API: Provide useful information about the current Droplet. @@ -37,9 +38,9 @@ def load(self): metadata = self.get_data("v1.json") for attr in metadata.keys(): - setattr(self,attr,metadata[attr]) + setattr(self, attr, metadata[attr]) return self def __str__(self): - return "%s" % self.droplet_id \ No newline at end of file + return "%s" % self.droplet_id diff --git a/digitalocean/Record.py b/digitalocean/Record.py index 9f2e5e58..837eb8bd 100644 --- a/digitalocean/Record.py +++ b/digitalocean/Record.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- -import requests from .baseapi import BaseAPI + class Record(BaseAPI): def __init__(self, domain_name=None, *args, **kwargs): self.domain = domain_name if domain_name else "" @@ -29,13 +29,13 @@ def create(self): Create a record for a domain """ input_params = { - "type": self.type, - "data": self.data, - "name": self.name, - "priority": self.priority, - "port": self.port, - "weight": self.weight - } + "type": self.type, + "data": self.data, + "name": self.name, + "priority": self.priority, + "port": self.port, + "weight": self.weight + } data = self.get_data( "domains/%s/records" % (self.domain), @@ -79,9 +79,9 @@ def load(self): if record: record = record[u'domain_record'] - #Setting the attribute values + # Setting the attribute values for attr in record.keys(): - setattr(self,attr,record[attr]) + setattr(self, attr, record[attr]) def __str__(self): return "%s %s" % (self.id, self.domain) diff --git a/digitalocean/Region.py b/digitalocean/Region.py index e64d8494..03cb60b3 100644 --- a/digitalocean/Region.py +++ b/digitalocean/Region.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from .baseapi import BaseAPI + class Region(BaseAPI): def __init__(self, *args, **kwargs): self.name = None @@ -11,4 +12,4 @@ def __init__(self, *args, **kwargs): super(Region, self).__init__(*args, **kwargs) def __str__(self): - return "%s" % self.name \ No newline at end of file + return "%s" % self.name diff --git a/digitalocean/SSHKey.py b/digitalocean/SSHKey.py index d3255b46..bdcd4636 100644 --- a/digitalocean/SSHKey.py +++ b/digitalocean/SSHKey.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- from .baseapi import BaseAPI -import requests + class SSHKey(BaseAPI): def __init__(self, *args, **kwargs): @@ -28,9 +28,9 @@ def load(self): ssh_key = data['ssh_key'] - #Setting the attribute values + # Setting the attribute values for attr in ssh_key.keys(): - setattr(self,attr,ssh_key[attr]) + setattr(self, attr, ssh_key[attr]) self.id = ssh_key['id'] def load_by_pub_key(self, public_key): @@ -53,9 +53,9 @@ def create(self): Create the SSH Key """ input_params = { - "name": self.name, - "public_key": self.public_key, - } + "name": self.name, + "public_key": self.public_key, + } data = self.get_data( "account/keys/", @@ -71,9 +71,9 @@ def edit(self): Edit the SSH Key """ input_params = { - "name": self.name, - "public_key": self.public_key, - } + "name": self.name, + "public_key": self.public_key, + } data = self.get_data( "account/keys/%s" % self.id, diff --git a/digitalocean/Size.py b/digitalocean/Size.py index eedae1b1..c8892aaa 100644 --- a/digitalocean/Size.py +++ b/digitalocean/Size.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from .baseapi import BaseAPI + class Size(BaseAPI): def __init__(self, *args, **kwargs): self.slug = None @@ -15,4 +16,4 @@ def __init__(self, *args, **kwargs): super(Size, self).__init__(*args, **kwargs) def __str__(self): - return "%s" % (self.slug) \ No newline at end of file + return "%s" % (self.slug) diff --git a/digitalocean/__init__.py b/digitalocean/__init__.py index d04d69c7..1072f63f 100644 --- a/digitalocean/__init__.py +++ b/digitalocean/__init__.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """digitalocean API to manage droplets""" -__version__ = "1.0.8" +__version__ = "1.1" __author__ = "Lorenzo Setale ( http://who.is.lorenzo.setale.me/? )" __author_email__ = "koalalorenzo@gmail.com" __license__ = "See: http://creativecommons.org/licenses/by-nd/3.0/ " diff --git a/digitalocean/baseapi.py b/digitalocean/baseapi.py index 0f2e16b4..dc0213e3 100644 --- a/digitalocean/baseapi.py +++ b/digitalocean/baseapi.py @@ -5,16 +5,20 @@ except: from urllib.parse import urljoin + class Error(Exception): """Base exception class for this module""" pass + class TokenError(Error): pass + class DataReadError(Error): pass + class BaseAPI(object): """ Basic api class for @@ -27,7 +31,7 @@ def __init__(self, *args, **kwargs): self.end_point = "https://api.digitalocean.com/v2/" for attr in kwargs.keys(): - setattr(self,attr,kwargs[attr]) + setattr(self, attr, kwargs[attr]) def __perform_get(self, url, headers=dict(), params=dict()): return requests.get(url, headers=headers, params=params) @@ -57,7 +61,7 @@ def __perform_request(self, url, type='GET', params=dict(), headers=dict()): if "https" not in url: url = urljoin(self.end_point, url) - headers.update({'Authorization':'Bearer ' + self.token}) + headers.update({'Authorization': 'Bearer ' + self.token}) if type == 'POST': r = self.__perform_post(url, headers=headers, params=params) elif type == 'PUT': diff --git a/setup.py b/setup.py index 6f16fbe4..3592a642 100644 --- a/setup.py +++ b/setup.py @@ -13,14 +13,15 @@ with open('README.md') as file: long_description = file.read() -setup(name='python-digitalocean', - version='1.0.8', - description='digitalocean.com API to manage Droplets and Images', - author='Lorenzo Setale ( http://who.is.lorenzo.setale.me/? )', - author_email='koalalorenzo@gmail.com', - url='https://github.com/koalalorenzo/python-digitalocean', - packages=['digitalocean'], - install_requires=['requests'], - test_suite='digitalocean.tests', - long_description=long_description - ) +setup( + name='python-digitalocean', + version='1.1', + description='digitalocean.com API to manage Droplets and Images', + author='Lorenzo Setale ( http://who.is.lorenzo.setale.me/? )', + author_email='koalalorenzo@gmail.com', + url='https://github.com/koalalorenzo/python-digitalocean', + packages=['digitalocean'], + install_requires=['requests'], + test_suite='digitalocean.tests', + long_description=long_description +)