Skip to content

Commit 6bedaa8

Browse files
committed
add argparse
1 parent ec56479 commit 6bedaa8

File tree

4 files changed

+102
-5
lines changed

4 files changed

+102
-5
lines changed

dlp/inspect_file.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
"""Sample app that uses the Data Loss Prevent API to inspect a file."""
16+
1517
from __future__ import print_function
1618

19+
import argparse
20+
1721

1822
# [START inspect_file]
1923
def inspect_file(filename, info_types=None, min_likelihood=None,
@@ -89,4 +93,33 @@ def inspect_file(filename, info_types=None, min_likelihood=None,
8993

9094

9195
if __name__ == '__main__':
92-
inspect_file("/usr/local/google/home/gorcester/Downloads/wQOVLom8Gsa.png", ["EMAIL_ADDRESS", "US_MALE_NAME", "US_FEMALE_NAME"])
96+
parser = argparse.ArgumentParser(
97+
description=__doc__)
98+
parser.add_argument('filename', help='The path to the file to inspect.')
99+
parser.add_argument('--info_types', action='append',
100+
help='Strings representing info types to look for. A full list of info '
101+
'categories and types is available from the API. Examples '
102+
'include "US_MALE_NAME", "US_FEMALE_NAME", "EMAIL_ADDRESS", '
103+
'"CANADA_SOCIAL_INSURANCE_NUMBER", "JAPAN_PASSPORT". If omitted, '
104+
'the API will use a limited default set. Specify this flag '
105+
'multiple times to specify multiple info types.')
106+
parser.add_argument('--min_likelihood',
107+
choices=['LIKELIHOOD_UNSPECIFIED', 'VERY_UNLIKELY', 'UNLIKELY',
108+
'POSSIBLE', 'LIKELY', 'VERY_LIKELY'],
109+
help='A string representing the minimum likelihood threshold that '
110+
'constitutes a match.')
111+
parser.add_argument('--max_findings', type=int,
112+
help='The maximum number of findings to report; 0 = no maximum.')
113+
parser.add_argument('--include_quote', type=bool,
114+
help='A boolean for whether to display a quote of the detected '
115+
'information in the results.')
116+
parser.add_argument('--mime_type',
117+
help='The MIME type of the file. If not specified, the type is '
118+
'inferred via the Python standard library\'s mimetypes module.')
119+
120+
args = parser.parse_args()
121+
122+
inspect_file(
123+
args.filename, info_types=args.info_types,
124+
min_likelihood=args.min_likelihood, include_quote=args.include_quote,
125+
mime_type=args.mime_type)

dlp/inspect_gcs_file.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
"""Sample app that uses the Data Loss Prevent API to inspect a file on Google
16+
Cloud Storage."""
17+
18+
1519
from __future__ import print_function
1620

21+
import argparse
22+
1723

1824
# [START inspect_gcs_file]
1925
def inspect_gcs_file(bucket, filename, info_types=None, min_likelihood=None,
@@ -88,6 +94,35 @@ def inspect_gcs_file(bucket, filename, info_types=None, min_likelihood=None,
8894
print('No findings.')
8995
# [END inspect_gcs_file]
9096

97+
9198
if __name__ == '__main__':
92-
# inspect_gcs_file('andrewsg-test', 'wQOVLom8Gsa.png', ["EMAIL_ADDRESS", "US_MALE_NAME", "US_FEMALE_NAME"])
93-
inspect_gcs_file('nodejs-docs-samples-dlp', 'test.txt', ["EMAIL_ADDRESS", "PHONE_NUMBER"])
99+
parser = argparse.ArgumentParser(
100+
description=__doc__)
101+
parser.add_argument('bucket',
102+
help='The name of the GCS bucket containing the file.')
103+
parser.add_argument('filename',
104+
help='The name of the file in the bucket, including the path, e.g. '
105+
'"images/myfile.png".')
106+
parser.add_argument('--info_types', action='append',
107+
help='Strings representing info types to look for. A full list of info '
108+
'categories and types is available from the API. Examples '
109+
'include "US_MALE_NAME", "US_FEMALE_NAME", "EMAIL_ADDRESS", '
110+
'"CANADA_SOCIAL_INSURANCE_NUMBER", "JAPAN_PASSPORT". If omitted, '
111+
'the API will use a limited default set. Specify this flag '
112+
'multiple times to specify multiple info types.')
113+
parser.add_argument('--min_likelihood',
114+
choices=['LIKELIHOOD_UNSPECIFIED', 'VERY_UNLIKELY', 'UNLIKELY',
115+
'POSSIBLE', 'LIKELY', 'VERY_LIKELY'],
116+
help='A string representing the minimum likelihood threshold that '
117+
'constitutes a match.')
118+
parser.add_argument('--max_findings', type=int,
119+
help='The maximum number of findings to report; 0 = no maximum.')
120+
parser.add_argument('--include_quote', type=bool,
121+
help='A boolean for whether to display a quote of the detected '
122+
'information in the results.')
123+
124+
args = parser.parse_args()
125+
126+
inspect_gcs_file(
127+
args.bucket, args.filename, info_types=args.info_types,
128+
min_likelihood=args.min_likelihood, include_quote=args.include_quote)

dlp/inspect_gcs_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def bucket(request):
4949
# Yield the object to the test code; lines after this execute as a teardown.
5050
yield bucket
5151

52+
# Delete the files.
5253
for blob in blobs:
5354
blob.delete()
5455

dlp/inspect_string.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
"""Sample app that uses the Data Loss Prevent API to inspect a string."""
16+
1517
from __future__ import print_function
1618

19+
import argparse
20+
1721

1822
# [START inspect_string]
1923
def inspect_string(item, info_types=None, min_likelihood=None,
@@ -79,5 +83,29 @@ def inspect_string(item, info_types=None, min_likelihood=None,
7983

8084

8185
if __name__ == '__main__':
82-
inspect_string("I'm Gary and my email is gary@example.com", ["EMAIL_ADDRESS", "US_MALE_NAME", "US_FEMALE_NAME"])
83-
# DO NOT SUBMIT
86+
parser = argparse.ArgumentParser(
87+
description=__doc__)
88+
parser.add_argument('item', help='The string to inspect.')
89+
parser.add_argument('--info_types', action='append',
90+
help='Strings representing info types to look for. A full list of info '
91+
'categories and types is available from the API. Examples '
92+
'include "US_MALE_NAME", "US_FEMALE_NAME", "EMAIL_ADDRESS", '
93+
'"CANADA_SOCIAL_INSURANCE_NUMBER", "JAPAN_PASSPORT". If omitted, '
94+
'the API will use a limited default set. Specify this flag '
95+
'multiple times to specify multiple info types.')
96+
parser.add_argument('--min_likelihood',
97+
choices=['LIKELIHOOD_UNSPECIFIED', 'VERY_UNLIKELY', 'UNLIKELY',
98+
'POSSIBLE', 'LIKELY', 'VERY_LIKELY'],
99+
help='A string representing the minimum likelihood threshold that '
100+
'constitutes a match.')
101+
parser.add_argument('--max_findings', type=int,
102+
help='The maximum number of findings to report; 0 = no maximum.')
103+
parser.add_argument('--include_quote', type=bool,
104+
help='A boolean for whether to display a quote of the detected '
105+
'information in the results.')
106+
107+
args = parser.parse_args()
108+
109+
inspect_string(
110+
args.item, info_types=args.info_types,
111+
min_likelihood=args.min_likelihood, include_quote=args.include_quote)

0 commit comments

Comments
 (0)