diff --git a/cranslib/decorators.py b/cranslib/decorators.py new file mode 100644 index 00000000..72c8e1f5 --- /dev/null +++ b/cranslib/decorators.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import functools + +def static_var(*couples): + """Decorator setting static variable + to a function. + + """ + # Using setattr magic, we set static + # variable on function. This avoid + # computing stuff again. + def decorate(fun): + functools.wraps(fun) + for (name, val) in couples: + setattr(fun, name, val) + return fun + return decorate + diff --git a/gestion/affichage.py b/gestion/affichage.py index 2cdfa33c..310976cb 100755 --- a/gestion/affichage.py +++ b/gestion/affichage.py @@ -8,10 +8,6 @@ # Contenu : # --------- # -# Décorateur : -# static_var([(name, val)]), un décorateur pour créer des variables -# statiques dans une fonction -# # Fonctions : # getTerminalSize(), une fonction qui récupère le couple # largeur, hauteur du terminal courant. @@ -39,12 +35,13 @@ import os import fcntl import termios import struct -import functools import time import re from locale import getpreferredencoding +from cranslib.decorators import static_var + OCT_NAMES = ["Pio", "Tio", "Gio", "Mio", "Kio"] OCT_SIZES = [1024**(len(OCT_NAMES) - i) for i in xrange(0, len(OCT_NAMES))] TERM_FORMAT = '\x1b\[[0-1];([0-9]|[0-9][0-9])m' @@ -90,21 +87,6 @@ def guess_preferred_encoding(): return encoding -def static_var(couples): - """Decorator setting static variable - to a function. - - """ - # Using setattr magic, we set static - # variable on function. This avoid - # computing stuff again. - def decorate(fun): - functools.wraps(fun) - for (name, val) in couples: - setattr(fun, name, val) - return fun - return decorate - def getTerminalSize(): """Dummy function to get term dimensions. Thanks to http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python @@ -278,7 +260,7 @@ def nostyle(dialog=False): return "\Zn" return "\033[1;0m" -@static_var([("styles", {})]) +@static_var(("styles", {})) def style(texte, what=None, dialog=False): """Pretty text is pretty On peut appliquer plusieurs styles d'affilée, ils seront alors traités