From 85093cf859252a374381a3ab92ca2d676d9338a6 Mon Sep 17 00:00:00 2001 From: Jorge Sanz Date: Wed, 27 Dec 2017 16:31:53 +0100 Subject: [PATCH] Fix how to handle env vars on examples and onprem tests (#70) * how to handle correctly environment parameters * fix rest of examples * fix onpremises tests * bump to 1.1.2 version * Update NEWS --- NEWS | 4 ++++ doc/source/conf.py | 4 ++-- examples/change_dataset_privacy.py | 19 ++++++++++++------- examples/check_query.py | 17 +++++++++++------ examples/create_anonymous_map.py | 18 ++++++++++++------ examples/create_named_map.py | 19 ++++++++++++------- examples/export_create_tables.py | 17 +++++++++++------ examples/export_dataset.py | 15 ++++++++++----- examples/export_map.py | 18 +++++++++++------- examples/import_and_merge.py | 15 ++++++++++----- examples/import_from_database.py | 15 ++++++++++----- examples/import_standard_table.py | 15 ++++++++++----- examples/import_sync_table.py | 20 ++++++++++++-------- examples/import_sync_table_as_dataset.py | 19 ++++++++++++------- examples/instantiate_named_map.py | 20 ++++++++++++-------- examples/kill_query.py | 14 +++++++++++--- examples/list_tables.py | 17 +++++++++++------ examples/map_info.py | 18 ++++++++++++------ examples/running_queries.py | 17 +++++++++++------ examples/sql_batch_api_jobs.py | 18 +++++++++++------- examples/table_info.py | 18 ++++++++++++------ examples/user_info.py | 20 +++++++++++++------- setup.py | 2 +- tests/conftest.py | 4 ++-- 24 files changed, 235 insertions(+), 128 deletions(-) diff --git a/NEWS b/NEWS index 443d93c..e4de1cc 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +Dec-27-2017: version 1.1.2 + - Fix on examples management of environment variables (#70) + - Fix onpremises tests (#70) + Oct-26-2017: version 1.1.1 - Fix for using with non-organization users (#67) - Fix in filter function (#68) diff --git a/doc/source/conf.py b/doc/source/conf.py index 9cbc8bc..a315714 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -55,9 +55,9 @@ # built documents. # # The short X.Y version. -version = u'1.1.1' +version = u'1.1.2' # The full version, including alpha/beta/rc tags. -release = u'1.1.1' +release = u'1.1.2' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/examples/change_dataset_privacy.py b/examples/change_dataset_privacy.py index de4eff5..3639ae8 100644 --- a/examples/change_dataset_privacy.py +++ b/examples/change_dataset_privacy.py @@ -28,29 +28,34 @@ help='One of: LINK, PUBLIC, PRIVATE') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Set authentification to CARTO -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + dataset_manager = DatasetManager(auth_client) + dataset = dataset_manager.get(args.dataset_name) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) -dataset_manager = DatasetManager(auth_client) -dataset = dataset_manager.get(args.dataset_name) # PRIVATE, PUBLIC, LINK dataset.privacy = args.privacy dataset.save() diff --git a/examples/check_query.py b/examples/check_query.py index f8679f8..89020b1 100644 --- a/examples/check_query.py +++ b/examples/check_query.py @@ -25,27 +25,32 @@ help='Set query to analyze') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Authenticate to CARTO account -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) -dataset_manager = DatasetManager(auth_client) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + dataset_manager = DatasetManager(auth_client) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) # SQL wrapper sql = SQLClient(APIKeyAuthClient(args.CARTO_BASE_URL, args.CARTO_API_KEY)) diff --git a/examples/create_anonymous_map.py b/examples/create_anonymous_map.py index 0fe3925..3cc1d1d 100644 --- a/examples/create_anonymous_map.py +++ b/examples/create_anonymous_map.py @@ -26,28 +26,34 @@ help='Path to the anonymous map JSON description file') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Set authentification to CARTO -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + anonymous = AnonymousMap(auth_client) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) + -anonymous = AnonymousMap(auth_client) with open(args.anonymous_map_json) as anonymous_map_json: template = json.load(anonymous_map_json) diff --git a/examples/create_named_map.py b/examples/create_named_map.py index 31e186c..50ae339 100644 --- a/examples/create_named_map.py +++ b/examples/create_named_map.py @@ -26,29 +26,34 @@ help='Path to the named map JSON description file') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Set authentification to CARTO -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + named_map_manager = NamedMapManager(auth_client) + n = NamedMap(named_map_manager.client) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) -named_map_manager = NamedMapManager(auth_client) -n = NamedMap(named_map_manager.client) with open(args.named_map_json) as named_map_json: template = json.load(named_map_json) diff --git a/examples/export_create_tables.py b/examples/export_create_tables.py index 8484899..24be279 100644 --- a/examples/export_create_tables.py +++ b/examples/export_create_tables.py @@ -14,27 +14,32 @@ description='Exports the CREATE TABLE scripts of all the account datasets') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Authenticate to CARTO account -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) -dataset_manager = DatasetManager(auth_client) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + dataset_manager = DatasetManager(auth_client) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) # SQL wrapper sql = SQLClient(APIKeyAuthClient(args.CARTO_BASE_URL, args.CARTO_API_KEY)) diff --git a/examples/export_dataset.py b/examples/export_dataset.py index 6d39db2..4ccd714 100644 --- a/examples/export_dataset.py +++ b/examples/export_dataset.py @@ -20,18 +20,18 @@ description='Exports a dataset') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') @@ -47,8 +47,13 @@ # Authenticate to CARTO account -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) # SQL wrapper sql = SQLClient(APIKeyAuthClient(args.CARTO_BASE_URL, args.CARTO_API_KEY)) diff --git a/examples/export_map.py b/examples/export_map.py index dca295a..fdf8ed2 100644 --- a/examples/export_map.py +++ b/examples/export_map.py @@ -25,28 +25,32 @@ help='The name of the map in CARTO') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Set authentification to CARTO -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) - -visualization = VisualizationManager(auth_client).get(args.map_name) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + visualization = VisualizationManager(auth_client).get(args.map_name) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) url = visualization.export() diff --git a/examples/import_and_merge.py b/examples/import_and_merge.py index dc32394..8bcf715 100644 --- a/examples/import_and_merge.py +++ b/examples/import_and_merge.py @@ -33,26 +33,31 @@ ' "files/*.csv"') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Set authentification to CARTO -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) # get username from base_url substring = re.search('https://(.+?).carto.com', args.CARTO_BASE_URL) diff --git a/examples/import_from_database.py b/examples/import_from_database.py index 040c4f1..6a4dcfc 100644 --- a/examples/import_from_database.py +++ b/examples/import_from_database.py @@ -39,26 +39,31 @@ help='An external database connection JSON object') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Set authentification to CARTO -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, "organization") +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) # get username from base_url substring = re.search('https://(.+?).carto.com', args.CARTO_BASE_URL) diff --git a/examples/import_standard_table.py b/examples/import_standard_table.py index 3afd975..ee792bc 100644 --- a/examples/import_standard_table.py +++ b/examples/import_standard_table.py @@ -27,26 +27,31 @@ ' Add it in double quotes') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Set authentification to CARTO -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) # get username from base_url substring = re.search('https://(.+?).carto.com', args.CARTO_BASE_URL) diff --git a/examples/import_sync_table.py b/examples/import_sync_table.py index 0683498..aa324b6 100644 --- a/examples/import_sync_table.py +++ b/examples/import_sync_table.py @@ -31,29 +31,33 @@ ' table in seconds (min: 900s)') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Set authentification to CARTO -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) - -syncTableManager = SyncTableJobManager(auth_client) -syncTable = syncTableManager.create(args.url, args.sync_time) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + syncTableManager = SyncTableJobManager(auth_client) + syncTable = syncTableManager.create(args.url, args.sync_time) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) # return the id of the sync logging.debug((syncTable.get_id())) diff --git a/examples/import_sync_table_as_dataset.py b/examples/import_sync_table_as_dataset.py index 4a48f63..9e93fa9 100644 --- a/examples/import_sync_table_as_dataset.py +++ b/examples/import_sync_table_as_dataset.py @@ -31,28 +31,33 @@ ' table in seconds (min: 900s)') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Set authentification to CARTO -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) -dataset_manager = DatasetManager(auth_client) -table = dataset_manager.create(args.url, args.sync_time) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + dataset_manager = DatasetManager(auth_client) + table = dataset_manager.create(args.url, args.sync_time) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) # get username from base_url substring = re.search('https://(.+?).carto.com', args.CARTO_BASE_URL) diff --git a/examples/instantiate_named_map.py b/examples/instantiate_named_map.py index 6b56279..5a6fd64 100644 --- a/examples/instantiate_named_map.py +++ b/examples/instantiate_named_map.py @@ -33,29 +33,33 @@ help='A valid token set when the named map was created') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Set authentification to CARTO -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) - -named_map_manager = NamedMapManager(auth_client) -named_map = named_map_manager.get(args.named_map_id) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + named_map_manager = NamedMapManager(auth_client) + named_map = named_map_manager.get(args.named_map_id) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) with open(args.template_json) as template_json: template = json.load(template_json) diff --git a/examples/kill_query.py b/examples/kill_query.py index 4d72a45..a194309 100644 --- a/examples/kill_query.py +++ b/examples/kill_query.py @@ -24,23 +24,31 @@ help='Set the pid of the query to kill') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) + # SQL wrapper sql = SQLClient(APIKeyAuthClient(args.CARTO_BASE_URL, args.CARTO_API_KEY)) diff --git a/examples/list_tables.py b/examples/list_tables.py index 019c9de..5ea3469 100644 --- a/examples/list_tables.py +++ b/examples/list_tables.py @@ -15,27 +15,32 @@ ' and indicating if they are cartodbfied or not') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Authenticate to CARTO account -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) -dataset_manager = DatasetManager(auth_client) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + dataset_manager = DatasetManager(auth_client) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) # SQL wrapper sql = SQLClient(APIKeyAuthClient(args.CARTO_BASE_URL, args.CARTO_API_KEY)) diff --git a/examples/map_info.py b/examples/map_info.py index f6eb04e..2f4ff13 100644 --- a/examples/map_info.py +++ b/examples/map_info.py @@ -33,28 +33,34 @@ help='Set the name of the map and export it') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Set authentification to CARTO -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + visualization_manager = VisualizationManager(auth_client) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) + -visualization_manager = VisualizationManager(auth_client) # Render map info or the name of the maps if args.map_name is None and args.export_map is None: diff --git a/examples/running_queries.py b/examples/running_queries.py index 91f94af..b97cbab 100644 --- a/examples/running_queries.py +++ b/examples/running_queries.py @@ -21,18 +21,18 @@ description='Return the running queries of the account') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') @@ -40,9 +40,14 @@ # Authenticate to CARTO account -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) -dataset_manager = DatasetManager(auth_client) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + dataset_manager = DatasetManager(auth_client) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) # SQL wrapper sql = SQLClient(APIKeyAuthClient(args.CARTO_BASE_URL, args.CARTO_API_KEY)) diff --git a/examples/sql_batch_api_jobs.py b/examples/sql_batch_api_jobs.py index ac8eeb1..9ff7c9a 100644 --- a/examples/sql_batch_api_jobs.py +++ b/examples/sql_batch_api_jobs.py @@ -30,28 +30,32 @@ help='Set the id of the job to check') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') args = parser.parse_args() # Set authentification to CARTO -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) - -batchSQLClient = BatchSQLClient(auth_client) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + batchSQLClient = BatchSQLClient(auth_client) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) # Batch SQL API operations if args.operation == 'create': diff --git a/examples/table_info.py b/examples/table_info.py index 0c491ca..e2734a5 100644 --- a/examples/table_info.py +++ b/examples/table_info.py @@ -29,18 +29,18 @@ help='Set the name of the table to explore') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') @@ -48,9 +48,15 @@ # Authenticate to CARTO account -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) -dataset_manager = DatasetManager(auth_client) + +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + dataset_manager = DatasetManager(auth_client) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) # SQL wrapper diff --git a/examples/user_info.py b/examples/user_info.py index 8996190..9b987b6 100644 --- a/examples/user_info.py +++ b/examples/user_info.py @@ -22,32 +22,38 @@ description='Return information from a specific user') parser.add_argument('--organization', type=str, dest='organization', - default=os.environ['CARTO_ORG'], + default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '', help='Set the name of the organization' + ' account (defaults to env variable CARTO_ORG)') parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', - default=os.environ['CARTO_API_URL'], + default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '', help='Set the base URL. For example:' + ' https://username.carto.com/ ' + '(defaults to env variable CARTO_API_URL)') parser.add_argument('--api_key', dest='CARTO_API_KEY', - default=os.environ['CARTO_API_KEY'], + default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '', help='Api key of the account' + ' (defaults to env variable CARTO_API_KEY)') parser.add_argument('--username', dest='CARTO_USER', - default=os.environ['CARTO_USER'], + default=os.environ['CARTO_USER'] if 'CARTO_USER' in os.environ else '', help='define username of the organization' + ' to check (defaults to env variable CARTO_USER)') args = parser.parse_args() # Set authentification to CARTO -auth_client = APIKeyAuthClient( - args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) -user_manager = UserManager(auth_client) +if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.CARTO_USER and args.organization: + auth_client = APIKeyAuthClient( + args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization) + user_manager = UserManager(auth_client) +else: + logger.error('You need to provide valid credentials, run with -h parameter for details') + import sys + sys.exit(1) + userInfo = [] print('\nThe attributes of the user are:\n') diff --git a/setup.py b/setup.py index 5415d92..332a5a5 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ author="Daniel CarriĆ³n", author_email="daniel@carto.com", description="SDK around CARTO's APIs", - version="1.1.1", + version="1.1.2", url="https://github.com/CartoDB/carto-python", install_requires=required, packages=["carto"]) diff --git a/tests/conftest.py b/tests/conftest.py index 9d38d90..901c5e8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -88,7 +88,7 @@ def non_verified_auth_client(): test requests to CARTO onpremises :return: NonVerifiedAPIKeyAuthClient instance """ - if ONPREM_API_KEY is None: + if not ONPREM_API_KEY: return None return NonVerifiedAPIKeyAuthClient(ONPREMISES_USR_BASE_URL, ONPREM_API_KEY, ONPREM_ORGANIZATION) @@ -100,7 +100,7 @@ def wrong_onprem_auth_client(): test requests to CARTO onpremises :return: NonVerifiedAPIKeyAuthClient instance """ - if ONPREM_API_KEY is None: + if not ONPREM_API_KEY: return None return APIKeyAuthClient(ONPREMISES_USR_BASE_URL, ONPREM_API_KEY, ONPREM_ORGANIZATION)