Skip to content

Commit e79c53b

Browse files
gcf-owl-bot[bot]dandhlee
authored andcommitted
chore(python): add nox session to sort python imports (#324)
Source-Link: googleapis/synthtool@1b71c10 Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:00c9d764fd1cd56265f12a5ef4b99a0c9e87cf261018099141e2ca5158890416 Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent c651586 commit e79c53b

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

securitycenter/snippets/noxfile.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
import nox
2424

25-
2625
# WARNING - WARNING - WARNING - WARNING - WARNING
2726
# WARNING - WARNING - WARNING - WARNING - WARNING
2827
# DO NOT EDIT THIS FILE EVER!
2928
# WARNING - WARNING - WARNING - WARNING - WARNING
3029
# WARNING - WARNING - WARNING - WARNING - WARNING
3130

3231
BLACK_VERSION = "black==22.3.0"
32+
ISORT_VERSION = "isort==5.10.1"
3333

3434
# Copy `noxfile_config.py` to your directory and modify it instead.
3535

@@ -168,12 +168,33 @@ def lint(session: nox.sessions.Session) -> None:
168168

169169
@nox.session
170170
def blacken(session: nox.sessions.Session) -> None:
171+
"""Run black. Format code to uniform standard."""
171172
session.install(BLACK_VERSION)
172173
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
173174

174175
session.run("black", *python_files)
175176

176177

178+
#
179+
# format = isort + black
180+
#
181+
182+
183+
@nox.session
184+
def format(session: nox.sessions.Session) -> None:
185+
"""
186+
Run isort to sort imports. Then run black
187+
to format code to uniform standard.
188+
"""
189+
session.install(BLACK_VERSION, ISORT_VERSION)
190+
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
191+
192+
# Use the --fss option to sort imports using strict alphabetical order.
193+
# See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
194+
session.run("isort", "--fss", *python_files)
195+
session.run("black", *python_files)
196+
197+
177198
#
178199
# Sample Tests
179200
#

securitycenter/snippets/snippets_findings.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,10 @@ def list_source(organization_id):
150150
def create_finding(source_name, finding_id):
151151
"""Creates a new finding."""
152152
# [START securitycenter_create_finding]
153+
import datetime
154+
153155
from google.cloud import securitycenter
154156
from google.cloud.securitycenter_v1 import Finding
155-
import datetime
156157

157158
# Create a new client.
158159
client = securitycenter.SecurityCenterClient()
@@ -414,8 +415,9 @@ def list_filtered_findings(source_name):
414415

415416
def list_findings_at_time(source_name):
416417
# [START securitycenter_list_findings_at_time]
418+
from datetime import datetime, timedelta
419+
417420
from google.cloud import securitycenter
418-
from datetime import timedelta, datetime
419421

420422
# Create a new client.
421423
client = securitycenter.SecurityCenterClient()
@@ -524,6 +526,7 @@ def group_findings_at_time(source_name):
524526
i = -1
525527
# [START securitycenter_group_findings_at_time]
526528
from datetime import datetime, timedelta
529+
527530
from google.cloud import securitycenter
528531

529532
# Create a client.

securitycenter/snippets/snippets_mute_config_test.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@
1818
import uuid
1919

2020
from _pytest.capture import CaptureFixture
21-
2221
from google.cloud import securitycenter
2322
from google.cloud.securitycenter_v1.services.security_center.pagers import (
2423
ListFindingsPager,
2524
)
26-
2725
import pytest
2826

2927
import snippets_mute_config
3028

31-
3229
# TODO(developer): Replace these variables before running the sample.
3330
PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]
3431
ORGANIZATION_ID = os.environ["GCLOUD_ORGANIZATION"]
@@ -55,7 +52,6 @@ def mute_rule():
5552
@pytest.fixture
5653
def finding(capsys: CaptureFixture):
5754
import snippets_findings
58-
5955
from snippets_findings import create_finding
6056

6157
snippets_findings.create_source(ORGANIZATION_ID)

0 commit comments

Comments
 (0)