Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #14 from arredond/arredond/carto_dataset_read_from…
Browse files Browse the repository at this point in the history
…_pipe

pass arguments through check_piped function + some typo fixes
  • Loading branch information
Jorge Sanz authored Jan 8, 2019
2 parents 9bc5980 + 27c7a97 commit dd72c1e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 23 deletions.
10 changes: 7 additions & 3 deletions carto_cli/carto_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os.path
import yaml

from carto_cli.utils import check_piped_arg

from .carto.version import version

default_config_file = os.environ.get(
Expand Down Expand Up @@ -36,13 +38,14 @@ def list(ctx):


@cli.command(help='Returns the user names that match the given string')
@click.argument('search')
@click.argument('search', callback=check_piped_arg, required=False)
@click.help_option('-h', '--help')
@click.pass_context
def search(ctx, search):
'''
Search for a string in the configuration file
'''

config = ctx.obj['config']
for user in ctx.obj['config']:
if 'user' in config[user] and config[user]['user'].find(search) > -1:
Expand All @@ -51,18 +54,19 @@ def search(ctx, search):
click.echo(user)


@cli.command(help='The user wou want to retrieve')
@cli.command(help='The user you want to retrieve')
@click.option('-o', '--output-file', type=click.File('w'), envvar='CARTO_ENV',
help="Output file to store the export commands, it can use the" +
" $CARTO_ENV environment variable")
@click.argument('user')
@click.argument('user', callback=check_piped_arg, required=False)
@click.help_option('-h', '--help')
@click.pass_context
def load(ctx, output_file, user):
'''
Tries to load the information as a easy copy&paste set of env vars
'''
config = ctx.obj['config']

if not user in config:
ctx.fail('User not found on config')
else:
Expand Down
8 changes: 5 additions & 3 deletions carto_cli/commands/batch/job.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import click
import json
import re
import sys

from carto_cli.carto import queries
from carto_cli.utils import check_piped_arg


@click.command(help="Display the ids of all your running jobs")
Expand All @@ -28,7 +30,7 @@ def list(ctx):


@click.command(help="Returns details about a job id (JSON)")
@click.argument('job_id')
@click.argument('job_id', callback=check_piped_arg, required=False)
@click.help_option('-h', '--help')
@click.pass_context
def read(ctx,job_id):
Expand All @@ -45,7 +47,7 @@ def read(ctx,job_id):


@click.command(help="Creates a new job and returns its ID")
@click.argument('sql', nargs=-1)
@click.argument('sql', nargs=-1, callback=check_piped_arg, required=False)
@click.help_option('-h', '--help')
@click.pass_context
def create(ctx,sql):
Expand All @@ -63,7 +65,7 @@ def create(ctx,sql):


@click.command(help="Cancels a job")
@click.argument('job_id')
@click.argument('job_id', callback=check_piped_arg, required=False)
@click.help_option('-h', '--help')
@click.pass_context
def cancel(ctx,job_id):
Expand Down
35 changes: 21 additions & 14 deletions carto_cli/commands/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from prettytable import PrettyTable
from carto_cli.carto import queries
from carto_cli.commands.sql.execute_sql import run as run_sql
from carto_cli.utils import check_piped_arg

from carto.permissions import PRIVATE, PUBLIC, LINK

Expand Down Expand Up @@ -182,10 +183,11 @@ def list_tables(ctx, format, filter):
@click.option('-f', '--format', default="json", help="Format of your results",
type=click.Choice(['json', 'csv', 'pretty']))
@click.help_option('-h', '--help')
@click.argument('table_name')
@click.argument('table_name', callback=check_piped_arg, required=False)
@click.pass_context
def schema(ctx, format, table_name):
carto_obj = ctx.obj['carto']

sql = queries.SCHEMA.format(table_name=table_name)
result = carto_obj.execute_sql(sql)

Expand Down Expand Up @@ -217,10 +219,11 @@ def schema(ctx, format, table_name):
@click.option('-f', '--format', default="json", help="Format of your results",
type=click.Choice(['json', 'csv', 'pretty']))
@click.help_option('-h', '--help')
@click.argument('table_name')
@click.argument('table_name', callback=check_piped_arg, required=False)
@click.pass_context
def triggers(ctx, format, table_name):
carto_obj = ctx.obj['carto']

sql = queries.TRIGGERS.format(table_name=table_name)
result = carto_obj.execute_sql(sql)
fieldnames = ['tgname']
Expand Down Expand Up @@ -253,10 +256,11 @@ def triggers(ctx, format, table_name):
@click.option('-f', '--format', default="json", help="Format of your results",
type=click.Choice(['json', 'csv', 'pretty']))
@click.help_option('-h', '--help')
@click.argument('table_name')
@click.argument('table_name', callback=check_piped_arg, required=False)
@click.pass_context
def indexes(ctx, format, table_name):
carto_obj = ctx.obj['carto']

sql = queries.INDEXES.format(table_name=table_name)
result = carto_obj.execute_sql(sql)
fieldnames = ['index_name', 'column_name', 'index_type']
Expand Down Expand Up @@ -288,10 +292,11 @@ def indexes(ctx, format, table_name):
@click.option('-r','--refresh',is_flag=True,default=False,
help="Force statistics refresh")
@click.help_option('-h', '--help')
@click.argument('table_name')
@click.argument('table_name', callback=check_piped_arg, required=False)
@click.pass_context
def describe(ctx, refresh, table_name):
carto_obj = ctx.obj['carto']

try:
if refresh:
result = carto_obj.execute_sql('vacuum analyze {}'.format(table_name))
Expand Down Expand Up @@ -356,11 +361,11 @@ def describe(ctx, refresh, table_name):
type=click.Choice(['gpkg','csv', 'shp','geojson']))
@click.option('-o','--output',type=click.File('wb'),
help="Output file to generate")
@click.argument('table_name')
@click.argument('table_name', callback=check_piped_arg, required=False)
@click.pass_context
def download(ctx, format, output, table_name):
carto_obj = ctx.obj['carto']

# Check table size
sql = "select count(*) from {}".format(table_name)
result = carto_obj.execute_sql(sql)
Expand Down Expand Up @@ -407,7 +412,7 @@ def download(ctx, format, output, table_name):
@click.help_option('-h', '--help')
@click.option('-s','--sync',default=None,help="Seconds between sync updates",
type=int)
@click.argument('path')
@click.argument('path', callback=check_piped_arg, required=False)
@click.pass_context
def upload(ctx, sync, path):
carto_obj = ctx.obj['carto']
Expand Down Expand Up @@ -443,11 +448,12 @@ def upload(ctx, sync, path):

@click.command(help="Deletes a dataset from your account")
@click.help_option('-h', '--help')
@click.argument('table_name')
@click.argument('table_name', callback=check_piped_arg, required=False)
@click.pass_context
def delete(ctx, table_name):
carto_obj = ctx.obj['carto']
dataset_manager = carto_obj.get_dataset_manager();

dataset_manager = carto_obj.get_dataset_manager()
dataset = dataset_manager.get(table_name)
if dataset:
dataset.delete()
Expand All @@ -467,7 +473,7 @@ def delete(ctx, table_name):
@click.option('-l', '--license', help="Set your dataset license")
@click.option('-a', '--attributions', help="Set your dataset attributions")
@click.option('-t', '--tags', help="Set your dataset tags, use commas to separate them")
@click.argument('dataset_name')
@click.argument('dataset_name', callback=check_piped_arg, required=False)
@click.pass_context
def edit(ctx, description, privacy, locked, license, attributions, tags, dataset_name):
carto_obj = ctx.obj['carto']
Expand Down Expand Up @@ -504,7 +510,7 @@ def edit(ctx, description, privacy, locked, license, attributions, tags, dataset
@click.pass_context
def rename(ctx, old, new):
carto_obj = ctx.obj['carto']
dataset_manager = carto_obj.get_dataset_manager();
dataset_manager = carto_obj.get_dataset_manager()
dataset = dataset_manager.get(old)
if dataset:
dataset.name = new
Expand Down Expand Up @@ -539,8 +545,8 @@ def merge(ctx, table_name_prefix,new_table_name):

# build the list of queries
query_list = [
'CREATE TABLE {} AS SELECT {} FROM {}'.format(new_table_name,columns,tables[0]),
];
'CREATE TABLE {} AS SELECT {} FROM {}'.format(new_table_name,columns,tables[0])
]
for table in tables[1:]:
query_list.append('''INSERT INTO {first_table} SELECT {columns} FROM {table}'''.format(
first_table = new_table_name,
Expand All @@ -560,10 +566,11 @@ def merge(ctx, table_name_prefix,new_table_name):

@click.command(help="Runs the cartodbfication of a table to convert it into a dataset")
@click.help_option('-h', '--help')
@click.argument('table_name')
@click.argument('table_name', callback=check_piped_arg, required=False)
@click.pass_context
def cartodbfy(ctx, table_name):
carto_obj = ctx.obj['carto']

job_details = carto_obj.batch_create(get_cartodbfy_query(
org=carto_obj.org_name,
user=carto_obj.user_name,
Expand Down
10 changes: 7 additions & 3 deletions carto_cli/commands/sql/execute_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from prettytable import PrettyTable
from carto_cli.carto import queries
from carto_cli.utils import check_piped_arg


try:
from StringIO import StringIO
Expand All @@ -19,10 +21,11 @@
@click.option('-ea','--explain-analyze',is_flag=True,default=False,help="Explains the query executing it")
@click.option('-eaj','--explain-analyze-json',is_flag=True,default=False,help="Explains the query executing it and return as a JSON")
@click.help_option('-h', '--help')
@click.argument('sql', nargs=-1)
@click.argument('sql', nargs=-1, callback=check_piped_arg, required=False)
@click.pass_context
def run(ctx,format,output,explain,explain_analyze,explain_analyze_json,sql):
carto_obj = ctx.obj['carto']

if type(sql) == tuple:
sql = ' '.join(sql)
try:
Expand Down Expand Up @@ -54,11 +57,12 @@ def run(ctx,format,output,explain,explain_analyze,explain_analyze_json,sql):


@click.command(help="Kills a query based on its pid")
@click.argument('pid',type=int)
@click.argument('pid', type=int, callback=check_piped_arg, required=False)
@click.help_option('-h', '--help')
@click.pass_context
def kill(ctx,pid):
carto_obj = ctx.obj['carto']

sql = queries.KILL_QUERY.format(pid)

try:
Expand All @@ -71,7 +75,7 @@ def kill(ctx,pid):
else:
ctx.fail('Invalid PID?')
else:
raise
raise Exception('No rows were returned')
except Exception as e:
ctx.fail("Error cancelling the query: {}".format(e))

Expand Down
17 changes: 17 additions & 0 deletions carto_cli/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
General purpose functions can be stored here.
"""

import sys

def check_piped_arg(ctx, param, value):
if value:
return value
else:
if not sys.stdin.isatty():
return sys.stdin.read().rstrip()
else:
ctx.fail(
f"Missing argument: {param.human_readable_name}.\n"
"Either pass it explicitly or pipe into the command"
)

0 comments on commit dd72c1e

Please sign in to comment.