static_var n'a pas vraiment de raison d'être dans affichage.py

This commit is contained in:
Pierre-Elliott Bécue 2015-03-22 02:12:27 +01:00
parent 394531a537
commit 3f1b7401ca
2 changed files with 23 additions and 21 deletions

20
cranslib/decorators.py Normal file
View file

@ -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

View file

@ -8,10 +8,6 @@
# Contenu : # Contenu :
# --------- # ---------
# #
# Décorateur :
# static_var([(name, val)]), un décorateur pour créer des variables
# statiques dans une fonction
#
# Fonctions : # Fonctions :
# getTerminalSize(), une fonction qui récupère le couple # getTerminalSize(), une fonction qui récupère le couple
# largeur, hauteur du terminal courant. # largeur, hauteur du terminal courant.
@ -39,12 +35,13 @@ import os
import fcntl import fcntl
import termios import termios
import struct import struct
import functools
import time import time
import re import re
from locale import getpreferredencoding from locale import getpreferredencoding
from cranslib.decorators import static_var
OCT_NAMES = ["Pio", "Tio", "Gio", "Mio", "Kio"] OCT_NAMES = ["Pio", "Tio", "Gio", "Mio", "Kio"]
OCT_SIZES = [1024**(len(OCT_NAMES) - i) for i in xrange(0, len(OCT_NAMES))] 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' TERM_FORMAT = '\x1b\[[0-1];([0-9]|[0-9][0-9])m'
@ -90,21 +87,6 @@ def guess_preferred_encoding():
return 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(): def getTerminalSize():
"""Dummy function to get term dimensions. """Dummy function to get term dimensions.
Thanks to http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python 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 "\Zn"
return "\033[1;0m" return "\033[1;0m"
@static_var([("styles", {})]) @static_var(("styles", {}))
def style(texte, what=None, dialog=False): def style(texte, what=None, dialog=False):
"""Pretty text is pretty """Pretty text is pretty
On peut appliquer plusieurs styles d'affilée, ils seront alors traités On peut appliquer plusieurs styles d'affilée, ils seront alors traités