From 99a8ade4cd3ebc69d62b688da5c21e56c93106f7 Mon Sep 17 00:00:00 2001 From: Michael Chisholm Date: Tue, 6 Jul 2021 20:40:50 -0400 Subject: [PATCH] pre-commit stylistic fixes --- stix2/base.py | 8 ++-- stix2/custom.py | 4 +- stix2/serialization.py | 1 - stix2/test/v21/test_custom.py | 70 +++++++++++++++++------------------ stix2/v21/__init__.py | 15 ++++---- stix2/v21/common.py | 4 +- stix2/v21/observables.py | 4 +- stix2/v21/sdo.py | 2 +- 8 files changed, 52 insertions(+), 56 deletions(-) diff --git a/stix2/base.py b/stix2/base.py index 24176f4..3ff01d9 100644 --- a/stix2/base.py +++ b/stix2/base.py @@ -137,7 +137,7 @@ class _STIXBase(collections.abc.Mapping): ) if registered_ext_class: registered_toplevel_extension_props.update( - registered_ext_class._toplevel_properties + registered_ext_class._toplevel_properties, ) else: has_unregistered_toplevel_extension = True @@ -175,7 +175,7 @@ class _STIXBase(collections.abc.Mapping): # properties defined on this instance as a result of toplevel property # extensions. defined_properties = collections.ChainMap( - self._properties, registered_toplevel_extension_props + self._properties, registered_toplevel_extension_props, ) assigned_properties = collections.ChainMap(kwargs, custom_props) @@ -186,7 +186,7 @@ class _STIXBase(collections.abc.Mapping): property_order = itertools.chain( self._properties, toplevel_extension_props, - sorted(all_custom_prop_names) + sorted(all_custom_prop_names), ) setting_kwargs = {} @@ -208,7 +208,7 @@ class _STIXBase(collections.abc.Mapping): # Detect any missing required properties required_properties = set( - get_required_properties(defined_properties) + get_required_properties(defined_properties), ) missing_kwargs = required_properties - setting_kwargs.keys() if missing_kwargs: diff --git a/stix2/custom.py b/stix2/custom.py index 7806179..adef768 100644 --- a/stix2/custom.py +++ b/stix2/custom.py @@ -110,14 +110,14 @@ def _custom_extension_builder(cls, type, properties, version, base_class): extension_type_prop = EnumProperty( [ "new-sdo", "new-sco", "new-sro", "property-extension", - "toplevel-property-extension" + "toplevel-property-extension", ], required=False, fixed=extension_type, ) nested_properties = { - "extension_type": extension_type_prop + "extension_type": extension_type_prop, } if extension_type == "toplevel-property-extension": diff --git a/stix2/serialization.py b/stix2/serialization.py index 236b987..7510b37 100644 --- a/stix2/serialization.py +++ b/stix2/serialization.py @@ -1,6 +1,5 @@ """STIX2 core serialization methods.""" -import copy import datetime as dt import io diff --git a/stix2/test/v21/test_custom.py b/stix2/test/v21/test_custom.py index 7aed1b7..f35b02d 100644 --- a/stix2/test/v21/test_custom.py +++ b/stix2/test/v21/test_custom.py @@ -10,7 +10,7 @@ import stix2.registry import stix2.v21 from ...exceptions import ( - DuplicateRegistrationError, InvalidValueError, MissingPropertiesError + DuplicateRegistrationError, InvalidValueError, MissingPropertiesError, ) from .constants import FAKE_TIME, IDENTITY_ID, MARKING_DEFINITION_ID @@ -1685,7 +1685,7 @@ def _register_extension(ext, props): stix2.v21.CustomExtension( ext_def_id, - props + props, )(ext) try: @@ -1703,8 +1703,8 @@ def test_nested_ext_prop_meta(): props = { "intprop": stix2.properties.IntegerProperty(required=True), "strprop": stix2.properties.StringProperty( - required=False, default=lambda: "foo" - ) + required=False, default=lambda: "foo", + ), } with _register_extension(TestExt, props) as ext_def_id: @@ -1715,9 +1715,9 @@ def test_nested_ext_prop_meta(): ext_def_id: { "extension_type": "property-extension", "intprop": "1", - "strprop": 2 - } - } + "strprop": 2, + }, + }, ) assert obj.extensions[ext_def_id].extension_type == "property-extension" @@ -1730,8 +1730,8 @@ def test_nested_ext_prop_meta(): ext_def_id: { "extension_type": "property-extension", "intprop": "1", - } - } + }, + }, ) # Ensure default kicked in @@ -1744,9 +1744,9 @@ def test_nested_ext_prop_meta(): ext_def_id: { "extension_type": "property-extension", # wrong value type - "intprop": "foo" - } - } + "intprop": "foo", + }, + }, ) with pytest.raises(InvalidValueError): @@ -1756,9 +1756,9 @@ def test_nested_ext_prop_meta(): ext_def_id: { "extension_type": "property-extension", # missing required property - "strprop": "foo" - } - } + "strprop": "foo", + }, + }, ) with pytest.raises(InvalidValueError): @@ -1770,8 +1770,8 @@ def test_nested_ext_prop_meta(): "intprop": 1, # Use of undefined property "foo": False, - } - } + }, + }, ) with pytest.raises(InvalidValueError): @@ -1783,8 +1783,8 @@ def test_nested_ext_prop_meta(): "extension_type": "new-sdo", "intprop": 1, "strprop": "foo", - } - } + }, + }, ) @@ -1796,8 +1796,8 @@ def test_toplevel_ext_prop_meta(): props = { "intprop": stix2.properties.IntegerProperty(required=True), "strprop": stix2.properties.StringProperty( - required=False, default=lambda: "foo" - ) + required=False, default=lambda: "foo", + ), } with _register_extension(TestExt, props) as ext_def_id: @@ -1808,9 +1808,9 @@ def test_toplevel_ext_prop_meta(): strprop=2, extensions={ ext_def_id: { - "extension_type": "toplevel-property-extension" - } - } + "extension_type": "toplevel-property-extension", + }, + }, ) assert obj.extensions[ext_def_id].extension_type == "toplevel-property-extension" @@ -1822,9 +1822,9 @@ def test_toplevel_ext_prop_meta(): intprop=1, extensions={ ext_def_id: { - "extension_type": "toplevel-property-extension" - } - } + "extension_type": "toplevel-property-extension", + }, + }, ) # Ensure default kicked in @@ -1836,9 +1836,9 @@ def test_toplevel_ext_prop_meta(): intprop="foo", # wrong value type extensions={ ext_def_id: { - "extension_type": "toplevel-property-extension" - } - } + "extension_type": "toplevel-property-extension", + }, + }, ) with pytest.raises(InvalidValueError): @@ -1850,8 +1850,8 @@ def test_toplevel_ext_prop_meta(): "extension_type": "toplevel-property-extension", # Use of undefined property "foo": False, - } - } + }, + }, ) with pytest.raises(MissingPropertiesError): @@ -1860,9 +1860,9 @@ def test_toplevel_ext_prop_meta(): strprop="foo", # missing required property extensions={ ext_def_id: { - "extension_type": "toplevel-property-extension" - } - } + "extension_type": "toplevel-property-extension", + }, + }, ) diff --git a/stix2/v21/__init__.py b/stix2/v21/__init__.py index ece8b36..2d29322 100644 --- a/stix2/v21/__init__.py +++ b/stix2/v21/__init__.py @@ -25,14 +25,13 @@ from .common import ( ) from .observables import ( URL, AlternateDataStream, ArchiveExt, Artifact, AutonomousSystem, - CustomObservable, Directory, DomainName, EmailAddress, - EmailMessage, EmailMIMEComponent, File, HTTPRequestExt, ICMPExt, - IPv4Address, IPv6Address, MACAddress, Mutex, NetworkTraffic, NTFSExt, - PDFExt, Process, RasterImageExt, SocketExt, Software, TCPExt, - UNIXAccountExt, UserAccount, WindowsPEBinaryExt, - WindowsPEOptionalHeaderType, WindowsPESection, WindowsProcessExt, - WindowsRegistryKey, WindowsRegistryValueType, WindowsServiceExt, - X509Certificate, X509V3ExtensionsType, + CustomObservable, Directory, DomainName, EmailAddress, EmailMessage, + EmailMIMEComponent, File, HTTPRequestExt, ICMPExt, IPv4Address, + IPv6Address, MACAddress, Mutex, NetworkTraffic, NTFSExt, PDFExt, Process, + RasterImageExt, SocketExt, Software, TCPExt, UNIXAccountExt, UserAccount, + WindowsPEBinaryExt, WindowsPEOptionalHeaderType, WindowsPESection, + WindowsProcessExt, WindowsRegistryKey, WindowsRegistryValueType, + WindowsServiceExt, X509Certificate, X509V3ExtensionsType, ) from .sdo import ( AttackPattern, Campaign, CourseOfAction, CustomObject, Grouping, Identity, diff --git a/stix2/v21/common.py b/stix2/v21/common.py index 9ba1096..b5f9c62 100644 --- a/stix2/v21/common.py +++ b/stix2/v21/common.py @@ -3,7 +3,7 @@ from collections import OrderedDict from . import _Extension -from ..custom import _custom_marking_builder, _custom_extension_builder +from ..custom import _custom_extension_builder, _custom_marking_builder from ..exceptions import InvalidValueError, PropertyPresenceError from ..markings import _MarkingsMixin from ..markings.utils import check_tlp_marking @@ -268,8 +268,6 @@ def CustomMarking(type='x-custom-marking', properties=None, extension_name=None) """ def wrapper(cls): if extension_name: - from . import observables - @CustomExtension(type=extension_name, properties=properties) class NameExtension: extension_type = 'property-extension' diff --git a/stix2/v21/observables.py b/stix2/v21/observables.py index 3b0d35a..4398772 100644 --- a/stix2/v21/observables.py +++ b/stix2/v21/observables.py @@ -18,7 +18,7 @@ from ..properties import ( TypeProperty, ) from .base import _Extension, _Observable, _STIXBase21 -from .common import GranularMarking, CustomExtension +from .common import CustomExtension, GranularMarking from .vocab import ( ACCOUNT_TYPE, ENCRYPTION_ALGORITHM, HASHING_ALGORITHM, NETWORK_SOCKET_ADDRESS_FAMILY, NETWORK_SOCKET_TYPE, @@ -877,7 +877,7 @@ def CustomObservable(type='x-custom-observable', properties=None, id_contrib_pro [ ('type', TypeProperty(type, spec_version='2.1')), ('spec_version', StringProperty(fixed='2.1')), - ('id', IDProperty(type, spec_version='2.1')) + ('id', IDProperty(type, spec_version='2.1')), ], properties, [ diff --git a/stix2/v21/sdo.py b/stix2/v21/sdo.py index 4a37e21..3fd4e04 100644 --- a/stix2/v21/sdo.py +++ b/stix2/v21/sdo.py @@ -19,7 +19,7 @@ from ..properties import ( from ..utils import NOW from .base import _DomainObject from .common import ( - CustomExtension, ExternalReference, GranularMarking, KillChainPhase + CustomExtension, ExternalReference, GranularMarking, KillChainPhase, ) from .vocab import ( ATTACK_MOTIVATION, ATTACK_RESOURCE_LEVEL, GROUPING_CONTEXT, IDENTITY_CLASS,