From ba087829349a25f40b2afe6325021de08ac6526a Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Wed, 20 Apr 2022 20:58:20 -0400 Subject: [PATCH] chore(python): add nox session to sort python imports (#324) Source-Link: https://github.com/googleapis/synthtool/commit/1b71c10e20de7ed3f97f692f99a0e3399b67049f Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:00c9d764fd1cd56265f12a5ef4b99a0c9e87cf261018099141e2ca5158890416 Co-authored-by: Owl Bot --- securitycenter/snippets/noxfile.py | 23 ++++++++++++++++++- securitycenter/snippets/snippets_findings.py | 7 ++++-- .../snippets/snippets_mute_config_test.py | 4 ---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/securitycenter/snippets/noxfile.py b/securitycenter/snippets/noxfile.py index 25f87a215d4c..3b3ffa5d2b0f 100644 --- a/securitycenter/snippets/noxfile.py +++ b/securitycenter/snippets/noxfile.py @@ -22,7 +22,6 @@ import nox - # WARNING - WARNING - WARNING - WARNING - WARNING # WARNING - WARNING - WARNING - WARNING - WARNING # DO NOT EDIT THIS FILE EVER! @@ -30,6 +29,7 @@ # WARNING - WARNING - WARNING - WARNING - WARNING BLACK_VERSION = "black==22.3.0" +ISORT_VERSION = "isort==5.10.1" # Copy `noxfile_config.py` to your directory and modify it instead. @@ -168,12 +168,33 @@ def lint(session: nox.sessions.Session) -> None: @nox.session def blacken(session: nox.sessions.Session) -> None: + """Run black. Format code to uniform standard.""" session.install(BLACK_VERSION) python_files = [path for path in os.listdir(".") if path.endswith(".py")] session.run("black", *python_files) +# +# format = isort + black +# + + +@nox.session +def format(session: nox.sessions.Session) -> None: + """ + Run isort to sort imports. Then run black + to format code to uniform standard. + """ + session.install(BLACK_VERSION, ISORT_VERSION) + python_files = [path for path in os.listdir(".") if path.endswith(".py")] + + # Use the --fss option to sort imports using strict alphabetical order. + # See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections + session.run("isort", "--fss", *python_files) + session.run("black", *python_files) + + # # Sample Tests # diff --git a/securitycenter/snippets/snippets_findings.py b/securitycenter/snippets/snippets_findings.py index 67f42242cac3..06cddc412f05 100644 --- a/securitycenter/snippets/snippets_findings.py +++ b/securitycenter/snippets/snippets_findings.py @@ -150,9 +150,10 @@ def list_source(organization_id): def create_finding(source_name, finding_id): """Creates a new finding.""" # [START securitycenter_create_finding] + import datetime + from google.cloud import securitycenter from google.cloud.securitycenter_v1 import Finding - import datetime # Create a new client. client = securitycenter.SecurityCenterClient() @@ -414,8 +415,9 @@ def list_filtered_findings(source_name): def list_findings_at_time(source_name): # [START securitycenter_list_findings_at_time] + from datetime import datetime, timedelta + from google.cloud import securitycenter - from datetime import timedelta, datetime # Create a new client. client = securitycenter.SecurityCenterClient() @@ -524,6 +526,7 @@ def group_findings_at_time(source_name): i = -1 # [START securitycenter_group_findings_at_time] from datetime import datetime, timedelta + from google.cloud import securitycenter # Create a client. diff --git a/securitycenter/snippets/snippets_mute_config_test.py b/securitycenter/snippets/snippets_mute_config_test.py index 9900cd9c59ef..5c531d10cda4 100644 --- a/securitycenter/snippets/snippets_mute_config_test.py +++ b/securitycenter/snippets/snippets_mute_config_test.py @@ -18,17 +18,14 @@ import uuid from _pytest.capture import CaptureFixture - from google.cloud import securitycenter from google.cloud.securitycenter_v1.services.security_center.pagers import ( ListFindingsPager, ) - import pytest import snippets_mute_config - # TODO(developer): Replace these variables before running the sample. PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] ORGANIZATION_ID = os.environ["GCLOUD_ORGANIZATION"] @@ -55,7 +52,6 @@ def mute_rule(): @pytest.fixture def finding(capsys: CaptureFixture): import snippets_findings - from snippets_findings import create_finding snippets_findings.create_source(ORGANIZATION_ID)