#
# Copyright (c) SAS Institute Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


"""
decorators
"""
import functools
import os

from rbuild import errors
from rbuild.handle import RbuildHandle


def _getHandle(l):
    if hasattr(l[0], 'handle') \
            and isinstance(l[0].handle, RbuildHandle):
        _handle = l[0].handle
    elif isinstance(l[1], RbuildHandle):
        _handle = l[1]
    else:
        raise errors.PluginError("Must wrap a plugin methodor the runCommand"
                                 " method of a BaseCommand subclass")
    return _handle


def requiresProduct(method):
    @functools.wraps(method)
    def wrapper(*args, **kwargs):
        _handle = _getHandle(args)
        if _handle.productStore is None:
            raise errors.MissingProductStoreError(os.getcwd())
        return method(*args, **kwargs)
    return wrapper


def requiresStage(method):
    @functools.wraps(method)
    def wrapper(*args, **kwargs):
        _handle = _getHandle(args)
        if _handle.productStore is None:
            raise errors.MissingProductStoreError(os.getcwd())
        if _handle.productStore._currentStage is None:
            raise errors.MissingActiveStageError(os.getcwd())
        return method(*args, **kwargs)
    return wrapper

@@ -688,24 +688,60 @@
 buildHandle%0A
+from rbuild.pluginapi import Plugin%0A
 %0A%0Adef _getHa
@@ -749,119 +749,343 @@
 dle(
-l
+args
 ):%0A    
-if hasattr(l%5B0%5D, '
+%22%22%22%0A    Search %60args%60 for a handle.%0A%0A    If the first item in %60args%60 is a Plugin, then return the Plugin's 
 handle
-') %5C
+.
 %0A    
-        and isinstance(l%5B0%5D.handle, RbuildHandle):%0A        _handle = l
+Else if the second item in %60args%60 is a RbuildHandle, then return it.%0A%0A    :param list args: list to search%0A    :return RbuildHandle: a handle%0A    %22%22%22%0A    if isinstance(args%5B0%5D, Plugin):%0A        return args
 %5B0%5D.
@@ -1111,17 +1111,20 @@
 nstance(
-l
+args
 %5B1%5D, Rbu
@@ -1147,19 +1147,19 @@
     
-_handle = l
+return args
 %5B1%5D%0A
@@ -1227,16 +1227,17 @@
 n method
+ 
 or the r
@@ -1321,27 +1321,8 @@
 s%22)%0A
-    return _handle%0A
 %0A%0Ade
@@ -1411,33 +1411,32 @@
 wargs):%0A        
-_
 handle = _getHan
@@ -1448,33 +1448,32 @@
 rgs)%0A        if 
-_
 handle.productSt
@@ -1706,17 +1706,16 @@
         
-_
 handle =
@@ -1735,33 +1735,32 @@
 rgs)%0A        if 
-_
 handle.productSt
@@ -1846,17 +1846,16 @@
      if 
-_
 handle.p

Refactor _getHandle helper function (APPENG-2757)