実行環境

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(中垣健志)

実行環境

ここで議論されてるすべてのものは正しく動きますが、代わりに、第13章で説明するコンポーネントを使用してアプリケーションを構築することをお勧めします。

While everything discussed here works fine, we recommend instead building your application using components, as described in chapter 13.

web2pyのモデルとコントローラのファイルはPythonのimport文を使用してインポートできないという点でPythonモジュールではありません。この理由は、モデルとコントローラが、準備された環境で実行されるように設計されているためです。その環境ではweb2pyのグローバルオブジェクト((request、response、session、cache、T)とヘルパー関数が予め投入されています。これは、Pythonが静的(構文)スコープの言語であるために必要です。そのとき、web2pyの環境は動的に作られます。

web2py model and controller files are not Python modules in that they cannot be imported using the Python import statement. The reason for this is that models and controllers are designed to be executed in a prepared environment that has been pre-populated with web2py global objects (request, response, session, cache and T) and helper functions. This is necessary because Python is a statically (lexically) scoped language, whereas the web2py environment is created dynamically.

web2py はexec_environment関数を用意して、モデルとコントローラに直接アクセスすることを可能にしています。exec_environment は、web2pyの実行環境を作り出し、ファイルをそれにロードし、その環境を格納するStorageオブジェクトを返します。Storageオブジェクトはまた、名前空間の機構として機能します。この実行環境で実行されるように設計された任意のPythonファイルは、exec_environmentを使って呼び出すことができます。exec_environmentには次のような利用方法があります:

web2py provides the exec_environment function to allow you to access models and controllers directly. exec_environment creates a web2py execution environment, loads the file into it and then returns a Storage object containing the environment. The Storage object also serves as a namespace mechanism. Any Python file designed to be executed in the execution environment can be loaded using exec_environment. Uses for exec_environment include:

  • 他のアプリケーションからのデータ(モデル)にアクセスします。

  • Accessing data (models) from other applications.

  • 他のモデルやコントローラからグローバルオブジェクトにアクセスします。

  • Accessing global objects from other models or controllers.

  • 他のコントローラからコントローラの関数を実行します。

  • Executing controller functions from other controllers.

  • サイト全体のヘルパーライブラリをロードします。

  • Loading site-wide helper libraries.

この例では、casアプリケーションのuserテーブルから、行が読まれています:

This example reads rows from the user table in the cas application:

1.

2.

3.

from gluon.shell import exec_environment

cas = exec_environment('applications/cas/models/db.py')

rows = cas.db().select(cas.db.user.ALL)

もう1つの例:次のコードを含む"other.py"コントローラを想定します:

Another example: suppose you have a controller "other.py" that contains:

1.

2.

def some_action():

return dict(remote_addr=request.env.remote_addr)

このアクションを他のコントローラから(またはweb2pyのシェルから)呼び出す方法は以下の通りです:

Here is how you can call this action from another controller (or from the web2py shell):

1.

2.

3.

from gluon.shell import exec_environment

other = exec_environment('applications/app/controllers/other.py', request=request)

result = other.some_action()

2行目の、request=requestは省略可能です。これは、現在のリクエストを"other"の環境に渡すことをもたらします。この引数がないと、その環境は、新規の空の(ただしrequest.folderを除く)リクエストオブジェクトを含むようになります。レスポンスやセッションオブジェクトもexec_environmentに渡すことが可能です。なお、リクエスト、レスポンス、セッションオブジェクトを渡すときは注意してください。呼び出されたアクションによる修正や、呼び出されたアクションにおけるコードの依存性は、予期せぬ副作用につながる可能性があります。

In line 2, request=request is optional. It has the effect of passing the current request to the environment of "other". Without this argument, the environment would contain a new and empty (apart from request.folder) request object. It is also possible to pass a response and a session object to exec_environment. Be careful when passing request, response and session objects --- modification by the called action or coding dependencies in the called action could lead to unexpected side effects.

3行目で呼ぶ関数は、ビューを実行しません。つまり、"some_action"で明示的にresponse.renderを呼ばない限り、単純に辞書を返します。

The function call in line 3 does not execute the view; it simply returns the dictionary unlessresponse.render is called explicitly by "some_action".

最後の注意:exec_environmentを不適切に使用しないでください。他のアプリケーションのアクションの結果がほしい場合、おそらくXML-RPC APIを実装すべきです(web2pyでXML-RPC APIを実装するのは容易でしょう)。そして、exec_environmentをリダイレクトの仕組みとして使用しないでください。代わりにredirectヘルパーを使用してください。

One final caution: don't use exec_environment inappropriately. If you want the results of actions in another application, you probably should implement an XML-RPC API (implementing an XML-RPC API with web2py is almost trivial). Don't use exec_environmentas a redirection mechanism; use the redirect helper.