init()¶
这是一个用于初始化 conanfile 值的可选方法,专为从 python_requires
继承而设计。假设我们有一个 base/1.1
配方
from conan import ConanFile
class MyConanfileBase:
license = "MyLicense"
settings = "os", # tuple!
class PyReq(ConanFile):
name = "base"
version = "1.1"
package_type = "python-require"
我们可以通过以下方式重用并继承它:
from conan import ConanFile
class Pkg(ConanFile):
license = "MIT"
settings = "arch", # tuple!
python_requires = "base/1.1"
python_requires_extend = "base.MyConanfileBase"
def init(self):
base = self.python_requires["base"].module.MyConanfileBase
self.settings = base.settings + self.settings # Note, adding 2 tuples = tuple
self.license = base.license # License is overwritten
最终的 Pkg
conanfile 将同时具有 os
和 arch
作为设置,以及 MyLicense
作为许可证。
为了扩展基类的 options
,需要调用 self.options.update()
方法
from conan import ConanFile
class BaseConan:
options = {"base": [True, False]}
default_options = {"base": True}
class PyReq(ConanFile):
name = "base"
version = "1.0.0"
package_type = "python-require"
当 init()
被调用时,self.options
对象已初始化。因此,更新 self.default_options
是无用的,需要使用基类的选项和基类的默认选项值来更新 self.options
from conan import ConanFile
class DerivedConan(ConanFile):
name = "derived"
python_requires = "base/1.0.0"
python_requires_extend = "base.BaseConan"
options = {"derived": [True, False]}
default_options = {"derived": False}
def init(self):
base = self.python_requires["base"].module.BaseConan
# Note we pass the base options and default_options
self.options.update(base.options, base.default_options)
如果需要无条件初始化类属性,例如 license
或 description
或任何其他来自非 conandata.yml 数据文件的属性,此方法也很有用。例如,您可以有一个 json 文件,其中包含有关库的 license
、description
和 author
信息
{"license": "MIT", "description": "This is my awesome library.", "author": "Me"}
然后,您可以从 init()
方法加载该信息
import os
import json
from conan import ConanFile
from conan.tools.files import load
class Pkg(ConanFile):
exports = "data.json" # Important that it is exported with the recipe
def init(self):
data = load(self, os.path.join(self.recipe_folder, "data.json"))
d = json.loads(data)
self.license = d["license"]
self.description = d["description"]
self.author = d["author"]
注意
最佳实践
尽量保持您的
python_requires
尽可能简单,不要重用其属性(这是init()
方法的主要需求),尽量避免此init()
方法的复杂性。通常,继承比组合有更多问题(换句话说,“优先使用组合而非继承”是通用的编程最佳实践),因此请尽可能避免使用它。请勿滥用
init()
用于此处列出的目的之外的用途,也不要使用 Python 私有ConanFile.__init__
构造函数。init()
方法在配方加载时执行。它不能包含基于设置、选项、配置的条件,也不能使用除上述python_requires
之外的任何依赖信息。