From b013e96affa668698eeeb09c33eca3302acb69de Mon Sep 17 00:00:00 2001 From: Marx314 Date: Fri, 15 Apr 2016 07:49:15 -0400 Subject: [PATCH] flake8 now automated (#5) * flake8 now automated * 120 chars max, auto-reformat intellij --- almanach/core/model.py | 42 ++++++++++----------- almanach/log_bootstrap.py | 2 - setup.py | 2 - test-requirements.txt | 3 +- tests/adapters/test_database_adapter.py | 17 +++++---- tests/core/test_controller.py | 9 ++--- tests/messages.py | 50 +++++++++++++++++-------- tox.ini | 9 ++++- 8 files changed, 78 insertions(+), 56 deletions(-) diff --git a/almanach/core/model.py b/almanach/core/model.py index 80b097b..f199393 100644 --- a/almanach/core/model.py +++ b/almanach/core/model.py @@ -14,7 +14,6 @@ class Entity(object): - def __init__(self, entity_id, project_id, start, end, last_event, name, entity_type): self.entity_id = entity_id self.project_id = project_id @@ -28,13 +27,13 @@ class Entity(object): return todict(self) def __eq__(self, other): - return (other.entity_id == self.entity_id - and other.project_id == self.project_id - and other.start == self.start - and other.end == self.end - and other.last_event == self.last_event - and other.name == self.name - and other.entity_type == self.entity_type) + return (other.entity_id == self.entity_id and + other.project_id == self.project_id and + other.start == self.start and + other.end == self.end and + other.last_event == self.last_event and + other.name == self.name and + other.entity_type == self.entity_type) class Instance(Entity): @@ -47,43 +46,42 @@ class Instance(Entity): self.os = OS(**os) def __eq__(self, other): - return (super(Instance, self).__eq__(other) - and other.flavor == self.flavor - and other.os == self.os - and other.metadata == self.metadata) + return (super(Instance, self).__eq__(other) and + other.flavor == self.flavor and + other.os == self.os and + other.metadata == self.metadata) class OS(object): - def __init__(self, os_type, distro, version): self.os_type = os_type self.distro = distro self.version = version def __eq__(self, other): - return (other.os_type == self.os_type - and other.distro == self.distro - and other.version == self.version) + return (other.os_type == self.os_type and + other.distro == self.distro and + other.version == self.version) class Volume(Entity): TYPE = "volume" - def __init__(self, entity_id, project_id, start, end, volume_type, size, last_event, name, attached_to=None, entity_type=TYPE): + def __init__(self, entity_id, project_id, start, end, volume_type, size, last_event, name, attached_to=None, + entity_type=TYPE): super(Volume, self).__init__(entity_id, project_id, start, end, last_event, name, entity_type) self.volume_type = volume_type self.size = size self.attached_to = attached_to or [] def __eq__(self, other): - return (super(Volume, self).__eq__(other) - and other.volume_type == self.volume_type - and other.size == self.size - and other.attached_to == self.attached_to) + return (super(Volume, self).__eq__(other) and + other.volume_type == self.volume_type and + other.size == self.size and + other.attached_to == self.attached_to) class VolumeType(object): - def __init__(self, volume_type_id, volume_type_name): self.volume_type_id = volume_type_id self.volume_type_name = volume_type_name diff --git a/almanach/log_bootstrap.py b/almanach/log_bootstrap.py index b9db399..bf0f060 100644 --- a/almanach/log_bootstrap.py +++ b/almanach/log_bootstrap.py @@ -15,8 +15,6 @@ import logging import pkg_resources -from logging import config - def get_config_file(): logging_conf = pkg_resources.resource_filename("almanach", "resources/config/logging.cfg") diff --git a/setup.py b/setup.py index e8e7e2d..06f694d 100644 --- a/setup.py +++ b/setup.py @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -#!/usr/bin/env python - from setuptools import setup setup( diff --git a/test-requirements.txt b/test-requirements.txt index d9b9530..9376d40 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -6,4 +6,5 @@ nose-cov==1.6 nose-blockage==0.1.2 flexmock==0.9.4 mongomock==2.0.0 -PyHamcrest==1.8.1 \ No newline at end of file +PyHamcrest==1.8.1 +flake8==2.5.4 \ No newline at end of file diff --git a/tests/adapters/test_database_adapter.py b/tests/adapters/test_database_adapter.py index 4399d35..76dd53a 100644 --- a/tests/adapters/test_database_adapter.py +++ b/tests/adapters/test_database_adapter.py @@ -13,13 +13,12 @@ # limitations under the License. import unittest -import mongomock - from datetime import datetime + +import mongomock from flexmock import flexmock, flexmock_teardown from hamcrest import assert_that, contains_inanyorder from pymongo import MongoClient - from almanach.adapters.database_adapter import DatabaseAdapter from almanach.common.volume_type_not_found_exception import VolumeTypeNotFoundException from almanach.common.almanach_exception import AlmanachException @@ -29,7 +28,6 @@ from tests.builder import a, instance, volume, volume_type class DatabaseAdapterTest(unittest.TestCase): - def setUp(self): config.read(config_file="resources/config/test.cfg") mongo_connection = mongomock.Connection() @@ -166,7 +164,8 @@ class DatabaseAdapterTest(unittest.TestCase): def test_list_entities_in_period(self): fake_entities_in_period = [ a(instance().with_id("in_the_period").with_start(2014, 1, 1, 7, 0, - 0).with_end(2014, 1, 1, 8, 0, 0).with_project_id("project_id")), + 0).with_end(2014, 1, 1, 8, 0, 0).with_project_id( + "project_id")), a(instance().with_id("running_has_started_before").with_start( 2014, 1, 1, 1, 0, 0).with_no_end().with_project_id("project_id")), a(instance().with_id("running_has_started_during").with_start( @@ -174,9 +173,11 @@ class DatabaseAdapterTest(unittest.TestCase): ] fake_entities_out_period = [ a(instance().with_id("before_the_period").with_start(2014, 1, 1, 0, - 0, 0).with_end(2014, 1, 1, 1, 0, 0).with_project_id("project_id")), + 0, 0).with_end(2014, 1, 1, 1, 0, 0).with_project_id( + "project_id")), a(instance().with_id("after_the_period").with_start(2014, 1, 1, 10, - 0, 0).with_end(2014, 1, 1, 11, 0, 0).with_project_id("project_id")), + 0, 0).with_end(2014, 1, 1, 11, 0, 0).with_project_id( + "project_id")), a(instance().with_id("running_has_started_after").with_start( 2014, 1, 1, 10, 0, 0).with_no_end().with_project_id("project_id")), ] @@ -206,7 +207,7 @@ class DatabaseAdapterTest(unittest.TestCase): self.adapter.update_active_entity(fake_entity) self.assertEqual(self.db.entity.find_one({"entity_id": fake_entity.entity_id})[ - "os"]["distro"], fake_entity.os.distro) + "os"]["distro"], fake_entity.os.distro) def test_insert_volume(self): count = self.db.entity.count() diff --git a/tests/core/test_controller.py b/tests/core/test_controller.py index 892bb13..f3d7bbf 100644 --- a/tests/core/test_controller.py +++ b/tests/core/test_controller.py @@ -13,13 +13,12 @@ # limitations under the License. import unittest -import pytz - from datetime import datetime, timedelta + +import pytz from dateutil import parser as date_parser from flexmock import flexmock, flexmock_teardown from nose.tools import assert_raises - from almanach import config from almanach.common.date_format_exception import DateFormatException from almanach.core.controller import Controller @@ -28,7 +27,6 @@ from tests.builder import a, instance, volume, volume_type class ControllerTest(unittest.TestCase): - def setUp(self): self.database_adapter = flexmock() @@ -363,7 +361,8 @@ class ControllerTest(unittest.TestCase): .once()) self.controller.create_volume(some_volume.entity_id, some_volume.project_id, '2015-10-21T16:25:00.000000Z', - some_volume.volume_type, some_volume.size, some_volume.name, some_volume.attached_to) + some_volume.volume_type, some_volume.size, some_volume.name, + some_volume.attached_to) def test_volume_updated(self): fake_volume = a(volume()) diff --git a/tests/messages.py b/tests/messages.py index 6ce146c..7c0e7f0 100644 --- a/tests/messages.py +++ b/tests/messages.py @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +from datetime import datetime, timedelta + import dateutil.parser import pytz -from datetime import datetime, timedelta - DEFAULT_VOLUME_TYPE = "5dadd67f-e21e-4c13-b278-c07b73b21250" @@ -31,7 +31,9 @@ def get_instance_create_end_sample(instance_id=None, tenant_id=None, flavor_name "os_distro": os_distro or "CentOS", "os_version": os_version or "6.4", "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 16, 29, 58, tzinfo=pytz.utc), - "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 16, 30, 02, tzinfo=pytz.utc), + "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 16, + 30, 02, + tzinfo=pytz.utc), "terminated_at": None, "deleted_at": None, "state": "active", @@ -52,8 +54,11 @@ def get_instance_delete_end_sample(instance_id=None, tenant_id=None, flavor_name "os_distro": os_distro or "centos", "os_version": os_version or "6.4", "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 16, 29, 58, tzinfo=pytz.utc), - "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 16, 30, 02, tzinfo=pytz.utc), - "terminated_at": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 18, 12, 5, 23, tzinfo=pytz.utc), + "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 16, + 30, 02, + tzinfo=pytz.utc), + "terminated_at": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 18, 12, 5, 23, + tzinfo=pytz.utc), "deleted_at": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 18, 12, 5, 23, tzinfo=pytz.utc), "state": "deleted" } @@ -70,7 +75,9 @@ def get_volume_create_end_sample(volume_id=None, tenant_id=None, volume_type=Non "volume_type": volume_type or DEFAULT_VOLUME_TYPE, "volume_size": volume_size or 50, "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc), - "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), + "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, + 18, 40, + tzinfo=pytz.utc), "status": "available" } kwargs["timestamp"] = kwargs["launched_at"] + timedelta(microseconds=200000) @@ -103,8 +110,11 @@ def get_volume_attach_icehouse_end_sample(volume_id=None, tenant_id=None, volume "volume_size": volume_size or 50, "attached_to": attached_to or "e7d44dea-21c1-452c-b50c-cbab0d07d7d3", "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc), - "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), - "timestamp": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), + "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, + 18, 40, + tzinfo=pytz.utc), + "timestamp": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18, + 40, tzinfo=pytz.utc), } return _get_volume_icehouse_payload("volume.attach.end", **kwargs) @@ -118,7 +128,8 @@ def get_volume_attach_kilo_end_sample(volume_id=None, tenant_id=None, volume_typ "volume_type": volume_type or DEFAULT_VOLUME_TYPE, "volume_size": volume_size or 50, "attached_to": attached_to, - "timestamp": timestamp + timedelta(seconds=1) if timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), + "timestamp": timestamp + timedelta(seconds=1) if timestamp else datetime(2014, 2, 14, 17, 18, 40, + tzinfo=pytz.utc), } return _get_volume_kilo_payload("volume.attach.end", **kwargs) @@ -132,7 +143,8 @@ def get_volume_detach_kilo_end_sample(volume_id=None, tenant_id=None, volume_typ "volume_type": volume_type or DEFAULT_VOLUME_TYPE, "volume_size": volume_size or 50, "attached_to": attached_to, - "timestamp": timestamp + timedelta(seconds=1) if timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), + "timestamp": timestamp + timedelta(seconds=1) if timestamp else datetime(2014, 2, 14, 17, 18, 40, + tzinfo=pytz.utc), } return _get_volume_kilo_payload("volume.detach.end", **kwargs) @@ -147,7 +159,9 @@ def get_volume_detach_end_sample(volume_id=None, tenant_id=None, volume_type=Non "volume_size": volume_size or 50, "attached_to": None, "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc), - "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), + "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, + 18, 40, + tzinfo=pytz.utc), "timestamp": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 23, 8, 1, 58, tzinfo=pytz.utc), "status": "detach" } @@ -164,7 +178,9 @@ def get_volume_rename_end_sample(volume_id=None, tenant_id=None, volume_type=Non "volume_size": volume_size or 50, "attached_to": None, "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc), - "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), + "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, + 18, 40, + tzinfo=pytz.utc), "timestamp": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 23, 8, 1, 58, tzinfo=pytz.utc), "status": "detach" } @@ -181,7 +197,9 @@ def get_volume_exists_sample(volume_id=None, tenant_id=None, volume_type=None, v "volume_size": volume_size or 50, "attached_to": None, "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc), - "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), + "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, + 18, 40, + tzinfo=pytz.utc), "timestamp": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 23, 8, 1, 58, tzinfo=pytz.utc), "status": "detach" } @@ -195,7 +213,8 @@ def _format_date(datetime_obj): def _get_instance_payload(event_type, instance_id=None, tenant_id=None, hostname=None, display_name=None, instance_type=None, instance_flavor_id=None, timestamp=None, created_at=None, launched_at=None, - deleted_at=None, terminated_at=None, state=None, os_type=None, os_distro=None, os_version=None, metadata={}): + deleted_at=None, terminated_at=None, state=None, os_type=None, os_distro=None, + os_version=None, metadata={}): instance_id = instance_id or "e7d44dea-21c1-452c-b50c-cbab0d07d7d3" os_type = os_type or "linux" os_distro = os_distro or "centos" @@ -272,7 +291,8 @@ def _get_instance_payload(event_type, instance_id=None, tenant_id=None, hostname def _get_volume_icehouse_payload(event_type, volume_id=None, tenant_id=None, display_name=None, volume_type=None, - volume_size=None, timestamp=None, created_at=None, launched_at=None, status=None, attached_to=None): + volume_size=None, timestamp=None, created_at=None, launched_at=None, status=None, + attached_to=None): volume_id = volume_id or "64a0ca7f-5f5a-4dc5-a1e1-e04e89eb95ed" tenant_id = tenant_id or "46eeb8e44298460899cf4b3554bfe11f" display_name = display_name or "mytenant-0001-myvolume" diff --git a/tox.ini b/tox.ini index 6090f4d..cfdf764 100644 --- a/tox.ini +++ b/tox.ini @@ -1,7 +1,14 @@ [tox] -envlist = py27 +envlist = py27,flake8 [testenv] deps = -r{toxinidir}/test-requirements.txt commands = nosetests --tests tests + +[testenv:flake8] +commands = flake8 + +[flake8] +show-source = True +max-line-length = 120 \ No newline at end of file