Bookmark and Share
Document Actions

Riferimenti Python
medio

Up one level
Nel precedente capitolo "Configurazione" molto spesso si è parlato di attributi pyref. Cerchiamo di capire più nel dettaglio come usarli a dovere.

Nel precedente capitolo "Configurazione" molto spesso si è parlato di attributi pyref. Questi si riferiscono al nome di un modulo o funzione. La pattern generale è:

pyref="location:function_name"

location può essere il nome di un modulo, oppure file:/path/to/filename.py. Se si tratta di un nome file letterale, allora il file può essere esuguito e trasformato in un modulo in questo modo. Tutti i riferimenti sono diretti a funzioni (o oggetti esigibili), quindi è necessario dare un nome alla funzione. Nota bene: il caso file: non è una URL, ma solo un path.

pyarg-foo="bar"

É possibile passare ulteriori argomenti ad hoc alle funzioni usando gli attributi in questa forma. Essa aggiungerebbe la parola chiave dell'argomento foo="bar" alla funzione di chiamata. Tutti gli argomenti hanno valori di stringa (unicode).

Esempi

É possibile usare il tag <theme> in questo modo:

<theme pyref="file:/home/me/src/customize-deliverance.py:get_theme"
       pyarg-default_theme="default"
       pyarg-base="/home/me/src/custom-themes"
       pyarg-base_url="/custom-themes" />

<proxy path="/custom-themes">
    <dest href="file:///home/me/src/custom-themes" />
</proxy>

Il codice dovrebbe quindi avere questo aspetto:

def get_theme(request, response, log, default_theme,
              base, base_url):
    host = request.host.split(':')[0]
    if os.path.exists(os.path.join(base, host, 'theme.html'):
        return base_url + '/' + host + '/theme.html'
    elif not default_theme:
        log.fatal(None,
                  "Theme for host %r doesn't exist and no default theme was given"
                  % host)
        raise AbortTheme("No theme found")
    else:
        log.debug(None,
                  "Falling back to default theme")
        return base_url + '/' + default_theme + '/theme.html'

Disabilitare

Per disabilitare i riferimenti Python usare deliverance-proxy con:

<server-settings>
  <execute-pyref>false</execute-pyref>
</server-settings>
by Dario Pollino last modified 2009-07-15 11:44