[filter,printing/templates] Import de jinja2 et pyparsing paresseux

Comme ça, pas besoin d'installer les lib correspondante là où on ne fait pas
des input/output utilisateur.
This commit is contained in:
Valentin Samir 2013-11-20 16:26:58 +01:00
parent 702f941469
commit 99b9f8bc12
2 changed files with 34 additions and 22 deletions

View file

@ -1,8 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
"""Plus rapide"""
import pyparsing
def simplify(l):
if not isinstance(l, list):
return l
@ -57,12 +56,19 @@ def toldapfilter(l):
return op + ''.join(['(%s)' % toldapfilter(i) for i in l[1:]])
txt = "".join(c for c in pyparsing.printables if c not in '()&|!=')
expr = pyparsing.nestedExpr("(", ")", pyparsing.Word(txt) | pyparsing.oneOf("& | != ="))
# Import et definition de la grammaire de parsing de façon paresseuse
expr=None
def pypexpr():
global expr
if not expr:
import pyparsing
txt = "".join(c for c in pyparsing.printables if c not in '()&|!=')
expr = pyparsing.nestedExpr("(", ")", pyparsing.Word(txt) | pyparsing.oneOf("& | != ="))
return expr
def human_to_list(data):
if data:
return collapse(toprefix(prioritize(expr.parseString("(%s)" % data).asList())))
return collapse(toprefix(prioritize(pypexpr().parseString("(%s)" % data).asList())))
def human_to_ldap(data):
return "(%s)" % toldapfilter(human_to_list(data))

View file

@ -1,11 +1,6 @@
#! /usr/bin/env python
#!/bin/bash /usr/scripts/python.sh
# -*- coding: utf-8 -*-
import sys
sys.path.append('/usr/scripts/')
import jinja2
from gestion.affich_tools import coul
from lc_ldap import objets
import time
import gestion.whos
@ -32,18 +27,26 @@ def blacklist(l):
def split(str, *arg):
return str.split(*arg)
template_path = '/usr/scripts/lc_ldap/printing/templates/'
templateLoader = jinja2.FileSystemLoader( searchpath=["/", template_path] )
templateEnv = jinja2.Environment( loader=templateLoader, trim_blocks=True )
templateEnv.filters['coul'] = coul
templateEnv.filters['blacklist'] = blacklist
templateEnv.filters['prise_etat'] = prise_etat
templateEnv.filters['timeformat'] = timeformat
templateEnv.filters['split'] = split
templateEnv=None
def template():
global templateEnv
if not templateEnv:
# un import paresseux, comme ça, pas la peine d'installer jinja2 sur les machines où il n'y en a pas besoin
import jinja2
template_path = '/usr/scripts/lc_ldap/printing/templates/'
templateLoader = jinja2.FileSystemLoader( searchpath=["/", template_path] )
templateEnv = jinja2.Environment( loader=templateLoader, trim_blocks=True )
templateEnv.filters['coul'] = coul
templateEnv.filters['blacklist'] = blacklist
templateEnv.filters['prise_etat'] = prise_etat
templateEnv.filters['timeformat'] = timeformat
templateEnv.filters['split'] = split
return templateEnv
def machine(machine, params):
params['o']=machine
return templateEnv.get_template("machine").render(params)
return template().get_template("machine").render(params)
def proprio(proprio, params):
params['o']=proprio
@ -61,14 +64,15 @@ def proprio(proprio, params):
def club(club, params):
params=proprio(club, params)
return templateEnv.get_template("club").render(params)
return template().get_template("club").render(params)
def adherent(adherent, params):
params=proprio(adherent, params)
return templateEnv.get_template("adherent").render(params)
return template().get_template("adherent").render(params)
def sprint(object, limit=5):
params={'limit':limit}
from lc_ldap import objets
if isinstance(object, objets.machine):
return machine(object, params)
elif isinstance(object, objets.adherent):
@ -77,3 +81,5 @@ def sprint(object, limit=5):
return club(object, params)
else:
return str(object)