Skip to content

Commit 74257e7

Browse files
fix!: rename fields that collide with builtins; retrieve job config for risk analysis jobs (#75)
fix: retrieve job config for risk analysis jobs fix!: rename fields that collide with builtins. * `ByteContentItem.type` -> `ByteContentItem.type_` * `MetadataLocation.type` -> `MetadataLocation.type_` * `Container.type` -> `Container.type_` * `Bucket.min` -> `Bucket.min_` * `Bucket.max `-> `Bucket.max_` * `DlpJob.type` -> `DlpJob.type_` * `GetDlpJobRequest.type` -> `GetDlpJobRequest.type_`
1 parent f35e866 commit 74257e7

File tree

5 files changed

+53
-30
lines changed

5 files changed

+53
-30
lines changed

dlp/snippets/README.rst

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Google Data Loss Prevention Python Samples
55
===============================================================================
66

77
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
8-
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-dlp&page=editor&open_in_editor=samples/snippets/README.rst
8+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=dlp/README.rst
99

1010

1111
This directory contains samples for Google Data Loss Prevention. `Google Data Loss Prevention`_ provides programmatic access to a powerful detection engine for personally identifiable information and other privacy-sensitive data in unstructured data streams.
@@ -37,12 +37,11 @@ credentials for applications.
3737
Install Dependencies
3838
++++++++++++++++++++
3939

40-
#. Clone python-dlp and change directory to the sample directory you want to use.
40+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
4141

4242
.. code-block:: bash
4343
44-
$ git clone https://github.com/googleapis/python-dlp.git
45-
$ cd samples/snippets
44+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
4645
4746
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.
4847

@@ -78,7 +77,7 @@ Quickstart
7877
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7978

8079
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
81-
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-dlp&page=editor&open_in_editor=samples/snippets/quickstart.py,samples/snippets/README.rst
80+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=dlp/quickstart.py,dlp/README.rst
8281

8382

8483

@@ -96,7 +95,7 @@ Inspect Content
9695
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9796

9897
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
99-
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-dlp&page=editor&open_in_editor=samples/snippets/inspect_content.py,samples/snippets/README.rst
98+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=dlp/inspect_content.py,dlp/README.rst
10099

101100

102101

@@ -134,7 +133,7 @@ Redact Content
134133
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
135134

136135
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
137-
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-dlp&page=editor&open_in_editor=samples/snippets/redact.py,samples/snippets/README.rst
136+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=dlp/redact.py,dlp/README.rst
138137

139138

140139

@@ -170,7 +169,7 @@ Metadata
170169
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
171170
172171
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
173-
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-dlp&page=editor&open_in_editor=samples/snippets/metadata.py,samples/snippets/README.rst
172+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=dlp/metadata.py,dlp/README.rst
174173
175174
176175
@@ -203,7 +202,7 @@ Jobs
203202
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
204203
205204
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
206-
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-dlp&page=editor&open_in_editor=samples/snippets/jobs.py,samples/snippets/README.rst
205+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=dlp/jobs.py,dlp/README.rst
207206
208207
209208
@@ -236,7 +235,7 @@ Templates
236235
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
237236
238237
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
239-
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-dlp&page=editor&open_in_editor=samples/snippets/templates.py,samples/snippets/README.rst
238+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=dlp/templates.py,dlp/README.rst
240239
241240
242241
@@ -269,7 +268,7 @@ Triggers
269268
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
270269
271270
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
272-
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-dlp&page=editor&open_in_editor=samples/snippets/triggers.py,samples/snippets/README.rst
271+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=dlp/triggers.py,dlp/README.rst
273272
274273
275274
@@ -302,7 +301,7 @@ Risk Analysis
302301
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
303302
304303
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
305-
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-dlp&page=editor&open_in_editor=samples/snippets/risk.py,samples/snippets/README.rst
304+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=dlp/risk.py,dlp/README.rst
306305
307306
308307
@@ -341,7 +340,7 @@ DeID
341340
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
342341
343342
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
344-
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-dlp&page=editor&open_in_editor=samples/snippets/deid.py,samples/snippets/README.rst
343+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=dlp/deid.py,dlp/README.rst
345344
346345
347346

dlp/snippets/inspect_content.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def inspect_file(
401401

402402
# Construct the item, containing the file's byte data.
403403
with open(filename, mode="rb") as f:
404-
item = {"byte_item": {"type": content_type_index, "data": f.read()}}
404+
item = {"byte_item": {"type_": content_type_index, "data": f.read()}}
405405

406406
# Convert the project id into a full resource id.
407407
parent = f"projects/{project}"

dlp/snippets/jobs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def list_dlp_jobs(project, filter_string=None, job_type=None):
7575

7676
# Call the API to get a list of jobs.
7777
response = dlp.list_dlp_jobs(
78-
request={"parent": parent, "filter": filter_string, "type": job_type}
78+
request={"parent": parent, "filter": filter_string, "type_": job_type}
7979
)
8080

8181
# Iterate over results.

dlp/snippets/noxfile.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,28 @@
3737

3838
TEST_CONFIG = {
3939
# You can opt out from the test for specific Python versions.
40-
"ignored_versions": ["2.7"],
40+
'ignored_versions': ["2.7"],
41+
42+
# Old samples are opted out of enforcing Python type hints
43+
# All new samples should feature them
44+
'enforce_type_hints': False,
45+
4146
# An envvar key for determining the project id to use. Change it
4247
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
4348
# build specific Cloud project. You can also use your own string
4449
# to use your own Cloud project.
45-
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
50+
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
4651
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
52+
4753
# A dictionary you want to inject into your test. Don't put any
4854
# secrets here. These values will override predefined values.
49-
"envs": {},
55+
'envs': {},
5056
}
5157

5258

5359
try:
5460
# Ensure we can import noxfile_config in the project's directory.
55-
sys.path.append(".")
61+
sys.path.append('.')
5662
from noxfile_config import TEST_CONFIG_OVERRIDE
5763
except ImportError as e:
5864
print("No user noxfile_config found: detail: {}".format(e))
@@ -67,12 +73,12 @@ def get_pytest_env_vars():
6773
ret = {}
6874

6975
# Override the GCLOUD_PROJECT and the alias.
70-
env_key = TEST_CONFIG["gcloud_project_env"]
76+
env_key = TEST_CONFIG['gcloud_project_env']
7177
# This should error out if not set.
72-
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
78+
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
7379

7480
# Apply user supplied envs.
75-
ret.update(TEST_CONFIG["envs"])
81+
ret.update(TEST_CONFIG['envs'])
7682
return ret
7783

7884

@@ -81,7 +87,7 @@ def get_pytest_env_vars():
8187
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"]
8288

8389
# Any default versions that should be ignored.
84-
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
90+
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
8591

8692
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
8793

@@ -130,16 +136,29 @@ def _determine_local_import_names(start_dir):
130136

131137
@nox.session
132138
def lint(session):
133-
session.install("flake8", "flake8-import-order")
139+
if not TEST_CONFIG['enforce_type_hints']:
140+
session.install("flake8", "flake8-import-order")
141+
else:
142+
session.install("flake8", "flake8-import-order", "flake8-annotations")
134143

135144
local_names = _determine_local_import_names(".")
136145
args = FLAKE8_COMMON_ARGS + [
137146
"--application-import-names",
138147
",".join(local_names),
139-
".",
148+
"."
140149
]
141150
session.run("flake8", *args)
151+
#
152+
# Black
153+
#
154+
142155

156+
@nox.session
157+
def blacken(session):
158+
session.install("black")
159+
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
160+
161+
session.run("black", *python_files)
143162

144163
#
145164
# Sample Tests
@@ -180,9 +199,9 @@ def py(session):
180199
if session.python in TESTED_VERSIONS:
181200
_session_tests(session)
182201
else:
183-
session.skip(
184-
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
185-
)
202+
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
203+
session.python
204+
))
186205

187206

188207
#
@@ -199,6 +218,11 @@ def _get_repo_root():
199218
break
200219
if Path(p / ".git").exists():
201220
return str(p)
221+
# .git is not available in repos cloned via Cloud Build
222+
# setup.py is always in the library's root, so use that instead
223+
# https://github.com/googleapis/synthtool/issues/792
224+
if Path(p / "setup.py").exists():
225+
return str(p)
202226
p = p.parent
203227
raise Exception("Unable to detect repository root.")
204228

dlp/snippets/redact.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def redact_image(
9191

9292
# Construct the byte_item, containing the file's byte data.
9393
with open(filename, mode="rb") as f:
94-
byte_item = {"type": content_type_index, "data": f.read()}
94+
byte_item = {"type_": content_type_index, "data": f.read()}
9595

9696
# Convert the project id into a full resource id.
9797
parent = f"projects/{project}"
@@ -146,7 +146,7 @@ def redact_image_all_text(
146146

147147
# Construct the byte_item, containing the file's byte data.
148148
with open(filename, mode="rb") as f:
149-
byte_item = {"type": google.cloud.dlp_v2.FileType.IMAGE, "data": f.read()}
149+
byte_item = {"type_": google.cloud.dlp_v2.FileType.IMAGE, "data": f.read()}
150150

151151
# Convert the project id into a full resource id.
152152
parent = f"projects/{project}"

0 commit comments

Comments
 (0)