Skip to content
Browse files

Merge pull request #1835 from tstrachota/cli_pylint_fix

cli - fix in import manifest async task
  • Loading branch information...
2 parents 6f0d45a + be379dd commit ba0eb5bbfb08eaef0b959c3c6321fabc3e242bb0 @tstrachota tstrachota committed Apr 3, 2013
Showing with 29 additions and 29 deletions.
  1. +1 −2 cli/src/katello/client/core/provider.py
  2. +28 −27 cli/src/katello/client/lib/async.py
View
3 cli/src/katello/client/core/provider.py
@@ -276,7 +276,6 @@ def check_options(self, validator):
validator.require('file')
-
def run(self):
provName = self.get_option('name')
orgName = self.get_option('org')
@@ -293,7 +292,7 @@ def run(self):
task = ImportManifestAsyncTask(self.api.import_manifest(prov["id"], f, force))
run_spinner_in_bg(wait_for_async_task, [task], message=_("Importing manifest, please wait... "))
- return task.evaluate_task_status(
+ return ImportManifestAsyncTask.evaluate_task_status(task,
failed = _("Provider [ %s ] failed to import manifest") % provName,
canceled = _("Provider [ %s ] canceled manifest import") % provName,
ok = _("Provider [ %s ] manifest import complete") % provName
View
55 cli/src/katello/client/lib/async.py
@@ -16,6 +16,7 @@
#
import os
+import re
try:
import json
@@ -141,29 +142,31 @@ def status_messages(self):
class ImportManifestAsyncTask(AsyncTask):
- def display_message(self):
+ @classmethod
+ def __format_display_message(cls, task):
"""
The message coming back with this task can contain stack trace and other unformatted
data. The relevant user message is found and displayed.
"""
message = ""
- for task in self._tasks:
+ for error in task.errors():
# First element is message data, second is stack trace
- #
+ full_error_msg = error[0]
+
# Resources::Candlepin::Owner: 409 Conflict {
# "displayMessage" : "Import is the same as existing data",
# "conflicts" : [ "MANIFEST_SAME" ]
# } (POST /candlepin/owners/Engineering/imports)
- #
- m = task["result"]["errors"][0]
- if m.startswith("Resources::Candlepin::Owner"):
- j = json.loads(m[m.index('{'):m.index('}')+1])
- message += " " + j['displayMessage']
+
+ m = re.match(".*Resources::Candlepin::Owner[^{]*(?P<json>{.*})[^}]*", full_error_msg, re.DOTALL)
+ if m is not None:
+ message += " " + json.loads(m.group("json"))['displayMessage']
else:
- message += m
- return str(message)
+ message += " " + full_error_msg
+ return str(message)
- def evaluate_task_status(self, failed="", canceled="", ok=""):
+ @classmethod
+ def evaluate_task_status(cls, task, failed="", canceled="", ok=""):
"""
Test task status and print the corresponding message
@@ -175,19 +178,10 @@ def evaluate_task_status(self, failed="", canceled="", ok=""):
:param ok: message that is printed when the task went ok
:return: EX_DATAERR on failure or cancel, otherwise EX_OK
"""
-
- if self.failed():
- print failed + ":" + self.display_message()
- return os.EX_DATAERR
- elif self.canceled():
- print canceled
- return os.EX_DATAERR
- else:
- if "status_messages" in dir(self):
- print ok + ":" + self.display_message()
- else:
- print ok
- return os.EX_OK
+ evaluate_task_status(task, failed, canceled, ok,
+ error_formatter=cls.__format_display_message,
+ status_formatter=cls.__format_display_message
+ )
def progress(left, total):
@@ -252,7 +246,7 @@ def status_messages(self):
-def evaluate_task_status(task, failed="", canceled="", ok=""):
+def evaluate_task_status(task, failed="", canceled="", ok="", error_formatter=None, status_formatter=None):
"""
Test task status and print the corresponding message
@@ -263,18 +257,25 @@ def evaluate_task_status(task, failed="", canceled="", ok=""):
:param canceled: message that is printed when the task was cancelled
:type ok: string
:param ok: message that is printed when the task went ok
+ :type error_formatter: function
+ :param error_formatter: formatter function used in case of task failure, default is format_sync_errors
+ :type status_formatter: function
+ :param status_formatter: formatter function used when the task succeeds, default is format_sync_status
:return: EX_DATAERR on failure or cancel, otherwise EX_OK
"""
+ error_formatter = error_formatter or format_sync_errors
+ status_formatter = status_formatter or format_sync_status
+
if task.failed():
- print failed + ":" + format_sync_errors(task)
+ print failed + ":" + error_formatter(task)
return os.EX_DATAERR
elif task.canceled():
print canceled
return os.EX_DATAERR
else:
if "status_messages" in dir(task):
- print ok + ":" + format_sync_status(task)
+ print ok + ":" + status_formatter(task)
else:
print ok
return os.EX_OK

0 comments on commit ba0eb5b

Please sign in to comment.
Something went wrong with that request. Please try again.