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 web browser and starts the Qt event loop. This is a standalone version |
11 of the web browser and starts the Qt event loop. This is a standalone version |
12 of the integrated web browser. It is based on QtWebEngine. |
12 of the integrated web browser. It is based on QtWebEngine. |
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 |
|
21 from eric7 import Globals |
|
22 |
|
23 |
|
24 def createArgparseNamespace(): |
|
25 """ |
|
26 Function to create an argument parser. |
|
27 |
|
28 @return created argument parser object |
|
29 @rtype argparse.ArgumentParser |
|
30 """ |
|
31 from eric7.UI.Info import Version |
|
32 |
|
33 # 1. create the argument parser |
|
34 parser = argparse.ArgumentParser( |
|
35 description="Web Browser application of the eric tool suite.", |
|
36 epilog="Copyright (c) 2002 - 2023 Detlev Offenbach <detlev@die-offenbachs.de>.", |
|
37 ) |
|
38 |
|
39 # 2. add the arguments |
|
40 parser.add_argument( |
|
41 "-V", |
|
42 "--version", |
|
43 action="version", |
|
44 version="%(prog)s {0}".format(Version), |
|
45 help="show version information and exit", |
|
46 ) |
|
47 parser.add_argument( |
|
48 "--config", |
|
49 metavar="config_dir", |
|
50 help="use the given directory as the one containing the config files", |
|
51 ) |
|
52 parser.add_argument( |
|
53 "--settings", |
|
54 metavar="settings_dir", |
|
55 help="use the given directory to store the settings files", |
|
56 ) |
|
57 parser.add_argument( |
|
58 "--name", |
|
59 metavar="browser name", |
|
60 default="", |
|
61 help="name to be used for the browser instance", |
|
62 ) |
|
63 parser.add_argument( |
|
64 "--new-tab", |
|
65 metavar="URL", |
|
66 action="append", |
|
67 help="open a new tab for the given URL", |
|
68 ) |
|
69 parser.add_argument( |
|
70 "--private", |
|
71 action="store_true", |
|
72 help="start the browser in private browsing mode", |
|
73 ) |
|
74 parser.add_argument( |
|
75 "--qthelp", |
|
76 action="store_true", |
|
77 help="start the browser with support for QtHelp", |
|
78 ) |
|
79 parser.add_argument( |
|
80 "--quiet", |
|
81 action="store_true", |
|
82 help="don't show any startup error messages", |
|
83 ) |
|
84 parser.add_argument( |
|
85 "--search", |
|
86 metavar="searchword", |
|
87 help="search for the given word", |
|
88 ) |
|
89 parser.add_argument( |
|
90 "--shutdown", |
|
91 action="store_true", |
|
92 help="shut down the browser instance", |
|
93 ) |
|
94 parser.add_argument( |
|
95 "--single", |
|
96 action="store_true", |
|
97 help="start the browser as a single application", |
|
98 ) |
|
99 parser.add_argument( |
|
100 "home", |
|
101 nargs="?", |
|
102 default="", |
|
103 metavar="file | URL", |
|
104 help="open a file or URL", |
|
105 ) |
|
106 |
|
107 # 3. create the Namespace object by parsing the command line |
|
108 args = parser.parse_args() |
|
109 return args |
|
110 |
|
111 |
|
112 args = createArgparseNamespace() |
|
113 if args.config: |
|
114 Globals.setConfigDir(args.config) |
|
115 if args.settings: |
|
116 from PyQt6.QtCore import QSettings |
|
117 |
|
118 SettingsDir = os.path.expanduser(args.settings) |
|
119 if not os.path.isdir(SettingsDir): |
|
120 os.makedirs(SettingsDir) |
|
121 QSettings.setPath( |
|
122 QSettings.Format.IniFormat, QSettings.Scope.UserScope, SettingsDir |
|
123 ) |
|
124 else: |
|
125 SettingsDir = None |
|
126 |
20 app = None |
127 app = None |
21 SettingsDir = None |
|
22 |
|
23 from eric7 import Globals |
|
24 |
|
25 for arg in sys.argv[:]: |
|
26 if arg.startswith("--config="): |
|
27 configDir = arg.replace("--config=", "") |
|
28 Globals.setConfigDir(configDir) |
|
29 sys.argv.remove(arg) |
|
30 elif arg.startswith("--settings="): |
|
31 from PyQt6.QtCore import QSettings |
|
32 |
|
33 SettingsDir = os.path.expanduser(arg.replace("--settings=", "")) |
|
34 if not os.path.isdir(SettingsDir): |
|
35 os.makedirs(SettingsDir) |
|
36 QSettings.setPath( |
|
37 QSettings.Format.IniFormat, QSettings.Scope.UserScope, SettingsDir |
|
38 ) |
|
39 sys.argv.remove(arg) |
|
40 |
128 |
41 try: |
129 try: |
42 from PyQt6 import QtWebEngineWidgets # __IGNORE_WARNING__ |
130 from PyQt6 import QtWebEngineWidgets # __IGNORE_WARNING__ |
43 from PyQt6.QtWebEngineCore import QWebEngineUrlScheme |
131 from PyQt6.QtWebEngineCore import QWebEngineUrlScheme |
44 except ImportError: |
132 except ImportError: |
60 ) |
148 ) |
61 app.exec() |
149 app.exec() |
62 sys.exit(100) |
150 sys.exit(100) |
63 |
151 |
64 from eric7.EricWidgets.EricApplication import EricApplication |
152 from eric7.EricWidgets.EricApplication import EricApplication |
65 from eric7.Globals import AppInfo |
|
66 from eric7.Toolbox import Startup |
153 from eric7.Toolbox import Startup |
67 from eric7.WebBrowser.WebBrowserSingleApplication import ( |
154 from eric7.WebBrowser.WebBrowserSingleApplication import ( |
68 WebBrowserSingleApplicationClient, |
155 WebBrowserSingleApplicationClient, |
69 ) |
156 ) |
70 |
157 |
71 |
158 |
72 def createMainWidget(argv): |
159 def createMainWidget(args): |
73 """ |
160 """ |
74 Function to create the main widget. |
161 Function to create the main widget. |
75 |
162 |
76 @param argv list of command line parameters |
163 @param args namespace object containing the parsed command line parameters |
77 @type list of str |
164 @type argparse.Namespace |
78 @return reference to the main widget |
165 @return reference to the main widget |
79 @rtype QWidget |
166 @rtype QWidget |
80 """ |
167 """ |
81 from eric7.WebBrowser.WebBrowserWindow import WebBrowserWindow |
168 from eric7.WebBrowser.WebBrowserWindow import WebBrowserWindow |
82 |
169 |
83 searchWord = None |
|
84 private = False |
|
85 qthelp = False |
|
86 single = False |
|
87 name = "" |
|
88 |
|
89 for arg in reversed(argv): |
|
90 if arg.startswith("--search="): |
|
91 searchWord = argv[1].split("=", 1)[1] |
|
92 argv.remove(arg) |
|
93 elif arg.startswith("--name="): |
|
94 name = arg.replace("--name=", "") |
|
95 argv.remove(arg) |
|
96 elif arg == "--private": |
|
97 private = True |
|
98 argv.remove(arg) |
|
99 elif arg == "--qthelp": |
|
100 qthelp = True |
|
101 argv.remove(arg) |
|
102 elif arg == "--single": |
|
103 single = True |
|
104 argv.remove(arg) |
|
105 elif arg.startswith(("--newtab=", "--")) or arg == "--quiet": |
|
106 # only needed until we reach this point |
|
107 argv.remove(arg) |
|
108 |
|
109 try: |
|
110 home = argv[1] |
|
111 except IndexError: |
|
112 home = "" |
|
113 |
|
114 browser = WebBrowserWindow( |
170 browser = WebBrowserWindow( |
115 home, |
171 args.home, |
116 ".", |
172 ".", |
117 None, |
173 None, |
118 "web_browser", |
174 "web_browser", |
119 searchWord=searchWord, |
175 searchWord=args.search, |
120 private=private, |
176 private=args.private, |
121 settingsDir=SettingsDir, |
177 settingsDir=SettingsDir, |
122 qthelp=qthelp, |
178 qthelp=args.qthelp, |
123 single=single, |
179 single=args.single, |
124 saname=name, |
180 saname=args.name, |
125 ) |
181 ) |
126 return browser |
182 return browser |
127 |
183 |
128 |
184 |
129 def main(): |
185 def main(): |
131 Main entry point into the application. |
187 Main entry point into the application. |
132 """ |
188 """ |
133 global app |
189 global app |
134 |
190 |
135 QGuiApplication.setDesktopFileName("eric7_browser") |
191 QGuiApplication.setDesktopFileName("eric7_browser") |
136 |
|
137 options = [ |
|
138 ( |
|
139 "--config=configDir", |
|
140 "use the given directory as the one containing the config files", |
|
141 ), |
|
142 ("--private", "start the browser in private browsing mode"), |
|
143 ("--qthelp", "start the browser with support for QtHelp"), |
|
144 ("--quiet", "don't show any startup error messages"), |
|
145 ("--search=word", "search for the given word"), |
|
146 ( |
|
147 "--settings=settingsDir", |
|
148 "use the given directory to store the settings files", |
|
149 ), |
|
150 ("--single", "start the browser as a single application"), |
|
151 ] |
|
152 appinfo = AppInfo.makeAppInfo( |
|
153 sys.argv, "eric Web Browser", "file", "web browser", options |
|
154 ) |
|
155 |
192 |
156 # set the library paths for plugins |
193 # set the library paths for plugins |
157 Startup.setLibraryPaths() |
194 Startup.setLibraryPaths() |
158 |
195 |
159 scheme = QWebEngineUrlScheme(b"eric") |
196 scheme = QWebEngineUrlScheme(b"eric") |
167 scheme = QWebEngineUrlScheme(b"qthelp") |
204 scheme = QWebEngineUrlScheme(b"qthelp") |
168 scheme.setSyntax(QWebEngineUrlScheme.Syntax.Path) |
205 scheme.setSyntax(QWebEngineUrlScheme.Syntax.Path) |
169 scheme.setFlags(QWebEngineUrlScheme.Flag.SecureScheme) |
206 scheme.setFlags(QWebEngineUrlScheme.Flag.SecureScheme) |
170 QWebEngineUrlScheme.registerScheme(scheme) |
207 QWebEngineUrlScheme.registerScheme(scheme) |
171 |
208 |
172 app = EricApplication(sys.argv) |
209 app = EricApplication(args) |
173 if "--private" not in sys.argv: |
210 if not args.private: |
174 client = WebBrowserSingleApplicationClient() |
211 client = WebBrowserSingleApplicationClient() |
175 res = client.connect() |
212 res = client.connect() |
176 if res > 0: |
213 if res > 0: |
177 if len(sys.argv) > 1: |
214 client.processArgs(args) |
178 client.processArgs(sys.argv[1:]) |
|
179 sys.exit(0) |
215 sys.exit(0) |
180 elif res < 0: |
216 elif res < 0: |
181 print("eric7_browser: {0}".format(client.errstr())) |
217 print("eric7_browser: {0}".format(client.errstr())) |
182 # __IGNORE_WARNING_M801__ |
218 # __IGNORE_WARNING_M801__ |
183 sys.exit(res) |
219 sys.exit(res) |
184 |
220 |
185 res = Startup.simpleAppStartup( |
221 res = Startup.appStartup(args, createMainWidget, installErrorHandler=True, app=app) |
186 sys.argv, appinfo, createMainWidget, installErrorHandler=True, app=app |
|
187 ) |
|
188 sys.exit(res) |
222 sys.exit(res) |
189 |
223 |
190 |
224 |
191 if __name__ == "__main__": |
225 if __name__ == "__main__": |
192 main() |
226 main() |