src/eric7/eric7_compare.py

branch
eric7
changeset 10303
ee1aadab1215
parent 10238
9ea4634a697e
child 10439
21c28b0f9e41
equal deleted inserted replaced
10302:8cb0dabf852f 10303:ee1aadab1215
10 This is the main Python script that performs the necessary initialization 10 This is the main Python script that performs the necessary initialization
11 of the Compare module and starts the Qt event loop. This is a standalone 11 of the Compare module and starts the Qt event loop. This is a standalone
12 version of the integrated Compare module. 12 version of the integrated Compare module.
13 """ 13 """
14 14
15 import argparse
15 import os 16 import os
16 import sys 17 import sys
17 18
18 from PyQt6.QtGui import QGuiApplication 19 from PyQt6.QtGui import QGuiApplication
19 20
20 for arg in sys.argv[:]:
21 if arg.startswith("--config="):
22 from eric7 import Globals
23 21
24 configDir = arg.replace("--config=", "") 22 def createArgparseNamespace():
25 Globals.setConfigDir(configDir) 23 """
26 sys.argv.remove(arg) 24 Function to create an argument parser.
27 elif arg.startswith("--settings="):
28 from PyQt6.QtCore import QSettings
29 25
30 settingsDir = os.path.expanduser(arg.replace("--settings=", "")) 26 @return created argument parser object
31 if not os.path.isdir(settingsDir): 27 @rtype argparse.ArgumentParser
32 os.makedirs(settingsDir) 28 """
33 QSettings.setPath( 29 from eric7.UI.Info import Version
34 QSettings.Format.IniFormat, QSettings.Scope.UserScope, settingsDir
35 )
36 sys.argv.remove(arg)
37 30
38 from eric7.Globals import AppInfo 31 # 1. create the argument parser
32 parser = argparse.ArgumentParser(
33 description="Simple graphical tool to compare two files side-by-side."
34 " It is part of the eric tool suite.",
35 epilog="Copyright (c) 2006 - 2023 Detlev Offenbach <detlev@die-offenbachs.de>.",
36 )
37
38 # 2. add the arguments
39 parser.add_argument(
40 "-V",
41 "--version",
42 action="version",
43 version="%(prog)s {0}".format(Version),
44 help="show version information and exit",
45 )
46 parser.add_argument(
47 "--config",
48 metavar="config_dir",
49 help="use the given directory as the one containing the config files",
50 )
51 parser.add_argument(
52 "--settings",
53 metavar="settings_dir",
54 help="use the given directory to store the settings files",
55 )
56 parser.add_argument(
57 "file_1",
58 nargs="?",
59 help="first file to be compared",
60 )
61 parser.add_argument(
62 "file_2",
63 nargs="?",
64 help="second file to be compared",
65 )
66
67 # 3. create the Namespace object by parsing the command line
68 args = parser.parse_args()
69 if args.file_1 and not args.file_2:
70 parser.error("Two files to be compared or none should be given.")
71 return args
72
73
74 args = createArgparseNamespace()
75 if args.config:
76 from eric7 import Globals
77
78 Globals.setConfigDir(args.config)
79 if args.settings:
80 from PyQt6.QtCore import QSettings
81
82 SettingsDir = os.path.expanduser(args.settings)
83 if not os.path.isdir(SettingsDir):
84 os.makedirs(SettingsDir)
85 QSettings.setPath(
86 QSettings.Format.IniFormat, QSettings.Scope.UserScope, SettingsDir
87 )
88
39 from eric7.Toolbox import Startup 89 from eric7.Toolbox import Startup
40 90
41 91
42 def createMainWidget(argv): 92 def createMainWidget(args):
43 """ 93 """
44 Function to create the main widget. 94 Function to create the main widget.
45 95
46 @param argv list of commandline parameters (list of strings) 96 @param args namespace object containing the parsed command line parameters
47 @return reference to the main widget (QWidget) 97 @type argparse.Namespace
98 @return reference to the main widget
99 @rtype QWidget
48 """ 100 """
49 from eric7.UI.CompareDialog import CompareWindow 101 from eric7.UI.CompareDialog import CompareWindow
50 102
51 if len(argv) >= 6: 103 if args.file_1 and args.file_2:
52 # assume last two entries are the files to compare 104 return CompareWindow([("", args.file_1), ("", args.file_2)])
53 file1 = (argv[-4], argv[-2])
54 file2 = (argv[-3], argv[-1])
55 return CompareWindow([file1, file2])
56 elif len(argv) >= 2:
57 return CompareWindow([("", argv[-2]), ("", argv[-1])])
58 else: 105 else:
59 return CompareWindow() 106 return CompareWindow()
60 107
61 108
62 def main(): 109 def main():
63 """ 110 """
64 Main entry point into the application. 111 Main entry point into the application.
65 """ 112 """
66 QGuiApplication.setDesktopFileName("eric7_compare") 113 QGuiApplication.setDesktopFileName("eric7_compare")
67 114
68 options = [ 115 res = Startup.appStartup(args, createMainWidget)
69 (
70 "--config=configDir",
71 "use the given directory as the one containing the config files",
72 ),
73 (
74 "--settings=settingsDir",
75 "use the given directory to store the settings files",
76 ),
77 ]
78 appinfo = AppInfo.makeAppInfo(
79 sys.argv, "eric Compare", "", "Simple graphical compare tool", options
80 )
81 res = Startup.simpleAppStartup(sys.argv, appinfo, createMainWidget)
82 sys.exit(res) 116 sys.exit(res)
83 117
84 118
85 if __name__ == "__main__": 119 if __name__ == "__main__":
86 main() 120 main()

eric ide

mercurial