Fix PY3 compatibility
* Change iteritems to items * Use six.string_types instead of basestring * Use open instead of file * encode to utf-8 before writing to file in tests * fix minor relative import in mistralclient.utils Also, this is blocking the port of Heat to use Python 3. Change-Id: Ie82975482754246d9761dd2cf9693fb852024a9b
This commit is contained in:
@@ -29,12 +29,12 @@ class Resource(object):
|
||||
self._set_attributes()
|
||||
|
||||
def _set_defaults(self):
|
||||
for k, v in self.defaults.iteritems():
|
||||
for k, v in self.defaults.items():
|
||||
if k not in self._data:
|
||||
self._data[k] = v
|
||||
|
||||
def _set_attributes(self):
|
||||
for k, v in self._data.iteritems():
|
||||
for k, v in self._data.items():
|
||||
try:
|
||||
setattr(self, k, v)
|
||||
except AttributeError:
|
||||
@@ -43,7 +43,7 @@ class Resource(object):
|
||||
|
||||
def __str__(self):
|
||||
vals = ", ".join(["%s='%s'" % (n, v)
|
||||
for n, v in self._data.iteritems()])
|
||||
for n, v in self._data.items()])
|
||||
return "%s [%s]" % (self.resource_name, vals)
|
||||
|
||||
|
||||
@@ -71,13 +71,13 @@ class ResourceManager(object):
|
||||
return [i for i in self.list() if _check_items(i, kwargs.items())]
|
||||
|
||||
def _ensure_not_empty(self, **kwargs):
|
||||
for name, value in kwargs.iteritems():
|
||||
for name, value in kwargs.items():
|
||||
if value is None or (isinstance(value, str) and len(value) == 0):
|
||||
raise APIException('%s is missing field "%s"' %
|
||||
(self.resource_class.__name__, name))
|
||||
|
||||
def _copy_if_defined(self, data, **kwargs):
|
||||
for name, value in kwargs.iteritems():
|
||||
for name, value in kwargs.items():
|
||||
if value is not None:
|
||||
data[name] = value
|
||||
|
||||
|
@@ -14,6 +14,8 @@
|
||||
|
||||
import json
|
||||
|
||||
import six
|
||||
|
||||
from mistralclient.api import base
|
||||
|
||||
|
||||
@@ -22,8 +24,8 @@ class Environment(base.Resource):
|
||||
|
||||
def _set_attributes(self):
|
||||
"""Override loading of the "variables" attribute from text to dict."""
|
||||
for k, v in self._data.iteritems():
|
||||
if k == 'variables' and isinstance(v, basestring):
|
||||
for k, v in self._data.items():
|
||||
if k == 'variables' and isinstance(v, six.string_types):
|
||||
v = json.loads(v)
|
||||
|
||||
try:
|
||||
|
@@ -41,7 +41,7 @@ class TestCLITriggersV2(base.BaseCommandTest):
|
||||
@mock.patch('mistralclient.api.v2.cron_triggers.CronTriggerManager.create')
|
||||
def test_create(self, mock, mock_open):
|
||||
mock.return_value = TRIGGER
|
||||
mock_open.return_value = mock.MagicMock(spec=file)
|
||||
mock_open.return_value = mock.MagicMock(spec=open)
|
||||
|
||||
result = self.call(
|
||||
cron_triggers_cmd.Create,
|
||||
|
@@ -56,7 +56,7 @@ class TestCLIEnvironmentsV2(base.BaseCommandTest):
|
||||
mock.return_value = ENVIRONMENT
|
||||
|
||||
with tempfile.NamedTemporaryFile() as f:
|
||||
f.write(content)
|
||||
f.write(content.encode('utf-8'))
|
||||
f.flush()
|
||||
file_path = os.path.abspath(f.name)
|
||||
result = self.call(environment_cmd.Create, app_args=[file_path])
|
||||
@@ -74,7 +74,7 @@ class TestCLIEnvironmentsV2(base.BaseCommandTest):
|
||||
mock.return_value = ENVIRONMENT
|
||||
|
||||
with tempfile.NamedTemporaryFile() as f:
|
||||
f.write(content)
|
||||
f.write(content.encode('utf-8'))
|
||||
f.flush()
|
||||
file_path = os.path.abspath(f.name)
|
||||
result = self.call(environment_cmd.Update, app_args=[file_path])
|
||||
|
@@ -53,7 +53,7 @@ class TestCLIWorkbooksV2(base.BaseCommandTest):
|
||||
@mock.patch('mistralclient.api.v2.workbooks.WorkbookManager.create')
|
||||
def test_create(self, mock, mock_open):
|
||||
mock.return_value = WORKBOOK
|
||||
mock_open.return_value = mock.MagicMock(spec=file)
|
||||
mock_open.return_value = mock.MagicMock(spec=open)
|
||||
|
||||
result = self.call(workbook_cmd.Create, app_args=['wb.yaml'])
|
||||
|
||||
@@ -63,7 +63,7 @@ class TestCLIWorkbooksV2(base.BaseCommandTest):
|
||||
@mock.patch('mistralclient.api.v2.workbooks.WorkbookManager.update')
|
||||
def test_update(self, mock, mock_open):
|
||||
mock.return_value = WORKBOOK
|
||||
mock_open.return_value = mock.MagicMock(spec=file)
|
||||
mock_open.return_value = mock.MagicMock(spec=open)
|
||||
|
||||
result = self.call(workbook_cmd.Update, app_args=['definition'])
|
||||
|
||||
@@ -113,7 +113,7 @@ class TestCLIWorkbooksV2(base.BaseCommandTest):
|
||||
@mock.patch('mistralclient.api.v2.workbooks.WorkbookManager.validate')
|
||||
def test_validate(self, mock, mock_open):
|
||||
mock.return_value = {'valid': True}
|
||||
mock_open.return_value = mock.MagicMock(spec=file)
|
||||
mock_open.return_value = mock.MagicMock(spec=open)
|
||||
|
||||
result = self.call(workbook_cmd.Validate, app_args=['wb.yaml'])
|
||||
|
||||
@@ -124,7 +124,7 @@ class TestCLIWorkbooksV2(base.BaseCommandTest):
|
||||
@mock.patch('mistralclient.api.v2.workbooks.WorkbookManager.validate')
|
||||
def test_validate_failed(self, mock, mock_open):
|
||||
mock.return_value = {'valid': False, 'error': 'Invalid DSL...'}
|
||||
mock_open.return_value = mock.MagicMock(spec=file)
|
||||
mock_open.return_value = mock.MagicMock(spec=open)
|
||||
|
||||
self.assertRaises(exc.MistralClientException,
|
||||
self.call,
|
||||
|
@@ -128,7 +128,7 @@ class TestCLIWorkflowsV2(base.BaseCommandTest):
|
||||
@mock.patch('mistralclient.api.v2.workflows.WorkflowManager.validate')
|
||||
def test_validate(self, mock, mock_open):
|
||||
mock.return_value = {'valid': True}
|
||||
mock_open.return_value = mock.MagicMock(spec=file)
|
||||
mock_open.return_value = mock.MagicMock(spec=open)
|
||||
|
||||
result = self.call(workflow_cmd.Validate, app_args=['wf.yaml'])
|
||||
|
||||
@@ -139,7 +139,7 @@ class TestCLIWorkflowsV2(base.BaseCommandTest):
|
||||
@mock.patch('mistralclient.api.v2.workflows.WorkflowManager.validate')
|
||||
def test_validate_failed(self, mock, mock_open):
|
||||
mock.return_value = {'valid': False, 'error': 'Invalid DSL...'}
|
||||
mock_open.return_value = mock.MagicMock(spec=file)
|
||||
mock_open.return_value = mock.MagicMock(spec=open)
|
||||
|
||||
self.assertRaises(exc.MistralClientException,
|
||||
self.call,
|
||||
|
@@ -12,7 +12,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import exceptions
|
||||
from mistralclient import exceptions
|
||||
|
||||
|
||||
def do_action_on_many(action, resources, success_msg, error_msg):
|
||||
|
Reference in New Issue
Block a user