-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_standard_file.py
executable file
·70 lines (54 loc) · 2.79 KB
/
simple_standard_file.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""simple_standard_file.py is a set of quick "just somehow ask the user where to
save/open a file" routines. Each does its best to ask the user a specific
question in the best way available and to degrade gracefully if the services
it's trying to use prove not to be available.
simple-standard-file.py is part of Patrick Mooney's personal Python library.
This program is licensed under the GPL v3 or, at your option, any later
version. See the file LICENSE.md for a copy of this licence.
"""
import sys
import patrick_logger # https://github.com/patrick-brian-mooney/python-personal-library/blob/master/patrick_logger.py
def do_save_dialog(**kwargs):
"""Shows a dialog asking the user where to save a file, or comes as close as
possible to doing so. Any keyword arguments passed in are piped to the
underlying function tkinter.filedialog.asksaveasfilename
Returns a path to the file that the user wants to create.
Adapted from more complex code in Zombie Apocalypse.
"""
patrick_logger.log_it("DEBUGGING: simple_standard_file.do_save_dialog() called", 2)
try: # Use Tkinter if possible
import tkinter
import tkinter.filedialog
tkinter.Tk().withdraw() # No root window
filename = tkinter.filedialog.asksaveasfilename()
except Exception as errrr: # If all else fails, ask the user to type a filename.
filename = input('Under what name would you like to save the file? ')
patrick_logger.log_it(' Selected file is %s' % filename, 2)
return filename
def do_open_dialog(**kwargs):
"""Shows a dialog asking the user which file to open, or comes as close as
possible to doing so. Any keyword arguments passed in are piped to the
underlying function tkinter.filedialog.askopenfilename
Returns a path to the file that the user wants to open.
Adapted from more complex code in Zombie Apocalypse.
"""
patrick_logger.log_it("DEBUGGING: simple_standard_file.do_open_dialog() called", 2)
try: # Use TKinter if possible
import tkinter
import tkinter.filedialog
tkinter.Tk().withdraw() # No root window
filename = tkinter.filedialog.askopenfilename(**kwargs)
except: # If all else fails, ask the user to type it.
filename = input('What file would you like to open? ')
if filename == tuple([]):
patrick_logger.log_it(' INFO: simple_standard_file: do_open_dialog() cancelled', 2)
filename = None
else:
patrick_logger.log_it(' INFO: simple_standard_file: Selected file is "%s"' % filename, 2)
return filename
if __name__ == "__main__":
patrick_logger.log_it(
"ERROR: %s is not a program you can run. It is a collection of software to be used by other software." %
sys.argv[0])