|
1 | 1 | from ..lsp_schema import Structure |
2 | | -from .helpers import (FormattedProperty, StructureKind, |
3 | | - format_class_properties, format_comment, |
4 | | - format_dict_properties, get_formatted_properties, |
5 | | - has_invalid_property_name, indentation) |
| 2 | +from .helpers import ( |
| 3 | + FormattedProperty, |
| 4 | + StructureKind, |
| 5 | + format_class_properties, |
| 6 | + format_comment, |
| 7 | + format_dict_properties, |
| 8 | + get_formatted_properties, |
| 9 | + has_invalid_property_name, |
| 10 | + indentation, |
| 11 | +) |
6 | 12 |
|
7 | 13 |
|
8 | 14 | def generate_structures(structures: list[Structure]) -> list[str]: |
9 | | - |
10 | 15 | def toString(structure: Structure) -> str: |
11 | 16 | kind = StructureKind.Class |
12 | | - if has_invalid_property_name(structure['properties']): |
| 17 | + if has_invalid_property_name(structure["properties"]): |
13 | 18 | kind = StructureKind.Function |
14 | 19 | return generate_structure(structure, structures, kind) |
15 | 20 |
|
16 | | - return [toString(structure) for structure in structures if not structure['name'].startswith('_')] |
| 21 | + return [ |
| 22 | + toString(structure) |
| 23 | + for structure in structures |
| 24 | + if not structure["name"].startswith("_") |
| 25 | + ] |
17 | 26 |
|
18 | 27 |
|
19 | | -def get_additional_properties(for_structure: Structure, structures: list[Structure], structure_kind: StructureKind) -> list[FormattedProperty]: |
20 | | - """Returns properties from extended and mixin types. """ |
| 28 | +def get_additional_properties( |
| 29 | + for_structure: Structure, structures: list[Structure], structure_kind: StructureKind |
| 30 | +) -> list[FormattedProperty]: |
| 31 | + """Returns properties from extended and mixin types.""" |
21 | 32 | result: list[FormattedProperty] = [] |
22 | | - additional_structures = for_structure.get('extends') or [] |
23 | | - additional_structures.extend(for_structure.get('mixins') or []) |
| 33 | + additional_structures = for_structure.get("extends") or [] |
| 34 | + additional_structures.extend(for_structure.get("mixins") or []) |
24 | 35 | for additional_structure in additional_structures: |
25 | | - if additional_structure['kind'] != 'reference': |
26 | | - raise Exception("Cannot generate extends. Currently only supports kind: 'reference', but received:", additional_structure['kind']) |
27 | | - structure = next(structure for structure in structures if structure["name"] == additional_structure['name']) |
| 36 | + if additional_structure["kind"] != "reference": |
| 37 | + raise Exception( |
| 38 | + "Cannot generate extends. Currently only supports kind: 'reference', but received:", |
| 39 | + additional_structure["kind"], |
| 40 | + ) |
| 41 | + structure = next( |
| 42 | + structure |
| 43 | + for structure in structures |
| 44 | + if structure["name"] == additional_structure["name"] |
| 45 | + ) |
28 | 46 | if structure: |
29 | | - properties = get_formatted_properties(structure['properties'], structure['name'], structure_kind) |
| 47 | + properties = get_formatted_properties( |
| 48 | + structure["properties"], structure["name"], structure_kind |
| 49 | + ) |
30 | 50 | result.extend(properties) |
31 | 51 | return result |
32 | 52 |
|
33 | 53 |
|
34 | | -def generate_structure(structure: Structure, structures: list[Structure], structure_kind: StructureKind) -> str: |
| 54 | +def generate_structure( |
| 55 | + structure: Structure, structures: list[Structure], structure_kind: StructureKind |
| 56 | +) -> str: |
35 | 57 | result = "" |
36 | | - symbol_name = structure['name'] |
37 | | - properties = get_formatted_properties(structure['properties'], structure['name'], structure_kind) |
38 | | - additional_properties = get_additional_properties(structure, structures, structure_kind) |
| 58 | + symbol_name = structure["name"] |
| 59 | + properties = get_formatted_properties( |
| 60 | + structure["properties"], structure["name"], structure_kind |
| 61 | + ) |
| 62 | + additional_properties = get_additional_properties( |
| 63 | + structure, structures, structure_kind |
| 64 | + ) |
39 | 65 |
|
40 | 66 | # add extended properties |
41 | | - taken_property_names = [property['name'] for property in properties] |
| 67 | + taken_property_names = [property["name"] for property in properties] |
42 | 68 | for additional_property in additional_properties: |
43 | | - if additional_property['name'] not in taken_property_names: |
| 69 | + if additional_property["name"] not in taken_property_names: |
44 | 70 | properties.append(additional_property) |
45 | 71 |
|
46 | 72 | if structure_kind == StructureKind.Function: |
47 | | - documentation = format_comment(structure.get('documentation'), '') |
| 73 | + documentation = format_comment(structure.get("documentation"), "") |
48 | 74 | result += f"{symbol_name} = TypedDict('{symbol_name}', " |
49 | 75 | result += "{\n" |
50 | 76 | result += f"{indentation}{format_dict_properties(properties)}\n" |
51 | 77 | result += "})" |
52 | 78 | if documentation: |
53 | | - result += f'\n{documentation}' |
| 79 | + result += f"\n{documentation}" |
54 | 80 | else: |
55 | | - documentation = format_comment(structure.get('documentation'), indentation) |
| 81 | + documentation = format_comment(structure.get("documentation"), indentation) |
56 | 82 | result += f"class {symbol_name}(TypedDict):\n" |
57 | 83 | if documentation: |
58 | 84 | result += f"{documentation}\n" |
|
0 commit comments