|
7 | 7 | astName_typing_TypeAlias, |
8 | 8 | fileExtension, |
9 | 9 | getElementsBe, |
| 10 | + getElementsDOT, |
| 11 | + getElementsGrab, |
| 12 | + getElementsMake, |
10 | 13 | getElementsTypeAlias, |
11 | 14 | keywordArgumentsIdentifier, |
12 | | - moduleIdentifierPrefix, |
13 | 15 | pathPackage, |
14 | 16 | pythonVersionMinorMinimum, |
15 | | - getElementsMake, |
16 | | - getElementsDOT, |
17 | 17 | ) |
| 18 | +from toolFactory.Z0Z_hardcoded import listPylanceErrors |
18 | 19 | from toolFactory.factory_annex import ( |
| 20 | + FunctionDefGrab_andDoAllOf, |
19 | 21 | FunctionDefMake_Attribute, |
20 | 22 | FunctionDefMake_Import, |
21 | | - listHandmadeMethodsGrab, |
22 | 23 | listHandmadeTypeAlias_astTypes, |
23 | | - listPylanceErrors, |
24 | 24 | ) |
25 | 25 | from toolFactory.docstrings import ClassDefDocstringBe, ClassDefDocstringMake, docstringWarning, ClassDefDocstringDOT |
26 | 26 | from typing import cast |
@@ -73,73 +73,19 @@ def writeModule(astModule: ast.Module, moduleIdentifier: str) -> None: |
73 | 73 | pathFilenameModule = PurePosixPath(pathPackage, moduleIdentifier + fileExtension) |
74 | 74 | writeStringToHere(pythonSource, pathFilenameModule) |
75 | 75 |
|
76 | | -def makeTypeAlias(): |
77 | | - def Z0Z_getTypeAliasSubcategory(): |
78 | | - if len(dictionaryVersions) == 1: |
79 | | - for versionMinor, listClassAs_astAttribute in dictionaryVersions.items(): |
80 | | - ast_stmt = ast.AnnAssign(astNameTypeAlias, astName_typing_TypeAlias, ast.BitOr.join([eval(classAs_astAttribute) for classAs_astAttribute in listClassAs_astAttribute]), 1) # pyright: ignore[reportAttributeAccessIssue] |
81 | | - if versionMinor > pythonVersionMinorMinimum: |
82 | | - ast_stmt = ast.If(ast.Compare(ast.Attribute(ast.Name('sys'), 'version_info') |
83 | | - , ops=[ast.GtE()] |
84 | | - , comparators=[ast.Tuple([ast.Constant(3), |
85 | | - ast.Constant(versionMinor)])]) |
86 | | - , body=[ast_stmt]) |
87 | | - # This branch is the simplest case: one TypeAlias for the attribute for all Python versions |
88 | | - astTypesModule.body.append(ast_stmt) |
89 | | - else: |
90 | | - # There is a smart way to do the following, but I don't see it right now. NOTE datacenter has the responsibility to aggregate all values <= pythonVersionMinorMinimum. |
91 | | - listVersionsMinor = sorted(dictionaryVersions.keys(), reverse=False) |
92 | | - if len(listVersionsMinor) > 2: |
93 | | - raise NotImplementedError("Hunter's code can't handle this.") |
94 | | - ast_stmtAtPythonMinimum = ast.AnnAssign(astNameTypeAlias, astName_typing_TypeAlias, ast.BitOr.join([eval(classAs_astAttribute) for classAs_astAttribute in dictionaryVersions[min(listVersionsMinor)]]), 1) # pyright: ignore[reportAttributeAccessIssue] |
95 | | - ast_stmtAbovePythonMinimum = ast.AnnAssign(astNameTypeAlias, astName_typing_TypeAlias, ast.BitOr.join([eval(classAs_astAttribute) for classAs_astAttribute in sorted(chain(*dictionaryVersions.values()), key=str.lower)]), 1) # pyright: ignore[reportAttributeAccessIssue] |
96 | | - |
97 | | - ast_stmt = ast.If(ast.Compare(ast.Attribute(ast.Name('sys'), 'version_info') |
98 | | - , ops=[ast.GtE()] |
99 | | - , comparators=[ast.Tuple([ast.Constant(3), |
100 | | - ast.Constant(max(listVersionsMinor))])]) |
101 | | - , body=[ast_stmtAbovePythonMinimum] |
102 | | - , orelse=[ast_stmtAtPythonMinimum]) |
103 | | - astTypesModule.body.append(ast_stmt) |
104 | | - |
105 | | - astTypesModule = ast.Module( |
106 | | - body=[docstringWarning |
107 | | - , ast.ImportFrom('typing', [ast.alias('Any'), ast.alias('TypeAlias', 'typing_TypeAlias')], 0) |
108 | | - , ast.Import([ast.alias('ast')]) |
109 | | - , ast.Import([ast.alias('sys')]) |
110 | | - , *listHandmadeTypeAlias_astTypes |
111 | | - ] |
112 | | - , type_ignores=[] |
113 | | - ) |
114 | | - |
115 | | - typeAliasData: dict[str, dict[str, dict[int, list[str]]]] = getElementsTypeAlias() |
116 | | - |
117 | | - for attribute, dictionaryAttribute in typeAliasData.items(): |
118 | | - hasDOTIdentifier: str = 'hasDOT' + attribute |
119 | | - hasDOTTypeAliasName_Load: ast.Name = ast.Name(hasDOTIdentifier) |
120 | | - hasDOTTypeAliasName_Store: ast.Name = ast.Name(hasDOTIdentifier, ast.Store()) |
121 | | - |
122 | | - if len(dictionaryAttribute) == 1: |
123 | | - astNameTypeAlias = hasDOTTypeAliasName_Store |
124 | | - for TypeAliasSubcategory, dictionaryVersions in dictionaryAttribute.items(): |
125 | | - Z0Z_getTypeAliasSubcategory() |
126 | | - else: |
127 | | - # See?! Sometimes, I can see a smart way to do things. This defaultdict builds a dictionary to mimic the |
128 | | - # process I'm already using to build the TypeAlias. |
129 | | - attributeDictionaryVersions: dict[int, list[str]] = defaultdict(list) |
130 | | - for TypeAliasSubcategory, dictionaryVersions in dictionaryAttribute.items(): |
131 | | - astNameTypeAlias: ast.Name = ast.Name(hasDOTIdentifier + '_' + TypeAliasSubcategory, ast.Store()) |
132 | | - if any(dictionaryVersions.keys()) <= pythonVersionMinorMinimum: |
133 | | - attributeDictionaryVersions[min(dictionaryVersions.keys())].append(ast.dump(astNameTypeAlias)) |
134 | | - else: |
135 | | - attributeDictionaryVersions[min(dictionaryVersions.keys())].append(ast.dump(astNameTypeAlias)) |
136 | | - attributeDictionaryVersions[max(dictionaryVersions.keys())].append(ast.dump(astNameTypeAlias)) |
137 | | - Z0Z_getTypeAliasSubcategory() |
138 | | - astNameTypeAlias = hasDOTTypeAliasName_Store |
139 | | - dictionaryVersions: dict[int, list[str]] = attributeDictionaryVersions |
140 | | - Z0Z_getTypeAliasSubcategory() |
141 | | - |
142 | | - writeModule(astTypesModule, '_astTypes') |
| 76 | +def writeClass(classIdentifier: str, list4ClassDefBody: list[ast.stmt], list4ModuleBody: list[ast.stmt], moduleIdentifierPrefix: str | None = '_tool') -> None: |
| 77 | + if moduleIdentifierPrefix: |
| 78 | + moduleIdentifier = moduleIdentifierPrefix + classIdentifier |
| 79 | + else: |
| 80 | + moduleIdentifier = classIdentifier |
| 81 | + return writeModule(ast.Module( |
| 82 | + body=[docstringWarning |
| 83 | + , *list4ModuleBody |
| 84 | + , ast.ClassDef(name=classIdentifier, bases=[], keywords=[], body=list4ClassDefBody, decorator_list=[]) |
| 85 | + ] |
| 86 | + , type_ignores=[] |
| 87 | + ) |
| 88 | + , moduleIdentifier) |
143 | 89 |
|
144 | 90 | def makeToolBe(): |
145 | 91 | list4ClassDefBody: list[ast.stmt] = [ClassDefDocstringBe] |
@@ -167,20 +113,13 @@ def makeToolBe(): |
167 | 113 |
|
168 | 114 | list4ClassDefBody.append(ast_stmt) |
169 | 115 |
|
170 | | - ClassDefBe = ast.ClassDef(name='Be', bases=[], keywords=[], body=list4ClassDefBody, decorator_list=[]) |
| 116 | + list4ModuleBody: list[ast.stmt] = [ |
| 117 | + ast.ImportFrom('typing', [ast.alias('TypeGuard')], 0) |
| 118 | + , ast.Import([ast.alias('ast')]) |
| 119 | + , ast.Import([ast.alias('sys')]) |
| 120 | + ] |
171 | 121 |
|
172 | | - ClassDef = ClassDefBe |
173 | | - writeModule(ast.Module( |
174 | | - body=[docstringWarning |
175 | | - , ast.ImportFrom('typing', [ast.alias('TypeGuard')], 0) |
176 | | - , ast.Import([ast.alias('ast')]) |
177 | | - , ast.Import([ast.alias('sys')]) |
178 | | - , ClassDef |
179 | | - ], |
180 | | - type_ignores=[] |
181 | | - ) |
182 | | - , moduleIdentifierPrefix + ClassDef.name) |
183 | | - del ClassDef |
| 122 | + writeClass('Be', list4ClassDefBody, list4ModuleBody) |
184 | 123 |
|
185 | 124 | def makeToolDOT(): |
186 | 125 | list4ClassDefBody: list[ast.stmt] = [ClassDefDocstringDOT] |
@@ -229,26 +168,33 @@ def makeToolDOT(): |
229 | 168 |
|
230 | 169 | list4ClassDefBody.append(ast_stmt) |
231 | 170 |
|
232 | | - ClassDefDOT = ast.ClassDef(name='DOT', bases=[], keywords=[], body=list4ClassDefBody, decorator_list=[]) |
233 | | - |
234 | | - ClassDef = ClassDefDOT |
235 | | - writeModule(ast.Module( |
236 | | - body=[docstringWarning |
| 171 | + list4ModuleBody: list[ast.stmt] = [docstringWarning |
237 | 172 | , ast.ImportFrom(module='astToolkit', names=[ast.alias(name='ast_Identifier'), ast.alias(name='ast_expr_Slice'), ast.alias(name='astDOTtype_param')], level=0) |
238 | 173 | , ast.ImportFrom(module='astToolkit._astTypes', names=[ast.alias(name='*')], level=0) |
239 | 174 | , ast.ImportFrom(module='collections.abc', names=[ast.alias(name='Sequence')], level=0) |
240 | 175 | , ast.ImportFrom(module='typing', names=[ast.alias(name='Any'), ast.alias(name='Literal'), ast.alias(name='overload')], level=0) |
241 | 176 | , ast.Import(names=[ast.alias(name='ast')]) |
242 | 177 | , ast.Import(names=[ast.alias(name='sys')]) |
243 | | - , ClassDef |
244 | | - ], |
245 | | - type_ignores=[] |
246 | | - ) |
247 | | - , moduleIdentifierPrefix + ClassDef.name) |
248 | | - del ClassDef |
| 178 | + ] |
| 179 | + |
| 180 | + writeClass('DOT', list4ClassDefBody, list4ModuleBody) |
249 | 181 |
|
250 | 182 | def makeToolGrab(): |
251 | | - pass |
| 183 | + list4ClassDefBody: list[ast.stmt] = [ClassDefDocstringBe] |
| 184 | + dictionaryToolElements: dict[str, dict[str, int]] = getElementsGrab() |
| 185 | + |
| 186 | + for attribute, dictionaryAttribute in dictionaryToolElements.items(): |
| 187 | + hasDOTIdentifier: str = 'hasDOT' + attribute |
| 188 | + hasDOTTypeAliasName_Load: ast.Name = ast.Name(hasDOTIdentifier) |
| 189 | + hasDOTTypeAliasName_Store: ast.Name = ast.Name(hasDOTIdentifier, ast.Store()) |
| 190 | + |
| 191 | + list4ModuleBody: list[ast.stmt] = [ |
| 192 | + ast.ImportFrom('typing', [ast.alias('TypeGuard')], 0) |
| 193 | + , ast.Import([ast.alias('ast')]) |
| 194 | + , ast.Import([ast.alias('sys')]) |
| 195 | + ] |
| 196 | + |
| 197 | + writeClass('Grab', list4ClassDefBody, list4ModuleBody) |
252 | 198 |
|
253 | 199 | def makeToolMake(): |
254 | 200 | list4ClassDefBody: list[ast.stmt] = [ClassDefDocstringMake] |
@@ -280,22 +226,83 @@ def makeToolMake(): |
280 | 226 |
|
281 | 227 | list4ClassDefBody.append(ast_stmt) |
282 | 228 |
|
283 | | - ClassDefMake = ast.ClassDef(name='Make', bases=[], keywords=[], body=list4ClassDefBody, decorator_list=[]) |
| 229 | + list4ModuleBody: list[ast.stmt] = [ |
| 230 | + ast.ImportFrom('typing', [ast.alias('TypeGuard')], 0) |
| 231 | + , ast.Import([ast.alias('ast')]) |
| 232 | + , ast.Import([ast.alias('sys')]) |
| 233 | + ] |
| 234 | + |
| 235 | + writeClass('Make', list4ClassDefBody, list4ModuleBody) |
| 236 | + |
| 237 | +def makeTypeAlias(): |
| 238 | + def Z0Z_getTypeAliasSubcategory(): |
| 239 | + if len(dictionaryVersions) == 1: |
| 240 | + for versionMinor, listClassAs_astAttribute in dictionaryVersions.items(): |
| 241 | + ast_stmt = ast.AnnAssign(astNameTypeAlias, astName_typing_TypeAlias, ast.BitOr.join([eval(classAs_astAttribute) for classAs_astAttribute in listClassAs_astAttribute]), 1) # pyright: ignore[reportAttributeAccessIssue] |
| 242 | + if versionMinor > pythonVersionMinorMinimum: |
| 243 | + ast_stmt = ast.If(ast.Compare(ast.Attribute(ast.Name('sys'), 'version_info') |
| 244 | + , ops=[ast.GtE()] |
| 245 | + , comparators=[ast.Tuple([ast.Constant(3), |
| 246 | + ast.Constant(versionMinor)])]) |
| 247 | + , body=[ast_stmt]) |
| 248 | + # This branch is the simplest case: one TypeAlias for the attribute for all Python versions |
| 249 | + astTypesModule.body.append(ast_stmt) |
| 250 | + else: |
| 251 | + # There is a smart way to do the following, but I don't see it right now. NOTE datacenter has the responsibility to aggregate all values <= pythonVersionMinorMinimum. |
| 252 | + listVersionsMinor = sorted(dictionaryVersions.keys(), reverse=False) |
| 253 | + if len(listVersionsMinor) > 2: |
| 254 | + raise NotImplementedError("Hunter's code can't handle this.") |
| 255 | + ast_stmtAtPythonMinimum = ast.AnnAssign(astNameTypeAlias, astName_typing_TypeAlias, ast.BitOr.join([eval(classAs_astAttribute) for classAs_astAttribute in dictionaryVersions[min(listVersionsMinor)]]), 1) # pyright: ignore[reportAttributeAccessIssue] |
| 256 | + ast_stmtAbovePythonMinimum = ast.AnnAssign(astNameTypeAlias, astName_typing_TypeAlias, ast.BitOr.join([eval(classAs_astAttribute) for classAs_astAttribute in sorted(chain(*dictionaryVersions.values()), key=str.lower)]), 1) # pyright: ignore[reportAttributeAccessIssue] |
284 | 257 |
|
285 | | - ClassDef = ClassDefMake |
286 | | - writeModule(ast.Module( |
| 258 | + ast_stmt = ast.If(ast.Compare(ast.Attribute(ast.Name('sys'), 'version_info') |
| 259 | + , ops=[ast.GtE()] |
| 260 | + , comparators=[ast.Tuple([ast.Constant(3), |
| 261 | + ast.Constant(max(listVersionsMinor))])]) |
| 262 | + , body=[ast_stmtAbovePythonMinimum] |
| 263 | + , orelse=[ast_stmtAtPythonMinimum]) |
| 264 | + astTypesModule.body.append(ast_stmt) |
| 265 | + |
| 266 | + astTypesModule = ast.Module( |
287 | 267 | body=[docstringWarning |
288 | | - , ast.ImportFrom('typing', [ast.alias('TypeGuard')], 0) |
| 268 | + , ast.ImportFrom('typing', [ast.alias('Any'), ast.alias('TypeAlias', 'typing_TypeAlias')], 0) |
289 | 269 | , ast.Import([ast.alias('ast')]) |
290 | 270 | , ast.Import([ast.alias('sys')]) |
291 | | - , ClassDef |
292 | | - ], |
293 | | - type_ignores=[] |
| 271 | + , *listHandmadeTypeAlias_astTypes |
| 272 | + ] |
| 273 | + , type_ignores=[] |
294 | 274 | ) |
295 | | - , moduleIdentifierPrefix + ClassDef.name) |
296 | | - del ClassDef |
| 275 | + |
| 276 | + dictionaryToolElements: dict[str, dict[str, dict[int, list[str]]]] = getElementsTypeAlias() |
| 277 | + |
| 278 | + for attribute, dictionaryAttribute in dictionaryToolElements.items(): |
| 279 | + hasDOTIdentifier: str = 'hasDOT' + attribute |
| 280 | + hasDOTTypeAliasName_Load: ast.Name = ast.Name(hasDOTIdentifier) |
| 281 | + hasDOTTypeAliasName_Store: ast.Name = ast.Name(hasDOTIdentifier, ast.Store()) |
| 282 | + |
| 283 | + if len(dictionaryAttribute) == 1: |
| 284 | + astNameTypeAlias = hasDOTTypeAliasName_Store |
| 285 | + for TypeAliasSubcategory, dictionaryVersions in dictionaryAttribute.items(): |
| 286 | + Z0Z_getTypeAliasSubcategory() |
| 287 | + else: |
| 288 | + # See?! Sometimes, I can see a smart way to do things. This defaultdict builds a dictionary to mimic the |
| 289 | + # process I'm already using to build the TypeAlias. |
| 290 | + attributeDictionaryVersions: dict[int, list[str]] = defaultdict(list) |
| 291 | + for TypeAliasSubcategory, dictionaryVersions in dictionaryAttribute.items(): |
| 292 | + astNameTypeAlias: ast.Name = ast.Name(hasDOTIdentifier + '_' + TypeAliasSubcategory, ast.Store()) |
| 293 | + if any(dictionaryVersions.keys()) <= pythonVersionMinorMinimum: |
| 294 | + attributeDictionaryVersions[min(dictionaryVersions.keys())].append(ast.dump(astNameTypeAlias)) |
| 295 | + else: |
| 296 | + attributeDictionaryVersions[min(dictionaryVersions.keys())].append(ast.dump(astNameTypeAlias)) |
| 297 | + attributeDictionaryVersions[max(dictionaryVersions.keys())].append(ast.dump(astNameTypeAlias)) |
| 298 | + Z0Z_getTypeAliasSubcategory() |
| 299 | + astNameTypeAlias = hasDOTTypeAliasName_Store |
| 300 | + dictionaryVersions: dict[int, list[str]] = attributeDictionaryVersions |
| 301 | + Z0Z_getTypeAliasSubcategory() |
| 302 | + |
| 303 | + writeModule(astTypesModule, '_astTypes') |
297 | 304 |
|
298 | 305 | if __name__ == "__main__": |
299 | | - # makeToolBe() |
300 | | - makeToolDOT() |
| 306 | + makeToolBe() |
| 307 | + # makeToolDOT() |
301 | 308 | # makeTypeAlias() |
0 commit comments