Skip to content

Commit

Permalink
Fix PEP8 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Saviq committed Jul 2, 2015
1 parent fc12ae5 commit 5b7895d
Show file tree
Hide file tree
Showing 21 changed files with 273 additions and 204 deletions.
39 changes: 14 additions & 25 deletions examples/api_test.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
#!/usr/bin/python

# Copyright (c) 2015 Canonical Ltd
#
# api_test.py: Test/demo of the python3-lxc API
#
# (C) Copyright Canonical Ltd. 2015
#
# Authors:
# Chuck Short <zulcss@ubuntu.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# http://www.apache.org/licenses/LICENSE-2.0
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
# USA
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import uuid
import sys
import subprocess
import time

# Let's pick a random name, avoiding clashes
CONTAINER_NAME = str(uuid.uuid1())
Expand All @@ -36,15 +25,15 @@
try:
lxd.container_defined(CONTAINER_NAME)
except Exception as e:
print "Container doesnt exist: %s" % e
print("Container doesnt exist: %s" % e)

config = {'name': CONTAINER_NAME,
'source': {'type': 'none'}}
lxd.container_init(config)
if lxd.container_defined(CONTAINER_NAME):
print "Container is running"
print("Container is running")
else:
print "Whoops!"
print("Whoops!")
containers = lxd.container_list()
for x in containers:
lxd.container_destroy(x)
4 changes: 1 addition & 3 deletions pylxd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@

import pbr.version


__version__ = pbr.version.VersionInfo(
'pylxd').version_string()
__version__ = pbr.version.VersionInfo('pylxd').version_string()
32 changes: 16 additions & 16 deletions pylxd/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
# License for the specific language governing permissions and limitations
# under the License.


from . import connection
from . import container
from . import certificate
from . import hosts
from . import image
from . import network
from . import operation
from . import profiles
from pylxd import certificate
from pylxd import connection
from pylxd import container
from pylxd import hosts
from pylxd import image
from pylxd import network
from pylxd import operation
from pylxd import profiles


class API(object):

def __init__(self, host=None, port=8443):
conn = self.connection = connection.LXDConnection(host=host, port=port)
self.hosts = hosts.LXDHost(conn)
Expand Down Expand Up @@ -210,31 +210,31 @@ def certificate_create(self, fingerprint):

# profiles
def profile_create(self, profile):
''' Create LXD profile '''
'''Create LXD profile'''
return self.profiles.profile_create(profile)

def profile_show(self, profile):
''' Show LXD profile '''
'''Show LXD profile'''
return self.profiles.profile_show(profile)

def profile_defined(self, profile):
''' Check to see if profile is defined. '''
'''Check to see if profile is defined'''
return self.profiles.profile_defined(profile)

def profile_list(self):
''' List LXD profiles '''
'''List LXD profiles'''
return self.profiles.profile_list()

def profile_update(self, profile, config):
''' Update LXD profile '''
'''Update LXD profile'''
return self.profiles.profile_update(profile, config)

def profile_rename(self, profile, config):
''' Rename LXD profile '''
'''Rename LXD profile'''
raise NotImplemented()

def profile_delete(self, profile):
''' Delete LXD profile '''
'''Delete LXD profile'''
return self.profiles.profile_delete(profile)

# lxd operations
Expand Down
3 changes: 2 additions & 1 deletion pylxd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
# under the License.
from __future__ import print_function

from . import connection
from pylxd import connection


class LXDBase(object):

def __init__(self, conn=None):
self.connection = conn or connection.LXDConnection()
8 changes: 5 additions & 3 deletions pylxd/certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

import json

from . import base
from . import connection
from pylxd import base


class LXDCertificate(base.LXDBase):

def certificate_list(self):
(state, data) = self.connection.get_object('GET', '/1.0/certificates')
return [certificate.split('/1.0/certitifcates/')[-1] for certificate in data['metadata']]
return [certificate.split('/1.0/certitifcates/')[-1]
for certificate in data['metadata']]

def certificate_show(self, fingerprint):
return self.connection.get_object('GET', '/1.0/certificates/%s'
Expand Down
7 changes: 5 additions & 2 deletions pylxd/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import socket
import ssl

from . import utils
from pylxd import utils


class UnixHTTPConnection(httplib.HTTPConnection):

def __init__(self, path, host='localhost', port=None, strict=None,
timeout=None):
httplib.HTTPConnection.__init__(self, host, port=port,
Expand Down Expand Up @@ -60,14 +61,16 @@ def _get_ssl_certs():


class LXDConnection(object):

def __init__(self, host=None, port=8443):
if host:
self.host = host
self.port = port
self.unix_socket = None
else:
if 'LXD_DIR' in os.environ:
self.unix_socket = os.path.join(os.environ['LXD_DIR'], 'unix.socket')
self.unix_socket = os.path.join(os.environ['LXD_DIR'],
'unix.socket')
else:
self.unix_socket = '/var/lib/lxd/unix.socket'
self.host, self.port = None, None
Expand Down
55 changes: 24 additions & 31 deletions pylxd/container.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Copyright (c) 2015 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand All @@ -14,34 +13,29 @@
# under the License.

import json
import os

from . import base
from . import connection
from pylxd import base


class LXDContainer(base.LXDBase):
# containers:

def container_list(self):
(state, data) = self.connection.get_object('GET', '/1.0/containers')
return [container.split('/1.0/containers/')[-1] for container in data['metadata']]

def container_defined(self, container):
(state, data) = self.connection.get_object('GET', '/1.0/containers/%s/state'
% container)
return data.get('status')
return [container.split('/1.0/containers/')[-1]
for container in data['metadata']]

def container_running(self, container):
(state, data) = self.connection.get_object('GET',
'/1.0/containers/%s/state'
% container)
(state, data) = self.connection.get_object(
'GET',
'/1.0/containers/%s/state' % container)
data = data.get('metadata')
container_running = False
if data['status'] in ['RUNNING', 'STARTING', 'FREEZING,FROZEN',
'THAWED']:
container_running = True
'THAWED']:
container_running = True
return container_running


def container_init(self, container):
return self.connection.get_object('POST', '/1.0/containers',
json.dumps(container))
Expand All @@ -54,13 +48,11 @@ def container_defined(self, container):
return self.connection.get_status('GET', '/1.0/containers/%s/state'
% container)


def container_state(self, container):
(state, data) = self.connection.get_object('GET', '/1.0/containers/%s/state'
% container)
(state, data) = self.connection.get_object(
'GET', '/1.0/containers/%s/state' % container)
return data['metadata']['status']


def container_start(self, container, timeout):
action = {'action': 'start', 'timeout': timeout}
return self.connection.get_object('PUT', '/1.0/containers/%s/state'
Expand Down Expand Up @@ -96,14 +88,15 @@ def container_destroy(self, container):
% container)

def get_container_log(self, container):
(state, data) = self.connection.get_object('GET', '/1.0/containers/%s?log=true'
% container)
(state, data) = self.connection.get_object(
'GET', '/1.0/containers/%s?log=true' % container)
return data['metadata']['log']

# file operations
def get_container_file(self, container, filename):
return self.connection.get_raw('GET', '/1.0/containers/%s/files?path=%s'
% (container, filename))
return self.connection.get_raw(
'GET',
'/1.0/containers/%s/files?path=%s' % (container, filename))

def container_publish(self, container):
return self.connection.get_object('POST', '/1.0/images',
Expand All @@ -121,18 +114,18 @@ def run_command(self, container, args, interactive, web_sockets, env):

# snapshots
def snapshot_list(self, container):
(state, data) = self.connection.get_object('GET',
'/1.0/containers/%s/snapshots'
% container)
(state, data) = self.connection.get_object(
'GET',
'/1.0/containers/%s/snapshots' % container)
return [snapshot.split('/1.0/containers/%s/snapshots/%s/'
% (container, container))[-1] \
% (container, container))[-1]
for snapshot in data['metadata']]

def snapshot_create(self, container, config):
return self.connection.get_object('POST',
'/1.0/containers/%s/snapshots'
% container,
json.dumps(config))
'/1.0/containers/%s/snapshots'
% container,
json.dumps(config))

def snapshot_info(self, container, snapshot):
return self.conncetion.get_object('GET',
Expand Down
21 changes: 20 additions & 1 deletion pylxd/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
import exceptions
# Copyright (c) 2015 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.


class ContainerUnDefined(Exception):
pass


class UntrustedHost(Exception):
pass


class ContainerProfileCreateFail(Exception):
pass


class ContainerProfileDeleteFail(Exception):
pass


class ImageInvalidSize(Exception):
pass


class APIError(Exception):

def __init__(self, error, status_code):
msg = 'Error %s - %s.' % (status_code, error)
Exception.__init__(self, msg)
Expand Down
24 changes: 16 additions & 8 deletions pylxd/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
# under the License.
from __future__ import print_function

from . import base
from pylxd import base


class LXDHost(base.LXDBase):

def host_ping(self):
try:
return self.connection.get_status('GET', '/1.0')
Expand All @@ -28,13 +29,20 @@ def host_info(self):
(state, data) = self.connection.get_object('GET', '/1.0')

return {
'lxd_api_compat_level': self.get_lxd_api_compat(data.get('metadata')),
'lxd_trusted_host': self.get_lxd_host_trust(data.get('metadata')),
'lxd_backing_fs': self.get_lxd_backing_fs(data.get('metadata')),
'lxd_driver': self.get_lxd_driver(data.get('metadata')),
'lxd_version': self.get_lxd_version(data.get('metadata')),
'lxc_version': self.get_lxc_version(data.get('metadata')),
'kernel_version': self.get_kernel_version(data.get('metadata'))
'lxd_api_compat_level':
self.get_lxd_api_compat(data.get('metadata')),
'lxd_trusted_host':
self.get_lxd_host_trust(data.get('metadata')),
'lxd_backing_fs':
self.get_lxd_backing_fs(data.get('metadata')),
'lxd_driver':
self.get_lxd_driver(data.get('metadata')),
'lxd_version':
self.get_lxd_version(data.get('metadata')),
'lxc_version':
self.get_lxc_version(data.get('metadata')),
'kernel_version':
self.get_kernel_version(data.get('metadata'))
}

def get_lxd_api_compat(self, data):
Expand Down

0 comments on commit 5b7895d

Please sign in to comment.