Skip to content

Create CLI tool #32

@turicas

Description

@turicas

I'd be nice to interact with the API using the command line (without needing to use curl or wget directly).
I've started a little script that can evolve to something we can ship with the library:

#!/usr/bin/env python
# coding: utf-8

import argparse
import sys

from pypln.api import PyPLN, Document


API_BASE = 'http://demo.pypln.org/'

def print_document(document):
    print('Filename: {}\n  {} bytes\n  URL: {}\n  Properties: {}'
            .format(document.blob, document.size, document.url,
                    document.properties_url))

def main():
    args = argparse.ArgumentParser()
    args.add_argument('username')
    args.add_argument('password')
    args.add_argument('--list-corpora', action='store_true')
    args.add_argument('--list-documents', action='store_true')
    args.add_argument('--list-documents-from-corpus', type=unicode)
    argv = args.parse_args()

    username = argv.username
    password = argv.password
    credentials = (username, password)

    pypln = PyPLN(API_BASE, credentials)
    if argv.list_corpora:
        for corpus in pypln.corpora():
            print('Corpus name: {}, {} documents'
                    .format(corpus.name, len(corpus.documents)))
    elif argv.list_documents:
        for document in pypln.documents():
            print_document(document)
    elif argv.list_documents_from_corpus:
        corpus_name = argv.list_documents_from_corpus
        found_corpora = [corpus for corpus in pypln.corpora()
                         if corpus.name == corpus_name]
        if not len(found_corpora):
            sys.stderr.write('ERROR: corpus "{}" not found.\n'
                    .format(corpus_name))
            exit(2)
        else:
            corpus = found_corpora[0]
            print('Retrieving documents from corpus "{}" ({} found)...\n'
                    .format(corpus_name, len(corpus.documents)))
            for document_url in corpus.documents:
                document = Document.from_url(document_url, credentials)
                print_document(document)
    else:
        sys.stderr.write('ERROR: you should choose one option.\n')
        exit(1)


if __name__ == '__main__':
    main()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions