[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:
parent
702f941469
commit
99b9f8bc12
2 changed files with 34 additions and 22 deletions
12
filter2.py
12
filter2.py
|
@ -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:]])
|
||||
|
||||
|
||||
# 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))
|
||||
|
|
|
@ -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,6 +27,13 @@ def blacklist(l):
|
|||
def split(str, *arg):
|
||||
return str.split(*arg)
|
||||
|
||||
|
||||
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 )
|
||||
|
@ -40,10 +42,11 @@ 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)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue