源码版本:H版

nova/api/openstack/__init__.py

APIRouter类:
def __init__(self, ext_mgr=None, init_only=None):
if ext_mgr is None:
if self.ExtensionManager:
ext_mgr = self.ExtensionManager()
else:
raise Exception(_("Must specify an ExtensionManager class"))
...

其中的ExtensionManager由子类nova/api/openstack/compute/__init__.py中的APIRouter指定为nova/api/openstack/compute/extensions.py中的ExtensionManager类,下面看其构造函数:

class ExtensionManager(base_extensions.ExtensionManager):
def __init__(self):
LOG.audit(_('Initializing extension manager.'))
self.cls_list = CONF.osapi_compute_extension
self.extensions = {}
self.sorted_ext_list = []
self._load_extensions()

其中self.cls_list是扩展包的引导类列表,此处为[nova.api.openstack.compute.contrib.standard_extensions](注意只有一个元素)。接着看self._load_extensions()的内容,由于ExtensionManager的类继承关系,该函数为nova.api.openstack. extensions ExtensionManager的函数,内容如下:

def _load_extensions(self):
extensions = list(self.cls_list)
for ext_factory in extensions:
try:
self.load_extension(ext_factory)
except Exception as exc:
LOG.warn(_('Failed to load extension %(ext_factory)s: '
'%(exc)s'),
{'ext_factory': ext_factory, 'exc': exc})

其中self.load_extension函数内容如下:

def load_extension(self, ext_factory):
LOG.debug(_("Loading extension %s"), ext_factory)
if isinstance(ext_factory, basestring):
factory = importutils.import_class(ext_factory)
else:
factory = ext_factory
LOG.debug(_("Calling extension factory %s"), ext_factory)
factory(self)

最终结果就是调用nova.api.openstack.compute.contrib.standard_extensions。接下来看standard_extensions函数,内容如下:

def standard_extensions(ext_mgr):
extensions.load_standard_extensions(ext_mgr, LOG, __path__, __package__)

其中extensions为nova.api.openstack.extensions,看load_standard_extensions内容如下:

def load_standard_extensions(ext_mgr, logger, path, package, ext_list=None):
our_dir = path[0]
for dirpath, dirnames, filenames in os.walk(our_dir):
relpath = os.path.relpath(dirpath, our_dir)
if relpath == '.':
relpkg = ''
else:
relpkg = '.%s' % '.'.join(relpath.split(os.sep))
for fname in filenames:
root, ext = os.path.splitext(fname)
if ext != '.py' or root == '__init__':
continue
classname = "%s%s" % (root[0].upper(), root[1:])
classpath = ("%s%s.%s.%s" %
(package, relpkg, root, classname))
if ext_list is not None and classname not in ext_list:
logger.debug("Skipping extension: %s" % classpath)
continue
try:
ext_mgr.load_extension(classpath)
except Exception as exc:
logger.warn(_('Failed to load extension %(classpath)s: '
'%(exc)s'),
{'classpath': classpath, 'exc': exc})
...

实际上也就是调用之前ExtensionManager的 load_extension函数加载模块,并构造模块中与模块名对应的类,由于这些类都是nova.api.openstack.extensions.ExtensionDescriptor的子类,且看其__init__函数:

def __init__(self, ext_mgr):
ext_mgr.register(self)
self.ext_mgr = ext_mgr

  可知构造这些扩展类会在ExtensionManager中进行注册,注册过程就是在ExtensionManag中将扩展类的实例存入ExtensionManager的extensions列表中,对应的key为扩展类的alias。

  综上所述,ExtensionManager的构造就是对其中的self.cls_list进行加载调用,self.cls_list包含扩展包的引导类或引导函数,此处为nova.api.openstack.compute.contrib.standard_extensions,该引导函数负责加载扩展包中模块,并构造相应的类实例,这些类实例在构造时会向ExtensionManager进行注册。

nova-api中ExtensionManager的构造的更多相关文章

  1. Entity Framework 6 Recipes 2nd Edition(9-3)译->找出Web API中发生了什么变化

    9-3. 找出Web API中发生了什么变化 问题 想通过基于REST的Web API服务对数据库进行插入,删除和修改对象图,而不必为每个实体类编写单独的更新方法. 此外, 用EF6的Code Fri ...

  2. Web Api中的get传值和post传值

    GET 方式 get方式传参 我们一般用于获取数据做条件筛选,也就是 “查” 1.无参 var look = function () { $.ajax({ type: "GET", ...

  3. 【ASP.NET Web API教程】4.3 ASP.NET Web API中的异常处理

    原文:[ASP.NET Web API教程]4.3 ASP.NET Web API中的异常处理 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内 ...

  4. ASP.NET Web API中的JSON和XML序列化

    ASP.NET Web API中的JSON和XML序列化 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok ...

  5. 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持

    HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...

  6. Asp.Net Web Api中使用Swagger

    关于swagger 设计是API开发的基础.Swagger使API设计变得轻而易举,为开发人员.架构师和产品所有者提供了易于使用的工具. 官方网址:https://swagger.io/solutio ...

  7. Java基础学习总结(67)——Java接口API中使用数组的缺陷

    如果你发现在一个接口使用有如下定义方法: public String[] getParameters(); 那么你应该认真反思.数组不仅仅老式,而且我们有合理的理由避免暴露它们.在这篇文章中,我将试图 ...

  8. openstack nova 源码解析 — Nova API 执行过程从(novaclient到Action)

    目录 目录 Nova API Nova API 的执行过程 novaclient 将 Commands 转换为标准的HTTP请求 PasteDeploy 将 HTTP 请求路由到具体的 WSGI Ap ...

  9. 在View and Data API中更改指定元素的颜色

    大家在使用View and Data API开发过程中,经常会用到的就是改变某些元素的颜色已区别显示.比如根据某些属性做不同颜色的专题显示,或者用不同颜色表示施工进度,或者只是简单的以颜色变化来提醒用 ...

随机推荐

  1. FileInputStream与FileOutputStream练习题 -------------------图片拷贝

    package com.outputstream; import java.io.File; import java.io.FileInputStream; import java.io.FileNo ...

  2. 论文《Network in Network》笔记

    论文:Lin M, Chen Q, Yan S. Network In Network[J]. Computer Science, 2013. 参考:关于CNN中1×1卷积核和Network in N ...

  3. js如何判断一个值是不是Array类型

    本来判断一个对象类型用typeof是最好的,不过对于Array类型是不适用的可以使用 instanceof操作符var arrayStr=new Array("1","2 ...

  4. #Leetcode# 817. Linked List Components

    https://leetcode.com/problems/linked-list-components/ We are given head, the head node of a linked l ...

  5. HDU 2078 复习时间

    http://acm.hdu.edu.cn/showproblem.php?pid=2078 Problem Description 为了能过个好年,xhd开始复习了,于是每天晚上背着书往教室跑.xh ...

  6. HDU 2107 Founding of HDU

    http://acm.hdu.edu.cn/showproblem.php?pid=2107 Problem Description 经过慎重的考虑,XHD,8600, LL,Linle以及RPG等A ...

  7. 关于对JSON.parse()与JSON.stringify()的理解

    JSON.parse()与JSON.stringify()的区别   JSON.parse()[从一个字符串中解析出json对象] 例子: //定义一个字符串 var data='{"nam ...

  8. set_magic_quotes_runtime

    在php的配置文件中,有个布尔值的设置,就是magic_quotes_runtime,当它打开时,php的大部分函数自动的给从外部引入的(包括数据库或者文件)数据中的溢出字符加上反斜线. 当然如果重复 ...

  9. [微软官网]SQLSERVER的版本信息

    来源:https://docs.microsoft.com/zh-cn/sql/sql-server/editions-and-components-of-sql-server-2017?view=s ...

  10. Nginx和Apache区别

    Nginx VS  Apache 相同点: 1.都是HTTP服务器软件 2.功能上都采用模块化结构设计 3.都支持通用的语言接口,如PHP.Perl.Python等 4.支持正向.反向代理.虚拟主机. ...