Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

OpenERP 模块动态加载原理及启动代码分析



  • 一般来说我们在编程中,对象定义都是预先定义好的。一些 OOP 语言(包括 Python/Java)允许对象是 自省的(也称为 反射)。即,自省对象能够描述自己:实例属于哪个类?类有哪些祖先?对象可以用哪些方法和属性?自省让处理对象的函数或方法根据传递给函数或方法的对象类型来做决定。即允许对象在运行时动态改变方法成员等属性。

    得益于OpenERP ORM 模型的精巧设计,实际上 OpenERP 运行时也是动态读取模块信息并动态构建对象的。如在模块开发中,继承了 'res.users', 新增一个方法或新增一个字段。在OpenERP 导入该模块后, OpenERP 会马上重构 'res.users' 对象并将新增的方法或字段添加到该对象。那么,OpenERP 是如何做到这点的呢? 让我们从OpenERP 的启动部分开始分析:

    首先,OpenERP 启动相关的服务, 这时并没有建立数据库链接和载入对象

    <br />&nbsp; &nbsp; if not config[&quot;stop_after_init&quot;]:<br />&nbsp; &nbsp; &nbsp; &nbsp; setup_pid_file()<br />&nbsp; &nbsp; &nbsp; &nbsp; # Some module register themselves when they are loaded so we need the<br />&nbsp; &nbsp; &nbsp; &nbsp; # services to be running before loading any registry.<br />&nbsp; &nbsp; &nbsp; &nbsp; if config&#91;&#039;workers&#039;]:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openerp.service.start_services_workers()<br />&nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openerp.service.start_services()<br />
    




    不过可以在配置文件中指定 'db_name' 参数,可以让 OpenERP 在启动时加载指定数据库的对象并启动 Cron。 实际生产环境中建议启用该参数,否则需要在启动OpenERP后,登录一次OpenERP 在会加载对象并启动CRON


    <br />&nbsp; &nbsp; if config&#91;&#039;db_name&#039;]:<br />&nbsp; &nbsp; &nbsp; &nbsp; for dbname in config&#91;&#039;db_name&#039;].split(&#039;,&#039;):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; preload_registry(dbname)<br />
    



    不指定 'db_name' 参数情况下,OpenERP 会直到用户登录时才会初始化指定数据库。

    <br />def login(db, login, password):<br />&nbsp; &nbsp; pool = pooler.get_pool(db)<br />&nbsp; &nbsp; user_obj = pool.get(&#039;res.users&#039;)<br />&nbsp; &nbsp; return user_obj.login(db, login, password)<br />
    



    打开 get_db 和 get_db_and_pool 定义

    <br />def get_db(db_name):<br />&nbsp; &nbsp; &quot;&quot;&quot;Return a database connection. The corresponding registry is initialized.&quot;&quot;&quot;<br />&nbsp; &nbsp; return get_db_and_pool(db_name)[0]<br /><br /><br />def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False):<br />&nbsp; &nbsp; &quot;&quot;&quot;Create and return a database connection and a newly initialized registry.&quot;&quot;&quot;<br />&nbsp; &nbsp; registry = RegistryManager.get(db_name, force_demo, status, update_module)<br />&nbsp; &nbsp; return registry.db, registry<br /><br />
    



    顺藤摸瓜,看RegistryManager.new 定义

    <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # This should be a method on Registry<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openerp.modules.load_modules(registry.db, force_demo, status, update_module)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except Exception:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; del cls.registries[db_name]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise<br />
    



    然后看到 load_modules, 终于要加载了,嘿嘿。

    <br /><br />&nbsp; &nbsp; &nbsp; &nbsp; # STEP 1: LOAD BASE (must be done before module dependencies can be computed for later steps) <br />&nbsp; &nbsp; &nbsp; &nbsp; graph = openerp.modules.graph.Graph()<br />&nbsp; &nbsp; &nbsp; &nbsp; graph.add_module(cr, &#039;base&#039;, force)<br />&nbsp; &nbsp; &nbsp; &nbsp; if not graph:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _logger.critical(&#039;module base cannot be loaded! (hint: verify addons-path)&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise osv.osv.except_osv(_(&#039;Could not load base module&#039;), _(&#039;module base cannot be loaded! (hint: verify addons-path)&#039;))<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; # processed_modules: for cleanup step after install<br />&nbsp; &nbsp; &nbsp; &nbsp; # loaded_modules: to avoid double loading<br />&nbsp; &nbsp; &nbsp; &nbsp; report = pool._assertion_report<br />&nbsp; &nbsp; &nbsp; &nbsp; loaded_modules, processed_modules = load_module_graph(cr, graph, status, perform_checks=update_module, report=report)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; if tools.config&#91;&#039;load_language&#039;]:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for lang in tools.config&#91;&#039;load_language&#039;].split(&#039;,&#039;):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tools.load_language(cr, lang)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; # STEP 2: Mark other modules to be loaded/updated<br />&nbsp; &nbsp; &nbsp; &nbsp; if update_module:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modobj = pool.get(&#039;ir.module.module&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (&#039;base&#039; in tools.config&#91;&#039;init&#039;]) or (&#039;base&#039; in tools.config&#91;&#039;update&#039;]):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _logger.info(&#039;updating modules list&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modobj.update_list(cr, SUPERUSER_ID)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _check_module_names(cr, itertools.chain(tools.config&#91;&#039;init&#039;].keys(), tools.config&#91;&#039;update&#039;].keys()))<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mods = [k for k in tools.config&#91;&#039;init&#039;] if tools.config&#91;&#039;init&#039;][k]]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if mods:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ids = modobj.search(cr, SUPERUSER_ID, &#91;&#039;&amp;&#039;, (&#039;state&#039;, &#039;=&#039;, &#039;uninstalled&#039;), (&#039;name&#039;, &#039;in&#039;, mods)])<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ids:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modobj.button_install(cr, SUPERUSER_ID, ids)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mods = [k for k in tools.config&#91;&#039;update&#039;] if tools.config&#91;&#039;update&#039;][k]]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if mods:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ids = modobj.search(cr, SUPERUSER_ID, &#91;&#039;&amp;&#039;, (&#039;state&#039;, &#039;=&#039;, &#039;installed&#039;), (&#039;name&#039;, &#039;in&#039;, mods)])<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ids:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modobj.button_upgrade(cr, SUPERUSER_ID, ids)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cr.execute(&quot;update ir_module_module set state=%s where name=%s&quot;, (&#039;installed&#039;, &#039;base&#039;))<br /><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; # STEP 3: Load marked modules (skipping base which was done in STEP 1)<br />&nbsp; &nbsp; &nbsp; &nbsp; # IMPORTANT: this is done in two parts, first loading all installed or<br />&nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; partially installed modules (i.e. installed/to upgrade), to<br />&nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offer a consistent system to the second part: installing<br />&nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newly selected modules.<br />&nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; We include the modules &#039;to remove&#039; in the first step, because<br />&nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; they are part of the &quot;currently installed&quot; modules. They will<br />&nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; be dropped in STEP 6 later, before restarting the loading<br />&nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process.<br />&nbsp; &nbsp; &nbsp; &nbsp; states_to_load = &#91;&#039;installed&#039;, &#039;to upgrade&#039;, &#039;to remove&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; processed = load_marked_modules(cr, graph, states_to_load, force, status, report, loaded_modules, update_module)<br />&nbsp; &nbsp; &nbsp; &nbsp; processed_modules.extend(processed)<br />&nbsp; &nbsp; &nbsp; &nbsp; if update_module:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; states_to_load = &#91;&#039;to install&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processed = load_marked_modules(cr, graph, states_to_load, force, status, report, loaded_modules, update_module)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processed_modules.extend(processed)<br /><br />
    



    这里,第一步是加载核心模块 ['base'],第二步加载需要升级或预载的模块,第三步加载已安装的模块。实际加载语句是:

    <br />&nbsp; &nbsp; &nbsp; &nbsp; processed = load_marked_modules(cr, graph, states_to_load, force, status, report, loaded_modules, update_module)<br />
    



    查看 load_marked_module 定义:

    <br />def load_marked_modules(cr, graph, states, force, progressdict, report, loaded_modules, perform_checks):<br />&nbsp; &nbsp; &quot;&quot;&quot;Loads modules marked with ``states``, adding them to ``graph`` and<br />&nbsp; &nbsp; &nbsp;  ``loaded_modules`` and returns a list of installed/upgraded modules.&quot;&quot;&quot;<br />&nbsp; &nbsp; processed_modules = &#91;]<br />&nbsp; &nbsp; while True:<br />&nbsp; &nbsp; &nbsp; &nbsp; cr.execute(&quot;SELECT name from ir_module_module WHERE state IN %s&quot; ,(tuple(states),))<br />&nbsp; &nbsp; &nbsp; &nbsp; module_list = [name for (name,) in cr.fetchall() if name not in graph]<br />&nbsp; &nbsp; &nbsp; &nbsp; graph.add_modules(cr, module_list, force)<br />&nbsp; &nbsp; &nbsp; &nbsp; _logger.debug(&#039;Updating graph with %d more modules&#039;, len(module_list))<br />&nbsp; &nbsp; &nbsp; &nbsp; loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks)<br />&nbsp; &nbsp; &nbsp; &nbsp; processed_modules.extend(processed)<br />&nbsp; &nbsp; &nbsp; &nbsp; loaded_modules.extend(loaded)<br />&nbsp; &nbsp; &nbsp; &nbsp; if not processed: break<br />&nbsp; &nbsp; return processed_modules<br />
    



    重点是 load_module_graph

    <br /><br />&nbsp; &nbsp; # register, instantiate and initialize models for each modules<br />&nbsp; &nbsp; for index, package in enumerate(graph):<br />&nbsp; &nbsp; &nbsp; &nbsp; module_name = package.name<br />&nbsp; &nbsp; &nbsp; &nbsp; module_id = package.id<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; if skip_modules and module_name in skip_modules:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; _logger.debug(&#039;module %s: loading objects&#039;, package.name)<br />&nbsp; &nbsp; &nbsp; &nbsp; migrations.migrate_module(package, &#039;pre&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; load_openerp_module(package.name)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; models = pool.load(cr, package)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; loaded_modules.append(package.name)<br />&nbsp; &nbsp; &nbsp; &nbsp; if hasattr(package, &#039;init&#039;) or hasattr(package, &#039;update&#039;) or package.state in (&#039;to install&#039;, &#039;to upgrade&#039;):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; init_module_models(cr, package.name, models)<br />&nbsp; &nbsp; &nbsp; &nbsp; pool._init_modules.add(package.name)<br />&nbsp; &nbsp; &nbsp; &nbsp; status&#91;&#039;progress&#039;] = float(index) / len(graph)<br /><br />
    



    主要是下面2句,

    <br />&nbsp; &nbsp; &nbsp; &nbsp; load_openerp_module(package.name)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; models = pool.load(cr, package)<br /><br />
    



    先看 load_openerp_module

    <br />&nbsp; &nbsp; initialize_sys_path()<br />&nbsp; &nbsp; try:<br />&nbsp; &nbsp; &nbsp; &nbsp; mod_path = get_module_path(module_name)<br />&nbsp; &nbsp; &nbsp; &nbsp; zip_mod_path = &#039;&#039; if not mod_path else mod_path + &#039;.zip&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; if not os.path.isfile(zip_mod_path):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; __import__(&#039;openerp.addons.&#039; + module_name)<br />&nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zimp = zipimport.zipimporter(zip_mod_path)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zimp.load_module(module_name)<br />
    



    上面代码中 import 了一个模块。如果您看过 digitalsatori 校长的大作 OpenERP与Python 元编程, 下面就涉及到元类了:

    import 的时候 就会调用元类的构造函数

    <br />&nbsp; &nbsp; def __init__(self, name, bases, attrs):<br />&nbsp; &nbsp; &nbsp; &nbsp; if not self._register:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self._register = True<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(MetaModel, self).__init__(name, bases, attrs)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; # The (OpenERP) module name can be in the `openerp.addons` namespace<br />&nbsp; &nbsp; &nbsp; &nbsp; # or not. For instance module `sale` can be imported as<br />&nbsp; &nbsp; &nbsp; &nbsp; # `openerp.addons.sale` (the good way) or `sale` (for backward<br />&nbsp; &nbsp; &nbsp; &nbsp; # compatibility).<br />&nbsp; &nbsp; &nbsp; &nbsp; module_parts = self.__module__.split(&#039;.&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; if len(module_parts) &gt; 2 and module_parts[0] == &#039;openerp&#039; and \<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; module_parts[1] == &#039;addons&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; module_name = self.__module__.split(&#039;.&#039;)[2]<br />&nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; module_name = self.__module__.split(&#039;.&#039;)[0]<br />&nbsp; &nbsp; &nbsp; &nbsp; if not hasattr(self, &#039;_module&#039;):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self._module = module_name<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; # Remember which models to instanciate for this module.<br />&nbsp; &nbsp; &nbsp; &nbsp; if not self._custom:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.module_to_models.setdefault(self._module, &#91;]).append(self)<br /><br />
    



    上面的代码基本上就是将自身类加入到 module_to_models 字典中。

    然后我们来看pool.load

    <br />&nbsp; &nbsp; def load(self, cr, module):<br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot; Load a given module in the registry.<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; At the Python level, the modules are already loaded, but not yet on a<br />&nbsp; &nbsp; &nbsp; &nbsp; per-registry level. This method populates a registry with the given<br />&nbsp; &nbsp; &nbsp; &nbsp; modules, i.e. it instanciates all the classes of a the given module<br />&nbsp; &nbsp; &nbsp; &nbsp; and registers them in the registry.<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; models_to_load = &#91;] # need to preserve loading order<br />&nbsp; &nbsp; &nbsp; &nbsp; # Instantiate registered classes (via the MetaModel automatic discovery<br />&nbsp; &nbsp; &nbsp; &nbsp; # or via explicit constructor call), and add them to the pool.<br />&nbsp; &nbsp; &nbsp; &nbsp; for cls in openerp.osv.orm.MetaModel.module_to_models.get(module.name, &#91;]):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # models register themselves in self.models<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model = cls.create_instance(self, cr)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if model._name not in models_to_load:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # avoid double-loading models whose declaration is split<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; models_to_load.append(model._name)<br />&nbsp; &nbsp; &nbsp; &nbsp; return [self.models[m] for m in models_to_load]<br /><br />
    



    这里我们可以看到 MetaModel 的身影,cls.create_instance(self, cr) 这里就是动态构造对象的核心代码。

    <br /><br />&nbsp; &nbsp; &nbsp; &nbsp; parent_names = getattr(cls, &#039;_inherit&#039;, None)<br />&nbsp; &nbsp; &nbsp; &nbsp; # 判断是否有继承父类 <br />&nbsp; &nbsp; &nbsp; &nbsp; if parent_names:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if isinstance(parent_names, (str, unicode)):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = cls._name or parent_names<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent_names = [parent_names]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = cls._name<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not name:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise TypeError(&#039;_name is mandatory in case of multiple inheritance&#039;)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for parent_name in ((type(parent_names)==list) and parent_names or [parent_names]):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 读取父类<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent_model = pool.get(parent_name)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not parent_model:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise TypeError(&#039;The model &quot;%s&quot; specifies an unexisting parent class &quot;%s&quot;\n&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;You may need to add a dependency on the parent class\&#039; module.&#039; % (name, parent_name))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not getattr(cls, &#039;_original_module&#039;, None) and name == parent_model._name:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls._original_module = parent_model._original_module<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent_class = parent_model.__class__<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nattr = {}<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 复制父类属性<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for s in attributes:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new = copy.copy(getattr(parent_model, s, {}))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if s == &#039;_columns&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Don&#039;t _inherit custom fields.<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for c in new.keys():<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if new[c].manual:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; del new[c]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Duplicate float fields because they have a .digits<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # cache (which must be per-registry, not server-wide).<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for c in new.keys():<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if new[c]._type == &#039;float&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new[c] = copy.copy(new[c])<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if hasattr(new, &#039;update&#039;):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new.update(cls.__dict__.get(s, {}))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif s==&#039;_constraints&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for c in cls.__dict__.get(s, &#91;]):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exist = False<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for c2 in range(len(new)):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #For _constraints, we should check field and methods as well<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if new[c2][2]==c[2] and (new[c2][0] == c[0] \<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; or getattr(new[c2][0],&#039;__name__&#039;, True) == \<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getattr(c[0],&#039;__name__&#039;, False)):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # If new class defines a constraint with<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # same function name, we let it override<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # the old one.<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new[c2] = c<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exist = True<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not exist:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new.append(c)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new.extend(cls.__dict__.get(s, &#91;]))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nattr[s] = new<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Keep links to non-inherited constraints, e.g. useful when exporting translations<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nattr&#91;&#039;_local_constraints&#039;] = cls.__dict__.get(&#039;_constraints&#039;, &#91;])<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nattr&#91;&#039;_local_sql_constraints&#039;] = cls.__dict__.get(&#039;_sql_constraints&#039;, &#91;])<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 调用元类构造函数<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = type(name, (cls, parent_class), dict(nattr, _register=False))<br />&nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls._local_constraints = getattr(cls, &#039;_constraints&#039;, &#91;])<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls._local_sql_constraints = getattr(cls, &#039;_sql_constraints&#039;, &#91;])<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; if not getattr(cls, &#039;_original_module&#039;, None):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls._original_module = cls._module<br />&nbsp; &nbsp; &nbsp; &nbsp; # 构造对象<br />&nbsp; &nbsp; &nbsp; &nbsp; obj = object.__new__(cls)<br />&nbsp; &nbsp; &nbsp; &nbsp; # 初始化对象<br />&nbsp; &nbsp; &nbsp; &nbsp; obj.__init__(pool, cr)<br />&nbsp; &nbsp; &nbsp; &nbsp; return obj<br />
    




    上面代码很重要,可以看到首先是判断该对象是否有继承父类,如果没有就直接构造,和动态没有什么关系。

    如果有继承父类, 就复制父类属性, 这里就是动态构建类的做法。

    假如有不同模块,都继承了同一个父类,那么如何保证类成员和属性是否加载完整或覆盖呢? 答案在于这句代码:

    <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent_model = pool.get(parent_name)<br />
    



    Registry.get 的定义

    <br />&nbsp; &nbsp; def get(self, model_name):<br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot; Return a model for a given name or None if it doesn&#039;t exist.&quot;&quot;&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; return self.models.get(model_name)<br />
    



    最后看看obj.init(pool, cr)初始化对象,做了什么动作?

    <br />&nbsp; &nbsp; def __init__(self, pool, cr):<br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot; Initialize a model and make it part of the given registry.<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; - copy the stored fields&#039; functions in the osv_pool,<br />&nbsp; &nbsp; &nbsp; &nbsp; - update the _columns with the fields found in ir_model_fields,<br />&nbsp; &nbsp; &nbsp; &nbsp; - ensure there is a many2one for each _inherits&#039;d parent,<br />&nbsp; &nbsp; &nbsp; &nbsp; - update the children&#039;s _columns,<br />&nbsp; &nbsp; &nbsp; &nbsp; - give a chance to each field to initialize itself.<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; pool.add(self._name, self)<br />&nbsp; &nbsp; &nbsp; &nbsp; self.pool = pool<br />
    



    pool.add(self._name, self) 定义如下:

    <br />&nbsp; &nbsp; def add(self, model_name, model):<br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot; Add or replace a model in the registry.&quot;&quot;&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; self.models[model_name] = model<br />
    



    到这里应该很非常清楚,Registry.models 保存了对象的 model 信息。这样多个对象继承同一父类时,按照加载顺序先后动态构建相关的类。

    至此,OpenERP 启动时动态加载模块分析完成。如模块安装、升级、卸载等, 则是通过 signal_registry_change 和 check_registry_signaling 处理,重新载入 Registry, 然后重新构建 OpenERP 对象。

    转自 http://buke.github.com/blog/2013/02/26/openerp-dynamic-loading-and-booting-way/ br />



  • 收藏..............



  • 马克..........



  • 前些日子 群里 总监讨论的问题 , 就是 两个 模块继承了 同一个 model 但是 没有super的情况,

    最后到底 哪个模块 会生效 ,,

    应该学这篇文章可以分析出来吧



  • mark



  • 前半部分还能勉强看懂,后部分越看越迷糊



  • 马克



  • 学习了!