Use object factory in annotations adapters
authorThierry Florac <thierry.florac@onf.fr>
Thu, 17 Jan 2019 10:38:59 +0100
changeset 85 421da87c4120
parent 84 688aa8041b11
child 86 6eec89e59203
Use object factory in annotations adapters
src/pyams_scheduler/trigger.py
--- a/src/pyams_scheduler/trigger.py	Fri Jan 11 13:16:59 2019 +0100
+++ b/src/pyams_scheduler/trigger.py	Thu Jan 17 10:38:59 2019 +0100
@@ -12,26 +12,22 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
-
-# import interfaces
-from pyams_scheduler.interfaces import ITaskSchedulingMode, ICronTaskScheduling, ICronTask, SCHEDULER_TASK_CRON_INFO, \
-    IDateTaskScheduling, IDateTask, ILoopTaskScheduling, SCHEDULER_TASK_DATE_INFO, ILoopTask, SCHEDULER_TASK_LOOP_INFO
-
-# import packages
 from apscheduler.triggers.cron import CronTrigger
 from apscheduler.triggers.date import DateTrigger
 from apscheduler.triggers.interval import IntervalTrigger
 from persistent import Persistent
+from zope.componentvocabulary.vocabulary import UtilityVocabulary
+from zope.schema.fieldproperty import FieldProperty
+
+from pyams_scheduler.interfaces import ICronTask, ICronTaskScheduling, IDateTask, IDateTaskScheduling, ILoopTask, \
+    ILoopTaskScheduling, ITaskSchedulingMode, SCHEDULER_TASK_CRON_INFO, SCHEDULER_TASK_DATE_INFO, \
+    SCHEDULER_TASK_LOOP_INFO
 from pyams_utils.adapter import adapter_config, get_annotation_adapter
 from pyams_utils.date import date_to_datetime
+from pyams_utils.factory import factory_config
 from pyams_utils.registry import utility_config
 from pyams_utils.timezone import tztime
 from pyams_utils.vocabulary import vocabulary_config
-from zope.componentvocabulary.vocabulary import UtilityVocabulary
-from zope.interface import implementer
-from zope.schema.fieldproperty import FieldProperty
 
 from pyams_scheduler import _
 
@@ -48,7 +44,7 @@
 # Cron-style scheduling mode
 #
 
-@implementer(ICronTaskScheduling)
+@factory_config(ICronTaskScheduling)
 class CronTaskScheduleInfo(Persistent):
     """Cron-style schedule info"""
 
@@ -65,6 +61,13 @@
     second = FieldProperty(ICronTaskScheduling['second'])
 
 
+@adapter_config(context=ICronTask, provides=ICronTaskScheduling)
+def cron_task_scheduler_info_factory(context):
+    """Cron-style task scheduling info factory"""
+    return get_annotation_adapter(context, SCHEDULER_TASK_CRON_INFO, ICronTaskScheduling,
+                                  notify=False, locate=False)
+
+
 @utility_config(name='Cron-style scheduling', provides=ITaskSchedulingMode)
 class CronTaskScheduler(object):
     """Cron-style scheduler mode"""
@@ -88,18 +91,11 @@
                            end_date=tztime(date_to_datetime(info.end_date)))
 
 
-@adapter_config(context=ICronTask, provides=ICronTaskScheduling)
-def cron_task_scheduler_info_factory(context):
-    """Cron-style task scheduling info factory"""
-    return get_annotation_adapter(context, SCHEDULER_TASK_CRON_INFO, CronTaskScheduleInfo,
-                                  notify=False, locate=False)
-
-
 #
 # Date-style scheduling mode
 #
 
-@implementer(IDateTaskScheduling)
+@factory_config(IDateTaskScheduling)
 class DateTaskScheduleInfo(Persistent):
     """Date-style schedule info"""
 
@@ -107,6 +103,13 @@
     start_date = FieldProperty(IDateTaskScheduling['start_date'])
 
 
+@adapter_config(context=IDateTask, provides=IDateTaskScheduling)
+def date_task_scheduler_info_factory(context):
+    """Date-style task scheduling info factory"""
+    return get_annotation_adapter(context, SCHEDULER_TASK_DATE_INFO, IDateTaskScheduling,
+                                  notify=False, locate=False)
+
+
 @utility_config(name='Date-style scheduling', provides=ITaskSchedulingMode)
 class DateTaskScheduler(object):
     """Date-style scheduler mode"""
@@ -121,18 +124,11 @@
         return DateTrigger(run_date=tztime(date_to_datetime(info.start_date)))
 
 
-@adapter_config(context=IDateTask, provides=IDateTaskScheduling)
-def date_task_scheduler_info_factory(context):
-    """Date-style task scheduling info factory"""
-    return get_annotation_adapter(context, SCHEDULER_TASK_DATE_INFO, DateTaskScheduleInfo,
-                                  notify=False, locate=False)
-
-
 #
 # Loop-style scheduling mode
 #
 
-@implementer(ILoopTaskScheduling)
+@factory_config(ILoopTaskScheduling)
 class LoopTaskScheduleInfo(Persistent):
     """Loop-style schedule info"""
 
@@ -146,6 +142,13 @@
     seconds = FieldProperty(ILoopTaskScheduling['seconds'])
 
 
+@adapter_config(context=ILoopTask, provides=ILoopTaskScheduling)
+def loop_task_scheduler_info_factory(context):
+    """Loop-style task scheduling info factory"""
+    return get_annotation_adapter(context, SCHEDULER_TASK_LOOP_INFO, ILoopTaskScheduling,
+                                  notify=False, locate=False)
+
+
 @utility_config(name='Loop-style scheduling', provides=ITaskSchedulingMode)
 class LoopTaskScheduler(object):
     """Loop-style scheduler mode"""
@@ -164,10 +167,3 @@
                                seconds=info.seconds,
                                start_date=tztime(date_to_datetime(info.start_date)),
                                end_date=tztime(date_to_datetime(info.end_date)))
-
-
-@adapter_config(context=ILoopTask, provides=ILoopTaskScheduling)
-def loop_task_scheduler_info_factory(context):
-    """Loop-style task scheduling info factory"""
-    return get_annotation_adapter(context, SCHEDULER_TASK_LOOP_INFO, LoopTaskScheduleInfo,
-                                  notify=False, locate=False)