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 hex editor and starts the Qt event loop. This is a standalone version |
11 of the hex editor and starts the Qt event loop. This is a standalone version |
12 of the integrated hex editor. |
12 of the integrated hex editor. |
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="Graphical tool of the eric tool suite to edit binary files.", |
|
34 epilog="Copyright (c) 2016 - 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", |
|
57 nargs="?", |
|
58 help="file to be opened for editing", |
|
59 ) |
|
60 |
|
61 # 3. create the Namespace object by parsing the command line |
|
62 args = parser.parse_args() |
|
63 return args |
|
64 |
|
65 |
|
66 args = createArgparseNamespace() |
|
67 if args.config: |
|
68 from eric7 import Globals |
|
69 |
|
70 Globals.setConfigDir(args.config) |
|
71 if args.settings: |
|
72 from PyQt6.QtCore import QSettings |
|
73 |
|
74 SettingsDir = os.path.expanduser(args.settings) |
|
75 if not os.path.isdir(SettingsDir): |
|
76 os.makedirs(SettingsDir) |
|
77 QSettings.setPath( |
|
78 QSettings.Format.IniFormat, QSettings.Scope.UserScope, SettingsDir |
|
79 ) |
|
80 |
39 from eric7.Toolbox import Startup |
81 from eric7.Toolbox import Startup |
40 |
82 |
41 |
83 |
42 def createMainWidget(argv): |
84 def createMainWidget(args): |
43 """ |
85 """ |
44 Function to create the main widget. |
86 Function to create the main widget. |
45 |
87 |
46 @param argv list of commandline parameters (list of strings) |
88 @param args namespace object containing the parsed command line parameters |
47 @return reference to the main widget (QWidget) |
89 @type argparse.Namespace |
|
90 @return reference to the main widget |
|
91 @rtype QWidget |
48 """ |
92 """ |
49 from eric7.HexEdit.HexEditMainWindow import HexEditMainWindow |
93 from eric7.HexEdit.HexEditMainWindow import HexEditMainWindow |
50 |
94 |
51 try: |
95 editor = HexEditMainWindow(args.file if args.file else "", None) |
52 fileName = argv[1] |
|
53 except IndexError: |
|
54 fileName = "" |
|
55 |
|
56 editor = HexEditMainWindow(fileName, None) |
|
57 return editor |
96 return editor |
58 |
97 |
59 |
98 |
60 def main(): |
99 def main(): |
61 """ |
100 """ |
62 Main entry point into the application. |
101 Main entry point into the application. |
63 """ |
102 """ |
64 QGuiApplication.setDesktopFileName("eric7_hexeditor") |
103 QGuiApplication.setDesktopFileName("eric7_hexeditor") |
65 |
104 |
66 options = [ |
105 res = Startup.appStartup(args, createMainWidget) |
67 ( |
|
68 "--config=configDir", |
|
69 "use the given directory as the one containing the config files", |
|
70 ), |
|
71 ( |
|
72 "--settings=settingsDir", |
|
73 "use the given directory to store the settings files", |
|
74 ), |
|
75 ("", "name of file to edit"), |
|
76 ] |
|
77 appinfo = AppInfo.makeAppInfo( |
|
78 sys.argv, "eric Hex Editor", "", "Little tool to edit binary files.", options |
|
79 ) |
|
80 res = Startup.simpleAppStartup(sys.argv, appinfo, createMainWidget) |
|
81 sys.exit(res) |
106 sys.exit(res) |
82 |
107 |
83 |
108 |
84 if __name__ == "__main__": |
109 if __name__ == "__main__": |
85 main() |
110 main() |