What makes pyramid unique

Post on 03-Jul-2015

3.910 views 1 download

Transcript of What makes pyramid unique

What Makes Pyramid Unique!

Django & Pylons Con 2012aodag

お前誰よ

Atsushi Odagiri

株式会社ビープラウド

http://twitter.com/aodaghttp://facebook.com/aodag

Pyramid 仕事で使ってます

機能追加のパッチを投げましたPythonは1.5から。

Pyramidは、repoze.bfg1.2のころから。

今日のお話

Pyramidって何?

どんな特徴が?実際どんな開発の仕方?

Pyramidとは?

Pylonsプロジェクトの

Webアプリケーションフレームワーク

Simple, Fast, Tested, Documented

No ORM, No Template Engine.

Pyramidができるまで

WSGI Zope

Pylons Repoze

Pyramid

Zopeから受け継いだもの

Zope Component ArchitectureURLトラバーサル

コンテキストベースのセキュリティ機構

Pylonsから受け継いだもの

WebOb, BeakerなどのWSGIコンポーネント

URLパターンでのディスパッチ

PasteScript(ただしPython3対応のために独自コマンドとして取り込まれた)

Pyramidの設計方針

仕組みをたくさん提供するポリシーを押し付けないコンポーネント指向を直接見せないことさらマイクロフレームワークを意識しない

特徴的なしくみ

デコレータベースの設定さまざまなフック例外ビュー

デコレータベースの設定

ユニットテスト中に副作用のあるデコレータ

が実行されると、うっとおしいですよね?

デコレータベースの設定

デコレータでビューなどを登録できる

ただし、いきなり登録するのではなくConfigurator.scan()をトリガーとして、初めて登録される。

さまざまなフック

フレームワークの途中に処理を割り込ませたいこと、

たくさんありますよね?

さまざまなフック

NewRequestTweenBeforeRenderなど

例外ビュー

処理を打ち切って、さっさと

レスポンスを返したいこと、

たくさんありますよね?

例外ビュー

例外クラスごとにそれぞれビューを設定可能

view predicate

同じ処理だけど、Ajaxリクエストと

ブラウザリクエストでテンプレートとか

変更したいよね?

view predicate

namecontextroute_namerequest_typerequest_methodrequest_parammatch_param

xhracceptheaderpath_infocustom_predicate

view predicate

@view_config(.... xhr=True, renderer="json")@view_config(... xhr=False, renderer="index.html")def get_something(request): .... return dict(items=items)

その他いろいろ

シングルトンは存在しない継承を多様しない

開発を助けるコマンド群

pcreatepserve

pshellprequest

proutespviewsptweens

pcreate

プロジェクトテンプレート(Scaffold)を展開する開発開始時の土台を作成するツール

ユーザー定義のプロジェクトテンプレートも作成できる

pserve

Webアプリケーションを実行する

--reloadオプションで、ファイル更新を検地してリロードしてくれる

daemon化やモニタリングしてリスタートさせることも可能

pshell

Webアプリケーション実行時の環境を擬似的に作成して、Pythonシェルを起動する

prequest

Webアプリケーションをオフラインで実行して結果を確認できる

proutes, pviews, ptweens

アプリケーション構成を確認できる。

例えば:● URLパターンが実際にどのViewにマッチするの

か● 具体的なURLに対応するViewはどれか● Tweenの実行順序はどのようになっているのか

よく使うライブラリ、ツール

ZODB3SQLAlchemyzope.sqlalchemydeform/colanderWebHelperscliff

distributezc.buildoutnosealembic

有力なアドオン

pyramid_tmpyramid_deformpyramid_debugtoolbarpyramid_exclogpyramid_zodbconn

Pyramidベースのフレームワーク

SubstanceDKottiCornice

GroundHogMetaTG

Pyramidでの開発

2つのベースアーキテクチャ

ZODB + トラバーサル

SQLAlchemy + URLディスパッチ

ZODB + トラバーサル

URLパターンとかいらない!

URL -> オブジェクトツリー

URLでオブジェクト取得

例:URL : /great/bucho/show

トラバーサル : root['great']['bucho']. showがない?

showという名前のViewを呼ぶ

Pyramidでの開発 環境構築

mkvirtualenv pycon2012easy_install pyramidpcreate -s zodb pycon2012cd pycon2012python setup.py develop

Pyramidでの開発

追加のライブラリなど

pyramid_deformdeform_bootstrapcolanderpillowrepoze.filesaferepoze.folder

setup.pyに依存ライブラリを追加する

install_requires = [..."pyramid_deform","deform_bootstrap","colander","pillow","repoze.filesafe","repoze.folder",

iniファイルにアドオンを追加する

pyramid.includes = .... pyramid_deform deform_bootstrap

Pyramidでの開発

モデルを書く

class Document(Persistent): def __init__(self, name, contents): super(Document, self).__init__() self.__name__ = name self.contents = contents

Pyramidでの開発

ビューを書く

@view_config(context=Document, renderer='templates/document.pt')def document_view(request): return dict()

テンプレートを書く

<html><body><h1>${context.__name__}</h1><div>${structure:context.contents}</div></body></html>

pshellでモデルデータを登録

$ pshell development.ini>>> from pyconjp2012.models import Document>>> doc = Document('test-document', 'this-is-document')>>> root[doc.__name__] = doc>>> doc.__parent__ = root>>> import transaction>>> transaction.commit()

prequestで試してみよう

$ prequest development.ini /test-document<html><body><h1>test-document</h1><div>this-is-document</div></body></html>

pserveでWebアプリケーションを実行

pserve development.ini --reload

ユーザー入力フォーム

スキーマ定義

class AddDocumentSchema(c.Schema): name = c.SchemaNode(c.String()) contents = c.SchemaNode(c.String(), widget=w.RichTextWidget())

ユーザー入力(実装)

class AddDocumentView(FormView): schema = AddDocumentSchema() buttons = ('save',)

def save_success(self, params): doc = Document(**params) self.request.context[doc.__name__] = doc doc.__parent__ = self.request.context return HTTPFound(self.request.resource_url(doc))

pyramid_deform (表示)

${structure:form}

Chameleonのテンプレートマクロ(定義)

<html metal:define-macro="html"><head><!-- いろんなヘッダ -->

</head><body metal:define-slot="main"></body></html>

Chameleonのテンプレートマクロ(利用)

<html metal:use-macro="base.macros.html"><body metal:fill-slot="main">${form}</body></html>

まとめ

使う場合言われるほど面倒ではない難しいこともない

使い切る場合手をいれれられる箇所が多いので、相対的に難しく感じるかも?

(´  > ω < )こわくないよー

Pyramidの今後

Pyramidベースのフレームワークが発展していく

Python3対応?ああ、去年やってましたね。

テンプレートの標準サポートをなくす方針

Pyramidの今後

管理アプリが欲しい!今世界中のPylonsどもが実装している。