src/eric7/eric7_diff.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 Diff module and starts the Qt event loop. This is a standalone 11 of the Diff module and starts the Qt event loop. This is a standalone
12 version of the integrated Diff module. 12 version of the integrated Diff 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 diff tool of the eric tool suite.",
34 epilog="Copyright (c) 2006 - 2023 Detlev Offenbach <detlev@die-offenbachs.de>.",
35 )
36
37 # 2. add the arguments
38 parser.add_argument(
39 "-V",
40 "--version",
41 action="version",
42 version="%(prog)s {0}".format(Version),
43 help="show version information and exit",
44 )
45 parser.add_argument(
46 "--config",
47 metavar="config_dir",
48 help="use the given directory as the one containing the config files",
49 )
50 parser.add_argument(
51 "--settings",
52 metavar="settings_dir",
53 help="use the given directory to store the settings files",
54 )
55 parser.add_argument(
56 "file_1",
57 nargs="?",
58 help="first file to be compared",
59 )
60 parser.add_argument(
61 "file_2",
62 nargs="?",
63 help="second file to be compared",
64 )
65
66 # 3. create the Namespace object by parsing the command line
67 args = parser.parse_args()
68 if args.file_1 and not args.file_2:
69 parser.error("Two files to be compared or none should be given.")
70 return args
71
72
73 args = createArgparseNamespace()
74 if args.config:
75 from eric7 import Globals
76
77 Globals.setConfigDir(args.config)
78 if args.settings:
79 from PyQt6.QtCore import QSettings
80
81 SettingsDir = os.path.expanduser(args.settings)
82 if not os.path.isdir(SettingsDir):
83 os.makedirs(SettingsDir)
84 QSettings.setPath(
85 QSettings.Format.IniFormat, QSettings.Scope.UserScope, SettingsDir
86 )
87
39 from eric7.Toolbox import Startup 88 from eric7.Toolbox import Startup
40 89
41 90
42 def createMainWidget(argv): # noqa: U100 91 def createMainWidget(args): # noqa: U100
43 """ 92 """
44 Function to create the main widget. 93 Function to create the main widget.
45 94
46 @param argv list of commandline parameters (list of strings) 95 @param args namespace object containing the parsed command line parameters
47 @return reference to the main widget (QWidget) 96 @type argparse.Namespace
97 @return reference to the main widget
98 @rtype QWidget
48 """ 99 """
49 from eric7.UI.DiffDialog import DiffWindow 100 from eric7.UI.DiffDialog import DiffWindow
50 101
51 return DiffWindow() 102 if args.file_1 and args.file_2:
103 return DiffWindow(files=[args.file_1, args.file_2])
104 else:
105 return DiffWindow()
52 106
53 107
54 def main(): 108 def main():
55 """ 109 """
56 Main entry point into the application. 110 Main entry point into the application.
57 """ 111 """
58 QGuiApplication.setDesktopFileName("eric7_diff") 112 QGuiApplication.setDesktopFileName("eric7_diff")
59 113
60 options = [ 114 res = Startup.appStartup(args, createMainWidget)
61 (
62 "--config=configDir",
63 "use the given directory as the one containing the config files",
64 ),
65 (
66 "--settings=settingsDir",
67 "use the given directory to store the settings files",
68 ),
69 ]
70 appinfo = AppInfo.makeAppInfo(
71 sys.argv, "eric Diff", "", "Simple graphical diff tool", options
72 )
73 res = Startup.simpleAppStartup(sys.argv, appinfo, createMainWidget)
74 sys.exit(res) 115 sys.exit(res)
75 116
76 117
77 if __name__ == "__main__": 118 if __name__ == "__main__":
78 main() 119 main()

eric ide

mercurial