View difference between Paste ID: Vaur3G7f and i5uYPUMP
SHOW: | | - or go back to the newest paste.
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
from __future__ import division
5
from __future__ import print_function
6
from __future__ import unicode_literals
7
from __future__ import absolute_import
8
9
# cross-version compatibility
10
import sys
11
if sys.version_info.major < 3:
12
    import Tkinter as tk
13
    import ttk
14
    import tkFileDialog as tkfiledialog
15
else:
16
    import tkinter as tk
17
    from tkinter import ttk
18
    from tkinter import filedialog as tkfiledialog
19
20
import os
21
from collections import OrderedDict
22
23
24
HOME_FOLDER_MODES = OrderedDict(
25
    [("Include /home/*", ""),
26
     ("Only include /home/* 's hidden files and folders.", "--exclude-home"),
27
     ("Exclude /home/*", "--exclude-home --no-hidden")])
28
29
30
class Example(ttk.Frame):
31
    def __init__(self, parent):
32
        # We can't use super() because in Python 2 the Tkinter objects
33
        # derive from old-style classes.
34-
        self.exclude_home = tk.BooleanVar()
34+
35-
        self.no_hidden = tk.BooleanVar()
35+
36
37
        # set theming
38
        self.style = ttk.Style()
39
        self.style.theme_use("default")
40
        self.pack(fill="both", expand=1)
41
42-
        self.exclude_home.set(False)
42+
43-
        self.no_hidden.set(False)
43+
44
        self.columnconfigure(2, weight=1)
45
        self.columnconfigure(3, weight=0)
46
47
        # Create the Tkinter Variables
48
        self.destination_folder = tk.StringVar()
49
        self.archiver = tk.StringVar()
50
        self.compression = tk.StringVar()
51
        self.home_folder = tk.StringVar()
52
        self.user_options = tk.StringVar()
53
        self.command = tk.StringVar()
54
55
        # Trace the Tkinter Variables!
56
        # http://stackoverflow.com/a/6549535/592289
57
        self.destination_folder.trace("w", self.construct_command)
58
        self.archiver.trace("w", self.construct_command)
59-
        # add widgets
59+
        self.compression.trace("w", self.construct_command)
60
        self.home_folder.trace("w", self.construct_command)
61
        self.user_options.trace("w", self.construct_command)
62
63
        # Set default values to the Tkinter Variables
64-
        self.add_checkbutton(row=5, text="Exclude /home but keep hidden files and folders", variable=self.exclude_home)
64+
65-
        self.add_checkbutton(row=6, text="Don't keep /home 's hidden files and folders.", variable=self.no_hidden)
65+
66
        self.compression.set("gzip")
67
        self.user_options.set("")
68
        self.construct_command()
69
70
        self.create_UI()
71
72
    def create_UI(self):
73
        self.add_choose_folder(row=1, text="Destination folder:", variable=self.destination_folder)
74
        self.add_combobox(row=2, text="Archiver:", variable=self.archiver, values=["tar", "bsdtar"])
75-
        button = ttk.Button(self, text="...", command=lambda:variable.set(tkfiledialog.askdirectory()), width=2)
75+
76
        self.add_entry(row=4, text="Additional archiver options:", variable=self.user_options)
77
        self.add_radiobuttons(row=5, variable=self.home_folder, modes=HOME_FOLDER_MODES)
78
        self.add_entry(row=8, text="Executable command:", variable=self.command)
79
80
    def add_radiobuttons(self, row, modes, variable):
81
        for i, (label, value) in enumerate(modes.items()):
82
            radio_button = ttk.Radiobutton(self, text=label, value=value, variable=variable)
83
            radio_button.grid(row=row + i, column=2, columnspan=1, sticky="nsew")
84
85
    def add_checkbutton(self, row, text, variable):
86
        check_button = ttk.Checkbutton(self, text=text, variable=variable)
87
        check_button.grid(row=row, column=1, columnspan=2, sticky="nsew")
88
89
    def add_choose_folder(self, row, text, variable):
90
        # create the widgets
91
        label = ttk.Label(self, text=text)
92
        entry = ttk.Entry(self, textvariable=variable, state="readonly")
93
        button = ttk.Button(self, text="...", width=2, command=lambda: variable.set(tkfiledialog.askdirectory()))
94
        # place the widgets
95
        label.grid(row=row, column=1, sticky="nse")
96
        entry.grid(row=row, column=2, sticky="nsew")
97
        button.grid(row=row, column=3)
98
99
    def add_combobox(self, row, text, variable, values):
100
        # create the widgets
101
        label = ttk.Label(self, text=text)
102
        box = ttk.Combobox(self, textvariable=variable, state="readonly", values=values)
103
        # place the widgets
104
        label.grid(row=row, column=1, sticky="nse")
105
        box.grid(row=row, column=2, sticky="nsew")
106
107
    def add_entry(self, row, text, variable):
108
        # create the widgets
109
        label = ttk.Label(self, text=text)
110
        entry = ttk.Entry(self, textvariable=variable)
111
        # place the widgets
112
        label.grid(row=row, column=1, sticky="nse")
113
        entry.grid(row=row, column=2, sticky="nsew")
114
115
    def construct_command(self, *args):
116
        """
117
        This callback function is called every time a Tkinter variable is
118
        changed.
119
        """
120
        arguments = [
121
            self.destination_folder.get(),
122
            "--archiver " + self.archiver.get(),
123
            "--compression " + self.compression.get(),
124
            self.home_folder.get(),
125
            self.user_options.get(),
126
        ]
127
        self.command.set("backup.sh " + " ".join(arguments))
128
129
130
131
def main():
132
    root = tk.Tk()
133
    root.title("backup.sh GUI")
134
    app = Example(root)
135
    app.update()
136
    root.minsize(root.winfo_width(), root.winfo_height())
137
    root.mainloop()
138
139
140
if __name__ == '__main__':
141
    main()