webpy: chargement automatique de la configuration depuis server.conf
This commit is contained in:
parent
070f5bfff6
commit
a76d28e9fd
|
@ -1,7 +1,7 @@
|
|||
#!/bin/bash
|
||||
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
||||
|
||||
source "$(dirname "$0")/../../ulib/ulib" && urequire DEFAULTS || exit 1
|
||||
source "$(dirname "$0")/../../ulib/ulib" || exit 1
|
||||
urequire DEFAULTS
|
||||
pyulibdir="$(abspath "$scriptdir/..")"
|
||||
utoolsdir="$(abspath "$scriptdir/../..")"
|
||||
|
||||
|
@ -18,18 +18,6 @@ while [ -n "$1" ]; do
|
|||
done
|
||||
[ -n "$default" ] && show_pc=1 && show_path=1 && show_ppath=1
|
||||
|
||||
function quote_pc() {
|
||||
local s
|
||||
s="${1//\\/\\\\}"
|
||||
s="${s//\"/\\\"}"
|
||||
s="${s//$/\\$}"
|
||||
s="${s//then
|
||||
/then }"
|
||||
s="${s//
|
||||
/; }"
|
||||
echo "$s"
|
||||
}
|
||||
|
||||
srcdir="$pyulibdir/src"
|
||||
testdir="$pyulibdir/test"
|
||||
|
||||
|
@ -53,7 +41,7 @@ array_del ppath "$testdir"
|
|||
array_ins ppath "$testdir"
|
||||
array_ins ppath "$srcdir"
|
||||
|
||||
[ -n "$show_pc" -a -n "$new_pc" ] && echo "export PROMPT_COMMAND=\"$(quote_pc "$new_pc")\""
|
||||
[ -n "$show_pc" -a -n "$new_pc" ] && echo "export PROMPT_COMMAND=$(qval "$new_pc")"
|
||||
IFS=:
|
||||
[ -n "$show_path" ] && echo "export PATH=\"${path[*]}\""
|
||||
[ -n "$show_ppath" ] && echo "export PYTHONPATH=\"${ppath[*]}\""
|
||||
|
|
|
@ -3,7 +3,7 @@ import ulib.base.i_need_py24
|
|||
|
||||
__all__ = ('nocache', 'auth', 'defaults', 'Page', 'Application')
|
||||
|
||||
import os, sys, base64, logging
|
||||
import os, sys, base64, logging, re, shlex
|
||||
from os import path
|
||||
|
||||
from ulib.base.base import isstr, isbool, isnum, isflt, seqof, make_prop
|
||||
|
@ -18,6 +18,8 @@ from ulib.ext import web
|
|||
|
||||
def checked(b):
|
||||
return b and u'checked="checked"' or u''
|
||||
def selected(b):
|
||||
return b and u'selected="selected"' or u''
|
||||
|
||||
def favicon(href):
|
||||
return ur'<link rel="shortcut icon" href="%s" />' % href
|
||||
|
@ -58,6 +60,7 @@ def blueprintcss(prefix=None, suppl=True):
|
|||
])
|
||||
DEFAULT_TEMPLATE_GLOBALS = {'_u': _u,
|
||||
'checked': checked,
|
||||
'selected': selected,
|
||||
'favicon': favicon,
|
||||
'css': css,
|
||||
'js': js,
|
||||
|
@ -272,7 +275,25 @@ class Page(object):
|
|||
def error(self): raise web.notfound()
|
||||
def index(self): return self.render()
|
||||
|
||||
class ShConfig(dict):
|
||||
def __init__(self, config, **defaults):
|
||||
super(ShConfig, self).__init__(**defaults)
|
||||
inf = open(config, 'rb')
|
||||
try: s = inf.read()
|
||||
finally: inf.close()
|
||||
parts = shlex.split(s, True)
|
||||
self.parse(parts)
|
||||
|
||||
RE_NAMEVALUE = re.compile(r'([^=]+)=(.*)')
|
||||
def parse(self, parts):
|
||||
for part in parts:
|
||||
mo = self.RE_NAMEVALUE.match(part)
|
||||
if mo is None: continue #XXX ignorer pour le moment
|
||||
name, value = mo.groups()
|
||||
self[name] = value
|
||||
|
||||
class Application(object):
|
||||
CONFIG = 'server.conf'
|
||||
HOST = '0.0.0.0'
|
||||
PORT = 12345
|
||||
DEBUG = False
|
||||
|
@ -293,10 +314,6 @@ class Application(object):
|
|||
_msgs, msgs = make_prop('_msgs')[:2]
|
||||
|
||||
def __init__(self, basedir=None, templatedir=None, host=None, port=None, debug=None):
|
||||
if host is not None: self.HOST = host
|
||||
if port is not None: self.PORT = port
|
||||
if debug is not None: self.DEBUG = debug
|
||||
|
||||
if self.basedir is None or basedir is not None:
|
||||
if basedir is None:
|
||||
# par défaut, basedir est répertoire qui contient la classe
|
||||
|
@ -314,6 +331,17 @@ class Application(object):
|
|||
templatedir = path.join(self.basedir, 'templates')
|
||||
self.templatedir = templatedir
|
||||
|
||||
config = path.join(self.basedir, self.CONFIG)
|
||||
if path.exists(config):
|
||||
c = ShConfig(config, HOST=None, PORT=None, DEBUG=None)
|
||||
if host is None: host = c['HOST']
|
||||
if port is None: port = integerF.parse((c['PORT']))
|
||||
if debug is None: debug = booleanF.parse(c['DEBUG'])
|
||||
|
||||
if host is not None: self.HOST = host
|
||||
if port is not None: self.PORT = port
|
||||
if debug is not None: self.DEBUG = debug
|
||||
|
||||
tg = self.template_globals
|
||||
if tg is None: tg = {}
|
||||
tg.update(DEFAULT_TEMPLATE_GLOBALS)
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
||||
|
||||
# adresse d'écoute
|
||||
#HOST=0.0.0.0
|
||||
|
||||
# port d'écoute
|
||||
#PORT=12345
|
||||
|
||||
# faut-il activer le mode debug?
|
||||
#DEBUG=1
|
|
@ -1,3 +1,4 @@
|
|||
#!/usr/bin/env python2
|
||||
# -*- coding: utf-8 mode: python -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
||||
|
||||
__all__ = ('Server',)
|
||||
|
@ -8,8 +9,6 @@ except: True, False = 1, 0
|
|||
from ulib.web import web, Application, Page, defaults
|
||||
|
||||
class Server(Application):
|
||||
PORT = 3000
|
||||
|
||||
def __init__(self, basedir=None, templatedir=None, host=None, port=None, debug=None):
|
||||
Application.__init__(self, basedir, templatedir, host, port, debug)
|
||||
|
||||
|
|
Loading…
Reference in New Issue