'See if token is from one of the imports on this <scope> elem.
Returns (<hit>, <num-tokens-consumed>) or (None, None) if not found.
XXX import_handler.import_blob_name() calls all have potential
to raise CodeIntelError.'

def __hit_from_elem_imports(self, tokens, elem):

    import_handler = self.citadel.import_handler_from_lang(self.trg.lang)
    first_token = tokens[0]
    possible_submodule_tokens = []
    self._check_infinite_recursion(first_token)
    orig_libs = self.libs
    for imp_elem in (i for i in elem if (i.tag == 'import')):
        libs = orig_libs
        self.debug("'%s    ...'    from    %r?", tokens[0], imp_elem)
        alias = imp_elem.get('alias')
        symbol_name = imp_elem.get('symbol')
        module_name = imp_elem.get('module')
        allow_parentdirlib = True
        if module_name.startswith('.'):
            allow_parentdirlib = False
            lookuppath = self.buf.path
            while module_name.startswith('.'):
                lookuppath = dirname(lookuppath)
                module_name = module_name[1:]
            libs = [self.mgr.db.get_lang_lib(self.trg.lang, 'curdirlib', [lookuppath])]
            if (not module_name):
                module_name = symbol_name
                symbol_name = None
        if symbol_name:
            if ((alias and (alias == first_token)) or ((not alias) and (symbol_name == first_token))):
                if allow_parentdirlib:
                    libs = self._add_parentdirlib(libs, module_name.split('.'))
                try:
                    blob = import_handler.import_blob_name(module_name, libs, self.ctlr)
                    if (symbol_name in blob.names):
                        return ((blob.names[symbol_name], (blob, [])), 1)
                    else:
                        self._set_reldirlib_from_blob(blob)
                        (hit, nconsumed) = self._hit_from_elem_imports(([first_token] + tokens[1:]), blob)
                        if hit:
                            return (hit, nconsumed)
                except CodeIntelError:
                    pass
                submodule_name = import_handler.sep.join([module_name, symbol_name])
                if allow_parentdirlib:
                    libs = self._add_parentdirlib(libs, submodule_name.split('.'))
                try:
                    subblob = import_handler.import_blob_name(submodule_name, libs, self.ctlr)
                    return ((subblob, (subblob, [])), 1)
                except CodeIntelError:
                    self.warn("could    not    import    '%s'    from    %s", first_token, imp_elem)
            elif (symbol_name == '*'):
                try:
                    if allow_parentdirlib:
                        libs = self._add_parentdirlib(libs, module_name.split('.'))
                    blob = import_handler.import_blob_name(module_name, libs, self.ctlr)
                except CodeIntelError:
                    pass
                else:
                    self._set_reldirlib_from_blob(blob)
                    try:
                        (hit, nconsumed) = self._hit_from_getattr(tokens, blob, (blob, []))
                    except CodeIntelError:
                        pass
                    else:
                        if hit:
                            return (hit, nconsumed)
        elif ((alias and (alias == first_token)) or ((not alias) and (module_name == first_token))):
            if allow_parentdirlib:
                libs = self._add_parentdirlib(libs, module_name.split('.'))
            blob = import_handler.import_blob_name(module_name, libs, self.ctlr)
            return ((blob, (blob, [])), 1)
        elif ('.' in module_name):
            module_tokens = module_name.split('.')
            if allow_parentdirlib:
                libs = self._add_parentdirlib(libs, module_tokens)
            if (module_tokens == tokens[:len(module_tokens)]):
                blob = import_handler.import_blob_name(module_name, libs, self.ctlr)
                return ((blob, (blob, [])), len(module_tokens))
            elif (module_tokens[0] == tokens[0]):
                possible_submodule_tokens.append(module_tokens)
    if possible_submodule_tokens:
        libs = orig_libs
        if allow_parentdirlib:
            libs = self._add_parentdirlib(libs, module_tokens)
        for i in range((len(module_tokens) - 1), 0, (-1)):
            for module_tokens in possible_submodule_tokens:
                if (module_tokens[:i] == tokens[:i]):
                    blob = import_handler.import_blob_name('.'.join(module_tokens[:i]), libs, self.ctlr)
                    return ((blob, (blob, [])), i)
    return (None, None)
