webpyapp: les variables de server.conf sont configurables

This commit is contained in:
Jephté Clain 2017-07-20 01:30:56 +04:00
parent d1a1d4d664
commit 0b39b2324b
1 changed files with 19 additions and 12 deletions

View File

@ -13,7 +13,7 @@ from ulib.base.args import get_args
from ulib.base.dates import rfc2822
from ulib.base.functions import apply_args
from ulib.base.words import plural
from ulib.formats import unicodeF, booleanF, integerF, floatF
from ulib.formats import unicodeF, strF, booleanF, integerF, floatF
from ulib.ext import web
def checked(b):
@ -307,8 +307,13 @@ class Page(object):
def index(self): return self.render()
class ShConfig(dict):
def __init__(self, config, **defaults):
super(ShConfig, self).__init__(**defaults)
_formats = None
def __init__(self, config, formats):
super(ShConfig, self).__init__()
self._formats = formats
for name in formats.keys():
self[name] = None
inf = open(config, 'rb')
try: s = inf.read()
finally: inf.close()
@ -321,7 +326,8 @@ class ShConfig(dict):
mo = self.RE_NAMEVALUE.match(part)
if mo is None: continue #XXX ignorer pour le moment
name, value = mo.groups()
self[name] = value
format = self._formats.get(name, unicodeF)
self[name] = format.parse(value)
class Application(object):
# si ce fichier existe dans basedir, alors forcer le mode développement: la
@ -329,7 +335,8 @@ class Application(object):
DEVEL_SF = '.devel'
# nom du fichier de configuration à charger automatiquement à partir de
# basedir. pour désactiver cette fonctionnalité, utiliser la valeur None
CONFIG = 'server.conf'
CONFIG_FILE = 'server.conf'
CONFIG_VARS = dict(HOST=strF, PORT=integerF, DEBUG=booleanF)
# configuration par défaut
HOST = '0.0.0.0'
PORT = 12345
@ -371,14 +378,14 @@ class Application(object):
devel = path.exists(path.join(self.basedir, self.DEVEL_SF))
if devel: einfo("Mode developpement activé")
if self.CONFIG is not None:
config = path.join(self.basedir, self.CONFIG)
if self.CONFIG_FILE is not None:
config = path.join(self.basedir, self.CONFIG_FILE)
if path.exists(config):
c = ShConfig(config, HOST=None, PORT=None, DEBUG=None)
if host is None and not devel: host = c['HOST']
if port is None: port = integerF.parse((c['PORT']))
if debug is None: debug = booleanF.parse(c['DEBUG'])
if debug is None and devel: debug = True
c = ShConfig(config, self.CONFIG_VARS)
for name, value in c.items():
if devel and name == 'HOST': continue
if devel and name == 'DEBUG' and value is None: value = True
if value is not None: setattr(self, name, value)
if host is not None: self.HOST = host
if port is not None: self.PORT = port