[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
16
filter2.py
16
filter2.py
|
@ -1,8 +1,7 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf8 -*-
|
||||||
"""Plus rapide"""
|
"""Plus rapide"""
|
||||||
|
|
||||||
import pyparsing
|
|
||||||
|
|
||||||
def simplify(l):
|
def simplify(l):
|
||||||
if not isinstance(l, list):
|
if not isinstance(l, list):
|
||||||
return l
|
return l
|
||||||
|
@ -57,12 +56,19 @@ def toldapfilter(l):
|
||||||
return op + ''.join(['(%s)' % toldapfilter(i) for i in l[1:]])
|
return op + ''.join(['(%s)' % toldapfilter(i) for i in l[1:]])
|
||||||
|
|
||||||
|
|
||||||
txt = "".join(c for c in pyparsing.printables if c not in '()&|!=')
|
# Import et definition de la grammaire de parsing de façon paresseuse
|
||||||
expr = pyparsing.nestedExpr("(", ")", pyparsing.Word(txt) | pyparsing.oneOf("& | != ="))
|
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):
|
def human_to_list(data):
|
||||||
if 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):
|
def human_to_ldap(data):
|
||||||
return "(%s)" % toldapfilter(human_to_list(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 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import sys
|
|
||||||
sys.path.append('/usr/scripts/')
|
|
||||||
import jinja2
|
|
||||||
|
|
||||||
from gestion.affich_tools import coul
|
from gestion.affich_tools import coul
|
||||||
from lc_ldap import objets
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import gestion.whos
|
import gestion.whos
|
||||||
|
@ -32,18 +27,26 @@ def blacklist(l):
|
||||||
def split(str, *arg):
|
def split(str, *arg):
|
||||||
return str.split(*arg)
|
return str.split(*arg)
|
||||||
|
|
||||||
template_path = '/usr/scripts/lc_ldap/printing/templates/'
|
|
||||||
templateLoader = jinja2.FileSystemLoader( searchpath=["/", template_path] )
|
templateEnv=None
|
||||||
templateEnv = jinja2.Environment( loader=templateLoader, trim_blocks=True )
|
def template():
|
||||||
templateEnv.filters['coul'] = coul
|
global templateEnv
|
||||||
templateEnv.filters['blacklist'] = blacklist
|
if not templateEnv:
|
||||||
templateEnv.filters['prise_etat'] = prise_etat
|
# un import paresseux, comme ça, pas la peine d'installer jinja2 sur les machines où il n'y en a pas besoin
|
||||||
templateEnv.filters['timeformat'] = timeformat
|
import jinja2
|
||||||
templateEnv.filters['split'] = split
|
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):
|
def machine(machine, params):
|
||||||
params['o']=machine
|
params['o']=machine
|
||||||
return templateEnv.get_template("machine").render(params)
|
return template().get_template("machine").render(params)
|
||||||
|
|
||||||
def proprio(proprio, params):
|
def proprio(proprio, params):
|
||||||
params['o']=proprio
|
params['o']=proprio
|
||||||
|
@ -61,14 +64,15 @@ def proprio(proprio, params):
|
||||||
|
|
||||||
def club(club, params):
|
def club(club, params):
|
||||||
params=proprio(club, params)
|
params=proprio(club, params)
|
||||||
return templateEnv.get_template("club").render(params)
|
return template().get_template("club").render(params)
|
||||||
|
|
||||||
def adherent(adherent, params):
|
def adherent(adherent, params):
|
||||||
params=proprio(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):
|
def sprint(object, limit=5):
|
||||||
params={'limit':limit}
|
params={'limit':limit}
|
||||||
|
from lc_ldap import objets
|
||||||
if isinstance(object, objets.machine):
|
if isinstance(object, objets.machine):
|
||||||
return machine(object, params)
|
return machine(object, params)
|
||||||
elif isinstance(object, objets.adherent):
|
elif isinstance(object, objets.adherent):
|
||||||
|
@ -77,3 +81,5 @@ def sprint(object, limit=5):
|
||||||
return club(object, params)
|
return club(object, params)
|
||||||
else:
|
else:
|
||||||
return str(object)
|
return str(object)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue