使用python中的云本机应用程序包的模块
cnab的Python项目详细描述
python cnab库
work-in-progress用于在python中使用CNAB的库。
CNAB客户可能有三个主要的兴趣领域:
索赔和签字是可选的,但一旦上述情况稳定,将继续进行。
安装
模块发布在PyPi上,可以从那里安装。
pip install cnab
解析bundle.json
这里没有什么特别之处,Bundle
类有一个from_dict
静态方法
生成完整的Bundle
对象。
importjsonfromcnabimportBundlewithopen("bundle.json")asf:data=json.load(f)bundle=Bundle.from_dict(data)
例如,这可以用于验证目的,或者为bundle.json
文件构建用户界面。
用python描述bundle.json
您还可以用python描述bundle.json
文件。这将正确地验证
基于当前规范的结构,允许构建自定义DSL或其他
生成bundle.json
文件的用户界面。
fromcnabimportBundle,InvocationImagebundle=Bundle(name="hello",version="0.1.0",invocation_images=[InvocationImage(image_type="docker",image="technosophos/helloworld:0.1.0",digest="sha256:aaaaaaa...",)],)print(bundle.to_json())
运行CNABS
该模块支持使用docker
驱动程序在cnab上运行操作。
fromcnabimportCNAB# The first argument can be a path to a bundle.json file, a dictionary# or a full `Bundle` objectapp=CNAB("fixtures/helloworld/bundle.json")# list available actionsprint(app.actions)# list available parametersprint(app.parameter)# run the install actionprint(app.run("install"))# run the install action specifying a parametersprint(app.run("install",parameters={"port":9090}))# Many applications will require credentialsapp=CNAB("fixtures/hellohelm/bundle.json")# list required credentialsprint(app.credentials)# Here we pass the value for the required credential# in this case by reading the existing configuration from diskwithopen("/home/garethr/.kube/config")asf:print(app.run("status",credentials={"kubeconfig":f.read()}))
请注意,这方面的错误处理正在进行中。
使用调用图像
pycnab
还有一个用于处理调用映像的类。
fromcnabimportCNABDirectorydirectory=CNABDirectory("fixtures/invocationimage")# Check whether the directory is valid# Raises `InvalidCNABDirectory` exception if invaliddirectory.valid()# Returns the text of the associated README file if presentdirectory.readme()# Returns the text of the associated LICENSE file if presentdirectory.license()
谢谢
感谢QuickType的引导,创建了基于当前json模式操作bundle.json
的python代码。