src/eric7/WebBrowser/OpenSearch/OpenSearchManager.py

branch
eric7
changeset 10436
f6881d10e995
parent 10373
093dcebe5ecb
child 10439
21c28b0f9e41
equal deleted inserted replaced
10435:c712d09cc839 10436:f6881d10e995
35 35
36 def __init__(self, parent=None): 36 def __init__(self, parent=None):
37 """ 37 """
38 Constructor 38 Constructor
39 39
40 @param parent reference to the parent object (QObject) 40 @param parent reference to the parent object
41 @type QObject
41 """ 42 """
42 if parent is None: 43 if parent is None:
43 parent = ericApp() 44 parent = ericApp()
44 super().__init__(parent) 45 super().__init__(parent)
45 46
62 63
63 def currentEngineName(self): 64 def currentEngineName(self):
64 """ 65 """
65 Public method to get the name of the current search engine. 66 Public method to get the name of the current search engine.
66 67
67 @return name of the current search engine (string) 68 @return name of the current search engine
69 @rtype str
68 """ 70 """
69 return self.__current 71 return self.__current
70 72
71 def setCurrentEngineName(self, name): 73 def setCurrentEngineName(self, name):
72 """ 74 """
73 Public method to set the current engine by name. 75 Public method to set the current engine by name.
74 76
75 @param name name of the new current engine (string) 77 @param name name of the new current engine
78 @type str
76 """ 79 """
77 if name not in self.__engines: 80 if name not in self.__engines:
78 return 81 return
79 82
80 self.__current = name 83 self.__current = name
83 86
84 def currentEngine(self): 87 def currentEngine(self):
85 """ 88 """
86 Public method to get a reference to the current engine. 89 Public method to get a reference to the current engine.
87 90
88 @return reference to the current engine (OpenSearchEngine) 91 @return reference to the current engine
92 @rtype OpenSearchEngine
89 """ 93 """
90 if not self.__current or self.__current not in self.__engines: 94 if not self.__current or self.__current not in self.__engines:
91 return None 95 return None
92 96
93 return self.__engines[self.__current] 97 return self.__engines[self.__current]
94 98
95 def setCurrentEngine(self, engine): 99 def setCurrentEngine(self, engine):
96 """ 100 """
97 Public method to set the current engine. 101 Public method to set the current engine.
98 102
99 @param engine reference to the new current engine (OpenSearchEngine) 103 @param engine reference to the new current engine
104 @type OpenSearchEngine
100 """ 105 """
101 if engine is None: 106 if engine is None:
102 return 107 return
103 108
104 for engineName in self.__engines: 109 for engineName in self.__engines:
108 113
109 def engine(self, name): 114 def engine(self, name):
110 """ 115 """
111 Public method to get a reference to the named engine. 116 Public method to get a reference to the named engine.
112 117
113 @param name name of the engine (string) 118 @param name name of the engine
114 @return reference to the engine (OpenSearchEngine) 119 @type str
120 @return reference to the engine
121 @rtype OpenSearchEngine
115 """ 122 """
116 if name not in self.__engines: 123 if name not in self.__engines:
117 return None 124 return None
118 125
119 return self.__engines[name] 126 return self.__engines[name]
120 127
121 def engineExists(self, name): 128 def engineExists(self, name):
122 """ 129 """
123 Public method to check, if an engine exists. 130 Public method to check, if an engine exists.
124 131
125 @param name name of the engine (string) 132 @param name name of the engine
126 @return flag indicating an existing engine (boolean) 133 @type str
134 @return flag indicating an existing engine
135 @rtype bool
127 """ 136 """
128 return name in self.__engines 137 return name in self.__engines
129 138
130 def allEnginesNames(self): 139 def allEnginesNames(self):
131 """ 140 """
132 Public method to get a list of all engine names. 141 Public method to get a list of all engine names.
133 142
134 @return sorted list of all engine names (list of strings) 143 @return sorted list of all engine names
144 @rtype list of str
135 """ 145 """
136 return sorted(self.__engines) 146 return sorted(self.__engines)
137 147
138 def enginesCount(self): 148 def enginesCount(self):
139 """ 149 """
140 Public method to get the number of available engines. 150 Public method to get the number of available engines.
141 151
142 @return number of engines (integer) 152 @return number of engines
153 @rtype int
143 """ 154 """
144 return len(self.__engines) 155 return len(self.__engines)
145 156
146 def addEngine(self, engine): 157 def addEngine(self, engine):
147 """ 158 """
148 Public method to add a new search engine. 159 Public method to add a new search engine.
149 160
150 @param engine URL of the engine definition file (QUrl) or 161 @param engine URL of the engine definition file (QUrl) or
151 name of a file containing the engine definition (string) 162 name of a file containing the engine definition
163 @type str
152 or reference to an engine object (OpenSearchEngine) 164 or reference to an engine object (OpenSearchEngine)
153 @return flag indicating success (boolean) 165 @return flag indicating success
166 @rtype bool
154 """ 167 """
155 from .OpenSearchEngine import OpenSearchEngine 168 from .OpenSearchEngine import OpenSearchEngine
156 169
157 if isinstance(engine, QUrl): 170 if isinstance(engine, QUrl):
158 return self.__addEngineByUrl(engine) 171 return self.__addEngineByUrl(engine)
163 176
164 def __addEngineByUrl(self, url): 177 def __addEngineByUrl(self, url):
165 """ 178 """
166 Private method to add a new search engine given its URL. 179 Private method to add a new search engine given its URL.
167 180
168 @param url URL of the engine definition file (QUrl) 181 @param url URL of the engine definition file
169 @return flag indicating success (boolean) 182 @type QUrl
183 @return flag indicating success
184 @rtype bool
170 """ 185 """
171 if not url.isValid(): 186 if not url.isValid():
172 return False 187 return False
173 188
174 reply = WebBrowserWindow.networkManager().get(QNetworkRequest(url)) 189 reply = WebBrowserWindow.networkManager().get(QNetworkRequest(url))
181 def __addEngineByFile(self, filename): 196 def __addEngineByFile(self, filename):
182 """ 197 """
183 Private method to add a new search engine given a filename. 198 Private method to add a new search engine given a filename.
184 199
185 @param filename name of a file containing the engine definition 200 @param filename name of a file containing the engine definition
186 (string) 201 @type str
187 @return flag indicating success (boolean) 202 @return flag indicating success
203 @rtype bool
188 """ 204 """
189 from .OpenSearchReader import OpenSearchReader 205 from .OpenSearchReader import OpenSearchReader
190 206
191 file_ = QFile(filename) 207 file_ = QFile(filename)
192 if not file_.open(QIODevice.OpenModeFlag.ReadOnly): 208 if not file_.open(QIODevice.OpenModeFlag.ReadOnly):
203 def __addEngineByEngine(self, engine): 219 def __addEngineByEngine(self, engine):
204 """ 220 """
205 Private method to add a new search engine given a reference to an 221 Private method to add a new search engine given a reference to an
206 engine. 222 engine.
207 223
208 @param engine reference to an engine object (OpenSearchEngine) 224 @param engine reference to an engine object
209 @return flag indicating success (boolean) 225 @type OpenSearchEngine
226 @return flag indicating success
227 @rtype bool
210 """ 228 """
211 if engine is None: 229 if engine is None:
212 return False 230 return False
213 231
214 if not engine.isValid(): 232 if not engine.isValid():
290 308
291 def removeEngine(self, name): 309 def removeEngine(self, name):
292 """ 310 """
293 Public method to remove an engine. 311 Public method to remove an engine.
294 312
295 @param name name of the engine (string) 313 @param name name of the engine
314 @type str
296 """ 315 """
297 if len(self.__engines) <= 1: 316 if len(self.__engines) <= 1:
298 return 317 return
299 318
300 if name not in self.__engines: 319 if name not in self.__engines:
317 336
318 def generateEngineFileName(self, engineName): 337 def generateEngineFileName(self, engineName):
319 """ 338 """
320 Public method to generate a valid engine file name. 339 Public method to generate a valid engine file name.
321 340
322 @param engineName name of the engine (string) 341 @param engineName name of the engine
323 @return valid engine file name (string) 342 @type str
343 @return valid engine file name
344 @rtype str
324 """ 345 """
325 fileName = "" 346 fileName = ""
326 347
327 # strip special characters 348 # strip special characters
328 for c in engineName: 349 for c in engineName:
339 360
340 def saveDirectory(self, dirName): 361 def saveDirectory(self, dirName):
341 """ 362 """
342 Public method to save the search engine definitions to files. 363 Public method to save the search engine definitions to files.
343 364
344 @param dirName name of the directory to write the files to (string) 365 @param dirName name of the directory to write the files to
366 @type str
345 """ 367 """
346 from .OpenSearchWriter import OpenSearchWriter 368 from .OpenSearchWriter import OpenSearchWriter
347 369
348 qdir = QDir() 370 qdir = QDir()
349 if not qdir.mkpath(dirName): 371 if not qdir.mkpath(dirName):
380 402
381 def loadDirectory(self, dirName): 403 def loadDirectory(self, dirName):
382 """ 404 """
383 Public method to load the search engine definitions from files. 405 Public method to load the search engine definitions from files.
384 406
385 @param dirName name of the directory to load the files from (string) 407 @param dirName name of the directory to load the files from
386 @return flag indicating success (boolean) 408 @type str
409 @return flag indicating success
410 @rtype bool
387 """ 411 """
388 if not os.path.exists(dirName): 412 if not os.path.exists(dirName):
389 return False 413 return False
390 414
391 success = False 415 success = False
438 def enginesDirectory(self): 462 def enginesDirectory(self):
439 """ 463 """
440 Public method to determine the directory containing the search engine 464 Public method to determine the directory containing the search engine
441 descriptions. 465 descriptions.
442 466
443 @return directory name (string) 467 @return directory name
468 @rtype str
444 """ 469 """
445 return os.path.join(Globals.getConfigDir(), "web_browser", "searchengines") 470 return os.path.join(Globals.getConfigDir(), "web_browser", "searchengines")
446 471
447 def __confirmAddition(self, engine): 472 def __confirmAddition(self, engine):
448 """ 473 """
449 Private method to confirm the addition of a new search engine. 474 Private method to confirm the addition of a new search engine.
450 475
451 @param engine reference to the engine to be added (OpenSearchEngine) 476 @param engine reference to the engine to be added
452 @return flag indicating the engine shall be added (boolean) 477 @type OpenSearchEngine
478 @return flag indicating the engine shall be added
479 @rtype bool
453 """ 480 """
454 if engine is None or not engine.isValid(): 481 if engine is None or not engine.isValid():
455 return False 482 return False
456 483
457 host = QUrl(engine.searchUrlTemplate()).host() 484 host = QUrl(engine.searchUrlTemplate()).host()
503 530
504 def convertKeywordSearchToUrl(self, keywordSearch): 531 def convertKeywordSearchToUrl(self, keywordSearch):
505 """ 532 """
506 Public method to get the search URL for a keyword search. 533 Public method to get the search URL for a keyword search.
507 534
508 @param keywordSearch search string for keyword search (string) 535 @param keywordSearch search string for keyword search
509 @return search URL (QUrl) 536 @type str
537 @return search URL
538 @rtype QUrl
510 """ 539 """
511 try: 540 try:
512 keyword, term = keywordSearch.split(" ", 1) 541 keyword, term = keywordSearch.split(" ", 1)
513 except ValueError: 542 except ValueError:
514 return QUrl() 543 return QUrl()
524 553
525 def engineForKeyword(self, keyword): 554 def engineForKeyword(self, keyword):
526 """ 555 """
527 Public method to get the engine for a keyword. 556 Public method to get the engine for a keyword.
528 557
529 @param keyword keyword to get engine for (string) 558 @param keyword keyword to get engine for
530 @return reference to the search engine object (OpenSearchEngine) 559 @type str
560 @return reference to the search engine object
561 @rtype OpenSearchEngine
531 """ 562 """
532 if keyword and keyword in self.__keywords: 563 if keyword and keyword in self.__keywords:
533 return self.__keywords[keyword] 564 return self.__keywords[keyword]
534 565
535 return None 566 return None
536 567
537 def setEngineForKeyword(self, keyword, engine): 568 def setEngineForKeyword(self, keyword, engine):
538 """ 569 """
539 Public method to set the engine for a keyword. 570 Public method to set the engine for a keyword.
540 571
541 @param keyword keyword to get engine for (string) 572 @param keyword keyword to get engine for
542 @param engine reference to the search engine object (OpenSearchEngine) 573 @type str
543 or None to remove the keyword 574 @param engine reference to the search engine object or None to
575 remove the keyword
576 @type OpenSearchEngine
544 """ 577 """
545 if not keyword: 578 if not keyword:
546 return 579 return
547 580
548 if engine is None: 581 if engine is None:
555 588
556 def keywordsForEngine(self, engine): 589 def keywordsForEngine(self, engine):
557 """ 590 """
558 Public method to get the keywords for a given engine. 591 Public method to get the keywords for a given engine.
559 592
560 @param engine reference to the search engine object (OpenSearchEngine) 593 @param engine reference to the search engine object
561 @return list of keywords (list of strings) 594 @type OpenSearchEngine
595 @return list of keywords
596 @rtype list of str
562 """ 597 """
563 return [k for k in self.__keywords if self.__keywords[k] == engine] 598 return [k for k in self.__keywords if self.__keywords[k] == engine]
564 599
565 def setKeywordsForEngine(self, engine, keywords): 600 def setKeywordsForEngine(self, engine, keywords):
566 """ 601 """
567 Public method to set the keywords for an engine. 602 Public method to set the keywords for an engine.
568 603
569 @param engine reference to the search engine object (OpenSearchEngine) 604 @param engine reference to the search engine object
570 @param keywords list of keywords (list of strings) 605 @type OpenSearchEngine
606 @param keywords list of keywords
607 @type list of str
571 """ 608 """
572 if engine is None: 609 if engine is None:
573 return 610 return
574 611
575 for keyword in self.keywordsForEngine(engine): 612 for keyword in self.keywordsForEngine(engine):

eric ide

mercurial