[wiki] Macro pour afficher le contenu d'un dictionnaire python

This commit is contained in:
Vincent Le Gallic 2014-12-16 22:31:28 +01:00
parent b036b3598f
commit bf67c39c9b

49
wiki/macro/DisplayDict.py Normal file
View file

@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
"""
MoinMoin - ShowAcl macro
@copyright: 2014 Vincent Le Gallic, Emmanuel Arrighi
@license: GNU GPL v3
Pour afficher un dictionnaire dans un tableau wiki.
"""
import sys
import os.path
import importlib
def macro_DisplayDict(macro, args):
"""Suppose que args est de la forme ``path:variable_name``"""
# Si on utilise une virgule, MoinMoin foire lamentablement… ("Too many arguments")
fichier, variable = args.split(":")
# On importe le fichier demandé
path = os.path.dirname(fichier)
if not path in sys.path:
sys.path.append(path)
filename = os.path.basename(fichier)
filename = filename.rsplit(os.path.extsep, 1)[0]
module = importlib.import_module(filename)
# On recharge le module sinon on affichera toujours le contenu à la date
# du dernier redémarrage du wiki
module = reload(module)
dict = module.__dict__[variable]
keys = dict.keys()
keys.sort()
text = ""
text += macro.formatter.table(1)
for k in keys:
text += macro.formatter.table_row(1)
text += macro.formatter.table_cell(1)
text += macro.formatter.text(k)
text += macro.formatter.table_cell(0)
text += macro.formatter.table_cell(1)
text += macro.formatter.text(dict[k])
text += macro.formatter.table_cell(0)
text += macro.formatter.table_row(0)
text += macro.formatter.table(0)
return text