Jump to content

User:Beluga

From The Document Foundation Wiki

Prospects for Python linting in CI

The easy fixes for issues found by Ruff linter are more or less done. Whole rules can be ignored and individual issues can be skipped by using noqa comments, but it would be nice to drive down the number of found issues some more.

I would add this rule to the ignore list: E741 Ambiguous variable name: `l`. It's about some fonts showing uppercase I and lowercase l as the same. On the command line it can be ignored with ruff check --ignore E741.

dev-tools

There's a handful of globals that throw the linter off:

esc-reporting/esc-collect.py:700:7: F841 Local variable `bugzillaData` is assigned to but never used
    |
698 | def runBuild(cfg):
699 |     try:
700 |       bugzillaData = get_bugzilla(cfg)
    |       ^^^^^^^^^^^^ F841
701 |     except Exception as e:
702 |       common.util_errorMail(cfg, 'esc-collect', 'ERROR: get_bugzilla failed with ' + str(e))
    |
    = help: Remove assignment to unused variable `bugzillaData`

The esc-reporting code would have to be refactored a bit to get rid of those.

helpcontent2

Some complaints remain for help to wiki conversion code, but it's obsolete, so could just be ignored.

dictionaries

Lightproof needs refactoring. We get lots of E402 Module level import not at top of file because the code expects certain variables (pkg, lang...) to be defined before some imports.

There are a bunch of F401 [*] `com.sun.star.linguistic2.SingleProofreadingError` imported but unused and F821 Undefined name `spellchecker` errors which are probably false positives, so would require refactoring.

core

pyuno

It appears these are false positives and should be ignored:

pyuno/source/loader/pythonloader.py:19:8: F401 [*] `uno` imported but unused
   |
17 | #   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 | #
19 | import uno
   |        ^^^ F401
20 | import unohelper
21 | import sys
   |
   = help: Remove unused import: `uno`

pyuno/source/module/uno.py:25:8: F401 [*] `socket` imported but unused
   |
24 | # since on Windows sal3.dll no longer calls WSAStartup
25 | import socket
   |        ^^^^^^ F401
26 | 
27 | # All functions and variables starting with a underscore (_) must be
   |
   = help: Remove unused import: `socket`

bin

Variables defined after they are used, but code author will leave them as-is for the moment:

bin/list-dispatch-commands.py:270:34: F821 Undefined name `command_name`
    |
268 |                     command = False
269 |                 elif square is True and line.endswith(']'):
270 |                     all_commands[command_name]['mode'] = mode
    |                                  ^^^^^^^^^^^^ F821
271 |                     square = False
272 |                 elif square is True:

bin/list-dispatch-commands.py:295:25: F821 Undefined name `params`
    |
293 |                 elif command is True and (param is True or line.startswith('(')) and line.endswith(')'):
294 |                     if param:
295 |                         params += line.strip(' (),').replace(', ', ',') #   At least 1 case of ", " in svx/sdi/svx.sdi line 8767
    |                         ^^^^^^ F821
296 |                                                                         #   At least 1 case of "( " in sw/sdi/swriter.sdi line 5477
297 |                     else:

Seems to need some refactoring, having a few of these:

librelogo/source/LibreLogo/LibreLogo.py:1350:3: E731 Do not assign a `lambda` expression, use a `def`
     |
1348 |       __match_localized_colors__[_.lng] = re.compile(r'<(/?)(' + '|'.join(__colors__[_.lng].keys()) + ')>', re.IGNORECASE)
1349 |   # replacement lambda function: if it's an opening tag, return with the argument, too
1350 |   get_fontcolor_tag = lambda m: "<fontcolor %s>" % m.group(2) if len(m.group(1)) == 0 else "</fontcolor>"
     |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
1351 |   st = re.sub(__match_localized_colors__[_.lng], get_fontcolor_tag, st)
     |
     = help: Rewrite `get_fontcolor_tag` as a `def`

scripting

Unclear things:

scripting/examples/python/HelloWorld.py:27:15: F821 Undefined name `XSCRIPTCONTEXT`
   |
25 |     # Get the doc from the scripting context which is made available to all
26 |     # scripts.
27 |     desktop = XSCRIPTCONTEXT.getDesktop()
   |               ^^^^^^^^^^^^^^ F821
28 |     model = desktop.getCurrentComponent()

scripting/source/pyprov/pythonscript.py:121:1: E402 Module level import not at top of file
    |
120 | #from com.sun.star.lang import typeOfXServiceInfo, typeOfXTypeProvider
121 | from com.sun.star.uno import RuntimeException
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E402
122 | from com.sun.star.lang import IllegalArgumentException
123 | from com.sun.star.container import NoSuchElementException

solenv

Why do we have solenv/bin/polib.py?

gdb import should not be removed as it might be needed in debugging:

solenv/gdb/boost/optional.py:21:8: F401 [*] `gdb` imported but unused
   |
19 | # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 | 
21 | import gdb
   |        ^^^ F401
22 | 
23 | import boost.util.printing as printing
   |
   = help: Remove unused import: `gdb`

These and similar are unclear:

solenv/gdb/libreoffice/util/uno.py:297:28: F821 Undefined name `members`
    |
295 |         def __init__(self, count, types, names):
296 |             self.count = count
297 |             self.members = members
    |                            ^^^^^^^ F821
298 |             self.names = names
299 |             self.pos = 0

solenv/gdb/libreoffice/util/uno.py:320:16: F821 Undefined name `_iterator`
    |
319 |     def attributes(self):
320 |         return _iterator(self._type['nMembers'], self._type['ppTypeRefs'],
    |                ^^^^^^^^^ F821
321 |                 self._type['ppMemberNames'])

Why does six.py need to be present here:

solenv/gdb/six.py:49:20: F821 Undefined name `basestring`
   |
47 |     MAXSIZE = sys.maxsize
48 | else:
49 |     string_types = basestring,
   |                    ^^^^^^^^^^ F821
50 |     integer_types = (int, long)
51 |     class_types = (type, types.ClassType)

testtools

Should these and the like be ignored:

testtools/source/bridgetest/pyuno/core.py:18:8: F401 [*] `pyuno` imported but unused
   |
16 | #   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 | #
18 | import pyuno
   |        ^^^^^ F401
19 | import uno
20 | import unittest
   |
   = help: Remove unused import: `pyuno`

testtools/source/bridgetest/pyuno/core.py:22:8: F401 [*] `types` imported but unused
   |
20 | import unittest
21 | import exceptions
22 | import types
   |        ^^^^^ F401
23 | 
24 | def suite(ctx):
   |
   = help: Remove unused import: `types`

What is the reason for this seemingly backwards code:

testtools/source/bridgetest/pyuno/core.py:311:18: F821 Undefined name `IllegalArgumentException`
    |
309 |           except AttributeError:
310 |                  wasHere = 1
311 |           except IllegalArgumentException:
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^ F821
312 |                  wasHere = 1     
313 |           self.assertTrue( wasHere, "wrong attribute test" )

The complete function:

def testErrors( self ):

    wasHere = 0
    try:
        self.tobj.a = 5
        self.fail("attribute a shouldn't exist")
    except AttributeError:
            wasHere = 1
    except IllegalArgumentException:
            wasHere = 1     
    self.assertTrue( wasHere, "wrong attribute test" )

    IllegalArgumentException = uno.getClass("com.sun.star.lang.IllegalArgumentException" )
    RuntimeException = uno.getClass("com.sun.star.uno.RuntimeException" )

Other mysteries in testtools:

unotest/source/python/org/libreoffice/unotest.py:31:39: F401 `com.sun.star.document.XDocumentEventListener` imported but unused; consider using `importlib.util.find_spec` to test for availability
   |
30 | try:
31 |     from com.sun.star.document import XDocumentEventListener
   |                                       ^^^^^^^^^^^^^^^^^^^^^^ F401
32 | except ImportError:
33 |     print("UNO API class not found: try to set URE_BOOTSTRAP variable")
   |
   = help: Remove unused import: `com.sun.star.document.XDocumentEventListener`

unotest

Unclear:

unotest/source/python/org/libreoffice/unotest.py:31:39: F401 `com.sun.star.document.XDocumentEventListener` imported but unused; consider using `importlib.util.find_spec` to test for availability
   |
30 | try:
31 |     from com.sun.star.document import XDocumentEventListener
   |                                       ^^^^^^^^^^^^^^^^^^^^^^ F401
32 | except ImportError:
33 |     print("UNO API class not found: try to set URE_BOOTSTRAP variable")
   |
   = help: Remove unused import: `com.sun.star.document.XDocumentEventListener`

wizards

Some things should be ignored and there are even comments about this added by easyhackers:

wizards/com/sun/star/wizards/common/Configuration.py:71:13: F821 Undefined name `com`
   |
69 |         # It would be good to use a linter that we can tell to ignore this
70 |         # type of thing (i.e. flake8 or ruff)
71 |         if (com.sun.star.uno.AnyConverter.isVoid(o)):
   |             ^^^ F821
72 |             return 0
73 |         return com.sun.star.uno.AnyConverter.toInt(o)

Ordering strangeness:

wizards/com/sun/star/wizards/ui/event/CommonListener.py:33:1: E402 Module level import not at top of file
   |
31 |         pass
32 | 
33 | from com.sun.star.awt import XItemListener
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E402
34 | class ItemListenerProcAdapter( unohelper.Base, XItemListener ):
35 |     def __init__(self, oProcToCall):

ScriptForge has some mysteries:

wizards/source/scriptforge/python/scriptforge.py:321:31: F841 Local variable `cstDims` is assigned to but never used
    |
319 |         script = ScriptForge.basicdispatcher
320 |         cstNoArgs = '+++NOARGS+++'
321 |         cstValue, cstVarType, cstDims, cstClass, cstType, cstService, cstName = 0, 1, 2, 2, 3, 4, 5
    |                               ^^^^^^^ F841
322 | 
323 |         def ConvertDictArgs():
    |
    = help: Remove assignment to unused variable `cstDims`

wizards/source/scriptforge/python/scriptforge.pyi:57:9: F821 Undefined name `SFScriptForge`
   |
55 | # List the available service types
56 | #   SFScriptForge
57 | ARRAY = SFScriptForge.SF_Array
   |         ^^^^^^^^^^^^^ F821
58 | BASIC = SFScriptForge.SF_Basic
59 | DICTIONARY = SFScriptForge.SF_Dictionary

How I downloaded Jay's Google Docs documents

Jay retired and put all his Google Docs links to his user page. I took a backup of all the ones except the "folderview" stuff.

I opened the wiki editor for the page and copied the whole contents to a text file.

I used a Python script to extract the URLs.

It has urlextract as a dependency, so I first installed it (on Linux) with

sudo pip install urlextract

This is the script:

from urlextract import URLExtract

extractor = URLExtract()
myfile = open("wikipage.txt")
urls = extractor.find_urls(myfile.read())
urls = [e[1:] for e in urls] # trim the [ before the url
print("\n".join(urls))

I copied the output to a text editor for further cleanup with find & replace and throwing stuff out. I put the documents and spreadsheets into different text files so all they had were the IDs separated with line breaks like so:

1YznFkDn91kMH6hoNsXkLzjRxzOJGdF1sqYXhPgPAN18
1FXFHzGzu2m9OcWu-m5BYf1Q-OBsDjkqiXtygLfPyhNk
1AmDXLkQiFK5OcOrtuGn7iAcqp2Yhc_eNNnlADk-ji-U
198zpaE2SKD0MIQUmSKb-s9vVCZy5dsdLg5JXhJ82iHg
1zBRA3a8B8lR_Dw-7sJ0jsxKYRw5AlnuvvKnQCYBFWRk

Then I created docx.sh:

#!/bin/bash

while IFS= read -r line; do
  curl -LOJ "https://docs.google.com/document/export?format=docx&id=${line}"
done < "$1"

and xlsx.sh:

#!/bin/bash

while IFS= read -r line; do
  curl -LOJ "https://docs.google.com/spreadsheets/export?id=${line}&exportFormat=xlsx"
done < "$1"

I ran them like so to download the files:

./docx.sh docx.txt

All pages under QA

https://wiki.documentfoundation.org/index.php?title=Special%3APrefixIndex&prefix=QA&namespace=0