From 1c49d07912da224d29d51ae3c13278bdd5cffa78 Mon Sep 17 00:00:00 2001 From: Steve Wilkerson Date: Wed, 9 Dec 2015 08:38:52 -0600 Subject: [PATCH] Remove string concatenation in favor of string formatting Some of the modules still utilized string concatenation instead of using formatting. In order to align with other modules in the project, I refactored these modules to use string formatting instead. Change-Id: I708392e1d03b6331a134419aa0ae9dc02a05c31b Closes-Bug: 1522738 --- watcher/api/controllers/v1/types.py | 2 +- watcher/api/middleware/parsable_error.py | 10 +++++----- watcher/applier/messaging/trigger.py | 2 +- watcher/db/sqlalchemy/models.py | 4 ++-- watcher/tests/api/v1/test_audit_templates.py | 14 +++++++------- watcher/tests/api/v1/test_audits.py | 2 +- watcher/tests/db/test_audit_template.py | 2 +- .../strategy/strategies/faker_cluster_state.py | 15 ++------------- 8 files changed, 20 insertions(+), 31 deletions(-) diff --git a/watcher/api/controllers/v1/types.py b/watcher/api/controllers/v1/types.py index 904a90c49..6916fc965 100644 --- a/watcher/api/controllers/v1/types.py +++ b/watcher/api/controllers/v1/types.py @@ -217,7 +217,7 @@ class JsonPatchType(wtypes.Base): @staticmethod def validate(patch): - _path = '/' + patch.path.split('/')[1] + _path = '/{0}'.format(patch.path.split('/')[1]) if _path in patch.internal_attrs(): msg = _("'%s' is an internal attribute and can not be updated") raise wsme.exc.ClientSideError(msg % patch.path) diff --git a/watcher/api/middleware/parsable_error.py b/watcher/api/middleware/parsable_error.py index 9d4a9d752..a6d1ad526 100644 --- a/watcher/api/middleware/parsable_error.py +++ b/watcher/api/middleware/parsable_error.py @@ -74,13 +74,13 @@ class ParsableErrorMiddleware(object): try: # simple check xml is valid body = [et.ElementTree.tostring( - et.ElementTree.fromstring('' - + '\n'.join(app_iter) - + ''))] + et.ElementTree.Element('error_message', + text='\n'.join(app_iter)))] except et.ElementTree.ParseError as err: LOG.error(_LE('Error parsing HTTP response: %s'), err) - body = ['%s' % state['status_code'] - + ''] + body = [et.ElementTree.tostring( + et.ElementTree.Element('error_message', + text=state['status_code']))] state['headers'].append(('Content-Type', 'application/xml')) else: if six.PY3: diff --git a/watcher/applier/messaging/trigger.py b/watcher/applier/messaging/trigger.py index 1c1c8d931..b66295908 100644 --- a/watcher/applier/messaging/trigger.py +++ b/watcher/applier/messaging/trigger.py @@ -34,7 +34,7 @@ class TriggerActionPlan(object): action_plan_uuid) cmd.execute() except Exception as e: - LOG.error("do_launch_action_plan " + unicode(e)) + LOG.exception(e) def launch_action_plan(self, context, action_plan_uuid): LOG.debug("Trigger ActionPlan %s" % action_plan_uuid) diff --git a/watcher/db/sqlalchemy/models.py b/watcher/db/sqlalchemy/models.py index a5312ef14..b65aabd31 100644 --- a/watcher/db/sqlalchemy/models.py +++ b/watcher/db/sqlalchemy/models.py @@ -33,14 +33,14 @@ from sqlalchemy.types import TypeDecorator, TEXT from watcher.common import paths - sql_opts = [ cfg.StrOpt('mysql_engine', default='InnoDB', help='MySQL engine to use.') ] -_DEFAULT_SQL_CONNECTION = 'sqlite:///' + paths.state_path_def('watcher.sqlite') +_DEFAULT_SQL_CONNECTION = 'sqlite:///{0}'.format( + paths.state_path_def('watcher.sqlite')) cfg.CONF.register_opts(sql_opts, 'database') db_options.set_defaults(cfg.CONF, _DEFAULT_SQL_CONNECTION, 'watcher.sqlite') diff --git a/watcher/tests/api/v1/test_audit_templates.py b/watcher/tests/api/v1/test_audit_templates.py index a8b47d3e3..0b6b0acb4 100644 --- a/watcher/tests/api/v1/test_audit_templates.py +++ b/watcher/tests/api/v1/test_audit_templates.py @@ -128,7 +128,7 @@ class TestListAuditTemplate(api_base.FunctionalTest): audit_template = obj_utils.create_test_audit_template( self.context, id=id_, uuid=utils.generate_uuid(), - name='My Audit Template ' + str(id_)) + name='My Audit Template {0}'.format(id_)) audit_template_list.append(audit_template.uuid) response = self.get_json('/audit_templates') self.assertEqual(len(audit_template_list), @@ -141,12 +141,12 @@ class TestListAuditTemplate(api_base.FunctionalTest): for id_ in [1, 2, 3]: audit_template = obj_utils.create_test_audit_template( self.context, id=id_, uuid=utils.generate_uuid(), - name='My Audit Template ' + str(id_)) + name='My Audit Template {0}'.format(id_)) audit_template_list.append(audit_template.uuid) for id_ in [4, 5]: audit_template = obj_utils.create_test_audit_template( self.context, id=id_, uuid=utils.generate_uuid(), - name='My Audit Template ' + str(id_)) + name='My Audit Template {0}'.format(id_)) audit_template.soft_delete() response = self.get_json('/audit_templates') self.assertEqual(3, len(response['audit_templates'])) @@ -158,12 +158,12 @@ class TestListAuditTemplate(api_base.FunctionalTest): for id_ in [1, 2, 3]: audit_template = obj_utils.create_test_audit_template( self.context, id=id_, uuid=utils.generate_uuid(), - name='My Audit Template ' + str(id_)) + name='My Audit Template {0}'.format(id_)) audit_template_list.append(audit_template.uuid) for id_ in [4, 5]: audit_template = obj_utils.create_test_audit_template( self.context, id=id_, uuid=utils.generate_uuid(), - name='My Audit Template ' + str(id_)) + name='My Audit Template {0}'.format(id_)) audit_template.soft_delete() audit_template_list.append(audit_template.uuid) response = self.get_json('/audit_templates', @@ -187,7 +187,7 @@ class TestListAuditTemplate(api_base.FunctionalTest): for id_ in range(5): obj_utils.create_test_audit_template( self.context, id=id_, uuid=utils.generate_uuid(), - name='My Audit Template ' + str(id_)) + name='My Audit Template {0}'.format(id_)) response = self.get_json('/audit_templates/?limit=3') self.assertEqual(3, len(response['audit_templates'])) @@ -199,7 +199,7 @@ class TestListAuditTemplate(api_base.FunctionalTest): for id_ in range(5): obj_utils.create_test_audit_template( self.context, id=id_, uuid=utils.generate_uuid(), - name='My Audit Template ' + str(id_)) + name='My Audit Template {0}'.format(id_)) response = self.get_json('/audit_templates') self.assertEqual(3, len(response['audit_templates'])) diff --git a/watcher/tests/api/v1/test_audits.py b/watcher/tests/api/v1/test_audits.py index f20f9d84f..29fac9e90 100644 --- a/watcher/tests/api/v1/test_audits.py +++ b/watcher/tests/api/v1/test_audits.py @@ -169,7 +169,7 @@ class TestListAudit(api_base.FunctionalTest): for id_ in range(5): audit_template = obj_utils.create_test_audit_template( self.context, - name='at' + str(id_), + name='at{0}'.format(id_), uuid=utils.generate_uuid()) obj_utils.create_test_audit( self.context, id=id_, uuid=utils.generate_uuid(), diff --git a/watcher/tests/db/test_audit_template.py b/watcher/tests/db/test_audit_template.py index 024579cfc..b7dbe6317 100644 --- a/watcher/tests/db/test_audit_template.py +++ b/watcher/tests/db/test_audit_template.py @@ -34,7 +34,7 @@ class DbAuditTemplateTestCase(base.DbTestCase): for i in range(1, 6): audit_template = utils.create_test_audit_template( uuid=w_utils.generate_uuid(), - name='My Audit Template ' + str(i)) + name='My Audit Template {0}'.format(i)) uuids.append(six.text_type(audit_template['uuid'])) res = self.dbapi.get_audit_template_list(self.context) res_uuids = [r.uuid for r in res] diff --git a/watcher/tests/decision_engine/strategy/strategies/faker_cluster_state.py b/watcher/tests/decision_engine/strategy/strategies/faker_cluster_state.py index 08a3dc853..22cccf78c 100644 --- a/watcher/tests/decision_engine/strategy/strategies/faker_cluster_state.py +++ b/watcher/tests/decision_engine/strategy/strategies/faker_cluster_state.py @@ -66,14 +66,12 @@ class FakerModelCollector(BaseClusterModelCollector): mem.set_capacity(hypervisor, 132) disk.set_capacity(hypervisor, 250) num_cores.set_capacity(hypervisor, 40) - # print("create "+str(hypervisor)) current_state_cluster.add_hypervisor(hypervisor) for i in range(0, count_vm): vm_uuid = "VM_{0}".format(i) vm = VM() vm.uuid = vm_uuid - # print("create "+str(vm)) mem.set_capacity(vm, 8) disk.set_capacity(vm, 10) num_cores.set_capacity(vm, 10) @@ -119,14 +117,12 @@ class FakerModelCollector(BaseClusterModelCollector): mem.set_capacity(node, 132) disk.set_capacity(node, 250) num_cores.set_capacity(node, 40) - # print("create "+str(node)) current_state_cluster.add_hypervisor(node) for i in range(0, count_vm): vm_uuid = "VM_{0}".format(i) vm = VM() vm.uuid = vm_uuid - # print("create "+str(vm)) mem.set_capacity(vm, 2) disk.set_capacity(vm, 20) num_cores.set_capacity(vm, 10) @@ -202,14 +198,12 @@ class FakerModelCollector(BaseClusterModelCollector): mem.set_capacity(node, 132) disk.set_capacity(node, 250) num_cores.set_capacity(node, 40) - # print("create "+str(node)) current_state_cluster.add_hypervisor(node) for i in range(0, count_vm): vm_uuid = "VM_{0}".format(i) vm = VM() vm.uuid = vm_uuid - # print("create "+str(vm)) mem.set_capacity(vm, 10) disk.set_capacity(vm, 25) num_cores.set_capacity(vm, 16) @@ -266,14 +260,12 @@ class FakerModelCollector(BaseClusterModelCollector): mem.set_capacity(node, 132) disk.set_capacity(node, 250) num_cores.set_capacity(node, 40) - # print("create "+str(node)) current_state_cluster.add_hypervisor(node) for i in range(0, count_vm): vm_uuid = "VM_{0}".format(i) vm = VM() vm.uuid = vm_uuid - # print("create "+str(vm)) mem.set_capacity(vm, 2) disk.set_capacity(vm, 20) num_cores.set_capacity(vm, 10) @@ -314,7 +306,6 @@ class FakerModelCollector(BaseClusterModelCollector): mem.set_capacity(node, 1) disk.set_capacity(node, 1) num_cores.set_capacity(node, 1) - # print("create "+str(node)) current_state_cluster.add_hypervisor(node) return current_state_cluster @@ -338,7 +329,7 @@ class FakerModelCollector(BaseClusterModelCollector): current_state_cluster.create_resource(disk) for i in range(0, count_node): - node_uuid = "Node_" + str(i) + node_uuid = "Node_{0}".format(i) node = Hypervisor() node.uuid = node_uuid node.hostname = "hostname_{0}".format(i) @@ -346,14 +337,12 @@ class FakerModelCollector(BaseClusterModelCollector): mem.set_capacity(node, 4) disk.set_capacity(node, 4) num_cores.set_capacity(node, 4) - # print("create "+str(node)) current_state_cluster.add_hypervisor(node) for i in range(0, count_vm): - vm_uuid = "VM_" + str(i) + vm_uuid = "VM_{0}".format(i) vm = VM() vm.uuid = vm_uuid - # print("create "+str(vm)) mem.set_capacity(vm, 2) disk.set_capacity(vm, 0) num_cores.set_capacity(vm, 4)