Skip to content

Commit 1ed030f

Browse files
committed
update to follow Node.js samples
1 parent a4b3a6e commit 1ed030f

File tree

6 files changed

+143
-46
lines changed

6 files changed

+143
-46
lines changed

dlp/dlp_inspect_image_file.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright 2018 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import sys
15+
16+
# [START dlp_inspect_text_file]
17+
# Import the Google Cloud Data Loss Prevention library
18+
import google.cloud.dlp
19+
20+
21+
def inspect_image_file(project_id='YOUR_PROJECT_ID',
22+
filepath='path/to/image.png'):
23+
# Instantiate a client
24+
dlp = google.cloud.dlp.DlpServiceClient()
25+
26+
# Get the bytes of the file
27+
with open(filepath, mode='rb') as f:
28+
item = {'byte_item': {'type': 'IMAGE', 'data': f.read()}}
29+
30+
# Construct the configuration
31+
inspect_config = {
32+
# The infoTypes of information to match
33+
'info_types': [
34+
{'name': 'PHONE_NUMBER'},
35+
{'name': 'EMAIL_ADDRESS'},
36+
{'name': 'CREDIT_CARD_NUMBER'},
37+
],
38+
# Whether to include the matching string
39+
'include_quote': True,
40+
}
41+
42+
# Convert the project id into a full resource id
43+
parent = dlp.project_path(project_id)
44+
45+
# Call the API
46+
response = dlp.inspect_content(parent, inspect_config, item)
47+
48+
# Print out the results
49+
if response.result.findings:
50+
for finding in response.result.findings:
51+
try:
52+
if finding.quote:
53+
print('Quote: {}'.format(finding.quote))
54+
except AttributeError:
55+
pass
56+
print('Info type: {}'.format(finding.info_type.name))
57+
print('Likelihood: {}'.format(finding.likelihood))
58+
else:
59+
print('No findings.')
60+
# [END dlp_inspect_text_file]
61+
62+
63+
if __name__ == '__main__':
64+
inspect_image_file(project_id=sys.argv[1], filepath=sys.argv[2])
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import os
15+
import subprocess
16+
import sys
1417

1518

16-
def test_inspect_file(capsys):
17-
import dlp_inspect_file
19+
def test_inspect_image_file():
20+
base_filepath = os.path.dirname(__file__)
21+
snippet_filepath = os.path.join(base_filepath, 'dlp_inspect_image_file.py')
22+
resource_filepath = os.path.join(base_filepath, 'resources', 'test.png')
23+
project_id = os.getenv('GOOGLE_CLOUD_PROJECT')
1824

19-
dlp_inspect_file.inspect_file()
20-
out, _ = capsys.readouterr()
21-
assert 'Info type: EMAIL_ADDRESS' in out
25+
out = subprocess.check_output(
26+
[sys.executable, snippet_filepath, project_id, resource_filepath])
27+
28+
assert 'Info type: EMAIL_ADDRESS' in str(out)

dlp/dlp_inspect_string.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import sys
1415

1516
# [START dlp_inspect_string]
16-
import os
17-
18-
# Import the client library.
17+
# Import the Google Cloud Data Loss Prevention library
1918
import google.cloud.dlp
2019

2120

22-
def inspect_string():
23-
# Instantiate a client.
21+
def inspect_string(project_id='YOUR_PROJECT_ID'):
22+
# Instantiate a client
2423
dlp = google.cloud.dlp.DlpServiceClient()
2524

25+
# Construct the configuration
2626
inspect_config = {
2727
# The infoTypes of information to match
2828
'info_types': [
@@ -34,20 +34,16 @@ def inspect_string():
3434
'include_quote': True,
3535
}
3636

37-
# Construct the `item`.
38-
content_string = 'My name is Gary Smith and my email is [email protected]'
39-
item = {'value': content_string}
37+
# Construct the `item`
38+
item = {'value': 'My name is Gary Smith and my email is [email protected]'}
4039

41-
# Convert the project id into a full resource id.
42-
# Before running this code, replace 'YOUR_PROJECT_ID' with your project ID
43-
# or set the GOOGLE_CLOUD_PROJECT environment variable to your project ID.
44-
project_id = os.getenv('GOOGLE_CLOUD_PROJECT') or 'YOUR_PROJECT_ID'
40+
# Convert the project id into a full resource id
4541
parent = dlp.project_path(project_id)
4642

47-
# Call the API.
43+
# Call the API
4844
response = dlp.inspect_content(parent, inspect_config, item)
4945

50-
# Print out the results.
46+
# Print out the results
5147
if response.result.findings:
5248
for finding in response.result.findings:
5349
try:
@@ -63,4 +59,4 @@ def inspect_string():
6359

6460

6561
if __name__ == '__main__':
66-
inspect_string()
62+
inspect_string(project_id=sys.argv[1])

dlp/dlp_inspect_string_test.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import os
15+
import subprocess
16+
import sys
1417

1518

16-
def test_inspect_file(capsys):
17-
import dlp_inspect_string
19+
def test_inspect_string():
20+
snippet_filepath = os.path.join(
21+
os.path.dirname(__file__), 'dlp_inspect_string.py')
22+
project_id = os.getenv('GOOGLE_CLOUD_PROJECT')
1823

19-
dlp_inspect_string.inspect_string()
20-
out, _ = capsys.readouterr()
21-
assert 'Info type: EMAIL_ADDRESS' in out
24+
out = subprocess.check_output(
25+
[sys.executable, snippet_filepath, project_id])
26+
27+
assert 'Info type: EMAIL_ADDRESS' in str(out)
Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import sys
1415

15-
# [START dlp_inspect_string]
16-
import os
17-
18-
# Import the client library.
16+
# [START dlp_inspect_text_file]
17+
# Import the Google Cloud Data Loss Prevention library
1918
import google.cloud.dlp
2019

2120

22-
def inspect_file():
23-
# Instantiate a client.
21+
def inspect_text_file(project_id='YOUR_PROJECT_ID',
22+
filepath='path/to/file.txt'):
23+
# Instantiate a client
2424
dlp = google.cloud.dlp.DlpServiceClient()
2525

26+
# Get the bytes of the file
27+
with open(filepath, mode='rb') as f:
28+
item = {'byte_item': {'type': 'TEXT_UTF8', 'data': f.read()}}
29+
30+
# Construct the configuration
2631
inspect_config = {
2732
# The infoTypes of information to match
2833
'info_types': [
@@ -34,23 +39,13 @@ def inspect_file():
3439
'include_quote': True,
3540
}
3641

37-
# Construct the item, containing the file's byte data.
38-
# Before running this code, replace the filename with your filepath
39-
filename = os.path.join(
40-
os.path.dirname(__file__), 'resources', 'test.txt')
41-
with open(filename, mode='rb') as f:
42-
item = {'byte_item': {'type': 'TEXT_UTF8', 'data': f.read()}}
43-
44-
# Convert the project id into a full resource id.
45-
# Before running this code, replace 'YOUR_PROJECT_ID' with your project ID
46-
# or set the GOOGLE_CLOUD_PROJECT environment variable to your project ID.
47-
project_id = os.getenv('GOOGLE_CLOUD_PROJECT') or 'YOUR_PROJECT_ID'
42+
# Convert the project id into a full resource id
4843
parent = dlp.project_path(project_id)
4944

50-
# Call the API.
45+
# Call the API
5146
response = dlp.inspect_content(parent, inspect_config, item)
5247

53-
# Print out the results.
48+
# Print out the results
5449
if response.result.findings:
5550
for finding in response.result.findings:
5651
try:
@@ -62,8 +57,8 @@ def inspect_file():
6257
print('Likelihood: {}'.format(finding.likelihood))
6358
else:
6459
print('No findings.')
65-
# [END dlp_inspect_string]
60+
# [END dlp_inspect_text_file]
6661

6762

6863
if __name__ == '__main__':
69-
inspect_file()
64+
inspect_text_file(project_id=sys.argv[1], filepath=sys.argv[2])

dlp/dlp_inspect_text_file_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2018 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import os
15+
import subprocess
16+
import sys
17+
18+
19+
def test_inspect_text_file():
20+
base_filepath = os.path.dirname(__file__)
21+
snippet_filepath = os.path.join(base_filepath, 'dlp_inspect_text_file.py')
22+
resource_filepath = os.path.join(base_filepath, 'resources', 'test.txt')
23+
project_id = os.getenv('GOOGLE_CLOUD_PROJECT')
24+
25+
out = subprocess.check_output(
26+
[sys.executable, snippet_filepath, project_id, resource_filepath])
27+
28+
assert 'Info type: PHONE_NUMBER' in str(out)
29+
assert 'Info type: EMAIL_ADDRESS' in str(out)

0 commit comments

Comments
 (0)