view globalneighbors/cli.py @ 7:254195d0bac2

partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 09:13:48 -0700
parents 5dba84370182
children
line wrap: on
line source

"""
command line interfaces
"""

import argparse
from .read import read_cities
from .schema import fields


class ValidationParser(argparse.ArgumentParser):
    """CLI option parser"""

    def __init__(self, **kwargs):
        kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
        argparse.ArgumentParser.__init__(self, **kwargs)
        self.add_arguments()
        self.options = None

    def add_arguments(self):
        """Abstract Base Class:  add your arguments here"""

    def parse_args(self, *args, **kw):
        options = argparse.ArgumentParser.parse_args(self, *args, **kw)
        self.validate(options)
        self.options = options
        return options

    def validate(self, options):
        """validate options"""


class CitiesParser(ValidationParser):

    def add_arguments(self):
        self.add_argument('cities',
                          type=argparse.FileType('r'),
                          help="path to TSV cities file, as documented in http://download.geonames.org/export/dump/")

    def read_cities(self):
        assert self.options
        return read_cities(self.options.cities, fields=fields)