From bbd293703e4a8d8177e58d7bfb0db9b2384cb769 Mon Sep 17 00:00:00 2001 From: Jessica Date: Thu, 11 Jun 2020 21:32:10 +0000 Subject: [PATCH 1/2] Add a simplified inspect string example --- dlp/inspect_content.py | 58 +++++++++++++++++++++++++++++++++++++ dlp/inspect_content_test.py | 10 +++++++ 2 files changed, 68 insertions(+) diff --git a/dlp/inspect_content.py b/dlp/inspect_content.py index 7d69fa7e05d..8e0bf08180b 100644 --- a/dlp/inspect_content.py +++ b/dlp/inspect_content.py @@ -22,6 +22,64 @@ import os +# [START dlp_inspect_string_basic] +def inspect_string_basic( + project, + content_string, + info_types=["PHONE_NUMBER"], +): + """Uses the Data Loss Prevention API to analyze strings for protected data. + Args: + project: The Google Cloud project id to use as a parent resource. + content_string: The string to inspect. + info_types: A list of strings representing info types to look for. + A full list of info type categories can be fetched from the API. + Returns: + None; the response from the API is printed to the terminal. + """ + + # Import the client library. + import google.cloud.dlp + + # Instantiate a client. + dlp = google.cloud.dlp_v2.DlpServiceClient() + + # Prepare info_types by converting the list of strings into a list of + # dictionaries (protos are also accepted). + info_types = [{"name": info_type} for info_type in info_types] + + # Construct the configuration dictionary. + inspect_config = { + "info_types": info_types, + "include_quote": True, + } + + # Construct the `item`. + item = {"value": content_string} + + # Convert the project id into a full resource id. + parent = dlp.project_path(project) + + # Call the API. + response = dlp.inspect_content(parent, inspect_config, item) + + # Print out the results. + if response.result.findings: + for finding in response.result.findings: + try: + if finding.quote: + print("Quote: {}".format(finding.quote)) + except AttributeError: + pass + print("Info type: {}".format(finding.info_type.name)) + print("Likelihood: {}".format(finding.likelihood)) + else: + print("No findings.") + + +# [END dlp_inspect_string_basic] + + # [START dlp_inspect_string] def inspect_string( project, diff --git a/dlp/inspect_content_test.py b/dlp/inspect_content_test.py index e211083b8b2..bdabda265c1 100644 --- a/dlp/inspect_content_test.py +++ b/dlp/inspect_content_test.py @@ -161,6 +161,16 @@ def bigquery_project(): bigquery_client.delete_dataset(dataset_ref, delete_contents=True) +def test_inspect_string_basic(capsys): + test_string = "String with a phone number: 234-555-6789" + + inspect_content.inspect_string_basic(GCLOUD_PROJECT, test_string) + + out, _ = capsys.readouterr() + assert "Info type: PHONE_NUMBER" in out + assert "Quote: 234-555-6789" in out + + def test_inspect_string(capsys): test_string = "My name is Gary Smith and my email is gary@example.com" From 69f2e4c038353fe2feea8d0334f8822611af0e5f Mon Sep 17 00:00:00 2001 From: Jessica Date: Fri, 12 Jun 2020 17:49:44 +0000 Subject: [PATCH 2/2] Remove unnecessary try-catch block - all findings in this examnple should have quotes. --- dlp/inspect_content.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/dlp/inspect_content.py b/dlp/inspect_content.py index 8e0bf08180b..fb2573e4bc8 100644 --- a/dlp/inspect_content.py +++ b/dlp/inspect_content.py @@ -66,11 +66,7 @@ def inspect_string_basic( # Print out the results. if response.result.findings: for finding in response.result.findings: - try: - if finding.quote: - print("Quote: {}".format(finding.quote)) - except AttributeError: - pass + print("Quote: {}".format(finding.quote)) print("Info type: {}".format(finding.info_type.name)) print("Likelihood: {}".format(finding.likelihood)) else: