From a76d28e9fd048e3dcfc4fc15ca84abaf94c9d3e2 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Tue, 18 Jul 2017 07:56:15 +0400 Subject: [PATCH] webpy: chargement automatique de la configuration depuis server.conf --- lib/pyulib/devel/spp | 18 ++---------- lib/pyulib/src/ulib/web/pages.py | 38 +++++++++++++++++++++---- lib/ulib/templates/webpyapp/server.conf | 10 +++++++ lib/ulib/templates/webpyapp/server.py | 3 +- 4 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 lib/ulib/templates/webpyapp/server.conf mode change 100644 => 100755 lib/ulib/templates/webpyapp/server.py diff --git a/lib/pyulib/devel/spp b/lib/pyulib/devel/spp index 74d958e..4dc38e9 100755 --- a/lib/pyulib/devel/spp +++ b/lib/pyulib/devel/spp @@ -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[*]}\"" diff --git a/lib/pyulib/src/ulib/web/pages.py b/lib/pyulib/src/ulib/web/pages.py index b5da14e..2ad6dab 100644 --- a/lib/pyulib/src/ulib/web/pages.py +++ b/lib/pyulib/src/ulib/web/pages.py @@ -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'' % 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) diff --git a/lib/ulib/templates/webpyapp/server.conf b/lib/ulib/templates/webpyapp/server.conf new file mode 100644 index 0000000..f1aa96d --- /dev/null +++ b/lib/ulib/templates/webpyapp/server.conf @@ -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 diff --git a/lib/ulib/templates/webpyapp/server.py b/lib/ulib/templates/webpyapp/server.py old mode 100644 new mode 100755 index e736a40..bf23959 --- a/lib/ulib/templates/webpyapp/server.py +++ b/lib/ulib/templates/webpyapp/server.py @@ -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)