Pyramid1.6的新功能

本文解释了 Pyramid 1.6版与其前代版本相比, Pyramid 1.5。它还记录了两个版本之间的向后不兼容以及添加到 Pyramid 1.6,以及软件依赖性更改和显著的文档添加。

向后不兼容

  • IPython和BPython支持已经从核心的pshell中移除。要继续在Pyramid1.6+上使用它们,必须显式安装绑定包。一种方法是添加 pyramid_ipython (或) pyramid_bpythoninstall_requires 包的一部分 setup.py 文件,然后重新运行 setup.py develop ::

    setup(
        #...
        install_requires=[
            'pyramid_ipython',         # new dependency
            'pyramid',
            #...
        ],
    )
    
  • request.response 当使用 render_to_response() 应用程序编程接口。现在有必要通过 response= 参数 render_to_response() 如果要为渲染器提供自定义响应对象。如果您没有通过一个,那么将使用当前响应工厂创建响应对象。几乎所有渲染器都会改变 request.response 响应对象(例如,JSON渲染器集 request.response.content_typeapplication/json )但是,当调用 render_to_response ,不期望返回的响应对象与稍后在请求中使用的响应对象相同。从返回的响应对象 render_to_response 现在明显不同于 request.response . 这不会更改渲染器的API。参见https://github.com/pylons/pyramid/pull/1563

  • 为了解决一个共同的问题,它现在是 ConfigurationError 注册一个在使用默认视图映射器时实际上是未绑定方法的视图可调用。由于PY3中不存在未绑定的方法,因此通过检查第一个参数是否已命名,可以检测到可能的错误。 self . 例如, config.add_view(ViewClass.some_method, ...) 实际上应该是 config.add_view(ViewClass, attr='some_method)' . 在PY2上,这一直是Pyramid中的一个问题,但向后不兼容性在PY3+上,在PY3+上,不能使用名为 self . 在这种情况下,它看起来太像一个常见错误,将引发异常。参见https://github.com/pylons/pyramid/pull/1498

功能添加

  • python 3.5和pypy3的兼容性。

  • pserve --reload 不再因语法错误而崩溃。参见https://github.com/pylons/pyramid/pull/2044

  • 静态资源的缓存总线已添加,可以通过新的 pyramid.config.Configurator.add_cache_buster() 应用程序编程接口。核心API通过查询字符串和资产清单(用于集成到自定义资产管道中)来提供缓存总线。参见https://github.com/pylons/pyramid/pull/1380和https://github.com/pylons/pyramid/pull/1583和https://github.com/pylons/pyramid/pull/2171

  • 当使用 override_asset() 应用程序编程接口。这使得在仍然能够使用 static_url() API和 add_static_view() . 以前不可能使用 add_static_view() 有绝对路径 and 生成内容的URL。此更改将取代呼叫, config.add_static_view('/abs/path', 'static')config.add_static_view('myapp:static', 'static')config.override_asset(to_override='myapp:static/', override_with='/abs/path/') . 这个 myapp:static 资产规格是完全组成的,不需要存在,它用于通过 request.static_url('myapp:static/foo.png') . 参见https://github.com/pylons/pyramid/pull/1252

  • 补充 set_response_factory() 以及 response_factory 的构造函数的关键字参数 Configurator 用于定义将返回自定义 Response 类。参见https://github.com/pylons/pyramid/pull/1499

  • 补充 pyramid.config.Configurator.root_package 属性和init参数,以帮助包含的包解析与创建配置程序的包相关的资源。这对于需要从设置中加载资产规格的附加组件尤其有用,在这种情况下,开发人员可以自然地定义相对于顶级包的导入或资产。参见https://github.com/pylons/pyramid/pull/1337

  • 整体改善 proutes 命令。补充 --format--glob 命令的参数,引入了 method 用于显示可用请求方法的列,并改进了 view 通过显示模块而不是仅显示 __repr__ . 参见https://github.com/pylons/pyramid/pull/1488

  • pserve 现在可以采取 -b--browser 在Web浏览器中打开服务器URL的选项。参见https://github.com/pylons/pyramid/pull/1533

  • 在python 3的视图中只支持关键字参数和函数注释。参见https://github.com/pylons/pyramid/pull/1556

  • 这个 append_slash 的参数 add_notfound_view() 现在将接受实现 IResponse 接口,并将其用作响应类,而不是默认的 HTTPFound . 参见https://github.com/pylons/pyramid/pull/1610

  • 这个 Configurator 已经增强了允许操作在提交周期内调用其他操作的能力。这使得更多的逻辑可以放在行动中,例如调用其他行动或将其分组以改进冲突检测的能力。我们还公开并记录了Pyramid使用的配置阶段,以便进一步帮助构建符合要求的附加组件。参见https://github.com/pylons/pyramid/pull/1513

  • 允许从呈现器返回迭代器。以前只能返回字节或Unicode。参见https://github.com/pylons/pyramid/pull/1417

  • 提高在 AuthTktCookieHelper 以及 SignedCookieSessionFactory 通过使用stdlib的 hmac.compare_digest 如果可用(如python 2.7.7+和3.3+)。参见https://github.com/pylons/pyramid/pull/1457

  • 提高 pcreate shell脚本输出。参见https://github.com/pylons/pyramid/pull/1453

  • 简化定义 notfoundforbidden 希望使用默认异常响应视图,但谓词和其他配置选项已更改的视图。这个 view 参数现在是可选的 add_notfound_view()add_forbidden_view() 参见https://github.com/pylons/pyramid/issues/494

  • 这个 pshell 脚本现在将加载 PYTHONSTARTUP 文件(如果在启动解释器之前在环境中定义了一个)。参见https://github.com/pylons/pyramid/pull/1448

  • 为状态代码添加新的HTTP异常对象 428 Precondition Required429 Too Many Requests431 Request Header Fields Too Large 在里面 pyramid.httpexceptions . 请参阅https://github.com/pylons/pyramid/pull/1372/files

  • pcreate 如果不使用scaffold参数运行,现在将打印缺少标志的信息以及可用scaffold的列表。参见https://github.com/pylons/pyramid/pull/1566和https://github.com/pylons/pyramid/issues/1297

  • pcreate 如果使用当前环境中已存在或可导入的项目名称的参数调用,现在将要求确认。请参阅https://github.com/pylons/pyramid/issues/1357和https://github.com/pylons/pyramid/pull/1837

  • 添加 pyramid.request.apply_request_extensions() 可用于测试以应用通过以下方式配置的任何请求扩展的函数 config.add_request_method . 以前,只有通过Pyramid的路由器才能测试扩展。参见https://github.com/pylons/pyramid/pull/1581

  • 使子类成为可能 pyramid.request.Request 并使用 pyramid.request.Request.add_request.method . 参见https://github.com/pylons/pyramid/issues/1529

  • 附加Shell pshell 现在可以注册为入口点。参见https://github.com/pylons/pyramid/pull/1891和https://github.com/pylons/pyramid/pull/2012

  • 注入的变量 pshell 现在显示文档字符串而不是默认值 str(obj) 如果可能的话。参见https://github.com/pylons/pyramid/pull/1929

废弃

  • 这个 pserve 命令的后台监控功能,以及 --monitor-restart ,已被弃用。这包括 [start,stop,restart,status] 子命令以及 --daemon--stop-daemon--pid-file--status--user--group 旗帜。参见https://github.com/pylons/pyramid/pull/2120和https://github.com/pylons/pyramid/pull/2189和https://github.com/pylons/pyramid/pull/1641

    请在将来使用真正的流程管理器,而不是依赖 pserve 去监控自己。存在许多选项,包括操作系统的服务(如systemd或upstart),以及基于python的解决方案(如circus和supervisor)。

    参见https://github.com/pylons/pyramid/pull/1641和https://github.com/pylons/pyramid/pull/2120

  • 这个 principal 参数 pyramid.security.remember() 重命名为 userid . 使用 principal 因为参数名仍然有效,并且将在接下来的几个版本中继续工作,但是会打印一个拒绝警告。

脚手架增强

  • 在脚手架中的日志格式化程序中添加行号以帮助调试。参见https://github.com/pylons/pyramid/pull/1326

  • 更新脚手架生成机械,返回版本 Pyramid 以及用于脚手架的文件。更新的 starteralchemyzodb 模板具有指向正确版本文档的链接,并反映 Pyramid 用于生成支架。

  • 从模板中删除了非ASCII版权符号,因为这导致脚手架无法生成项目。

文档增强功能

  • 已从快速教程中删除日志配置 ini 文件,除了脚手架和日志相关章节,以避免需要解释得太早。

  • 改进和澄清文件 Pyramid 定义为 principal 和A userid 在它的安全API中。参见https://github.com/pylons/pyramid/pull/1399

  • 将文档移动到 acceptpyramid.config.Configurator.add_view() 不再是谓词列表的一部分。请参阅https://github.com/pylons/pyramid/issues/1391,获取一份声明 not_ 失败了 accept . 与@mcdonc的讨论得出结论,不应将其作为谓词进行记录。请参阅https://github.com/pylons/pyramid/pull/1487了解此公关。

  • 澄清之前暗示的 ISession.invalidate API文档。

  • 添加命令行程序的文档 (p* 脚本)。参见https://github.com/pylons/pyramid/pull/2191