Added constants for strings
authorThierry Florac <thierry.florac@onf.fr>
Mon, 19 Jun 2017 15:08:50 +0200
changeset 89 b32bcb6a311e
parent 88 2baf5c2ba8b7
child 90 aa78027158d6
Added constants for strings
src/pyams_utils/progress.py
--- a/src/pyams_utils/progress.py	Mon Jun 19 14:25:40 2017 +0200
+++ b/src/pyams_utils/progress.py	Mon Jun 19 15:08:50 2017 +0200
@@ -29,13 +29,19 @@
 _local = local()
 
 
+PROGRESS_CACHE_NAME = 'PyAMS::progress'
+PROGRESS_LOCK_NAME = 'PyAMS::progress::lock'
+PROGRESS_TASKS_CACHE_KEY = 'PyAMS::progress::running_tasks'
+PROGRESS_TASK_KEY = 'PyAMS::progress::task::{0}'
+
+
 def get_tasks_cache():
     """Get cache storing tasks list"""
     try:
         tasks_cache = _local.running_tasks_cache
     except AttributeError:
         manager = cache.CacheManager(**cache.cache_regions['persistent'])
-        tasks_cache = _local.running_tasks_cache = manager.get_cache('PyAMS::progress')
+        tasks_cache = _local.running_tasks_cache = manager.get_cache(PROGRESS_CACHE_NAME)
     return tasks_cache
 
 
@@ -45,23 +51,23 @@
         local_cache = _local.progress_cache
     except AttributeError:
         manager = cache.CacheManager(**cache.cache_regions['default'])
-        local_cache = _local.progress_cache = manager.get_cache('PyAMS::progress')
+        local_cache = _local.progress_cache = manager.get_cache(PROGRESS_CACHE_NAME)
     return local_cache
 
 
 def get_running_tasks():
     """Get list of running tasks"""
     tasks_cache = get_tasks_cache()
-    return tasks_cache.get_value('PyAMS_utils::running_tasks', createfunc=set)
+    return tasks_cache.get_value(PROGRESS_TASKS_CACHE_KEY, createfunc=set)
 
 
 def set_running_tasks(tasks):
     """Update list of running tasks"""
     tasks_cache = get_tasks_cache()
-    tasks_cache.set_value('PyAMS_utils::running_tasks', tasks)
+    tasks_cache.set_value(PROGRESS_TASKS_CACHE_KEY, tasks)
 
 
-@locked(name='progress_status')
+@locked(name=PROGRESS_LOCK_NAME)
 def init_progress_status(progress_id, owner, label, tags=None, length=None, current=None):
     """Initialize progress status for given task ID"""
     status = {'status': 'running',
@@ -72,7 +78,7 @@
               'current': current,
               'started': datetime.utcnow()}
     # Store task status
-    cache_key = 'PyAMS_task::{0}'.format(progress_id)
+    cache_key = PROGRESS_TASK_KEY.format(progress_id)
     progress_cache = get_progress_cache()
     progress_cache.set_value(cache_key, status)
     # Store task in running tasks list
@@ -81,11 +87,11 @@
     set_running_tasks(tasks)
 
 
-@locked(name='progress_status')
+@locked(name=PROGRESS_LOCK_NAME)
 def get_progress_status(progress_id):
     """Get status of given task"""
     progress_cache = get_progress_cache()
-    cache_key = 'PyAMS_task::{0}'.format(progress_id)
+    cache_key = PROGRESS_TASK_KEY.format(progress_id)
     try:
         status = progress_cache.get_value(cache_key)
     except KeyError:
@@ -99,11 +105,11 @@
     return status
 
 
-@locked(name='progress_status')
+@locked(name=PROGRESS_LOCK_NAME)
 def set_progress_status(progress_id, status='running', message=None, length=None, current=None):
     """Set status of given task"""
     progress_cache = get_progress_cache()
-    cache_key = 'PyAMS_task::{0}'.format(progress_id)
+    cache_key = PROGRESS_TASK_KEY.format(progress_id)
     try:
         task_status = progress_cache.get_value(cache_key)
     except KeyError: