Debian Codegraph @

wxgeometrie/GUI/mode_script.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 ##--------------------------------------##
4 # WxGeometrie #
5 # mode script #
6 ##--------------------------------------##
7 # WxGeometrie
8 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
9 # Copyright (C) 2005-2013 Nicolas Pourcelot
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25 from ..geolib.feuille import Feuille
26 from ..API.canvas import Canvas
27 from ..API.sauvegarde import ouvrir_fichierGEO
28 from ..pylib import print_error
29 from .. import param
30
31
32 def _err(msg, *msgs):
33 msg = "== Erreur: %s ==" %msg
34 if msgs:
35 msg += '\n---\n' + '\n'.join(msgs) + '\n---'
36 return msg
37
38
39 def mode_script(input = None, output = None):
40 try:
41 if input is None:
42 input = input('Adresse du fichier de script ou du fichier .geo :')
43
44 if input.endswith('.geo') or input.endswith('.geoz'):
45 fgeo, message = ouvrir_fichierGEO(input)
46 if fgeo is None:
47 return _err(message)
48 try:
49 commandes = fgeo.contenu["Figure"][0]
50 except KeyError:
51 return _err("Le fichier '%s' ne comporte pas de figure." %input)
52
53 else:
54 try:
55 with open(input, 'r') as f:
56 commandes = f.read()
57 except IOError:
58 print_error()
59 return _err("Fichier introuvable: '%s'" % input)
60
61 feuille = Feuille()
62 canvas = Canvas(feuille = feuille)
63 feuille.canvas = canvas
64 try:
65 feuille.charger(commandes)
66 except Exception:
67 print_error()
68 return _err("Commandes incorrectes", commandes)
69
70 if output is None:
71 output = input('Adresse du fichier de sortie (png/svg/...) :')
72
73 try:
74 print(canvas.fenetre)
75 canvas.exporter(output, echelle = param.echelle_cm)
76 except IOError:
77 print_error()
78 return _err("Impossible d'exporter dans '%s'. Vérifiez les permissions ou l'espace disque." % output)
79
80 except Exception:
81 print_error()
82 return _err("Erreur d'exécution du mode script.")