Esempio n. 1
0
    def __call__(self, method):
        # Get the first definition of the method in the calling scope
        original_method = caller_locals().get(method.__name__)

        # Create a multimethod out of the original method if it
        # isn't one already.
        if not type(original_method) == SpecMultiMethod:
            original_method = SpecMultiMethod(original_method)

        if self.spec is not None:
            original_method.register(self.spec, method)

        return original_method
Esempio n. 2
0
    def __call__(self, method):
        # Get the first definition of the method in the calling scope
        original_method = caller_locals().get(method.__name__)

        # Create a multimethod out of the original method if it
        # isn't one already.
        if not type(original_method) == SpecMultiMethod:
            original_method = SpecMultiMethod(original_method)

        if self.spec is not None:
            original_method.register(self.spec, method)

        return original_method
Esempio n. 3
0
    def __call__(self, method):
        # In Python 2, Get the first definition of the method in the
        # calling scope by looking at the caller's locals. In Python 3,
        # we handle this using MultiMethodMeta.__prepare__.
        if MultiMethodMeta._locals is None:
            MultiMethodMeta._locals = caller_locals()

        # Create a multimethod with this name if there is not one already
        original_method = MultiMethodMeta._locals.get(method.__name__)
        if not type(original_method) == SpecMultiMethod:
            original_method = SpecMultiMethod(original_method)

        if self.spec is not None:
            original_method.register(self.spec, method)

        return original_method
Esempio n. 4
0
    def __call__(self, method):
        """This annotation lets packages declare multiple versions of
        methods like install() that depend on the package's spec.

        For example:

           .. code-block:: python

              class SomePackage(Package):
                  ...

                  def install(self, prefix):
                      # Do default install

                  @when('target=x86_64:')
                  def install(self, prefix):
                      # This will be executed instead of the default install if
                      # the package's target is in the x86_64 family.

                  @when('target=ppc64:')
                  def install(self, prefix):
                      # This will be executed if the package's target is in
                      # the ppc64 family

           This allows each package to have a default version of install() AND
           specialized versions for particular platforms.  The version that is
           called depends on the architecutre of the instantiated package.

           Note that this works for methods other than install, as well.  So,
           if you only have part of the install that is platform specific, you
           could do this:

           .. code-block:: python

              class SomePackage(Package):
                  ...
                  # virtual dependence on MPI.
                  # could resolve to mpich, mpich2, OpenMPI
                  depends_on('mpi')

                  def setup(self):
                      # do nothing in the default case
                      pass

                  @when('^openmpi')
                  def setup(self):
                      # do something special when this is built with OpenMPI for
                      # its MPI implementations.


                  def install(self, prefix):
                      # Do common install stuff
                      self.setup()
                      # Do more common install stuff

           Note that the default version of decorated methods must
           *always* come first.  Otherwise it will override all of the
           platform-specific versions.  There's not much we can do to get
           around this because of the way decorators work.
        """
        # In Python 2, Get the first definition of the method in the
        # calling scope by looking at the caller's locals. In Python 3,
        # we handle this using MultiMethodMeta.__prepare__.
        if MultiMethodMeta._locals is None:
            MultiMethodMeta._locals = caller_locals()

        # Create a multimethod with this name if there is not one already
        original_method = MultiMethodMeta._locals.get(method.__name__)
        if not type(original_method) == SpecMultiMethod:
            original_method = SpecMultiMethod(original_method)

        if self.spec is not None:
            original_method.register(self.spec, method)

        return original_method