Commit cf41f8d2 authored by Louis's avatar Louis
Browse files

Ajout de d'attributs `name`, `dirlevel` et modification du code de l'attribut `thumb`.

Les principales raisons sont :

* permettre d'avoir accès à `dirlevel` ailleurs dans le code ;
* avoir un moyen d'avoir le nom de l'exercice de la même manière avec
  les TexExercise et LegacyExercise ;
* simplification de l'attribut thumb en conséquence.
parent 2f65031c
......@@ -7,7 +7,6 @@ import pkgutil
import types
import sys
class Exercise(object):
''' Base class for all exercise types. '''
......@@ -18,6 +17,15 @@ class Exercise(object):
class TexExercise(Exercise):
''' Exercise with TeX support. '''
@classmethod
def name(cls):
return cls.__name__
@classmethod
def thumb(cls):
from pyromaths.Values import data_dir
return os.path.join(data_dir(), 'ex', cls.dirlevel, 'img', "%s.png" % cls.name())
def tex_statement(self):
''' Return problem statement in TeX format. '''
raise NotImplementedError()
......@@ -36,6 +44,10 @@ class LegacyExercise(TexExercise):
def __init__(self):
self.stat, self.ans = self.function[0]()
@classmethod
def name(cls):
return cls.function[0].__name__
def tex_statement(self):
return self.stat
......@@ -55,7 +67,7 @@ def __module(filename):
return relative
def __legacy(function, thumb):
def __legacy(function, dirlevel):
''' Create a new class proxying for a legacy exercise 'function'. '''
# Create a proxy class inheriting from LegacyExercise for this function
module = __module(function.func_code.co_filename)
......@@ -65,8 +77,8 @@ def __legacy(function, thumb):
dict(description=function.description,
level=function.level,
module=module,
thumb=thumb,
function=(function,),
dirlevel=dirlevel,
)
)
......@@ -110,7 +122,6 @@ def __import(name=__name__, parent=None):
def _exercises(pkg):
''' List exercises in 'pkg' modules. '''
from pyromaths.Values import data_dir
# level defaults to description, then unknown
if 'level' not in dir(pkg): pkg.level = u"Inconnu"
for _, name, ispkg in pkgutil.iter_modules(pkg.__path__, pkg.__name__ + '.'):
......@@ -125,16 +136,14 @@ def _exercises(pkg):
level = __level(element.level if 'level' in dir(element)
else mod.level)
dirlevel = os.path.split(pkg.__path__[0])[1]
if __isexercise(element) or __islegacy(element):
thumb = os.path.join(data_dir(), 'ex', dirlevel, 'img', "%s.png" % element.__name__)
if __isexercise(element):
dirlevel = os.path.split(pkg.__path__[0])[1]
element.level = level
element.thumb = thumb
if __isexercise(element):
element.dirlevel = dirlevel
yield element
elif __islegacy(element):
element.level = level
yield __legacy(element, thumb)
yield __legacy(element, dirlevel)
def _subpackages(pkg):
''' List 'pkg' sub-packages. '''
......
......@@ -695,7 +695,7 @@ class Tab(QtGui.QWidget):
# Image
img = QtGui.QLabel(self.widget)
img.setText(r'<img src="%s"/>' % os.path.join(DATADIR, 'images', 'whatsthis.png'))
img.setToolTip(r'<img src="%s"/>' % self.exos[i].thumb)
img.setToolTip(r'<img src="%s"/>' % self.exos[i].thumb())
layout.addWidget(img)
# Label
label = QtGui.QLabel(self.widget)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment