示例#1
0
 def __init__(self, calcs=None):
     super(Calculation, self).__init__(calcs)
     #: dictionary of calculation sources added to the Formula layer
     self.deg_sources = {}
     #: calculation source objects
     self.calc_obj = {}
     #: calculations
     self.calcs = CalcRegistry()
示例#2
0
class Calculation(Layer):
    """
    Layer containing formulas.
    """
    def __init__(self, calcs=None):
        super(Calculation, self).__init__(calcs)
        #: dictionary of calculation sources added to the Formula layer
        self.deg_sources = {}
        #: calculation source objects
        self.calc_obj = {}
        #: calculations
        self.calcs = CalcRegistry()
        # layers are initialized by the model

    def add(self, deg_source, module, package=None):
        """
        """
        # import module
        mod = importlib.import_module(module, package)
        # get calculation source class definition from module
        if deg_source.startswith('_'):
                err_msg = 'No "%s" attribute in "%s".' % (deg_source, mod)
                raise AttributeError(err_msg)
        self.deg_sources[deg_source] = getattr(mod, deg_source)
        # instantiate the calc object
        self.calc_obj[deg_source] = self.deg_sources[deg_source]()
        # register calc and dependencies in registry
        deg_src_obj = self.calc_obj[deg_source]
        self.calcs.register({deg_source: deg_src_obj},
                               {deg_source: deg_src_obj.dependencies},
                               {deg_source: deg_src_obj.always_calc},
                               {deg_source: deg_src_obj.frequency})

    def open(self, deg_source, module, package=None):
        self.add(deg_source, module, package=None)

    def load(self):
        """
        Add deg_source to layer.
        """
        for k, v in self.layer.iteritems():
            self.add(k, v['module'], v.get('package'))