pytilpack.flask¶
必要なextra
pip install pytilpack[flask]
pytilpack.flask
¶
Flask関連のユーティリティ。
ResponseType = flask.Response | werkzeug.test.TestResponse
module-attribute
¶
レスポンスの型。
RouteInfo
¶
Bases: NamedTuple
ルーティング情報を保持するクラス。
属性:
| 名前 | タイプ | デスクリプション |
|---|---|---|
endpoint |
str
|
エンドポイント名 |
url_parts |
list[str]
|
URLのパーツのリスト |
arg_names |
list[str]
|
URLパーツの引数名のリスト |
ProxyFix(flaskapp, x_for=1, x_proto=1, x_host=0, x_port=0, x_prefix=1)
¶
Bases: ProxyFix
リバースプロキシ対応。
nginx.conf設定例:: proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Port $server_port; proxy_set_header X-Forwarded-Prefix $http_x_forwarded_prefix;
ソースコード位置: pytilpack/flask/proxy_fix.py
assert_bytes(response, status_code=200, content_type=None)
¶
テストコード用。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
response
|
ResponseType
|
レスポンス |
必須 |
status_code
|
int
|
期待するステータスコード |
200
|
content_type
|
str | Iterable[str] | None
|
期待するContent-Type |
None
|
発生:
| タイプ | デスクリプション |
|---|---|
AssertionError
|
ステータスコードが異なる場合 |
戻り値:
| タイプ | デスクリプション |
|---|---|
bytes
|
レスポンスボディ |
ソースコード位置: pytilpack/flask/asserts.py
assert_html(response, status_code=200, content_type='__default__', strict=False, tmp_path=None)
¶
テストコード用。
html5libが必要なので注意。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
response
|
ResponseType
|
レスポンス |
必須 |
status_code
|
int
|
期待するステータスコード |
200
|
content_type
|
str | Iterable[str] | None
|
期待するContent-Type |
'__default__'
|
strict
|
bool
|
Trueの場合、HTML5の仕様に従ったパースを行う |
False
|
tmp_path
|
Path | None
|
一時ファイルを保存するディレクトリ |
None
|
発生:
| タイプ | デスクリプション |
|---|---|
AssertionError
|
ステータスコードが異なる場合 |
戻り値:
| タイプ | デスクリプション |
|---|---|
str
|
レスポンスボディ (bs4.BeautifulSoup) |
ソースコード位置: pytilpack/flask/asserts.py
assert_json(response, status_code=200, content_type='application/json')
¶
テストコード用。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
response
|
ResponseType
|
レスポンス |
必須 |
status_code
|
int
|
期待するステータスコード |
200
|
content_type
|
str | Iterable[str] | None
|
期待するContent-Type |
'application/json'
|
発生:
| タイプ | デスクリプション |
|---|---|
AssertionError
|
ステータスコードが異なる場合 |
戻り値:
| タイプ | デスクリプション |
|---|---|
Any
|
レスポンスのjson |
ソースコード位置: pytilpack/flask/asserts.py
assert_xml(response, status_code=200, content_type='__default__')
¶
テストコード用。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
response
|
ResponseType
|
レスポンス |
必須 |
status_code
|
int
|
期待するステータスコード |
200
|
content_type
|
str | Iterable[str] | None
|
期待するContent-Type |
'__default__'
|
発生:
| タイプ | デスクリプション |
|---|---|
AssertionError
|
ステータスコードが異なる場合 |
戻り値:
| タイプ | デスクリプション |
|---|---|
str
|
レスポンスのxml |
ソースコード位置: pytilpack/flask/asserts.py
assert_sse(response, status_code=200)
¶
テストコード用。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
response
|
ResponseType
|
レスポンス |
必須 |
status_code
|
int
|
期待するステータスコード |
200
|
発生:
| タイプ | デスクリプション |
|---|---|
AssertionError
|
ステータスコードが異なる場合、またはContent-Typeが異なる場合 |
戻り値:
| タイプ | デスクリプション |
|---|---|
ResponseType
|
レスポンス |
ソースコード位置: pytilpack/flask/asserts.py
assert_response(response, status_code=200)
¶
テストコード用。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
response
|
ResponseType
|
レスポンス |
必須 |
status_code
|
int
|
期待するステータスコード |
200
|
発生:
| タイプ | デスクリプション |
|---|---|
AssertionError
|
ステータスコードが異なる場合 |
戻り値:
| タイプ | デスクリプション |
|---|---|
ResponseType
|
レスポンス |
ソースコード位置: pytilpack/flask/asserts.py
check_status_code(status_code, valid_status_code)
¶
Deprecated.
ソースコード位置: pytilpack/flask/asserts.py
check_content_type(content_type, valid_content_types)
¶
Deprecated.
ソースコード位置: pytilpack/flask/asserts.py
generate_secret_key(cache_path)
¶
Deprecated.
ソースコード位置: pytilpack/flask/misc.py
data_url(data, mime_type)
¶
小さい画像などのバイナリデータをURLに埋め込んだものを作って返す。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
data
|
bytes
|
埋め込むデータ |
必須 |
mime_type
|
str
|
例:'image/png' |
必須 |
get_next_url()
¶
ログイン後遷移用のnextパラメータ用のURLを返す。
prefer_markdown()
¶
AcceptヘッダーでmarkdownがHTMLより優先されているかを返す。
参考: https://vercel.com/blog/making-agent-friendly-pages-with-content-negotiation
(CDNやプロキシがAcceptヘッダーを書き換える場合があるという話もあるが…。)
戻り値:
| タイプ | デスクリプション |
|---|---|
bool
|
markdownがHTMLより優先されている場合True、そうでなければFalse |
ソースコード位置: pytilpack/flask/misc.py
static_url_for(filename, cache_busting=True, cache_timestamp='when_not_debug', **kwargs)
¶
静的ファイルのURLを生成する。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
filename
|
str
|
静的ファイルの名前 |
必須 |
cache_busting
|
bool
|
キャッシュバスティングを有効にするかどうか (デフォルト: True) |
True
|
cache_timestamp
|
bool | Literal['when_not_debug']
|
キャッシュバスティングするときのファイルの最終更新日時をプロセス単位でキャッシュするか否か。 - True: プロセス単位でキャッシュする。プロセスの再起動やSIGHUPなどをしない限り更新されない。 - False: キャッシュしない。常に最新を参照する。 - "when_not_debug": デバッグモードでないときのみキャッシュする。 |
'when_not_debug'
|
**kwargs
|
Any
|
flask.url_forに渡す追加の引数 |
{}
|
戻り値:
| タイプ | デスクリプション |
|---|---|
str
|
静的ファイルのURL |
ソースコード位置: pytilpack/flask/misc.py
get_safe_url(target, host_url, default_url)
¶
Deprecated.
ソースコード位置: pytilpack/flask/misc.py
get_routes(app)
¶
ルーティング情報を取得する。
戻り値:
| タイプ | デスクリプション |
|---|---|
list[RouteInfo]
|
ルーティング情報のリスト。 |
ソースコード位置: pytilpack/flask/misc.py
run(app, host='localhost', port=5000)
¶
Flaskアプリを実行するコンテキストマネージャ。テストコードなど用。
ソースコード位置: pytilpack/flask/misc.py
asserts
¶
Flaskのテストコード用アサーション関数。
ResponseType = flask.Response | werkzeug.test.TestResponse
module-attribute
¶
レスポンスの型。
assert_bytes(response, status_code=200, content_type=None)
¶
テストコード用。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
response
|
ResponseType
|
レスポンス |
必須 |
status_code
|
int
|
期待するステータスコード |
200
|
content_type
|
str | Iterable[str] | None
|
期待するContent-Type |
None
|
発生:
| タイプ | デスクリプション |
|---|---|
AssertionError
|
ステータスコードが異なる場合 |
戻り値:
| タイプ | デスクリプション |
|---|---|
bytes
|
レスポンスボディ |
ソースコード位置: pytilpack/flask/asserts.py
assert_html(response, status_code=200, content_type='__default__', strict=False, tmp_path=None)
¶
テストコード用。
html5libが必要なので注意。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
response
|
ResponseType
|
レスポンス |
必須 |
status_code
|
int
|
期待するステータスコード |
200
|
content_type
|
str | Iterable[str] | None
|
期待するContent-Type |
'__default__'
|
strict
|
bool
|
Trueの場合、HTML5の仕様に従ったパースを行う |
False
|
tmp_path
|
Path | None
|
一時ファイルを保存するディレクトリ |
None
|
発生:
| タイプ | デスクリプション |
|---|---|
AssertionError
|
ステータスコードが異なる場合 |
戻り値:
| タイプ | デスクリプション |
|---|---|
str
|
レスポンスボディ (bs4.BeautifulSoup) |
ソースコード位置: pytilpack/flask/asserts.py
assert_json(response, status_code=200, content_type='application/json')
¶
テストコード用。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
response
|
ResponseType
|
レスポンス |
必須 |
status_code
|
int
|
期待するステータスコード |
200
|
content_type
|
str | Iterable[str] | None
|
期待するContent-Type |
'application/json'
|
発生:
| タイプ | デスクリプション |
|---|---|
AssertionError
|
ステータスコードが異なる場合 |
戻り値:
| タイプ | デスクリプション |
|---|---|
Any
|
レスポンスのjson |
ソースコード位置: pytilpack/flask/asserts.py
assert_xml(response, status_code=200, content_type='__default__')
¶
テストコード用。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
response
|
ResponseType
|
レスポンス |
必須 |
status_code
|
int
|
期待するステータスコード |
200
|
content_type
|
str | Iterable[str] | None
|
期待するContent-Type |
'__default__'
|
発生:
| タイプ | デスクリプション |
|---|---|
AssertionError
|
ステータスコードが異なる場合 |
戻り値:
| タイプ | デスクリプション |
|---|---|
str
|
レスポンスのxml |
ソースコード位置: pytilpack/flask/asserts.py
assert_sse(response, status_code=200)
¶
テストコード用。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
response
|
ResponseType
|
レスポンス |
必須 |
status_code
|
int
|
期待するステータスコード |
200
|
発生:
| タイプ | デスクリプション |
|---|---|
AssertionError
|
ステータスコードが異なる場合、またはContent-Typeが異なる場合 |
戻り値:
| タイプ | デスクリプション |
|---|---|
ResponseType
|
レスポンス |
ソースコード位置: pytilpack/flask/asserts.py
assert_response(response, status_code=200)
¶
テストコード用。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
response
|
ResponseType
|
レスポンス |
必須 |
status_code
|
int
|
期待するステータスコード |
200
|
発生:
| タイプ | デスクリプション |
|---|---|
AssertionError
|
ステータスコードが異なる場合 |
戻り値:
| タイプ | デスクリプション |
|---|---|
ResponseType
|
レスポンス |
ソースコード位置: pytilpack/flask/asserts.py
check_status_code(status_code, valid_status_code)
¶
Deprecated.
ソースコード位置: pytilpack/flask/asserts.py
check_content_type(content_type, valid_content_types)
¶
Deprecated.
ソースコード位置: pytilpack/flask/asserts.py
misc
¶
Flask関連のその他のユーティリティ。
RouteInfo
¶
Bases: NamedTuple
ルーティング情報を保持するクラス。
属性:
| 名前 | タイプ | デスクリプション |
|---|---|---|
endpoint |
str
|
エンドポイント名 |
url_parts |
list[str]
|
URLのパーツのリスト |
arg_names |
list[str]
|
URLパーツの引数名のリスト |
generate_secret_key(cache_path)
¶
Deprecated.
ソースコード位置: pytilpack/flask/misc.py
data_url(data, mime_type)
¶
小さい画像などのバイナリデータをURLに埋め込んだものを作って返す。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
data
|
bytes
|
埋め込むデータ |
必須 |
mime_type
|
str
|
例:'image/png' |
必須 |
get_next_url()
¶
ログイン後遷移用のnextパラメータ用のURLを返す。
prefer_markdown()
¶
AcceptヘッダーでmarkdownがHTMLより優先されているかを返す。
参考: https://vercel.com/blog/making-agent-friendly-pages-with-content-negotiation
(CDNやプロキシがAcceptヘッダーを書き換える場合があるという話もあるが…。)
戻り値:
| タイプ | デスクリプション |
|---|---|
bool
|
markdownがHTMLより優先されている場合True、そうでなければFalse |
ソースコード位置: pytilpack/flask/misc.py
static_url_for(filename, cache_busting=True, cache_timestamp='when_not_debug', **kwargs)
¶
静的ファイルのURLを生成する。
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
filename
|
str
|
静的ファイルの名前 |
必須 |
cache_busting
|
bool
|
キャッシュバスティングを有効にするかどうか (デフォルト: True) |
True
|
cache_timestamp
|
bool | Literal['when_not_debug']
|
キャッシュバスティングするときのファイルの最終更新日時をプロセス単位でキャッシュするか否か。 - True: プロセス単位でキャッシュする。プロセスの再起動やSIGHUPなどをしない限り更新されない。 - False: キャッシュしない。常に最新を参照する。 - "when_not_debug": デバッグモードでないときのみキャッシュする。 |
'when_not_debug'
|
**kwargs
|
Any
|
flask.url_forに渡す追加の引数 |
{}
|
戻り値:
| タイプ | デスクリプション |
|---|---|
str
|
静的ファイルのURL |
ソースコード位置: pytilpack/flask/misc.py
get_safe_url(target, host_url, default_url)
¶
Deprecated.
ソースコード位置: pytilpack/flask/misc.py
get_routes(app)
¶
ルーティング情報を取得する。
戻り値:
| タイプ | デスクリプション |
|---|---|
list[RouteInfo]
|
ルーティング情報のリスト。 |
ソースコード位置: pytilpack/flask/misc.py
run(app, host='localhost', port=5000)
¶
Flaskアプリを実行するコンテキストマネージャ。テストコードなど用。
ソースコード位置: pytilpack/flask/misc.py
proxy_fix
¶
リバースプロキシ対応。
ProxyFix(flaskapp, x_for=1, x_proto=1, x_host=0, x_port=0, x_prefix=1)
¶
Bases: ProxyFix
リバースプロキシ対応。
nginx.conf設定例:: proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Port $server_port; proxy_set_header X-Forwarded-Prefix $http_x_forwarded_prefix;