-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPyMerge.py
More file actions
157 lines (130 loc) · 5.54 KB
/
Copy pathPyMerge.py
File metadata and controls
157 lines (130 loc) · 5.54 KB
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
"""
###########################################################################
File: PyMerge.py
Author:
Description: Main entry point for the program.
Copyright (C) PyMerge Team 2019
This program 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.
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
###########################################################################
"""
"""
This is the entry point for the PyMerge program. Command line arguments will be passed
to this file and the functions in this file will invoke the GUI. There will be no merge/compare/GUI
algorithms in this file. It will only call the main GUI and application functions.
"""
import sys
import dependency_chk
dependency_chk.check()
import main_window
import utilities
class PyMergeCLI(object):
def __init__(self, *args):
self.options: list = self.sanitize(args[0][1:])
self.file_size_lim: int = 20000 # 2KB limit until interface for C implementation of LCS is done
self.cli()
def cli(self):
"""
Main command-line interface function.
:return: No return value
"""
left_file: str = ""
right_file: str = ""
opt_length: int = len(self.options)
if opt_length == 0:
self.invoke_application(left_file, right_file)
return
elif opt_length == 1:
if self.options[0] == "--help":
self.help_func()
elif self.options[0] == "--about":
self.about_func()
return
elif opt_length == 2:
if utilities.check_paths(self.options[0], self.options[1]):
left_file = self.options[0]
right_file = self.options[1]
elif self.options[0] == "--file":
print("Error: 2 files required for comparison.")
elif opt_length == 3 and self.options[0] == "--file" and utilities.check_paths(self.options[1], self.options[2]):
left_file = self.options[1]
right_file = self.options[2]
elif opt_length == 4 and \
self.options[0] == "--file" and \
self.options[2] == "--file" and \
utilities.check_paths(self.options[1], self.options[3]):
left_file = self.options[1]
right_file = self.options[3]
else:
print("Error: Invalid options. Type '--help' for information.")
return
self.invoke_application(left_file, right_file)
@staticmethod
def help_func():
print(
"""
------------------------------------------------------------
PyMerge
------------------------------------------------------------
--file: File to compare.\n\tUsage: '[--file] <left_file> <right_file>'
--help: Show command line options.\n\tUsage: '[--help]'
--about: Link to the PyMerge project README. \n\n
"""
)
@staticmethod
def about_func():
print(
"PyMerge is a cross-platform file diff/merge tool.\n\n"
"The PyMerge project page can be found here: https://github.com/WSU-4110/PyMerge\n"
)
@staticmethod
def sanitize(options: list or set) -> list or set:
"""
Santizes raw user input to something that can be used without error.
:param options: list of options provided by user.
:return:
"""
sanitized: list = []
file_options: set = {"--f", "-file", "-f"}
help_options: set = {"--h", "-h", "--he", "-he", "--hel", "-hel", "--help", "-help"}
about_options: set = {"--about", "-about", "--abot", "-abot", "--abut", "-abut", "--abt", "-abt", "--info",
"-info"}
for n in range(len(options)):
options[n] = str(options[n].replace(" ", ""))
option_lower: str = options[n].lower()
if option_lower == " " or options[n] == "":
continue
elif option_lower in file_options:
sanitized.append("--file")
elif option_lower in help_options:
sanitized.append("--help")
elif option_lower in about_options:
sanitized.append("--about")
else:
sanitized.append(options[n])
return sanitized
def invoke_application(self, file1: str, file2: str):
"""Invoke the main application here"""
if file1 != "" or file2 != "":
if self.validate_files(file1, file2, path_check=False):
main_window.start_main(file1, file2)
else:
main_window.start_main()
print(file1, file2)
def validate_files(self, file1, file2, path_check=False):
size_valid = utilities.validate_file_size(file2, self.file_size_lim) and \
utilities.validate_file_size(file2, self.file_size_lim)
ext_valid = utilities.valid_file_ext(file1) and utilities.valid_file_ext(file2)
paths_valid = utilities.check_paths(file1, file2) if path_check else True
if size_valid and ext_valid and paths_valid:
return True
if __name__ == '__main__':
PyMergeCLI(sys.argv)