Exemplo n.º 1
0
def main(argv=None):
    """
    Main execution function for cli

    :param argv:
    """
    if argv is None:
        argv = sys.argv

    conf = None
    parser, args, other = argparser(argv)

    api_credentials_given = (args.apikey and args.apihost)
    if args.save_config:
        if not api_credentials_given:
            raise parser.error('--save-config requires --apikey and --apihost')
        conf = RcConf(config_location=args.config,
                      autocreate=True, config={'apikey': args.apikey,
                                               'apihost': args.apihost})
        sys.exit()

    if not conf:
        conf = RcConf(config_location=args.config, autoload=True)
        if not conf:
            if not api_credentials_given:
                parser.error('Could not find config file and missing '
                             '--apikey or --apihost in params')

    apikey = args.apikey or conf['apikey']
    apihost = args.apihost or conf['apihost']
    method = args.method

    # if we don't have method here it's an error
    if not method:
        parser.error('Please specify method name')

    try:
        margs = dict(map(lambda s: s.split(':', 1), other))
    except Exception:
        sys.stderr.write('Error parsing arguments \n')
        sys.exit()
    if args.format == FORMAT_PRETTY:
        print 'Calling method %s => %s' % (method, apihost)

    json_resp = api_call(apikey, apihost, method, **margs)
    if json_resp['error']:
        json_data = json_resp['error']
    else:
        json_data = json_resp['result']
    if args.format == FORMAT_JSON:
        print json.dumps(json_data)
    elif args.format == FORMAT_PRETTY:
        print 'Server response \n%s' % (
                json.dumps(json_data, indent=4, sort_keys=True))
    return 0
Exemplo n.º 2
0
def _run(argv):
    conf = None
    parser, args, other = argparser(argv)

    api_credentials_given = (args.apikey and args.apihost)
    if args.save_config:
        if not api_credentials_given:
            raise parser.error('--save-config requires --apikey and --apihost')
        conf = RcConf(config_location=args.config,
                      autocreate=True, config={'apikey': args.apikey,
                                               'apihost': args.apihost})
        sys.exit()

    if not conf:
        conf = RcConf(config_location=args.config, autoload=True)
        if not conf:
            if not api_credentials_given:
                parser.error('Could not find config file and missing '
                             '--apikey or --apihost in params')

    apikey = args.apikey or conf['apikey']
    host = args.apihost or conf['apihost']
    DEFAULT_FILENAME = 'gistfile1.txt'
    if other:
        # skip multifiles for now
        filename = other[0]
        if filename == '-':
            filename = DEFAULT_FILENAME
            gist_content = ''
            for line in fileinput.input('-'):
                gist_content += line
        else:
            with open(filename, 'rb') as f:
                gist_content = f.read()

    else:
        filename = DEFAULT_FILENAME
        gist_content = None
        # little bit hacky but cross platform check where the
        # stdin comes from we skip the terminal case it can be handled by '-'
        mode = os.fstat(0).st_mode
        if stat.S_ISFIFO(mode):
            # "stdin is piped"
            gist_content = sys.stdin.read()
        elif stat.S_ISREG(mode):
            # "stdin is redirected"
            gist_content = sys.stdin.read()
        else:
            # "stdin is terminal"
            pass

    # make sure we don't upload binary stuff
    if gist_content and '\0' in gist_content:
        raise Exception('Error: binary files upload is not possible')

    filename = os.path.basename(args.filename or filename)
    if gist_content:
        files = {
            filename: {
                'content': gist_content,
                'lexer': None
            }
        }

        margs = dict(
            lifetime=args.lifetime,
            description=args.description,
            gist_type='private' if args.private else 'public',
            files=files
        )

        json_data = api_call(apikey, host, 'create_gist', **margs)['result']
        if args.format == FORMAT_JSON:
            print json.dumps(json_data)
        elif args.format == FORMAT_PRETTY:
            print json_data
            print 'Created %s gist %s' % (json_data['gist']['type'],
                                          json_data['gist']['url'])
    return 0
Exemplo n.º 3
0
def _run(argv):
    conf = None
    parser, args, other = argparser(argv)

    api_credentials_given = (args.apikey and args.apihost)
    if args.save_config:
        if not api_credentials_given:
            raise parser.error('--save-config requires --apikey and --apihost')
        conf = RcConf(config_location=args.config,
                      autocreate=True,
                      config={
                          'apikey': args.apikey,
                          'apihost': args.apihost
                      })
        sys.exit()

    if not conf:
        conf = RcConf(config_location=args.config, autoload=True)
        if not conf:
            if not api_credentials_given:
                parser.error('Could not find config file and missing '
                             '--apikey or --apihost in params')

    apikey = args.apikey or conf['apikey']
    host = args.apihost or conf['apihost']
    DEFAULT_FILENAME = 'gistfile1.txt'
    if other:
        # skip multifiles for now
        filename = other[0]
        if filename == '-':
            filename = DEFAULT_FILENAME
            gist_content = ''
            for line in fileinput.input('-'):
                gist_content += line
        else:
            with open(filename, 'rb') as f:
                gist_content = f.read()

    else:
        filename = DEFAULT_FILENAME
        gist_content = None
        # little bit hacky but cross platform check where the
        # stdin comes from we skip the terminal case it can be handled by '-'
        mode = os.fstat(0).st_mode
        if stat.S_ISFIFO(mode):
            # "stdin is piped"
            gist_content = sys.stdin.read()
        elif stat.S_ISREG(mode):
            # "stdin is redirected"
            gist_content = sys.stdin.read()
        else:
            # "stdin is terminal"
            pass

    # make sure we don't upload binary stuff
    if gist_content and '\0' in gist_content:
        raise Exception('Error: binary files upload is not possible')

    filename = os.path.basename(args.filename or filename)
    if gist_content:
        files = {filename: {'content': gist_content, 'lexer': None}}

        margs = dict(lifetime=args.lifetime,
                     description=args.description,
                     gist_type='private' if args.private else 'public',
                     files=files)

        json_data = api_call(apikey, host, 'create_gist', **margs)['result']
        if args.format == FORMAT_JSON:
            print json.dumps(json_data)
        elif args.format == FORMAT_PRETTY:
            print json_data
            print 'Created %s gist %s' % (json_data['gist']['type'],
                                          json_data['gist']['url'])
    return 0