带自动完成和数据类的boto3
autoboto的Python项目详细描述
安装
pip install autoboto
简介
- 项目状态:alpha。在探索时,您应该仅将其用于特殊查询 美国焊接学会。它是为那些大致知道自己想从aws获得什么,但不想 上下滚动长(和好)BOTO3文档页面,找出 正确的方法和参数名称。我们正处于21世纪,对于面向用户的代码, 自动完成应该可以。
- 所有响应对象都是数据类。
- 所有响应对象都有一个response_metadata,这是一个未修改的字典 通常在响应字典的ResponseMetadata键下返回。
- 传递嵌套对象不像传递嵌套字典那么容易,但它有好处。
- 方法名与boto3中的相同,但参数名已从^{tt3}更改$ 到snake_case。
- boto3不从botocore生成的自定义方法(例如,s3.upload_file 只是委托给Boto3客户端,目前没有可用的文档。 参数名与原始方法中的相同。
fromautoboto.servicesimports3s3_client=s3.Client()forbucketins3_client.list_buckets().buckets:print(bucket.name)forobjins3_client.list_objects_v2(bucket_name=bucket.name).contents:print(f" - {obj.key}")
您也可以分页:
forpageins3_client.list_objects_v2(bucket_name=bucket.name).paginate():forobjinpage.contents:print(f" - {obj.key}")
代码生成
您不需要阅读本节。这是关于如何生成autoboto代码。
当您从pypi.org安装autoboto时,该包已经包含为所有服务生成的代码 BOTO3支持的。
如果要重新生成代码,可以使用包含的botogen执行此操作。
代码生成包括导入生成的模块和实例化生成的客户机类。 为此,您需要AWS_PROFILE环境变量指向一个aws配置文件 已设置region。
python -m botogen --services s3,cloudformation,lambda
成分
- autoboto-包,生成的代码放在发布之前。不要手动添加任何内容。 此目录中的所有文件都可能被覆盖。
- botogen-负责生成autoboto的代码
- botogen.autoboto_template-此包的内容最终会出现在生成的autoboto包中。
- .gitignore文件位于botogen/autoboto_template下,指示git忽略目录中的所有文件。 这样就不会将autoboto包中生成的代码添加到版本控制中。 因此,在autoboto开发期间,当您向botogen/autoboto_template添加新文件时, 您需要添加-f标志来强制将它们添加到git。
- botogen.indentist-通用python代码生成器
目录结构
build/ All build artifacts are put here release/ Release builds happen here 20180909_135602/ individual release build directory; Added to sys.path autoboto/ generated autoboto package; an augmented copy of botogen/autoboto_complete core/ examples/ services/ __init__.py test/ Test builds happen here 20180909_135330/ individual test build directory; Added to sys.path autoboto_20180909_135330/ generated autoboto package; an augmented copy of botogen/autoboto_complete core/ examples/ services/ __init__.py test-packages/ Target directory for test builds -- where the generated packages are put after successful completion of a build and tests passing on the generated code.
- build_dir–生成所有生成工件的临时目录。在上面的例子中, build/test/20180909_135330和build/release/20180909_135602是生成目录。
- target_package–生成的目标包的名称;用于生成代码的import语句中。 autoboto在生成版本时;autoboto_{timestamp}在测试中。
- target_dir–放置目标包的目录。 默认情况下它是当前目录,但在测试中它是build/test-packages。
注释
不要在生成代码的测试中使用来自botogen.autoboto_template的任何导入 因为生成的代码不是从那里导入的。相反,使用专用夹具 (具有讽刺意味的是,自动完成功能不起作用)。
测试
tests目录包含单元测试和集成测试。对于单元测试,您不需要aws帐户。
pytest tests/unit
要跨多个python版本运行测试,请使用tox。要运行tox,需要首先生成 autoboto包,否则它将失败。
如果您使用的是pyenv和virtualenvs,请不要在virtualenv中运行tox。