'Return all available elem_type names beginning with expr'

def _element_names_from_scope_starting_with_expr(self, expr, scoperef, elem_type, scope_names, elem_retriever):

    self.log('%s_names_from_scope_starting_with_expr::    expr:    %r,    scoperef:    %r    for    %r', elem_type, expr, scoperef, scope_names)
    global_blob = self._elem_from_scoperef(self._get_global_scoperef(scoperef))
    all_names = set()
    for scope_type in scope_names:
        elemlist = []
        if (scope_type == 'locals'):
            elemlist = [self._elem_from_scoperef(scoperef)]
        elif (scope_type == 'globals'):
            elemlist = [global_blob]
        elif (scope_type == 'namespace'):
            elemlist = []
            namespace = self._namespace_elem_from_scoperef(scoperef)
            if (namespace is not None):
                elemlist.append(namespace)
                fqn = namespace.get('name')
                lpath = (fqn,)
                for lib in self.libs:
                    hits = lib.hits_from_lpath(lpath, self.ctlr, curr_buf=self.buf)
                    elemlist += [elem for (elem, scoperef) in hits]
        elif (scope_type == 'builtins'):
            lib = self.buf.stdlib
            names = lib.toplevel_cplns(prefix=expr, ilk=elem_type)
            all_names.update([n for (ilk, n) in names])
        elif (scope_type == 'imports'):
            for lib in self.libs:
                names = lib.toplevel_cplns(prefix=expr, ilk=elem_type)
                all_names.update([n for (ilk, n) in names])
            elemlist = self._get_all_import_blobs_for_elem(global_blob)
        for elem in elemlist:
            names = elem_retriever(elem)
            if expr:
                if isinstance(names, dict):
                    try:
                        names = names[expr]
                    except KeyError:
                        names = []
                else:
                    names = [x for x in names if x.startswith(expr)]
            self.log('%s_names_from_scope_starting_with_expr::    adding    %s:    %r', elem_type, scope_type, names)
            all_names.update(names)
    return self._convertListToCitdl(elem_type, all_names)
