Debian Codegraph @

wxgeometrie/param/modules.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 ######################################
4 #
5 # Détection des modules
6 #
7 ######################################
8 #
9 # WxGeometrie
10 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
11 # Copyright (C) 2005-2013 Nicolas Pourcelot
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #
27 ######################################
28
29 import os
30 from runpy import run_path
31
32 from .parametres import modules_par_defaut
33
34 # Modules a importer
35 # ----------------
36 _skip_dir = ('OLD', '__pycache__')
37 _modules_dir = os.path.normpath(os.path.join(__file__, '..', '..', 'modules'))
38
39 def _detecter_modules():
40 modules = []
41 descriptions = {}
42 def err(nom, msg):
43 print("Warning: %s n'est pas un module valide (%s)." %(nom, msg))
44 for nom in os.listdir(_modules_dir):
45 if nom not in _skip_dir and os.path.isdir(os.path.join(_modules_dir, nom)):
46 description_file = os.path.join(_modules_dir, nom, 'description.py')
47 if os.path.isfile(description_file):
48 try:
49 compile(nom + '=0', '', 'single') # On teste si le nom est valide
50 try:
51 d = {}
52 d = run_path(description_file, d)
53 if d['description']['groupe'] != "Modules":
54 # Sert à désactiver les modules en construction.
55 continue
56 descriptions[nom] = d['description']
57 modules.append(nom)
58 except:
59 err(nom, "fichier '%s' incorrect" %description_file)
60 except Exception:
61 err(nom, "nom de module invalide")
62 else:
63 err(nom, "fichier 'description.py' introuvable")
64 return modules, descriptions
65
66 try:
67 modules, descriptions_modules = _detecter_modules()
68 except OSError:
69 print("Warning: impossible de détecter les modules (répertoire '%s') !" % _modules_dir)
70 modules = []
71 descriptions_modules = {}
72
73
74 modules_actifs = dict.fromkeys(modules, False)
75
76 for nom in modules_par_defaut:
77 modules_actifs[nom] = True
78
79
80 def _key(nom):
81 # les modules activés par défaut apparaissent en premier,
82 # les autres sont classés par ordre alphabétique.
83 key = [1000000, nom]
84 if nom in modules_par_defaut:
85 key[0] = modules_par_defaut.index(nom)
86 return key
87
88 modules.sort(key = _key)