2019-03-19 16:15:19 +00:00
|
|
|
#
|
|
|
|
# Copyright (c) 2019, The Board of Trustees of the Leland Stanford Junior University
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
# list of conditions and the following disclaimer.
|
|
|
|
#
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
# and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# * Neither the name of the copyright holder nor the names of its
|
|
|
|
# contributors may be used to endorse or promote products derived from
|
|
|
|
# this software without specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from .base import BaseTask
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class TaskRegistry:
|
|
|
|
"""
|
|
|
|
A container for all the tasks supported by the library
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.store = dict()
|
|
|
|
|
|
|
|
def __setitem__(self, name, cls):
|
|
|
|
if name in self.store:
|
|
|
|
raise ValueError(f'Duplicate task ${name}')
|
|
|
|
self.store[name] = cls
|
|
|
|
|
|
|
|
def __getitem__(self, name):
|
|
|
|
if name not in self.store:
|
|
|
|
logger.warning(f'Unrecognized task ${name}, using generic code')
|
|
|
|
self.store[name] = BaseTask
|
|
|
|
return self.store[name]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.store)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.store)
|
|
|
|
|
|
|
|
|
|
|
|
_registry = TaskRegistry()
|
2019-03-19 17:51:01 +00:00
|
|
|
_registry['generic'] = BaseTask
|
2019-03-19 16:15:19 +00:00
|
|
|
|
|
|
|
def task_name_to_cls_name(name):
|
|
|
|
return name.split('.')[0]
|
|
|
|
|
|
|
|
|
|
|
|
def register_task(name):
|
|
|
|
def decorator(cls):
|
|
|
|
_registry[name] = cls
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2019-03-19 18:14:32 +00:00
|
|
|
def get_tasks(names, args):
|
2019-03-19 16:15:19 +00:00
|
|
|
tasks = {
|
2019-03-19 18:14:32 +00:00
|
|
|
name: (_registry[task_name_to_cls_name(name)])(name, args) for name in names
|
2019-03-19 16:15:19 +00:00
|
|
|
}
|
|
|
|
return [tasks[name] for name in names]
|