diff --git a/filter2.py b/filter2.py index 7f30d68..a3302a1 100644 --- a/filter2.py +++ b/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:]]) -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)) diff --git a/printing/templates.py b/printing/templates.py index ba912bd..e4e9b43 100644 --- a/printing/templates.py +++ b/printing/templates.py @@ -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) + +