2011-09-27 5 views
15

각 피라미드 응용 프로그램에는 관련 설정이 포함 된 .ini 파일이 있습니다. 거기에 자신의 구성 값을 추가 할 수 있습니다 만약 내가 궁금피라미드 및 .ini 구성

[app:main] 
use = egg:MyProject 
pyramid.reload_templates = true 
pyramid.debug_authorization = false 
pyramid.debug_notfound = false 
pyramid.debug_routematch = false 
... 

및 (주로보기 호출에서) 실행시에 읽을 예를 들어, 기본처럼 보일 수 있습니다. 예를 들어, 별도의 .ini 파일을 가지고 시작하는 동안 그것을 구문 분석 나는

[app:main] 
blog.title = "Custom blog name" 
blog.comments_enabled = true 
... 

을 할 수도 아니면 더 나은 무엇입니까?

답변

26

물론 가능합니다.

진입 점 기능 (main(global_config, **settings)은 대부분 __init__.py)에서 settings 변수에 액세스 할 수 있습니다. 당신의 .ini 예를 들어

:

[app:main] 
blog.title = "Custom blog name" 
blog.comments_enabled = true 

당신의 __init__.py에서 :

def main(global_config, **settings): 
    config = Configurator(settings=settings) 
    blog_title = settings['blog.title'] 
    # you can also access you settings via config 
    comments_enabled = config.registry.settings['blog.comments_enabled'] 
    return config.make_wsgi_app() 

latest Pyramid docs에 따르면, 당신은 request.registry.settings를 통해보기 기능의 설정에 액세스 할 수 있습니다. 또한, 내가 아는 한, event.request.registry.settings을 통해 이벤트 구독자가 될 것입니다.

다른 파일 사용에 관한 질문에 대해서는, 내가했던 것과 같이 점선으로 된 표기법을 사용하여 모든 설정을 일반 init 파일에 저장하는 것이 좋습니다.