'Find the scope referenced by namelist, or if it is not itself a scope,
the containing scope
@param namelist {seq of str} The path to the object, e.g. ["foo", "bar"]
@param attrlist {seq of str} The types of attributes to look at
@param scope {JSObject} The starting scope; defaults to the current scope
@returns {JSObject} The object, if it is a scope; otherwise, the scope
containing the object, if it is a variable.'

def _locateScopeForName(self, namelist, attrlist=('variables',), scope=None):

    if (not namelist):
        return None
    namelist = namelist[:]
    if (scope is None):
        scope = self.currentScope
    log.debug('Finding    in    scope:    %s.%r    with    names:    %r', scope.name, attrlist, namelist)
    if ('variables' in attrlist):
        attrlist += ('members',)
        noVariables = False
    else:
        attrlist += ('variables',)
        noVariables = True
    aliasDepth = 0
    firstAliasFound = None
    while scope:
        currentScope = scope
        log.debug('Looking    in    scope    %r', currentScope.name)
        foundScope = None
        namePos = (-1)
        while (namePos < (len(namelist) - 1)):
            namePos = (namePos + 1)
            name = namelist[namePos]
            for attrToLookIn in attrlist:
                log.debug('Looking    for    name:%r    in    %s    of    scope    with    name:%r', name, attrToLookIn, currentScope.name)
                namesDict = getattr(currentScope, attrToLookIn, None)
                if namesDict:
                    foundScope = namesDict.get(name)
                    if (isinstance(foundScope, JSAlias) and (aliasDepth < 10)):
                        if (firstAliasFound is None):
                            firstAliasFound = foundScope
                        namelist[:(namePos + 1)] = foundScope.target
                        currentScope = scope = foundScope.scope
                        namePos = (-1)
                        aliasDepth += 1
                        log.debug('_locateScopeForName:    encountered    alias    %r,    restarting    with    %r    on    %r', name, namelist, scope.name)
                        break
                    elif ((attrToLookIn == 'variables') and noVariables):
                        continue
                    if foundScope:
                        log.debug('_locateScopeForName:    Found    scope    %r    for:    %r', foundScope.name, name)
                        if ((type != 'variables') or (namePos < (len(namelist) - 1))):
                            currentScope = foundScope
                        break
            else:
                break
        else:
            log.debug('Found    %r    in    scope:%s.%s', namelist, currentScope.name, attrToLookIn)
            return currentScope
        scope = scope.parent
    if firstAliasFound:
        log.debug('Found    alias,    but    no    alias    target,    returning    just    the    alias:    %r', firstAliasFound.name)
        return firstAliasFound
    log.debug('NO    scope    found    for:    %r', namelist)
    return None
