Task.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #
  2. # This file is part of the LibreOffice project.
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. #
  8. # This file incorporates work covered by the following license notice:
  9. #
  10. # Licensed to the Apache Software Foundation (ASF) under one or more
  11. # contributor license agreements. See the NOTICE file distributed
  12. # with this work for additional information regarding copyright
  13. # ownership. The ASF licenses this file to you under the Apache
  14. # License, Version 2.0 (the "License"); you may not use this file
  15. # except in compliance with the License. You may obtain a copy of
  16. # the License at http://www.apache.org/licenses/LICENSE-2.0 .
  17. import traceback
  18. from .TaskEvent import TaskEvent
  19. class Task:
  20. successful = 0
  21. failed = 0
  22. maximum = 0
  23. taskName = ""
  24. listeners = []
  25. subtaskName = ""
  26. def __init__(self, taskName_, subtaskName_, max_):
  27. self.taskName = taskName_
  28. self.subtaskName = subtaskName_
  29. self.maximum = max_
  30. def start(self):
  31. self.fireTaskStarted()
  32. def fail(self):
  33. self.fireTaskFailed()
  34. def getMax(self):
  35. return self.maximum
  36. def setMax(self, max_):
  37. self.maximum = max_
  38. self.fireTaskStatusChanged()
  39. def advance(self, success_):
  40. if success_:
  41. self.successful += 1
  42. print ("Success :", self.successful)
  43. else:
  44. self.failed += 1
  45. print ("Failed :", self.failed)
  46. self.fireTaskStatusChanged()
  47. if (self.failed + self.successful == self.maximum):
  48. self.fireTaskFinished()
  49. def fireTaskStatusChanged(self):
  50. te = TaskEvent(self, TaskEvent.TASK_STATUS_CHANGED)
  51. for i in range(len(self.listeners)):
  52. self.listeners[i].taskStatusChanged(te)
  53. def fireTaskStarted(self):
  54. te = TaskEvent(self, TaskEvent.TASK_STARTED)
  55. for i in range(len(self.listeners)):
  56. self.listeners[i].taskStarted(te)
  57. def fireTaskFailed(self):
  58. te = TaskEvent(self, TaskEvent.TASK_FAILED)
  59. for i in range(len(self.listeners)):
  60. self.listeners[i].taskFinished(te)
  61. def fireTaskFinished(self):
  62. te = TaskEvent(self, TaskEvent.TASK_FINISHED)
  63. for i in range(len(self.listeners)):
  64. self.listeners[i].taskFinished(te)