diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 309e53b..3dba8ef 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,3 +6,8 @@ args: - --max-line-length=160 - id: check-merge-conflict + +- repo: https://github.com/FalconSocial/pre-commit-python-sorter + sha: 1.0.4 + hooks: + - id: python-import-sorter diff --git a/stix2/__init__.py b/stix2/__init__.py index 84560ad..a3595d2 100644 --- a/stix2/__init__.py +++ b/stix2/__init__.py @@ -2,20 +2,20 @@ # flake8: noqa +from . import exceptions from .bundle import Bundle -from .observables import Artifact, AutonomousSystem, Directory, DomainName, \ - EmailAddress, EmailMessage, File, IPv4Address, IPv6Address, MACAddress, \ - Mutex, NetworkTraffic, Process, Software, URL, UserAccount, WindowsRegistryKey, \ - X509Certificate -from .other import ExternalReference, KillChainPhase, MarkingDefinition, \ - GranularMarking, StatementMarking, TLPMarking -from .sdo import AttackPattern, Campaign, CourseOfAction, Identity, Indicator, \ - IntrusionSet, Malware, ObservedData, Report, ThreatActor, Tool, \ - Vulnerability +from .observables import (URL, Artifact, AutonomousSystem, Directory, + DomainName, EmailAddress, EmailMessage, File, + IPv4Address, IPv6Address, MACAddress, Mutex, + NetworkTraffic, Process, Software, UserAccount, + WindowsRegistryKey, X509Certificate) +from .other import (ExternalReference, GranularMarking, KillChainPhase, + MarkingDefinition, StatementMarking, TLPMarking) +from .sdo import (AttackPattern, Campaign, CourseOfAction, Identity, Indicator, + IntrusionSet, Malware, ObservedData, Report, ThreatActor, + Tool, Vulnerability) from .sro import Relationship, Sighting from .utils import get_dict -from . import exceptions - OBJ_MAP = { 'attack-pattern': AttackPattern, diff --git a/stix2/base.py b/stix2/base.py index f9c4391..a99805c 100644 --- a/stix2/base.py +++ b/stix2/base.py @@ -3,14 +3,12 @@ import collections import copy import datetime as dt - import json - -from .exceptions import ExtraFieldsError, ImmutableError, InvalidObjRefError, \ - InvalidValueError, MissingFieldsError, RevokeError, \ - UnmodifiablePropertyError -from .utils import format_datetime, get_timestamp, NOW, parse_into_datetime +from .exceptions import (ExtraFieldsError, ImmutableError, InvalidObjRefError, + InvalidValueError, MissingFieldsError, RevokeError, + UnmodifiablePropertyError) +from .utils import NOW, format_datetime, get_timestamp, parse_into_datetime __all__ = ['STIXJSONEncoder', '_STIXBase'] diff --git a/stix2/other.py b/stix2/other.py index 9b7f03a..7b58c0b 100644 --- a/stix2/other.py +++ b/stix2/other.py @@ -4,7 +4,7 @@ from .base import _STIXBase from .properties import (IDProperty, ListProperty, Property, ReferenceProperty, SelectorProperty, StringProperty, TimestampProperty, TypeProperty) -from .utils import get_dict, NOW +from .utils import NOW, get_dict class ExternalReference(_STIXBase): diff --git a/stix2/properties.py b/stix2/properties.py index 9e4ad9e..5b0a677 100644 --- a/stix2/properties.py +++ b/stix2/properties.py @@ -5,12 +5,10 @@ import datetime as dt import inspect import re import uuid - -from dateutil import parser -import pytz from six import text_type - -from .base import _STIXBase, Observable +import pytz +from dateutil import parser +from .base import Observable, _STIXBase from .exceptions import DictionaryKeyError diff --git a/stix2/test/conftest.py b/stix2/test/conftest.py index ec967b5..d1f3330 100644 --- a/stix2/test/conftest.py +++ b/stix2/test/conftest.py @@ -5,8 +5,8 @@ import pytest import stix2 -from .constants import FAKE_TIME -from .constants import INDICATOR_KWARGS, MALWARE_KWARGS, RELATIONSHIP_KWARGS +from .constants import (FAKE_TIME, INDICATOR_KWARGS, MALWARE_KWARGS, + RELATIONSHIP_KWARGS) # Inspired by: http://stackoverflow.com/a/24006251 diff --git a/stix2/test/test_attack_pattern.py b/stix2/test/test_attack_pattern.py index c0891a5..fe399a1 100644 --- a/stix2/test/test_attack_pattern.py +++ b/stix2/test/test_attack_pattern.py @@ -2,10 +2,12 @@ import datetime as dt import pytest import pytz + import stix2 from .constants import ATTACK_PATTERN_ID + EXPECTED = """{ "created": "2016-05-12T08:17:27Z", "description": "...", diff --git a/stix2/test/test_bundle.py b/stix2/test/test_bundle.py index 6386a66..a849f3e 100644 --- a/stix2/test/test_bundle.py +++ b/stix2/test/test_bundle.py @@ -2,6 +2,7 @@ import pytest import stix2 + EXPECTED_BUNDLE = """{ "id": "bundle--00000000-0000-0000-0000-000000000004", "objects": [ diff --git a/stix2/test/test_campaign.py b/stix2/test/test_campaign.py index 7f6c4e6..9920019 100644 --- a/stix2/test/test_campaign.py +++ b/stix2/test/test_campaign.py @@ -2,10 +2,12 @@ import datetime as dt import pytest import pytz + import stix2 from .constants import CAMPAIGN_ID + EXPECTED = """{ "created": "2016-04-06T20:03:00Z", "created_by_ref": "identity--f431f809-377b-45e0-aa1c-6a4751cae5ff", diff --git a/stix2/test/test_course_of_action.py b/stix2/test/test_course_of_action.py index f566e10..263eae2 100644 --- a/stix2/test/test_course_of_action.py +++ b/stix2/test/test_course_of_action.py @@ -2,10 +2,12 @@ import datetime as dt import pytest import pytz + import stix2 from .constants import COURSE_OF_ACTION_ID + EXPECTED = """{ "created": "2016-04-06T20:03:48Z", "created_by_ref": "identity--f431f809-377b-45e0-aa1c-6a4751cae5ff", diff --git a/stix2/test/test_external_reference.py b/stix2/test/test_external_reference.py index 1e25fd2..3470bd4 100644 --- a/stix2/test/test_external_reference.py +++ b/stix2/test/test_external_reference.py @@ -3,8 +3,10 @@ import re import pytest + import stix2 + VERIS = """{ "external_id": "0001AA7F-C601-424A-B2B8-BE6C9F5164E7", "source_name": "veris", diff --git a/stix2/test/test_identity.py b/stix2/test/test_identity.py index 41c87bb..3952d25 100644 --- a/stix2/test/test_identity.py +++ b/stix2/test/test_identity.py @@ -2,10 +2,12 @@ import datetime as dt import pytest import pytz + import stix2 from .constants import IDENTITY_ID + EXPECTED = """{ "created": "2015-12-21T19:59:11Z", "id": "identity--311b2d2d-f010-5473-83ec-1edf84858f4c", diff --git a/stix2/test/test_indicator.py b/stix2/test/test_indicator.py index baf4b38..3e865b2 100644 --- a/stix2/test/test_indicator.py +++ b/stix2/test/test_indicator.py @@ -3,10 +3,12 @@ import re import pytest import pytz + import stix2 from .constants import FAKE_TIME, INDICATOR_ID, INDICATOR_KWARGS + EXPECTED_INDICATOR = """{ "created": "2017-01-01T00:00:01Z", "id": "indicator--01234567-89ab-cdef-0123-456789abcdef", diff --git a/stix2/test/test_intrusion_set.py b/stix2/test/test_intrusion_set.py index 19fb641..3241ced 100644 --- a/stix2/test/test_intrusion_set.py +++ b/stix2/test/test_intrusion_set.py @@ -2,10 +2,12 @@ import datetime as dt import pytest import pytz + import stix2 from .constants import INTRUSION_SET_ID + EXPECTED = """{ "aliases": [ "Zookeeper" diff --git a/stix2/test/test_kill_chain_phases.py b/stix2/test/test_kill_chain_phases.py index 037e930..92dfd66 100644 --- a/stix2/test/test_kill_chain_phases.py +++ b/stix2/test/test_kill_chain_phases.py @@ -4,6 +4,7 @@ import pytest import stix2 + LMCO_RECON = """{ "kill_chain_name": "lockheed-martin-cyber-kill-chain", "phase_name": "reconnaissance" diff --git a/stix2/test/test_malware.py b/stix2/test/test_malware.py index f3bfccd..9874a3c 100644 --- a/stix2/test/test_malware.py +++ b/stix2/test/test_malware.py @@ -3,10 +3,12 @@ import re import pytest import pytz + import stix2 from .constants import FAKE_TIME, MALWARE_ID, MALWARE_KWARGS + EXPECTED_MALWARE = """{ "created": "2016-05-12T08:17:27Z", "id": "malware--fedcba98-7654-3210-fedc-ba9876543210", diff --git a/stix2/test/test_markings.py b/stix2/test/test_markings.py index b3cc479..f1f07db 100644 --- a/stix2/test/test_markings.py +++ b/stix2/test/test_markings.py @@ -2,11 +2,13 @@ import datetime as dt import pytest import pytz + import stix2 from stix2.other import TLP_WHITE from .constants import MARKING_DEFINITION_ID + EXPECTED_TLP_MARKING_DEFINITION = """{ "created": "2017-01-20T00:00:00Z", "definition": { diff --git a/stix2/test/test_observed_data.py b/stix2/test/test_observed_data.py index 2910d3b..80a5b14 100644 --- a/stix2/test/test_observed_data.py +++ b/stix2/test/test_observed_data.py @@ -3,10 +3,12 @@ import re import pytest import pytz + import stix2 -from .constants import OBSERVED_DATA_ID from ..exceptions import InvalidValueError +from .constants import OBSERVED_DATA_ID + EXPECTED = """{ "created": "2016-04-06T19:58:16Z", diff --git a/stix2/test/test_properties.py b/stix2/test/test_properties.py index 7321a5c..d1c949c 100644 --- a/stix2/test/test_properties.py +++ b/stix2/test/test_properties.py @@ -4,9 +4,9 @@ from stix2.exceptions import DictionaryKeyError from stix2.observables import EmailMIMEComponent from stix2.properties import (BinaryProperty, BooleanProperty, DictionaryProperty, EmbeddedObjectProperty, - HashesProperty, HexProperty, - IDProperty, IntegerProperty, ListProperty, - Property, ReferenceProperty, StringProperty, + HashesProperty, HexProperty, IDProperty, + IntegerProperty, ListProperty, Property, + ReferenceProperty, StringProperty, TimestampProperty, TypeProperty) from .constants import FAKE_TIME diff --git a/stix2/test/test_relationship.py b/stix2/test/test_relationship.py index 869107d..e05c262 100644 --- a/stix2/test/test_relationship.py +++ b/stix2/test/test_relationship.py @@ -5,8 +5,8 @@ import pytz import stix2 -from .constants import FAKE_TIME, INDICATOR_ID, MALWARE_ID, RELATIONSHIP_ID -from .constants import RELATIONSHIP_KWARGS +from .constants import (FAKE_TIME, INDICATOR_ID, MALWARE_ID, RELATIONSHIP_ID, + RELATIONSHIP_KWARGS) EXPECTED_RELATIONSHIP = """{ diff --git a/stix2/test/test_report.py b/stix2/test/test_report.py index 46a0a16..cec217a 100644 --- a/stix2/test/test_report.py +++ b/stix2/test/test_report.py @@ -2,10 +2,12 @@ import datetime as dt import pytest import pytz + import stix2 from .constants import INDICATOR_KWARGS, REPORT_ID + EXPECTED = """{ "created": "2015-12-21T19:59:11Z", "created_by_ref": "identity--a463ffb3-1bd9-4d94-b02d-74e4f1658283", diff --git a/stix2/test/test_threat_actor.py b/stix2/test/test_threat_actor.py index 5844775..93e8179 100644 --- a/stix2/test/test_threat_actor.py +++ b/stix2/test/test_threat_actor.py @@ -2,10 +2,12 @@ import datetime as dt import pytest import pytz + import stix2 from .constants import THREAT_ACTOR_ID + EXPECTED = """{ "created": "2016-04-06T20:03:48Z", "created_by_ref": "identity--f431f809-377b-45e0-aa1c-6a4751cae5ff", diff --git a/stix2/test/test_tool.py b/stix2/test/test_tool.py index 3193807..d7d3bef 100644 --- a/stix2/test/test_tool.py +++ b/stix2/test/test_tool.py @@ -2,10 +2,12 @@ import datetime as dt import pytest import pytz + import stix2 from .constants import TOOL_ID + EXPECTED = """{ "created": "2016-04-06T20:03:48Z", "created_by_ref": "identity--f431f809-377b-45e0-aa1c-6a4751cae5ff", diff --git a/stix2/test/test_versioning.py b/stix2/test/test_versioning.py index 4663263..34f1c56 100644 --- a/stix2/test/test_versioning.py +++ b/stix2/test/test_versioning.py @@ -1,4 +1,5 @@ import pytest + import stix2 diff --git a/stix2/test/test_vulnerability.py b/stix2/test/test_vulnerability.py index 565f077..751460c 100644 --- a/stix2/test/test_vulnerability.py +++ b/stix2/test/test_vulnerability.py @@ -2,10 +2,12 @@ import datetime as dt import pytest import pytz + import stix2 from .constants import VULNERABILITY_ID + EXPECTED = """{ "created": "2016-05-12T08:17:27Z", "external_references": [ diff --git a/stix2/utils.py b/stix2/utils.py index a2493e2..6f3425f 100644 --- a/stix2/utils.py +++ b/stix2/utils.py @@ -3,8 +3,8 @@ import datetime as dt import json -from dateutil import parser import pytz +from dateutil import parser # Sentinel value for fields that should be set to the current time. # We can't use the standard 'default' approach, since if there are multiple diff --git a/tox.ini b/tox.ini index 16e46e0..106db9d 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py26,py27,py33,py34,py35,py36,pycodestyle +envlist = py26,py27,py33,py34,py35,py36,pycodestyle,isort-check [testenv] deps = @@ -18,11 +18,10 @@ passenv = CI TRAVIS TRAVIS_* [testenv:pycodestyle] deps = flake8 - flake8-import-order pycodestyle commands = pycodestyle ./stix2 - flake8 --max-line-length=160 --import-order-style='google' + flake8 --max-line-length=160 [pycodestyle] ignore= @@ -31,6 +30,10 @@ max-line-length=160 [flake8] max-line-length=160 +[testenv:isort-check] +deps = isort +commands = isort -ns __init__.py -c -rc stix2 + [travis] python = 2.6: py26