|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2018 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""This application demonstrates how to perform create operations |
| 18 | +on Product set in Cloud Vision Product Search. |
| 19 | +
|
| 20 | +For more information, see the tutorial page at |
| 21 | +https://cloud.google.com/vision/product-search/docs/ |
| 22 | +""" |
| 23 | + |
| 24 | +# [START product_search_import] |
| 25 | +import argparse |
| 26 | + |
| 27 | +from google.cloud import vision_v1p3beta1 as vision |
| 28 | +# [END product_search_import] |
| 29 | + |
| 30 | + |
| 31 | +# [START product_search_add_product_to_product_set] |
| 32 | +def add_product_to_product_set( |
| 33 | + project_id, location, product_id, product_set_id): |
| 34 | + """Add a product to a product set. |
| 35 | + Args: |
| 36 | + project_id: Id of the project. |
| 37 | + location: A compute region name. |
| 38 | + product_id: Id of the product. |
| 39 | + product_set_id: Id of the product set. |
| 40 | + """ |
| 41 | + client = vision.ProductSearchClient() |
| 42 | + |
| 43 | + # Get the full path of the product set. |
| 44 | + product_set_path = client.product_set_path( |
| 45 | + project=project_id, location=location, |
| 46 | + product_set=product_set_id) |
| 47 | + |
| 48 | + # Get the full path of the product. |
| 49 | + product_path = client.product_path( |
| 50 | + project=project_id, location=location, product=product_id) |
| 51 | + |
| 52 | + # Add the product to the product set. |
| 53 | + client.add_product_to_product_set( |
| 54 | + name=product_set_path, product=product_path) |
| 55 | + print('Product added to product set.') |
| 56 | +# [END product_search_add_product_to_product_set] |
| 57 | + |
| 58 | + |
| 59 | +# [START product_search_list_products_in_product_set] |
| 60 | +def list_products_in_product_set( |
| 61 | + project_id, location, product_set_id): |
| 62 | + """List all products in a product set. |
| 63 | + Args: |
| 64 | + project_id: Id of the project. |
| 65 | + location: A compute region name. |
| 66 | + product_set_id: Id of the product set. |
| 67 | + """ |
| 68 | + client = vision.ProductSearchClient() |
| 69 | + |
| 70 | + # Get the full path of the product set. |
| 71 | + product_set_path = client.product_set_path( |
| 72 | + project=project_id, location=location, |
| 73 | + product_set=product_set_id) |
| 74 | + |
| 75 | + # List all the products available in the product set. |
| 76 | + products = client.list_products_in_product_set(name=product_set_path) |
| 77 | + |
| 78 | + # Display the product information. |
| 79 | + for product in products: |
| 80 | + print('Product name: {}'.format(product.name)) |
| 81 | + print('Product id: {}'.format(product.name.split('/')[-1])) |
| 82 | + print('Product display name: {}'.format(product.display_name)) |
| 83 | + print('Product description: {}'.format(product.description)) |
| 84 | + print('Product category: {}'.format(product.product_category)) |
| 85 | + print('Product labels: {}'.format(product.product_labels)) |
| 86 | +# [END product_search_list_products_in_product_set] |
| 87 | + |
| 88 | + |
| 89 | +# [START product_search_remove_product_from_product_set] |
| 90 | +def remove_product_from_product_set( |
| 91 | + project_id, location, product_id, product_set_id): |
| 92 | + """Remove a product from a product set. |
| 93 | + Args: |
| 94 | + project_id: Id of the project. |
| 95 | + location: A compute region name. |
| 96 | + product_id: Id of the product. |
| 97 | + product_set_id: Id of the product set. |
| 98 | + """ |
| 99 | + client = vision.ProductSearchClient() |
| 100 | + |
| 101 | + # Get the full path of the product set. |
| 102 | + product_set_path = client.product_set_path( |
| 103 | + project=project_id, location=location, |
| 104 | + product_set=product_set_id) |
| 105 | + |
| 106 | + # Get the full path of the product. |
| 107 | + product_path = client.product_path( |
| 108 | + project=project_id, location=location, product=product_id) |
| 109 | + |
| 110 | + # Remove the product from the product set. |
| 111 | + client.remove_product_from_product_set( |
| 112 | + name=product_set_path, product=product_path) |
| 113 | + print('Product removed from product set.') |
| 114 | +# [END product_search_remove_product_from_product_set] |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == '__main__': |
| 118 | + parser = argparse.ArgumentParser( |
| 119 | + description=__doc__, |
| 120 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 121 | + subparsers = parser.add_subparsers(dest='command') |
| 122 | + parser.add_argument( |
| 123 | + '--project_id', |
| 124 | + help='Project id. Required', |
| 125 | + required=True) |
| 126 | + parser.add_argument( |
| 127 | + '--location', |
| 128 | + help='Compute region name', |
| 129 | + default='us-west1') |
| 130 | + |
| 131 | + add_product_to_product_set_parser = subparsers.add_parser( |
| 132 | + 'add_product_to_product_set', help=add_product_to_product_set.__doc__) |
| 133 | + add_product_to_product_set_parser.add_argument('product_id') |
| 134 | + add_product_to_product_set_parser.add_argument('product_set_id') |
| 135 | + |
| 136 | + list_products_in_product_set_parser = subparsers.add_parser( |
| 137 | + 'list_products_in_product_set', |
| 138 | + help=list_products_in_product_set.__doc__) |
| 139 | + list_products_in_product_set_parser.add_argument('product_set_id') |
| 140 | + |
| 141 | + remove_product_from_product_set_parser = subparsers.add_parser( |
| 142 | + 'remove_product_from_product_set', |
| 143 | + help=remove_product_from_product_set.__doc__) |
| 144 | + remove_product_from_product_set_parser.add_argument('product_id') |
| 145 | + remove_product_from_product_set_parser.add_argument('product_set_id') |
| 146 | + |
| 147 | + args = parser.parse_args() |
| 148 | + |
| 149 | + if args.command == 'add_product_to_product_set': |
| 150 | + add_product_to_product_set( |
| 151 | + args.project_id, args.location, args.product_id, |
| 152 | + args.product_set_id) |
| 153 | + elif args.command == 'list_products_in_product_set': |
| 154 | + list_products_in_product_set( |
| 155 | + args.project_id, args.location, args.product_set_id) |
| 156 | + elif args.command == 'remove_product_from_product_set': |
| 157 | + remove_product_from_product_set( |
| 158 | + args.project_id, args.location, args.product_id, |
| 159 | + args.product_set_id) |
0 commit comments