From 10788068c8f541797b198bede94c3919bd2697d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20Michalak-Szmaci=C5=84ski?= Date: Fri, 24 Feb 2023 07:16:19 +0100 Subject: [PATCH] Add to flake8 in workflow and fix python files (#25272) --- .flake8 | 145 ++++++++++++++++++ .github/workflows/lint.yml | 7 + .../examples/matter_idl_plugin/__init__.py | 23 ++- .../matter_idl/generators/__init__.py | 10 +- .../matter_idl/generators/bridge/__init__.py | 3 - .../generators/cpp/application/__init__.py | 10 +- .../matter_idl/generators/java/__init__.py | 9 +- .../matter_idl/generators/types.py | 17 +- .../py_matter_idl/matter_idl/lint/__init__.py | 2 + .../matter_idl/lint/lint_rules_parser.py | 17 +- .../py_matter_idl/matter_idl/lint/types.py | 14 +- .../matter_idl/matter_idl_parser.py | 27 ++-- .../matter_idl/matter_idl_types.py | 2 +- .../matter_idl/test_generators.py | 1 - .../matter_idl/test_matter_idl_parser.py | 13 +- .../matter_idl/test_xml_parser.py | 45 ++++-- .../matter_idl/zapxml/handlers/handlers.py | 7 +- .../matter_idl/zapxml/handlers/parsing.py | 2 +- 18 files changed, 267 insertions(+), 87 deletions(-) diff --git a/.flake8 b/.flake8 index 28e6208b8c07ee..a43d67e2c4a111 100644 --- a/.flake8 +++ b/.flake8 @@ -1,2 +1,147 @@ [flake8] max-line-length = 132 +exclude = third_party + .* + out/* + scripts/idl/* + ./examples/common/QRCode/* + # temporarily scan only directories with fixed files + # TODO: Remove the paths below when all bugs are fixed + src/tools/chip-cert/* + src/test_driver/openiotsdk/* + src/test_driver/mbed/* + src/test_driver/linux-cirque/* + src/test_driver/esp32/* + src/test_driver/efr32/* + build/chip/java/tests/* + build/chip/linux/* + build/config/linux/* + credentials/fetch-paa-certs-from-dcl.py + docs/_extensions/external_content.py + examples/chef/chef.py + examples/chef/sample_app_util/zap_file_parser.py + examples/common/pigweed/rpc_console/py/chip_rpc/console.py + examples/lighting-app/python/lighting.py + examples/platform/mbed/ota/generate_ota_list_image.py + examples/platform/nxp/k32w/k32w0/scripts/detokenizer.py + scripts/build/build/target.py + scripts/build/build/targets.py + scripts/build/builders/android.py + scripts/build/builders/bouffalolab.py + scripts/build/builders/cc13x2x7_26x2x7.py + scripts/build/builders/efr32.py + scripts/build/builders/esp32.py + scripts/build/builders/genio.py + scripts/build/builders/gn.py + scripts/build/builders/imx.py + scripts/build/builders/infineon.py + scripts/build/builders/nrf.py + scripts/build/test.py + scripts/codegen.py + scripts/codepregen.py + scripts/error_table.py + scripts/examples/gn_to_cmakelists.py + scripts/examples/tests/test.py + scripts/flashing/bouffalolab_firmware_utils.py + scripts/flashing/cyw30739_firmware_utils.py + scripts/flashing/nrfconnect_firmware_utils.py + scripts/gen_chip_version.py + scripts/gen_test_driver.py + scripts/helpers/bloat_check.py + scripts/pregenerate/using_codegen.py + scripts/pregenerate/using_zap.py + scripts/py_matter_yamltests/matter_yamltests/constraints.py + scripts/py_matter_yamltests/matter_yamltests/definitions.py + scripts/py_matter_yamltests/matter_yamltests/fixes.py + scripts/py_matter_yamltests/matter_yamltests/parser.py + scripts/py_matter_yamltests/matter_yamltests/pics_checker.py + scripts/py_matter_yamltests/matter_yamltests/pseudo_clusters/pseudo_clusters.py + scripts/py_matter_yamltests/matter_yamltests/yaml_loader.py + scripts/py_matter_yamltests/test_pics_checker.py + scripts/py_matter_yamltests/test_spec_definitions.py + scripts/py_matter_yamltests/test_yaml_loader.py + scripts/py_matter_yamltests/test_yaml_parser.py + scripts/run-clang-tidy-on-compile-commands.py + scripts/setup/nrfconnect/update_ncs.py + scripts/tests/chiptest/__init__.py + scripts/tests/chiptest/runner.py + scripts/tests/chiptest/test_definition.py + scripts/tests/chiptest/yamltest_with_chip_repl_tester.py + scripts/tests/java/base.py + scripts/tests/java/commissioning_test.py + scripts/tests/java/discover_test.py + scripts/tests/run_java_test.py + scripts/tests/run_python_test.py + scripts/tests/run_test_suite.py + scripts/tools/check_zcl_file_sync.py + scripts/tools/convert_ini.py + scripts/tools/generate_esp32_chip_factory_bin.py + scripts/tools/memory/memdf/__init__.py + scripts/tools/memory/report_summary.py + scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py + scripts/tools/nrfconnect/nrfconnect_generate_partition.py + scripts/tools/nrfconnect/tests/test_generate_factory_data.py + scripts/tools/nxp/factory_data_generator/custom.py + scripts/tools/nxp/factory_data_generator/default.py + scripts/tools/nxp/factory_data_generator/generate.py + scripts/tools/silabs/FactoryDataProvider.py + scripts/tools/telink/mfg_tool.py + scripts/tools/zap/generate.py + scripts/tools/zap/prune_outputs.py + scripts/tools/zap/test_generate.py + scripts/tools/zap/version_update.py + scripts/tools/zap/zap_download.py + scripts/tools/zap_convert_all.py + src/app/ota_image_tool.py + src/app/tests/suites/certification/information.py + src/app/zap_cluster_list.py + src/controller/python/build-chip-wheel.py + src/controller/python/chip-device-ctrl.py + src/controller/python/chip/CertificateAuthority.py + src/controller/python/chip/ChipBleUtility.py + src/controller/python/chip/ChipBluezMgr.py + src/controller/python/chip/ChipCommissionableNodeCtrl.py + src/controller/python/chip/ChipCoreBluetoothMgr.py + src/controller/python/chip/ChipDeviceCtrl.py + src/controller/python/chip/ChipReplStartup.py + src/controller/python/chip/ChipStack.py + src/controller/python/chip/ChipUtility.py + src/controller/python/chip/FabricAdmin.py + src/controller/python/chip/ble/scan_devices.py + src/controller/python/chip/clusters/Attribute.py + src/controller/python/chip/clusters/CHIPClusters.py + src/controller/python/chip/clusters/ClusterObjects.py + src/controller/python/chip/clusters/Command.py + src/controller/python/chip/clusters/Objects.py + src/controller/python/chip/clusters/__init__.py + src/controller/python/chip/discovery/__init__.py + src/controller/python/chip/interaction_model/__init__.py + src/controller/python/chip/interaction_model/delegate.py + src/controller/python/chip/internal/commissioner.py + src/controller/python/chip/setup_payload/setup_payload.py + src/controller/python/chip/storage/__init__.py + src/controller/python/chip/tlv/__init__.py + src/controller/python/chip/utils/CommissioningBuildingBlocks.py + src/controller/python/chip/yaml/__init__.py + src/controller/python/chip/yaml/format_converter.py + src/controller/python/chip/yaml/runner.py + src/controller/python/test/test_scripts/base.py + src/controller/python/test/test_scripts/cluster_objects.py + src/controller/python/test/test_scripts/mobile-device-test.py + src/controller/python/test/test_scripts/network_commissioning.py + src/controller/python/test/unit_tests/test_cluster_objects.py + src/controller/python/test/unit_tests/test_tlv.py + src/lib/asn1/gen_asn1oid.py + src/pybindings/pycontroller/build-chip-wheel.py + src/pybindings/pycontroller/pychip/__init__.py + src/python_testing/TC_ACE_1_3.py + src/python_testing/TC_ACE_1_4.py + src/python_testing/TC_CGEN_2_4.py + src/python_testing/TC_DA_1_7.py + src/python_testing/TC_RR_1_1.py + src/python_testing/TC_SC_3_6.py + src/python_testing/TC_TestEventTrigger.py + src/python_testing/hello_test.py + src/python_testing/matter_testing_support.py + src/setup_payload/python/generate_setup_payload.py + src/setup_payload/tests/run_python_setup_payload_gen_test.py diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 8f2fe9fc73af67..089642b7c28914 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -214,3 +214,10 @@ jobs: if: always() run: | git grep -n 'emberAfWriteAttribute' -- './*' ':(exclude).github/workflows/lint.yml' ':(exclude)src/app/util/af.h' ':(exclude)zzz_generated/app-common/app-common/zap-generated/attributes/Accessors.cpp' ':(exclude)src/app/zap-templates/templates/app/attributes/Accessors-src.zapt' ':(exclude)src/app/util/attribute-table.cpp' ':(exclude)examples/common/pigweed/rpc_services/Attributes.h' ':(exclude)src/app/util/attribute-table.h' ':(exclude)src/app/util/ember-compatibility-functions.cpp' && exit 1 || exit 0 + + # Run python Linter (flake8) and verify python files + # ignore some style errors, restyler should do that + - name: Check for errors using flake8 Python linter + if: always() + run: | + flake8 --extend-ignore=E501,W391 diff --git a/scripts/py_matter_idl/examples/matter_idl_plugin/__init__.py b/scripts/py_matter_idl/examples/matter_idl_plugin/__init__.py index 2389d423009b49..c1783f8651d592 100644 --- a/scripts/py_matter_idl/examples/matter_idl_plugin/__init__.py +++ b/scripts/py_matter_idl/examples/matter_idl_plugin/__init__.py @@ -13,11 +13,10 @@ # limitations under the License. import os -from typing import List import jinja2 from matter_idl.generators import CodeGenerator, GeneratorStorage -from matter_idl.matter_idl_types import Attribute, Cluster, ClusterSide, Command, Field, Idl +from matter_idl.matter_idl_types import Cluster, ClusterSide, Command, Field, Idl def toUpperSnakeCase(s): @@ -57,9 +56,18 @@ def toEnumEntryName(enumEntry, enumName): def toProtobufType(zapType: str) -> str: """ Convert zap type to protobuf type """ - u32Types = ["uint32", "enum8", "enum16", "enum32", "bitmap8", "bitmap16", "bitmap32", "cluster_id", "attrib_id", "event_id", "command_id", - "endpoint_no", "group_id", "devtype_id", "fabric_idx", "vendor_id", "status_code", "faulttype", "levelcontroloptions", "percent100ths", "percent"] - u64Types = ["uint64", "enum64", "bitmap64", "node_id", "fabric_id", "int40u", "int48u", "int56u", "int64u"] + u32Types = [ + "uint32", "enum8", "enum16", "enum32", "bitmap8", + "bitmap16", "bitmap32", "cluster_id", "attrib_id", + "event_id", "command_id", "endpoint_no", "group_id", + "devtype_id", "fabric_idx", "vendor_id", "status_code", + "faulttype", "levelcontroloptions", "percent100ths", + "percent" + ] + u64Types = [ + "uint64", "enum64", "bitmap64", "node_id", "fabric_id", + "int40u", "int48u", "int56u", "int64u" + ] i32Types = ["int32", "int8s", "int16s", "int24s", "int32s"] i64Types = ["int64", "int40s", "int48s", "int56s", "int64s"] floatTypes = ["float", "double"] @@ -149,7 +157,7 @@ def commandResponseArgs(command: Command, cluster: Cluster): def toEncodedTag(tag, typeNum: EncodingDataType): """ Return the final encoded tag from the given field number and field encoded data type. - The Matter field type information is encoded into the upper range of the protobuf field + The Matter field type information is encoded into the upper range of the protobuf field tag for stateless translation to Matter TLV. """ tag = (int(typeNum) << 19) | int(tag) return tag @@ -228,7 +236,8 @@ def internal_render_all(self): if cluster.side != ClusterSide.CLIENT: continue - filename = "proto/%s_cluster.proto" % toLowerSnakeCase(cluster.name) + filename = "proto/%s_cluster.proto" % toLowerSnakeCase( + cluster.name) # Header containing a macro to initialize all cluster plugins self.internal_render_one_output( diff --git a/scripts/py_matter_idl/matter_idl/generators/__init__.py b/scripts/py_matter_idl/matter_idl/generators/__init__.py index 779c5ad800ace2..0c944e68a650fd 100644 --- a/scripts/py_matter_idl/matter_idl/generators/__init__.py +++ b/scripts/py_matter_idl/matter_idl/generators/__init__.py @@ -13,11 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import logging -import jinja2 - +import os from typing import Dict + +import jinja2 from matter_idl.matter_idl_types import Idl from .filters import RegisterCommonFilters @@ -102,7 +102,7 @@ class CodeGenerator: 'render' will perform a rendering of all files. As special optimizations, CodeGenerators generally will try to read - existing data and will not re-write content if not changed (so that + existing data and will not re-write content if not changed (so that write time of files do not change and rebuilds are not triggered). """ @@ -146,7 +146,7 @@ def internal_render_one_output(self, template_path: str, output_file_name: str, if dry-run was requested during `render`. NOTE: to make this method suitable for rebuilds, this file will NOT alter - the timestamp of the output file if the file content would not + the timestamp of the output file if the file content would not change (i.e. no write will be invoked in that case.) Args: diff --git a/scripts/py_matter_idl/matter_idl/generators/bridge/__init__.py b/scripts/py_matter_idl/matter_idl/generators/bridge/__init__.py index 2c38924f2c5b0a..0fcb14d44d4f2e 100644 --- a/scripts/py_matter_idl/matter_idl/generators/bridge/__init__.py +++ b/scripts/py_matter_idl/matter_idl/generators/bridge/__init__.py @@ -13,12 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import enum import logging import re -from typing import List, Set, Union -from matter_idl import matter_idl_types from matter_idl.generators import CodeGenerator, GeneratorStorage from matter_idl.generators.types import (BasicInteger, BasicString, FundamentalType, IdlBitmapType, IdlEnumType, IdlType, ParseDataType, TypeLookupContext) diff --git a/scripts/py_matter_idl/matter_idl/generators/cpp/application/__init__.py b/scripts/py_matter_idl/matter_idl/generators/cpp/application/__init__.py index b1f061b56cce84..5cc22583a87192 100644 --- a/scripts/py_matter_idl/matter_idl/generators/cpp/application/__init__.py +++ b/scripts/py_matter_idl/matter_idl/generators/cpp/application/__init__.py @@ -12,16 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import enum -import logging -from typing import List, Set, Union +from typing import List -from matter_idl import matter_idl_types from matter_idl.generators import CodeGenerator, GeneratorStorage -from matter_idl.generators.types import (BasicInteger, BasicString, FundamentalType, IdlBitmapType, IdlEnumType, IdlType, - ParseDataType, TypeLookupContext) -from matter_idl.matter_idl_types import Attribute, Cluster, ClusterSide, Command, DataType, Field, FieldQuality, Idl -from stringcase import capitalcase +from matter_idl.matter_idl_types import Cluster, ClusterSide, Idl def serverClustersOnly(clusters: List[Cluster]) -> List[Cluster]: diff --git a/scripts/py_matter_idl/matter_idl/generators/java/__init__.py b/scripts/py_matter_idl/matter_idl/generators/java/__init__.py index 3ea1a670492449..ed47f7137d06d4 100644 --- a/scripts/py_matter_idl/matter_idl/generators/java/__init__.py +++ b/scripts/py_matter_idl/matter_idl/generators/java/__init__.py @@ -17,7 +17,6 @@ import logging from typing import List, Set, Union -from matter_idl import matter_idl_types from matter_idl.generators import CodeGenerator, GeneratorStorage from matter_idl.generators.types import (BasicInteger, BasicString, FundamentalType, IdlBitmapType, IdlEnumType, IdlType, ParseDataType, TypeLookupContext) @@ -76,7 +75,7 @@ def CallbackName(attr: Attribute, cluster: Cluster, context: TypeLookupContext) Figure out what callback name to use when a variable requires a read callback. These are split into native types, like Boolean/Float/Double/CharString, where - one callback type can support anything. + one callback type can support anything. For specific types (e.g. A struct) codegen will generate its own callback name specific to that type. @@ -240,7 +239,7 @@ def boxed_java_type(self): elif t == FundamentalType.DOUBLE: return "Double" else: - raise Error("Unknown fundamental type") + raise Exception("Unknown fundamental type") elif type(t) == BasicInteger: if t.byte_count >= 4: return "Long" @@ -277,7 +276,7 @@ def boxed_java_signature(self): elif t == FundamentalType.DOUBLE: return "Ljava/lang/Double;" else: - raise Error("Unknown fundamental type") + raise Exception("Unknown fundamental type") elif type(t) == BasicInteger: if t.byte_count >= 4: return "Ljava/lang/Long;" @@ -301,7 +300,7 @@ def EncodableValueFrom(field: Field, context: TypeLookupContext) -> EncodableVal Filter to convert a standard field to an EncodableValue. This converts the AST information (field name/info + lookup context) into - a java-generator specific wrapper that can be manipulated and + a java-generator specific wrapper that can be manipulated and queried for properties like java native name or JNI string signature. """ attrs = set() diff --git a/scripts/py_matter_idl/matter_idl/generators/types.py b/scripts/py_matter_idl/matter_idl/generators/types.py index 5d1930808e3b48..87c24465237b33 100644 --- a/scripts/py_matter_idl/matter_idl/generators/types.py +++ b/scripts/py_matter_idl/matter_idl/generators/types.py @@ -13,8 +13,9 @@ # limitations under the License. import enum +import logging from dataclasses import dataclass -from typing import List, Optional, Union +from typing import Optional, Union from matter_idl import matter_idl_types # to explicitly say 'Enum' from matter_idl.matter_idl_types import DataType @@ -80,7 +81,7 @@ def idl_name(self): elif self == FundamentalType.DOUBLE: return "double" else: - raise Error("Type not handled: %r" % self) + raise Exception("Type not handled: %r" % self) @property def byte_count(self): @@ -91,7 +92,7 @@ def byte_count(self): elif self == FundamentalType.DOUBLE: return 8 else: - raise Error("Type not handled: %r" % self) + raise Exception("Type not handled: %r" % self) @property def bits(self): @@ -109,11 +110,11 @@ class IdlEnumType: @property def byte_count(self): - return base_type.byte_count() + return self.base_type.byte_count() @property def bits(self): - return base_type.bits() + return self.base_type.bits() @dataclass @@ -128,11 +129,11 @@ class IdlBitmapType: @property def byte_count(self): - return base_type.byte_count() + return self.base_type.byte_count() @property def bits(self): - return base_type.bits() + return self.base_type.bits() class IdlItemType(enum.Enum): @@ -399,7 +400,7 @@ def ParseDataType(data_type: DataType, lookup: TypeLookupContext) -> Union[Basic if lookup.find_struct(data_type.name): result.item_type = IdlItemType.STRUCT else: - logging.warn( + logging.warning( "Data type %s is NOT known, but treating it as a generic IDL type." % data_type) return result diff --git a/scripts/py_matter_idl/matter_idl/lint/__init__.py b/scripts/py_matter_idl/matter_idl/lint/__init__.py index 503bdf4086f77e..3ab2b9f7badd25 100644 --- a/scripts/py_matter_idl/matter_idl/lint/__init__.py +++ b/scripts/py_matter_idl/matter_idl/lint/__init__.py @@ -13,3 +13,5 @@ # limitations under the License. from .lint_rules_parser import CreateParser + +__all__ = [CreateParser] diff --git a/scripts/py_matter_idl/matter_idl/lint/lint_rules_parser.py b/scripts/py_matter_idl/matter_idl/lint/lint_rules_parser.py index c03a0aa6806168..39461810f1e0d3 100755 --- a/scripts/py_matter_idl/matter_idl/lint/lint_rules_parser.py +++ b/scripts/py_matter_idl/matter_idl/lint/lint_rules_parser.py @@ -2,19 +2,17 @@ import logging import os -import traceback import xml.etree.ElementTree -from dataclasses import dataclass, field -from typing import List, Mapping, Optional +from dataclasses import dataclass +from typing import List, Mapping -import stringcase from lark import Lark from lark.visitors import Discard, Transformer, v_args try: from .types import (AttributeRequirement, ClusterCommandRequirement, ClusterRequirement, RequiredAttributesRule, RequiredCommandsRule) -except: +except ImportError: import sys sys.path.append(os.path.join(os.path.abspath( @@ -102,7 +100,7 @@ def DecodeClusterFromXml(element: xml.etree.ElementTree.Element): required_attributes=required_attributes, required_commands=required_commands ) - except Exception as e: + except Exception: logging.exception("Failed to decode cluster %r" % element) return None @@ -204,7 +202,7 @@ def positive_integer(self, tokens): """Numbers in the grammar are integers or hex numbers. """ if len(tokens) != 1: - raise Error("Unexpected argument counts") + raise Exception("Unexpected argument counts") return parseNumberString(tokens[0].value) @@ -220,7 +218,7 @@ def id(self, tokens): """An id is a string containing an identifier """ if len(tokens) != 1: - raise Error("Unexpected argument counts") + raise Exception("Unexpected argument counts") return tokens[0].value def ESCAPED_STRING(self, s): @@ -279,7 +277,8 @@ def CreateParser(file_name: str): """ Generates a parser that will process a ".matter" file into a IDL """ - return Parser(Lark.open('lint_rules_grammar.lark', rel_to=__file__, parser='lalr', propagate_positions=True), file_name=file_name) + return Parser( + Lark.open('lint_rules_grammar.lark', rel_to=__file__, parser='lalr', propagate_positions=True), file_name=file_name) if __name__ == '__main__': diff --git a/scripts/py_matter_idl/matter_idl/lint/types.py b/scripts/py_matter_idl/matter_idl/lint/types.py index 7814b83d373b97..c2b07ddef9b1b4 100644 --- a/scripts/py_matter_idl/matter_idl/lint/types.py +++ b/scripts/py_matter_idl/matter_idl/lint/types.py @@ -14,7 +14,7 @@ from abc import ABC, abstractmethod from dataclasses import dataclass, field -from typing import List, Optional +from typing import List, Mapping, Optional from matter_idl.matter_idl_types import ClusterSide, Idl, ParseMetaData @@ -150,12 +150,12 @@ def _ServerClusterDefinition(self, name: str, location: Optional[LocationInFile] ] if not cluster_definition: self._AddLintError( - "Cluster definition for %s not found" % cluster.name, location) + "Cluster definition for %s not found" % name, location) return None if len(cluster_definition) > 1: self._AddLintError( - "Multiple cluster definitions found for %s" % cluster.name, location) + "Multiple cluster definitions found for %s" % name, location) return None return cluster_definition[0] @@ -198,7 +198,9 @@ def _LintImpl(self): if check.code not in attribute_codes: self._AddLintError("EP%d:%s does not expose %s(%d) attribute" % - (endpoint.number, cluster.name, check.name, check.code), self._ParseLocation(cluster.parse_meta)) + (endpoint.number, cluster.name, + check.name, check.code), + self._ParseLocation(cluster.parse_meta)) for requirement in self._mandatory_clusters: if requirement.endpoint_id != endpoint.number: @@ -221,8 +223,8 @@ def __init__(self, name): super(RequiredCommandsRule, self).__init__(name) # Maps cluster id to mandatory cluster requirement - self._mandatory_commands: Maping[int, - List[ClusterCommandRequirement]] = {} + self._mandatory_commands: Mapping[int, + List[ClusterCommandRequirement]] = {} def __repr__(self): result = "RequiredCommandsRule{\n" diff --git a/scripts/py_matter_idl/matter_idl/matter_idl_parser.py b/scripts/py_matter_idl/matter_idl/matter_idl_parser.py index 4a3c0b47f33c96..8683965743ffe8 100755 --- a/scripts/py_matter_idl/matter_idl/matter_idl_parser.py +++ b/scripts/py_matter_idl/matter_idl/matter_idl_parser.py @@ -2,19 +2,24 @@ import functools import logging -from enum import Enum from lark import Lark from lark.visitors import Transformer, v_args try: - from .matter_idl_types import * -except: + from .matter_idl_types import (AccessPrivilege, Attribute, AttributeInstantiation, AttributeOperation, AttributeQuality, + AttributeStorage, Bitmap, Cluster, ClusterSide, Command, CommandQuality, ConstantEntry, DataType, + DeviceType, Endpoint, Enum, Event, EventPriority, EventQuality, Field, FieldQuality, Idl, + ParseMetaData, ServerClusterInstantiation, Struct, StructQuality, StructTag) +except ImportError: import os import sys sys.path.append(os.path.abspath(os.path.dirname(__file__))) - from matter_idl_types import * + from matter_idl_types import (AccessPrivilege, Attribute, AttributeInstantiation, AttributeOperation, AttributeQuality, + AttributeStorage, Bitmap, Cluster, ClusterSide, Command, CommandQuality, ConstantEntry, DataType, + DeviceType, Endpoint, Enum, Event, EventPriority, EventQuality, Field, FieldQuality, Idl, + ParseMetaData, ServerClusterInstantiation, Struct, StructQuality, StructTag) def UnionOfAllFlags(flags_list): @@ -91,7 +96,7 @@ def positive_integer(self, tokens): """Numbers in the grammar are integers or hex numbers. """ if len(tokens) != 1: - raise Error("Unexpected argument counts") + raise Exception("Unexpected argument counts") n = tokens[0].value if n.startswith('0x'): @@ -117,14 +122,14 @@ def id(self, tokens): """An id is a string containing an identifier """ if len(tokens) != 1: - raise Error("Unexpected argument counts") + raise Exception("Unexpected argument counts") return tokens[0].value def type(self, tokens): """A type is just a string for the type """ if len(tokens) != 1: - raise Error("Unexpected argument counts") + raise Exception("Unexpected argument counts") return tokens[0].value def data_type(self, tokens): @@ -134,7 +139,7 @@ def data_type(self, tokens): elif len(tokens) == 2: return DataType(name=tokens[0], max_length=tokens[1]) else: - raise Error("Unexpected size for data type") + raise Exception("Unexpected size for data type") @v_args(inline=True) def constant_entry(self, id, number): @@ -382,7 +387,8 @@ def endpoint_server_cluster(self, meta, id, *content): attributes.append(item) else: events.add(item) - return AddServerClusterToEndpointTransform(ServerClusterInstantiation(parse_meta=meta, name=id, attributes=attributes, events_emitted=events)) + return AddServerClusterToEndpointTransform( + ServerClusterInstantiation(parse_meta=meta, name=id, attributes=attributes, events_emitted=events)) @v_args(inline=True, meta=True) def cluster(self, meta, side, name, code, *content): @@ -448,7 +454,8 @@ def CreateParser(skip_meta: bool = False): # - 0.39s LALR parsing of all-clusters-app.matter # - 2.26s Earley parsing of the same thing. # For this reason, every attempt should be made to make the grammar context free - return ParserWithLines(Lark.open('matter_grammar.lark', rel_to=__file__, start='idl', parser='lalr', propagate_positions=True), skip_meta) + return ParserWithLines(Lark.open( + 'matter_grammar.lark', rel_to=__file__, start='idl', parser='lalr', propagate_positions=True), skip_meta) if __name__ == '__main__': diff --git a/scripts/py_matter_idl/matter_idl/matter_idl_types.py b/scripts/py_matter_idl/matter_idl/matter_idl_types.py index 362f921b2a8a82..11df3b17771778 100644 --- a/scripts/py_matter_idl/matter_idl/matter_idl_types.py +++ b/scripts/py_matter_idl/matter_idl/matter_idl_types.py @@ -1,6 +1,6 @@ import enum from dataclasses import dataclass, field -from typing import List, Optional, Set, Union +from typing import List, Optional, Union from lark.tree import Meta diff --git a/scripts/py_matter_idl/matter_idl/test_generators.py b/scripts/py_matter_idl/matter_idl/test_generators.py index 5d7034d20367ae..39a42dbe4a0786 100755 --- a/scripts/py_matter_idl/matter_idl/test_generators.py +++ b/scripts/py_matter_idl/matter_idl/test_generators.py @@ -25,7 +25,6 @@ try: from matter_idl.matter_idl_parser import CreateParser except ImportError: - import sys sys.path.append(os.path.abspath( os.path.join(os.path.dirname(__file__), '..'))) diff --git a/scripts/py_matter_idl/matter_idl/test_matter_idl_parser.py b/scripts/py_matter_idl/matter_idl/test_matter_idl_parser.py index be7bd82e0ee9c9..4d429bd22e3a07 100755 --- a/scripts/py_matter_idl/matter_idl/test_matter_idl_parser.py +++ b/scripts/py_matter_idl/matter_idl/test_matter_idl_parser.py @@ -16,15 +16,20 @@ try: from .matter_idl_parser import CreateParser - from .matter_idl_types import * -except: + from .matter_idl_types import (AccessPrivilege, Attribute, AttributeInstantiation, AttributeQuality, AttributeStorage, Bitmap, + Cluster, ClusterSide, Command, CommandQuality, ConstantEntry, DataType, DeviceType, Endpoint, + Enum, Event, EventPriority, EventQuality, Field, FieldQuality, Idl, ParseMetaData, + ServerClusterInstantiation, Struct, StructQuality, StructTag) +except ImportError: import os import sys sys.path.append(os.path.abspath(os.path.dirname(__file__))) from matter_idl_parser import CreateParser - from matter_idl_types import * - + from matter_idl_types import (AccessPrivilege, Attribute, AttributeInstantiation, AttributeQuality, AttributeStorage, Bitmap, + Cluster, ClusterSide, Command, CommandQuality, ConstantEntry, DataType, DeviceType, Endpoint, + Enum, Event, EventPriority, EventQuality, Field, FieldQuality, Idl, ParseMetaData, + ServerClusterInstantiation, Struct, StructQuality, StructTag) import unittest diff --git a/scripts/py_matter_idl/matter_idl/test_xml_parser.py b/scripts/py_matter_idl/matter_idl/test_xml_parser.py index 5d8d53201b2723..98f36c08f57e84 100755 --- a/scripts/py_matter_idl/matter_idl/test_xml_parser.py +++ b/scripts/py_matter_idl/matter_idl/test_xml_parser.py @@ -15,19 +15,23 @@ import io import unittest -from typing import List, Optional, Union +from typing import List, Union try: - from matter_idl.matter_idl_types import * + from matter_idl.matter_idl_types import (AccessPrivilege, Attribute, AttributeQuality, Bitmap, Cluster, ClusterSide, Command, + ConstantEntry, DataType, Event, EventPriority, EventQuality, Field, FieldQuality, Idl, + Struct, StructQuality, StructTag) from matter_idl.zapxml import ParseSource, ParseXmls -except: +except ImportError: import os import sys sys.path.append(os.path.abspath( os.path.join(os.path.dirname(__file__), '..'))) - from matter_idl.matter_idl_types import * + from matter_idl.matter_idl_types import (AccessPrivilege, Attribute, AttributeQuality, Bitmap, Cluster, ClusterSide, Command, + ConstantEntry, DataType, Event, EventPriority, EventQuality, Field, FieldQuality, Idl, + Struct, StructQuality, StructTag) from matter_idl.zapxml import ParseSource, ParseXmls @@ -57,9 +61,11 @@ def testCluster(self): Test 0x1234 - SomeIntAttribute + SomeIntAttribute - + AttributeWithAccess @@ -87,13 +93,21 @@ def testCluster(self): name='Test', code=0x1234, attributes=[ - Attribute(definition=Field(data_type=DataType(name='INT32U'), code=11, name='SomeIntAttribute', - qualities=FieldQuality.NULLABLE), qualities=AttributeQuality.READABLE, - readacl=AccessPrivilege.VIEW, writeacl=AccessPrivilege.OPERATE), - Attribute(definition=Field(data_type=DataType(name='INT8U'), code=22, name='AttributeWithAccess', - qualities=FieldQuality.OPTIONAL), - qualities=AttributeQuality.READABLE | AttributeQuality.WRITABLE, readacl=AccessPrivilege.OPERATE, - writeacl=AccessPrivilege.MANAGE) + Attribute(definition=Field( + data_type=DataType(name='INT32U'), + code=11, + name='SomeIntAttribute', + qualities=FieldQuality.NULLABLE), + qualities=AttributeQuality.READABLE, + readacl=AccessPrivilege.VIEW, writeacl=AccessPrivilege.OPERATE), + + Attribute(definition=Field( + data_type=DataType(name='INT8U'), + code=22, name='AttributeWithAccess', + qualities=FieldQuality.OPTIONAL), + qualities=AttributeQuality.READABLE | AttributeQuality.WRITABLE, + readacl=AccessPrivilege.OPERATE, + writeacl=AccessPrivilege.MANAGE) ], structs=[ Struct(name='GetSomeDataRequest', @@ -114,7 +128,8 @@ def testCluster(self): tag=StructTag.RESPONSE, code=0x44) ], commands=[ - Command(name='GetSomeData', code=33, input_param='GetSomeDataRequest', output_param='GetSomeDataResponse', + Command(name='GetSomeData', code=33, + input_param='GetSomeDataRequest', output_param='GetSomeDataResponse', invokeacl=AccessPrivilege.ADMINISTER) ]) ])) @@ -264,7 +279,7 @@ def testSkipsNotProcessedFields(self): Some copyright here... testing that we skip over comments --> - + Window Covering Closures diff --git a/scripts/py_matter_idl/matter_idl/zapxml/handlers/handlers.py b/scripts/py_matter_idl/matter_idl/zapxml/handlers/handlers.py index fcae981517cdc7..1bb9cff8856853 100644 --- a/scripts/py_matter_idl/matter_idl/zapxml/handlers/handlers.py +++ b/scripts/py_matter_idl/matter_idl/zapxml/handlers/handlers.py @@ -13,9 +13,10 @@ # limitations under the License. import logging -from typing import List, Optional, Union -from matter_idl.matter_idl_types import * +from matter_idl.matter_idl_types import (Attribute, Bitmap, Cluster, ClusterSide, Command, CommandQuality, ConstantEntry, DataType, + Enum, Event, EventPriority, EventQuality, Field, FieldQuality, Idl, Struct, StructQuality, + StructTag) from .base import BaseHandler, HandledDepth from .context import Context, IdlPostProcessor @@ -355,10 +356,8 @@ def __init__(self, context: Context, cluster: Cluster, attrs): if name.endswith('Request'): request_name = name - command_name = name[:-7] else: request_name = name+'Request' - command_name = name self._struct.name = request_name diff --git a/scripts/py_matter_idl/matter_idl/zapxml/handlers/parsing.py b/scripts/py_matter_idl/matter_idl/zapxml/handlers/parsing.py index 40553edc0d4d1e..bdae86a512f4ea 100644 --- a/scripts/py_matter_idl/matter_idl/zapxml/handlers/parsing.py +++ b/scripts/py_matter_idl/matter_idl/zapxml/handlers/parsing.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from matter_idl.matter_idl_types import * +from matter_idl.matter_idl_types import AccessPrivilege, Attribute, AttributeQuality, DataType, Field, FieldQuality def ParseInt(value: str) -> int: