-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfile_tools.py
338 lines (299 loc) · 9.92 KB
/
file_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2007 Philippe LAWRENCE
#
# This file is part of pyBar.
# pyBar is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# pyBar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyBar; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
import sys
from gi.repository import Gtk
import Const
def we_are_frozen():
"""Returns whether we are frozen via py2exe.
This will affect how we find out where we are located."""
return hasattr(sys, "frozen")
def module_path():
""" This will get us the program's directory,
even if we are frozen using py2exe"""
#if we_are_frozen():
# return os.path.dirname(sys.executable, sys.getfilesystemencoding( ))
return os.path.dirname(__file__)
def set_user_dir():
"""Crée le dossier pour les fichiers utilisateur"""
path = Const.PATH
if not os.path.isdir(path):
import shutil
os.mkdir(path)
path = os.path.join(path, Const.DIREXEMPLES)
os.mkdir(path)
script_path = module_path()
src = os.path.join(script_path, "exemples")
try:
files = os.listdir(src)
except OSError:
files = []
for file in files:
file_src = os.path.join(src, file)
if os.path.isdir(file_src):
continue
shutil.copy(file_src, path)
# -----------------------------------------------------
#
# OUVERTURE D'UN NOUVEAU FICHIER
#
#-------------------------------------------------------
def recursive_file_select(path):
path = file_save(path)
if path is None:
return None
if not path[-4:] == ".dat" :
path += ".dat"
if save_as_ok_func(path):
return path
else:
recursive_file_select()
def exit_as_ok_func(filename):
dialog = Gtk.MessageDialog(transient_for=None,
modal=True,
destroy_with_parent=True,
message_type=Gtk.MessageType.QUESTION,
buttons=Gtk.ButtonsType.YES_NO,
text = "Enregistrer le fichier '%s'?" % filename)
dialog.add_button("Oui pour tous", 2)
dialog.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
dialog.set_icon_from_file("glade/logo.png")
result = dialog.run()
dialog.destroy()
if result == Gtk.ResponseType.YES:
return 1
if result == Gtk.ResponseType.CANCEL:
return -1
if result == 2:
return 2
return 0
def exit_as_ok_func2(message):
dialog = Gtk.MessageDialog(transient_for=None,
modal=True,
destroy_with_parent=True,
message_type=Gtk.MessageType.QUESTION,
buttons=Gtk.ButtonsType.YES_NO,
text = message)
dialog.set_icon_from_file("glade/logo.png")
result = dialog.run()
dialog.destroy()
if result != Gtk.ResponseType.YES:
return False
return True
def save_as_ok_func(filename):
if filename is None:
return
if os.path.exists(filename):
dialog = Gtk.MessageDialog(transient_for=None,
modal=True,
destroy_with_parent=True,
message_type=Gtk.MessageType.QUESTION,
buttons=Gtk.ButtonsType.YES_NO,
text = "Ecraser le fichier '%s'?" % filename)
dialog.set_icon_from_file("glade/logo.png")
result = dialog.run()
dialog.destroy()
if result != Gtk.ResponseType.YES:
return False
return True
return True
def open_as_ok_func(filename):
if filename is None:
return
if os.path.exists(filename):
dialog = Gtk.MessageDialog(transient_for=None,
modal=True,
destroy_with_parent=True,
message_type=Gtk.MessageType.INFO,
buttons=Gtk.ButtonsType.OK,
text = "Le fichier '%s' est déjà ouvert" % filename)
dialog.set_icon_from_file("glade/logo.png")
result = dialog.run()
dialog.destroy()
def open_dialog_resol():
"""Fenêtre d'affichage de choix de la résolution"""
dialog = Gtk.Dialog(transient_for=None,
modal=True,
destroy_with_parent=True,
title = "Exportation bitmap")
dialog.add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK)
dialog.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT)
dialog.set_icon_from_file("glade/logo.png")
vbox = Gtk.Box(spacing=0, orientation=Gtk.Orientation.VERTICAL)
vbox.set_border_width(5)
label = Gtk.Label(label="Choix de la résolution")
label.set_size_request(-1, 30, xalign=0.)
vbox.pack_start(label, False, True, 0)
adj = Gtk.Adjustment(value=50., lower=0., upper=100., step_increment=10., page_increment=20.0)
spin = Gtk.SpinButton.new(adj, 0, 0)
spin.set_size_request(30, -1)
#spin.set_wrap(True) ???
vbox.pack_start(spin, False, False, 0)
dialog.vbox.add(vbox)
dialog.show_all()
result = dialog.run()
dialog.destroy()
if result == Gtk.ResponseType.OK:
return spin.get_value()
return False
def open_dialog_bars(bars, selected_bars):
"""Fenêtre d'affichage de sélection des barres actives"""
dialog = Gtk.Dialog(transient_for=None,
modal=True,
destroy_with_parent=True,
title = "Choix des barres")
dialog.add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK)
dialog.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT)
dialog.set_icon_from_file("glade/logo.png")
vbox = Gtk.Box(spacing=0, orientation=Gtk.Orientation.VERTICAL)
vbox.set_border_width(5)
label = Gtk.Label(label="Choix des barres", xalign=0.)
label.set_size_request(-1, 30)
vbox.pack_start(label, False, True, 0)
buttons = []
keys = list(bars.keys())
keys.sort()
for bar in keys:
button = Gtk.CheckButton(label=bars[bar])
buttons.append(button)
if bar in selected_bars:
button.set_active(True)
vbox.pack_start(button, False, False, 0)
dialog.vbox.add(vbox)
dialog.show_all()
result = dialog.run()
dialog.destroy()
if result == Gtk.ResponseType.OK:
resu = {}
for i, button in enumerate(buttons):
if button.get_active():
key = keys[i]
resu[key] = bars[key]
return resu
return False
def file_export(path, preselect=None):
"""Return selected file name or None"""
# Create a new file selection widget
dialog = Gtk.FileChooserDialog(
title="Enregistrer sous",
action=Gtk.FileChooserAction.SAVE,
)
dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
"Enregistrer", Gtk.ResponseType.OK)
dialog.set_icon_from_file("glade/logo.png")
dialog.set_current_folder(path)
dialog.set_default_response(Gtk.ResponseType.OK)
filter = Gtk.FileFilter()
filter.set_name("PNG")
filter.add_mime_type("image/png")
filter.add_pattern("*.png")
dialog.add_filter(filter)
filter = Gtk.FileFilter()
filter.set_name("SVG")
filter.add_pattern("*.svg")
dialog.add_filter(filter)
# select a specific file
if preselect:
dialog.set_current_name(preselect)
reponse = dialog.run()
if reponse == Gtk.ResponseType.OK:
filter = dialog.get_filter()
format = filter.get_name()
file = dialog.get_filename()
#if sys.platform == 'win32':
# file = file.decode('utf-8')
if format == 'JPEG' and (not file[-4:] == '.jpg' and not file[-4:] == '.jpeg'):
file += '.jpg'
elif format == 'PNG' and not file[-4:] == '.png':
file += '.png'
elif format == 'SVG' and not file[-4:] == '.svg':
file += '.svg'
dialog.destroy()
return file, format
dialog.destroy()
return None
def file_save(path, ext=".dat", preselect=None):
"""Return selected file name or None"""
# Create a new file selection widget
dialog = Gtk.FileChooserDialog(
title="Enregistrer sous",
action=Gtk.FileChooserAction.SAVE,
)
dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
"Enregistrer", Gtk.ResponseType.OK)
dialog.set_icon_from_file("glade/logo.png")
dialog.set_current_folder(path)
dialog.set_default_response(Gtk.ResponseType.OK)
filtre = Gtk.FileFilter()
filtre.set_name("Fichier données")
filtre.add_pattern("*%s" % ext)
dialog.add_filter(filtre)
# select a specific file
if preselect:
dialog.set_current_name(preselect)
reponse = dialog.run()
if reponse == Gtk.ResponseType.OK:
file = dialog.get_filename()
#if sys.platform == 'win32':
# file = file.decode('utf-8')
file_ext = os.path.splitext(file)[1].lower()
if not file_ext == ext:
#if not file[-4:] == ext:
file += ext
else:
file = None
dialog.destroy()
return file
# -----------------------------------------------------
#
# OUVERTURE D'UN FICHIER
#
#-------------------------------------------------------
def file_selection(path, window):
"""Return selected file name or None"""
dialog = Gtk.FileChooserDialog(
title="Sélectionner un fichier",
action=Gtk.FileChooserAction.OPEN,
)
dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
"Ouvrir", Gtk.ResponseType.OK)
dialog.set_default_response(Gtk.ResponseType.OK)
dialog.set_icon_from_file("glade/logo.png")
script_path = os.path.dirname(os.path.realpath(__file__))
dir_exemple = os.path.join(script_path, "exemples")
dialog.add_shortcut_folder(dir_exemple)
if path is None:
path = dir_exemple
dialog.set_current_folder(path)
filtre = Gtk.FileFilter()
filtre.set_name("Fichiers %s" % Const.SOFT)
filtre.add_pattern("*.dat")
filtre.add_pattern("*.dxf")
filtre.add_pattern("*.DXF")
dialog.add_filter(filtre)
reponse = dialog.run()
if reponse == Gtk.ResponseType.OK:
file = dialog.get_filename()
# suppression juin 2014
#if sys.platform == 'win32':
# file = file.decode('utf-8')
else:
file = None
dialog.destroy()
return file