| Conditions | 18 |
| Total Lines | 72 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like asyncua.common.structures104.make_structure_code() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | from enum import Enum |
||
| 51 | def make_structure_code(data_type, name, sdef): |
||
| 52 | """ |
||
| 53 | given a StructureDefinition object, generate Python code |
||
| 54 | """ |
||
| 55 | if sdef.StructureType not in (ua.StructureType.Structure, ua.StructureType.StructureWithOptionalFields): |
||
| 56 | # if sdef.StructureType != ua.StructureType.Structure: |
||
| 57 | raise NotImplementedError(f"Only StructureType implemented, not {ua.StructureType(sdef.StructureType).name} for node {name} with DataTypdeDefinition {sdef}") |
||
| 58 | |||
| 59 | code = f""" |
||
| 60 | |||
| 61 | class {name}: |
||
| 62 | |||
| 63 | ''' |
||
| 64 | {name} structure autogenerated from StructureDefinition object |
||
| 65 | ''' |
||
| 66 | |||
| 67 | data_type = ua.NodeId({data_type.Identifier}, {data_type.NamespaceIndex}) |
||
| 68 | |||
| 69 | """ |
||
| 70 | counter = 0 |
||
| 71 | # FIXME: with subscturutre weprobably need to add all fields from parents |
||
| 72 | # this requires network call etc... |
||
| 73 | if sdef.StructureType == ua.StructureType.StructureWithOptionalFields: |
||
| 74 | code += ' ua_switches = {\n' |
||
| 75 | for field in sdef.Fields: |
||
| 76 | |||
| 77 | if field.IsOptional: |
||
| 78 | code += f" '{field.Name}': ('Encoding', {counter}),\n" |
||
| 79 | counter += 1 |
||
| 80 | code += " }\n\n" |
||
| 81 | |||
| 82 | code += ' ua_types = [\n' |
||
| 83 | if sdef.StructureType == ua.StructureType.StructureWithOptionalFields: |
||
| 84 | code += " ('Encoding', 'Byte'),\n" |
||
| 85 | uatypes = [] |
||
| 86 | for field in sdef.Fields: |
||
| 87 | prefix = 'ListOf' if field.ValueRank >= 1 else '' |
||
| 88 | if field.DataType.NamespaceIndex == 0 and field.DataType.Identifier in ua.ObjectIdNames: |
||
| 89 | uatype = ua.ObjectIdNames[field.DataType.Identifier] |
||
| 90 | elif field.DataType in ua.extension_objects_by_datatype: |
||
| 91 | uatype = ua.extension_objects_by_datatype[field.DataType].__name__ |
||
| 92 | elif field.DataType in ua.enums_by_datatype: |
||
| 93 | uatype = ua.enums_by_datatype[field.DataType].__name__ |
||
| 94 | else: |
||
| 95 | # FIXME: we are probably missing many custom tyes here based on builtin types |
||
| 96 | # maybe we can use ua_utils.get_base_data_type() |
||
| 97 | raise RuntimeError(f"Unknown datatype for field: {field} in structure:{name}, please report") |
||
| 98 | if field.ValueRank >= 1 and uatype == 'Char': |
||
| 99 | uatype = 'String' |
||
| 100 | uatypes.append((field, uatype)) |
||
| 101 | code += f" ('{field.Name}', '{prefix + uatype}'),\n" |
||
| 102 | code += " ]\n" |
||
| 103 | code += f""" |
||
| 104 | def __str__(self): |
||
| 105 | vals = [f"{{name}}:{{val}}" for name, val in self.__dict__.items()] |
||
| 106 | return f"{name}({{','.join(vals)}})" |
||
| 107 | |||
| 108 | __repr__ = __str__ |
||
| 109 | |||
| 110 | def __init__(self): |
||
| 111 | """ |
||
| 112 | if not sdef.Fields: |
||
| 113 | code += " pass" |
||
| 114 | if sdef.StructureType == ua.StructureType.StructureWithOptionalFields: |
||
| 115 | code += " self.Encoding = 0\n" |
||
| 116 | for field, uatype in uatypes: |
||
| 117 | if field.ValueRank >= 1: |
||
| 118 | default_value = "[]" |
||
| 119 | else: |
||
| 120 | default_value = get_default_value(uatype) |
||
| 121 | code += f" self.{field.Name} = {default_value}\n" |
||
| 122 | return code |
||
| 123 | |||
| 257 |