Sat, 26 Apr 2025 12:34:32 +0200
MicroPython
- Added a configuration option to disable the support for the no longer produced Pimoroni Pico Wireless Pack.
9214 | 1 | # -*- coding: utf-8 -*- |
2 | ||
11090
f5f5f5803935
Updated copyright for 2025.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
3 | # Copyright (c) 2022 - 2025 Detlev Offenbach <detlev@die-offenbachs.de> |
9214 | 4 | # |
5 | ||
6 | """ | |
7 | Module implementing some utility functions for the Black based code formatting. | |
8 | """ | |
9 | ||
10 | import re | |
11 | ||
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9453
diff
changeset
|
12 | import black |
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9453
diff
changeset
|
13 | |
9436
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
14 | from PyQt6.QtCore import QCoreApplication, pyqtSlot |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
15 | |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
16 | from eric7.EricWidgets import EricMessageBox |
9214 | 17 | |
18 | ||
19 | def getDefaultConfiguration(): | |
20 | """ | |
21 | Function to generate a default set of configuration parameters. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
22 | |
9214 | 23 | @return dictionary containing the default parameters |
24 | @rtype dict | |
25 | """ | |
26 | return { | |
27 | "target-version": set(), | |
28 | "line-length": black.DEFAULT_LINE_LENGTH, | |
29 | "skip-string-normalization": False, | |
30 | "skip-magic-trailing-comma": False, | |
31 | "extend-exclude": "", | |
32 | "exclude": black.DEFAULT_EXCLUDES, # not shown in config dialog | |
33 | "force-exclude": "", # not shown in config dialog | |
34 | } | |
35 | ||
36 | ||
37 | def compileRegExp(regexp): | |
38 | """ | |
39 | Function to compile a given regular expression. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
40 | |
9214 | 41 | @param regexp regular expression to be compiled |
42 | @type str | |
43 | @return compiled regular expression object | |
44 | @rtype re.Pattern | |
45 | """ | |
46 | if "\n" in regexp: | |
47 | # multi line regexp | |
48 | regexp = f"(?x){regexp}" | |
49 | compiled = re.compile(regexp) | |
50 | return compiled | |
51 | ||
52 | ||
53 | def validateRegExp(regexp): | |
54 | """ | |
55 | Function to validate a given regular expression. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
56 | |
9214 | 57 | @param regexp regular expression to be validated |
58 | @type str | |
59 | @return tuple containing a flag indicating validity and an error message | |
60 | @rtype tuple of (bool, str) | |
61 | """ | |
62 | if regexp: | |
63 | try: | |
64 | compileRegExp(regexp) | |
65 | return True, "" | |
66 | except re.error as e: | |
67 | return ( | |
68 | False, | |
69 | QCoreApplication.translate( | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
70 | "BlackUtilities", "Invalid regular expression: {0}" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
71 | ).format(str(e)), |
9214 | 72 | ) |
73 | except IndexError: | |
74 | return ( | |
75 | False, | |
76 | QCoreApplication.translate( | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
77 | "BlackUtilities", "Invalid regular expression: missing group name" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
78 | ), |
9214 | 79 | ) |
80 | else: | |
81 | return ( | |
82 | False, | |
83 | QCoreApplication.translate( | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
84 | "BlackUtilities", "A regular expression must be given." |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9214
diff
changeset
|
85 | ), |
9214 | 86 | ) |
9436
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
87 | |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
88 | |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
89 | @pyqtSlot() |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
90 | def aboutBlack(): |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
91 | """ |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
92 | Slot to show an 'About Black' dialog. |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
93 | """ |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
94 | EricMessageBox.information( |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
95 | None, |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
96 | QCoreApplication.translate("BlackUtilities", "About Black"), |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
97 | QCoreApplication.translate( |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
98 | "BlackUtilities", |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
99 | """<p><b>Black Version {0}</b></p>""" |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
100 | """<p><i>Black</i> is the uncompromising Python code""" |
9453
e5065dde905d
Code Formatting
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9436
diff
changeset
|
101 | """ formatter.</p>""", |
9436
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
102 | ).format(black.__version__), |
731d146193e2
Added an 'About Black' entry to the Code Formatting menus of the project sources browser and the editor.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
103 | ) |