odk/examples/python/Spreadsheet/ChartTypeChange.py |    3 +-
 odk/examples/python/Spreadsheet/EuroAdaption.py    |   30 ++++++++++++++++-----
 odk/examples/python/Spreadsheet/SCalc.py           |   20 ++++++++++----
 3 files changed, 41 insertions(+), 12 deletions(-)

New commits:
commit f97944cca2f64ce8cdf1d001b6ba9c751047d45a
Author:     Leonard Sasse <[email protected]>
AuthorDate: Thu Mar 28 11:21:03 2024 +0100
Commit:     Ilmari Lauhakangas <[email protected]>
CommitDate: Tue May 21 11:43:46 2024 +0200

    tdf#158803 pyflakes F821: undefined name 'com' and remove bare except 
clauses
    
    Change-Id: Id116753a19a4fa5a29ad9a4f61f5ba3bf1ce95d1
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165451
    Tested-by: Jenkins
    Reviewed-by: Ilmari Lauhakangas <[email protected]>
    Tested-by: Ilmari Lauhakangas <[email protected]>

diff --git a/odk/examples/python/Spreadsheet/ChartTypeChange.py 
b/odk/examples/python/Spreadsheet/ChartTypeChange.py
index 6ee2c31d6766..74b8f42a0f94 100644
--- a/odk/examples/python/Spreadsheet/ChartTypeChange.py
+++ b/odk/examples/python/Spreadsheet/ChartTypeChange.py
@@ -14,6 +14,7 @@ from typing import Union
 
 import officehelper
 from com.sun.star.awt import Rectangle
+from com.sun.star.lang import IndexOutOfBoundsException
 
 
 def main():
@@ -85,7 +86,7 @@ def main():
 def insert_into_cell(column: int, row: int, value: Union[str, float], sheet):
     try:
         cell = sheet[row, column]
-    except com.sun.star.lang.IndexOutOfBoundsException:
+    except IndexOutOfBoundsException:
         print("Could not get Cell", file=sys.stderr)
         traceback.print_exc()
     else:
diff --git a/odk/examples/python/Spreadsheet/EuroAdaption.py 
b/odk/examples/python/Spreadsheet/EuroAdaption.py
index dc3cbbfb47a3..920af8650581 100644
--- a/odk/examples/python/Spreadsheet/EuroAdaption.py
+++ b/odk/examples/python/Spreadsheet/EuroAdaption.py
@@ -25,7 +25,10 @@ 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:
+    # removing bare except: and handling it similar to the try/except already
+    # committed in './SCalc.py'
+    except Exception as e:
+        print(f"Failed to get desktop: {e}")
         traceback.print_exc()
         sys.exit(1)
     return desktop
@@ -47,7 +50,10 @@ def get_number_format_key(number_formats, format: str, 
language) -> int:
             # If not exist, create a new one
             if (key := number_formats.addNew(format, language)) == -1:
                 key == 0
-    except:
+    # removing bare except: and handling it similar to the try/except already
+    # committed in './SCalc.py'
+    except Exception as e:
+        print(f"Failed to get key: {e}")
         traceback.print_exc()
     return key
 
@@ -73,7 +79,10 @@ def create_example_data(sheet, number_formats):
             cell.NumberFormat = number_format_key
             cell_range = sheet[counter + 1:counter + 2, 2:3]
             cell_range.NumberFormat = number_format_key
-    except:
+    # removing bare except: and handling it similar to the try/except already
+    # committed in './SCalc.py'
+    except Exception as e:
+        print(f"Failed to create example data: {e}")
         traceback.print_exc()
 
 
@@ -119,7 +128,10 @@ def convert(sheet, number_formats, old_symbol: str, 
new_symbol: str, factor: flo
                 if sheet_cell_ranges.getCount() > 0:
                     for cell in sheet_cell_ranges.getCells():
                         cell.Value = cell.Value / factor
-    except:
+    # removing bare except: and handling it similar to the try/except already
+    # committed in './SCalc.py'
+    except Exception as e:
+        print(f"Failed to convert currency: {e}")
         traceback.print_exc()
 
 
@@ -131,7 +143,10 @@ def main():
     try:
         doc = desktop.loadComponentFromURL("private:factory/scalc", "_blank", 
0, tuple())
         print("Create a new Spreadsheet")
-    except:
+    # removing bare except: and handling it similar to the try/except already
+    # committed in './SCalc.py'
+    except Exception as e:
+        print(f"Failed to load component from URL: {e}")
         traceback.print_exc()
         return
 
@@ -141,7 +156,10 @@ def main():
 
     try:
         sheet = doc.Sheets[0]
-    except:
+    # removing bare except: and handling it similar to the try/except already
+    # committed in './SCalc.py'
+    except Exception as e:
+        print(f"Failed to get sheet: {e}")
         traceback.print_exc()
         return
 
diff --git a/odk/examples/python/Spreadsheet/SCalc.py 
b/odk/examples/python/Spreadsheet/SCalc.py
index c016b96ef12d..91e256a64fcc 100644
--- a/odk/examples/python/Spreadsheet/SCalc.py
+++ b/odk/examples/python/Spreadsheet/SCalc.py
@@ -13,6 +13,7 @@ import traceback
 
 import officehelper
 from com.sun.star.awt import Rectangle
+from com.sun.star.lang import IndexOutOfBoundsException
 
 """
 Step 1: get the remote component context from the office
@@ -35,7 +36,10 @@ def main():
         desktop = srv_mgr.createInstanceWithContext(
             "com.sun.star.frame.Desktop", remote_context
         )
-    except:
+    # removing bare except: and handling it similar to the try/except already
+    # committed in other parts of this script
+    except Exception as e:
+        print(f"Couldn't get Sheet: {e}")
         traceback.print_exc()
         sys.exit(1)
 
@@ -48,7 +52,10 @@ def main():
     doc_url = "private:factory/scalc"
     try:
         doc = desktop.loadComponentFromURL(doc_url, "_blank", 0, tuple())
-    except:
+    # removing bare except: and handling it similar to the try/except already
+    # committed in other parts of this script
+    except Exception as e:
+        print(f"Couldn't get Sheet: {e}")
         traceback.print_exc()
         return
 
@@ -69,7 +76,10 @@ def main():
         cell_styles["My Style2"] = cell_style
         cell_style.IsCellBackgroundTransparent = False
         cell_style.CellBackColor = 13421823
-    except:
+    # removing bare except: and handling it similar to the try/except already
+    # committed in other parts of this script
+    except Exception as e:
+        print(f"Couldn't get Sheet: {e}")
         traceback.print_exc()
 
     # oooooooooooooooooooooooooooStep 
4oooooooooooooooooooooooooooooooooooooooooo
@@ -196,7 +206,7 @@ def main():
 def insert_into_cell(column: int, row: int, value: str, sheet, flag: str):
     try:
         cell = sheet[row, column]
-    except com.sun.star.lang.IndexOutOfBoundsException:
+    except IndexOutOfBoundsException:
         print("Could not get Cell", file=sys.stderr)
         traceback.print_exc()
     else:
@@ -210,7 +220,7 @@ def change_backcolor(left: int, top: int, right: int, 
bottom: int, template: str
     try:
         cell_range = sheet[top:bottom + 1, left:right + 1]
         cell_range.CellStyle = template
-    except com.sun.star.lang.IndexOutOfBoundsException:
+    except IndexOutOfBoundsException:
         print("Could not get CellRange", file=sys.stderr)
         traceback.print_exc()
     except Exception as e:

Reply via email to