当前位置: 首页>>代码示例>>Python>>正文


Python PythonSourceTreeAnalyser.analyze_file方法代码示例

本文整理汇总了Python中openmdao.util.dep.PythonSourceTreeAnalyser.analyze_file方法的典型用法代码示例。如果您正苦于以下问题:Python PythonSourceTreeAnalyser.analyze_file方法的具体用法?Python PythonSourceTreeAnalyser.analyze_file怎么用?Python PythonSourceTreeAnalyser.analyze_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在openmdao.util.dep.PythonSourceTreeAnalyser的用法示例。


在下文中一共展示了PythonSourceTreeAnalyser.analyze_file方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: PkgResourcesFactory

# 需要导入模块: from openmdao.util.dep import PythonSourceTreeAnalyser [as 别名]
# 或者: from openmdao.util.dep.PythonSourceTreeAnalyser import analyze_file [as 别名]
class PkgResourcesFactory(Factory):
    """A Factory that loads plugins using the pkg_resources API, which means
    it searches through egg info of distributions in order to find any entry
    point groups corresponding to openmdao plugin types, e.g.,
    openmdao.component, openmdao.variable, etc.
    """

    def __init__(self, groups=plugin_groups.keys(), search_path=None):
        super(PkgResourcesFactory, self).__init__()
        self._have_new_types = True
        self._groups = copy.copy(groups)
        self._search_path = search_path
        self.env = Environment(search_path)
        self.tree_analyser = PythonSourceTreeAnalyser()

    def create(self, typ, version=None, server=None,
               res_desc=None, **ctor_args):
        """Create and return an object of the given type, with
        optional name, version, server id, and resource description.
        """
        if server is not None or res_desc is not None:
            return None
        klass = self._load(typ, version)
        if klass is None:
            return None
        else:
            return klass(**ctor_args)

    def _load(self, typ, version):
        """Return class for *typ* and *version*."""
        classes = self._get_type_dict()
        try:
            lst = classes[typ]
            dist = lst[0]
            groups = lst[1]
            klass = dist.load_entry_point(groups[0], typ)
            if version is not None and dist.version != version:
                return None
            return klass
        except KeyError:
            if self._search_path is None:
                return None
            # try to look in the whole environment
            for group in self._groups:
                for proj in self.env:
                    for dist in self.env[proj]:
                        if version is not None and version != dist.version:
                            continue
                        ep = dist.get_entry_info(group, typ)
                        if ep is not None:
                            dist.activate()
                            klass = ep.load(require=True, env=self.env)
                            self._have_new_types = True
                            return klass
                        if version is None:
                            # newest version didn't have entry point, so skip to next project
                            break
        return None

    def _entry_map_info(self, distiter):
        dct = {}
        for group in plugin_groups.keys():
            for dist in distiter:
                for name, value in dist.get_entry_map(group).items():
                    lst = dct.setdefault(name, (dist, [], set()))
                    lst[1].append(group)
                    lst[2].add(value.module_name)
        return dct

    def _get_type_dict(self):
        if self._have_new_types:
            self._entry_pt_classes = self._entry_map_info(working_set)
        return self._entry_pt_classes

    def _get_meta_info(self, typ_list, groups, typ_dict):
        distset = set()
        for name, lst in typ_dict.items():
            dist = lst[0]
            modules = lst[2]
            distset.add(dist.project_name)
            ifaces = set()
            for g in lst[1]:
                ifaces.update(plugin_groups[g])

            meta = {
                'version': dist.version,
                'ifaces': set(ifaces),
            }

            for modname in modules:
                fpath = find_module(modname)
                if fpath is not None:
                    fanalyzer = self.tree_analyser.analyze_file(fpath, use_cache=True)
                    meta['bases'] = fanalyzer.classes[name].bases
                    meta['ifaces'].update(fanalyzer.classes[name].meta['ifaces'])

            meta['ifaces'] = list(meta['ifaces'])
            if groups.intersection(lst[1]):
                typ_list.append((name, meta))
        self.tree_analyser.flush_cache()
#.........这里部分代码省略.........
开发者ID:hitej,项目名称:meta-core,代码行数:103,代码来源:pkg_res_factory.py


注:本文中的openmdao.util.dep.PythonSourceTreeAnalyser.analyze_file方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。