From 558b20d1acc553f4ffc4361e74e1b955b98a661d Mon Sep 17 00:00:00 2001 From: Ilmari Lauhakangas Date: Sat, 27 Jul 2024 14:54:40 +0300 Subject: [PATCH] comphelper, msicreator, odk: fix issues found by Ruff linter Change-Id: I5ef65baeb0100b9d70b5c59c1d5f6f43ab951a22 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/171092 Reviewed-by: Ilmari Lauhakangas Tested-by: Jenkins Tested-by: Ilmari Lauhakangas --- .../qa/python/test_sequence_output_stream.py | 2 +- msicreator/create_installer.py | 3 +- msicreator/createmsi.py | 6 +- .../python/HelloTextTableShape.py | 2 +- .../DocumentHandling/DocumentConverter.py | 2 +- .../python/Miscellaneous/InputEvents.py | 109 +++++++++--------- odk/examples/python/Text/BookmarkInsertion.py | 12 +- odk/examples/python/Text/GraphicsInserter.py | 8 +- odk/examples/python/Text/HardFormatting.py | 4 +- odk/examples/python/Text/SWriter.py | 6 +- odk/examples/python/Text/StyleCreation.py | 4 +- .../python/Text/StyleInitialization.py | 9 +- .../python/Text/TextDocumentStructure.py | 4 +- odk/examples/python/Text/TextReplace.py | 8 +- odk/examples/python/Text/WriterSelector.py | 4 +- odk/source/helper/addon_console.py | 1 - 16 files changed, 94 insertions(+), 90 deletions(-) diff --git a/comphelper/qa/python/test_sequence_output_stream.py b/comphelper/qa/python/test_sequence_output_stream.py index 6f1294960e29..3496c46303af 100644 --- a/comphelper/qa/python/test_sequence_output_stream.py +++ b/comphelper/qa/python/test_sequence_output_stream.py @@ -30,7 +30,7 @@ class TestSequenceOutputStream(unittest.TestCase): self.data = uno.ByteSequence(b"some data") try: self.service_manager = self._uno.getContext().getServiceManager() - except: + except Exception: raise RuntimeError("Cannot create service factory!") if self.service_manager is None: raise RuntimeError("Cannot create service factory!") diff --git a/msicreator/create_installer.py b/msicreator/create_installer.py index 01394a48c4a3..5e40bb9547ca 100644 --- a/msicreator/create_installer.py +++ b/msicreator/create_installer.py @@ -1,4 +1,5 @@ -import os, sys +import os +import sys from shutil import copytree, copy2, move, rmtree import json import createmsi diff --git a/msicreator/createmsi.py b/msicreator/createmsi.py index d728e64520d2..0f720b74d380 100644 --- a/msicreator/createmsi.py +++ b/msicreator/createmsi.py @@ -14,7 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys, os, subprocess, shutil, uuid, json, re +import sys +import os +import subprocess +import uuid +import json from glob import glob import platform import xml.etree.ElementTree as ET diff --git a/odk/examples/DevelopersGuide/FirstSteps/HelloTextTableShape/python/HelloTextTableShape.py b/odk/examples/DevelopersGuide/FirstSteps/HelloTextTableShape/python/HelloTextTableShape.py index 2e2ab2ee65b3..437ca5491d92 100644 --- a/odk/examples/DevelopersGuide/FirstSteps/HelloTextTableShape/python/HelloTextTableShape.py +++ b/odk/examples/DevelopersGuide/FirstSteps/HelloTextTableShape/python/HelloTextTableShape.py @@ -37,7 +37,7 @@ class HelloTextTableShape: self.remote_context = officehelper.bootstrap() print("Connected to a running office ...") return self.remote_context.ServiceManager - except Exception as e: + except Exception: traceback.print_exc() sys.exit(1) diff --git a/odk/examples/python/DocumentHandling/DocumentConverter.py b/odk/examples/python/DocumentHandling/DocumentConverter.py index c2683b8c7cb7..4613a8b797e2 100644 --- a/odk/examples/python/DocumentHandling/DocumentConverter.py +++ b/odk/examples/python/DocumentHandling/DocumentConverter.py @@ -50,7 +50,7 @@ def convert(src_file, dest_file, to_type): PropertyValue(Name="FilterName", Value=to_type), ) try: - doc.storeAsURL(dest_url, opts); + doc.storeAsURL(dest_url, opts) finally: doc.dispose() diff --git a/odk/examples/python/Miscellaneous/InputEvents.py b/odk/examples/python/Miscellaneous/InputEvents.py index 9fee6aadc29a..8dc443e2f6d2 100644 --- a/odk/examples/python/Miscellaneous/InputEvents.py +++ b/odk/examples/python/Miscellaneous/InputEvents.py @@ -7,14 +7,15 @@ # file, You can obtain one at https://mozilla.org/MPL/2.0/. # -import uno, unohelper; -from com.sun.star.awt import XKeyHandler; -from com.sun.star.awt import XKeyListener; -from com.sun.star.awt import XMouseClickHandler; -from com.sun.star.awt import XMouseMotionHandler; -from com.sun.star.awt import XMouseListener; -from com.sun.star.awt import XMouseMotionListener; -from com.sun.star.accessibility import AccessibleRole; +import uno +import unohelper +from com.sun.star.awt import XKeyHandler +from com.sun.star.awt import XKeyListener +from com.sun.star.awt import XMouseClickHandler +from com.sun.star.awt import XMouseMotionHandler +from com.sun.star.awt import XMouseListener +from com.sun.star.awt import XMouseMotionListener +from com.sun.star.accessibility import AccessibleRole """ @@ -34,36 +35,36 @@ SOFFICE_CONNECTION_URI = "uno:socket,host=localhost,port=2083;urp;StarOffice.Com def demo(): # Connect to LibreOffice process - localComponentContext = uno.getComponentContext(); - localServiceManager = localComponentContext.getServiceManager(); - resolver = localServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localComponentContext); - remoteComponentContext = resolver.resolve(SOFFICE_CONNECTION_URI); + localComponentContext = uno.getComponentContext() + localServiceManager = localComponentContext.getServiceManager() + resolver = localServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localComponentContext) + remoteComponentContext = resolver.resolve(SOFFICE_CONNECTION_URI) # Get the currently opened view context. - remoteServiceManager = remoteComponentContext.getServiceManager(); - desktop = remoteServiceManager.createInstance("com.sun.star.frame.Desktop"); - xComponent = desktop.getCurrentComponent(); # e.g. SwXTextDocument, ScModelObj, SdXImpressDocument + remoteServiceManager = remoteComponentContext.getServiceManager() + desktop = remoteServiceManager.createInstance("com.sun.star.frame.Desktop") + xComponent = desktop.getCurrentComponent() # e.g. SwXTextDocument, ScModelObj, SdXImpressDocument if "com.sun.star.document.OfficeDocument" not in xComponent.getSupportedServiceNames(): - print("No OfficeDocument opened."); - exit(1); + print("No OfficeDocument opened.") + exit(1) else: - handler = MyXKeyMouseClickMotionHandler("handler"); + handler = MyXKeyMouseClickMotionHandler("handler") # Events in the document view area. Not in the surrounding UI. - xController = xComponent.getCurrentController(); # xModel.getCurrentController() - xController.addKeyHandler(handler); # XUserInputInterception.addKeyHandler() - xController.addMouseClickHandler(handler); # XUserInputInterception.addMouseClickHandler() + xController = xComponent.getCurrentController() # xModel.getCurrentController() + xController.addKeyHandler(handler) # XUserInputInterception.addKeyHandler() + xController.addMouseClickHandler(handler) # XUserInputInterception.addMouseClickHandler() - listener = MyXKeyMouseClickMotionHandler("listener"); + listener = MyXKeyMouseClickMotionHandler("listener") # TODO: Register to e.value.Source of Handler events. # That's the correct source. - xWindow = xController.ComponentWindow; + xWindow = xController.ComponentWindow # In writer we're usually looking for: # xWindow.Windows[0].Windows[0] recursive_windows("", xWindow, lambda subXWindow: ( subXWindow.addKeyListener(listener), subXWindow.addMouseListener(listener), #subXWindow.addMouseMotionListener(listener), # very much events - )); + )) # Maybe the event handlers can also be registered via one of the objects # this function iterates through. But currently this function just @@ -71,72 +72,72 @@ def demo(): #recursive_acc_ctx("", xWindow.AccessibleContext.AccessibleParent); #recursive_acc_ctx("", xWindow); - input("Waiting for events. Press Enter to quit...\n"); + input("Waiting for events. Press Enter to quit...\n") def recursive_acc_ctx(path, obj): print("recursive_acc_ctx: " + path + find_accessible_role(obj.getAccessibleContext().getAccessibleRole())) for i in range(obj.getAccessibleContext().AccessibleChildCount): - recursive_acc_ctx(path+str(i)+": ", obj.getAccessibleContext().getAccessibleChild(i)); + recursive_acc_ctx(path+str(i)+": ", obj.getAccessibleContext().getAccessibleChild(i)) def recursive_windows(path, xWindow, func): print("recursive_windows: " + path + find_accessible_role(xWindow.getAccessibleContext().getAccessibleRole())) - func(xWindow); + func(xWindow) try: - windows = xWindow.getWindows(); # XVclContainer.getWindows() - except: - return; + windows = xWindow.getWindows() # XVclContainer.getWindows() + except Exception: + return for i in range(len(windows)): - subXWindow = windows[i]; - recursive_windows(path+str(i)+": ", subXWindow, func); + subXWindow = windows[i] + recursive_windows(path+str(i)+": ", subXWindow, func) def find_accessible_role(role_int): for role_name in dir(AccessibleRole): if role_int == eval("AccessibleRole." + role_name): - return role_name; + return role_name class MyXKeyMouseClickMotionHandler(unohelper.Base, XKeyHandler, XKeyListener, XMouseClickHandler, XMouseMotionHandler, XMouseListener, XMouseMotionListener): def __init__(self, name): # XKeyHandler, XKeyListener - self.name = name; + self.name = name def keyPressed(self, e): # XKeyHandler, XKeyListener - self.key_evt(e, "pressed"); - return False; # False: don't consume (run other event handlers) + self.key_evt(e, "pressed") + return False # False: don't consume (run other event handlers) def keyReleased(self, e): - self.key_evt(e, "released"); - return False; + self.key_evt(e, "released") + return False def mousePressed(self, e): # XMouseClickHandler, XMouseListener - self.mouse_evt(e, "pressed"); - return False; + self.mouse_evt(e, "pressed") + return False def mouseReleased(self, e): # XMouseClickHandler, XMouseListener - self.mouse_evt(e, "released"); - return False; + self.mouse_evt(e, "released") + return False def mouseEntered(self, e): # XMouseListener - self.mouse_evt(e, "entered"); - return False; + self.mouse_evt(e, "entered") + return False def mouseExited(self, e): # XMouseListener - self.mouse_evt(e, "exited"); - return False; + self.mouse_evt(e, "exited") + return False def mouseDragged(self, e): # XMouseMotionHandler, XMouseMotionListener - self.mouse_evt(e, "dragged"); - return False; + self.mouse_evt(e, "dragged") + return False def mouseMoved(self, e): # XMouseMotionHandler, XMouseMotionListener - self.mouse_evt(e, "moved"); - return False; + self.mouse_evt(e, "moved") + return False def disposing(self, s): - print(self.name + "# disposing"); + print(self.name + "# disposing") def key_evt(self, e, action): #print(self.name + "# key "+action+": " + str(e)); # very much output - print(self.name + "# key "+action+" (code: " + str(e.value.KeyCode.real) + "): " + e.value.KeyChar.value); + print(self.name + "# key "+action+" (code: " + str(e.value.KeyCode.real) + "): " + e.value.KeyChar.value) def mouse_evt(self, e, action): #print(self.name + "# mouse "+action+": " + str(e)); # very much output - print(self.name + "# mouse "+action+": Modifiers: "+str(e.value.Modifiers)+"; Buttons: "+str(e.value.Buttons)+"; X: "+str(e.value.X)+"; Y: "+str(e.value.Y)+"; ClickCount: "+str(e.value.ClickCount)+"; PopupTrigger: "+str(e.value.PopupTrigger)); + print(self.name + "# mouse "+action+": Modifiers: "+str(e.value.Modifiers)+"; Buttons: "+str(e.value.Buttons)+"; X: "+str(e.value.X)+"; Y: "+str(e.value.Y)+"; ClickCount: "+str(e.value.ClickCount)+"; PopupTrigger: "+str(e.value.PopupTrigger)) -demo(); +demo() # vim: set shiftwidth=4 softtabstop=4 expandtab: diff --git a/odk/examples/python/Text/BookmarkInsertion.py b/odk/examples/python/Text/BookmarkInsertion.py index 5b801f6121ea..d65887cc21fe 100644 --- a/odk/examples/python/Text/BookmarkInsertion.py +++ b/odk/examples/python/Text/BookmarkInsertion.py @@ -54,7 +54,7 @@ def create_example_text(component): cursor.collapseToEnd() cursor.setString(THIRD_PARAGRAPH) cursor.gotoStart(False) - except: + except Exception: traceback.print_exc() @@ -71,7 +71,7 @@ def find_first(document, search_str): descriptor.setSearchString(search_str) descriptor.setPropertyValue("SearchRegularExpression", True) return document.findFirst(descriptor) - except: + except Exception: traceback.print_exc() return None @@ -89,7 +89,7 @@ def insert_bookmark(document, text_range, bookmark_name): bookmark.setName(bookmark_name) document.getText().insertTextContent(text_range, bookmark, True) print("Insert bookmark:", bookmark_name) - except: + except Exception: traceback.print_exc() @@ -106,7 +106,7 @@ def mark_list(component, mlist, prefix): if not search: continue insert_bookmark(component, search, f"{prefix}{i}") - except: + except Exception: traceback.print_exc() sys.exit(1) @@ -121,7 +121,7 @@ def get_desktop(): else: desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context) print("Connected to a running office ...") - except: + except Exception: traceback.print_exc() sys.exit(1) return desktop @@ -135,7 +135,7 @@ def main(): # Open an empty text document. try: doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, tuple([])) - except: + except Exception: traceback.print_exc() sys.exit(1) diff --git a/odk/examples/python/Text/GraphicsInserter.py b/odk/examples/python/Text/GraphicsInserter.py index 3d09aedd5a13..635f03e25276 100644 --- a/odk/examples/python/Text/GraphicsInserter.py +++ b/odk/examples/python/Text/GraphicsInserter.py @@ -34,14 +34,14 @@ def insert_graphic(filename): try: graphic = doc.createInstance("com.sun.star.text.TextGraphicObject") - except: + except Exception: traceback.print_exc(file=log_file) return log_file.write("inserting graphic\n") try: text.insertTextContent(cursor, graphic, True) - except: + except Exception: print("Could not insert Content") traceback.print_exc() return @@ -70,7 +70,7 @@ def insert_graphic(filename): graphic.setPropertyValue("Width", 4400) # Setting the height graphic.setPropertyValue("Height", 4000) - except: + except Exception: print("Couldn't set property 'GraphicURL'") traceback.print_exc(file=log_file) @@ -89,7 +89,7 @@ def main(): args = parser.parse_args() try: insert_graphic(args.image) - except: + except Exception: traceback.print_exc() sys.exit(1) diff --git a/odk/examples/python/Text/HardFormatting.py b/odk/examples/python/Text/HardFormatting.py index 3196ac33dd7c..388947ff1efc 100644 --- a/odk/examples/python/Text/HardFormatting.py +++ b/odk/examples/python/Text/HardFormatting.py @@ -27,7 +27,7 @@ def get_desktop(): print("Can't create a desktop. No connection, no remote office servicemanager available!") else: desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context) - except: + except Exception: traceback.print_exc() sys.exit(1) return desktop @@ -85,7 +85,7 @@ def main(): text_range.setPropertyToDefault("CharWeight") # Then, check again check_property_state(text_range, text_range.getPropertyState("CharWeight")) - except: + except Exception: traceback.print_exc() sys.exit(1) diff --git a/odk/examples/python/Text/SWriter.py b/odk/examples/python/Text/SWriter.py index 3af42f352e70..9927c2647b99 100644 --- a/odk/examples/python/Text/SWriter.py +++ b/odk/examples/python/Text/SWriter.py @@ -32,8 +32,8 @@ def main(): print("Opening an empty Writer document") doc_url = "private:factory/swriter" doc = desktop.loadComponentFromURL(doc_url, "_blank", 0, tuple()) - except Exception as e: - print(f"d", file=sys.stderr) + except Exception: + print("d", file=sys.stderr) traceback.print_exc() sys.exit(1) @@ -55,7 +55,7 @@ def generate(doc): try: cursor.setPropertyValue("CharColor", 255) cursor.setPropertyValue("CharShadowed", True) - except: + except Exception: print("Couldn't change the color", file=sys.stderr) traceback.print_exc() diff --git a/odk/examples/python/Text/StyleCreation.py b/odk/examples/python/Text/StyleCreation.py index 629b530e9f83..126189f47a88 100644 --- a/odk/examples/python/Text/StyleCreation.py +++ b/odk/examples/python/Text/StyleCreation.py @@ -66,7 +66,7 @@ def get_desktop(): print("Can't create a desktop. No connection, no remote office servicemanager available!") else: desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context) - except: + except Exception: traceback.print_exc() sys.exit(1) return desktop @@ -81,7 +81,7 @@ def main(): doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, tuple()) create_style(doc) apply_style(doc) - except: + except Exception: traceback.print_exc() sys.exit(1) diff --git a/odk/examples/python/Text/StyleInitialization.py b/odk/examples/python/Text/StyleInitialization.py index a4d29b34431e..84f67369a781 100644 --- a/odk/examples/python/Text/StyleInitialization.py +++ b/odk/examples/python/Text/StyleInitialization.py @@ -23,7 +23,7 @@ def get_desktop(): print("Can't create a desktop. No connection, no remote office servicemanager available!") else: desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context) - except: + except Exception: traceback.print_exc() sys.exit(1) return desktop @@ -43,13 +43,13 @@ def main(): try: cursor.setPropertyValue("CharFontName", "Arial") - except: + except Exception: pass text.insertString(cursor, "Headline", False) try: cursor.setPropertyValue("CharFontName", "Liberation Sans") - except: + except Exception: pass text.insertControlCharacter(cursor, PARAGRAPH_BREAK, False) text.insertString(cursor, "A very short paragraph for illustration only", False) @@ -72,7 +72,6 @@ def main(): paragraph_enumeration = text.createEnumeration() while paragraph_enumeration.hasMoreElements(): paragraph = paragraph_enumeration.nextElement() - paragraph_text = paragraph.getAnchor().getString() # Create a cursor from this paragraph paragraph_cursor = paragraph.getAnchor().getText().createTextCursor() @@ -96,7 +95,7 @@ def main(): text_range.setPropertyValue("ParaStyleName", style_name) print("Apply the paragraph style:", style_name) break - except: + except Exception: traceback.print_exc() print("Done") diff --git a/odk/examples/python/Text/TextDocumentStructure.py b/odk/examples/python/Text/TextDocumentStructure.py index da677113a188..e082bd8345ed 100644 --- a/odk/examples/python/Text/TextDocumentStructure.py +++ b/odk/examples/python/Text/TextDocumentStructure.py @@ -28,7 +28,7 @@ def main(): text = doc.getText() create_example_data(text) display_structure(text) - except: + except Exception: traceback.print_exc() sys.exit(1) @@ -44,7 +44,7 @@ def create_example_data(text): cursor.gotoEndOfWord(True) cursor.setPropertyValue("CharWeight", BOLD) print("create example data") - except: + except Exception: traceback.print_exc() diff --git a/odk/examples/python/Text/TextReplace.py b/odk/examples/python/Text/TextReplace.py index 3aeb12cfa88e..c1b94a58c49e 100644 --- a/odk/examples/python/Text/TextReplace.py +++ b/odk/examples/python/Text/TextReplace.py @@ -23,7 +23,7 @@ def get_desktop(): print("Can't create a desktop. No connection, no remote office servicemanager available!") else: desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context) - except: + except Exception: traceback.print_exc() sys.exit(1) return desktop @@ -38,7 +38,7 @@ def main(): try: doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, tuple()) - except: + except Exception: traceback.print_exc() sys.exit(1) @@ -57,7 +57,7 @@ def main(): replaced_cnt = doc.replaceAll(replace_descriptor) if replaced_cnt > 0: print("Replaced", british_word, "with", us_word) - except: + except Exception: traceback.print_exc() print("Done") @@ -94,7 +94,7 @@ def create_example_data(doc): ) text.insertString(cursor, content, False) cursor.gotoStart(False) - except: + except Exception: traceback.print_exc() diff --git a/odk/examples/python/Text/WriterSelector.py b/odk/examples/python/Text/WriterSelector.py index f822fded0631..3b659fd43947 100644 --- a/odk/examples/python/Text/WriterSelector.py +++ b/odk/examples/python/Text/WriterSelector.py @@ -54,9 +54,9 @@ def main(): # XComponent.dispose. try: doc.close(False) - except: + except Exception: doc.dispose() - except: + except Exception: traceback.print_exc() sys.exit(1) diff --git a/odk/source/helper/addon_console.py b/odk/source/helper/addon_console.py index a8961729a6a2..f3ab1e0c295e 100755 --- a/odk/source/helper/addon_console.py +++ b/odk/source/helper/addon_console.py @@ -9,7 +9,6 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. # -import xml.etree.ElementTree as ET import os from prompt_toolkit import prompt import xml.dom.minidom