view globalneighbors/cli.py @ 15:21095c9006e5

city page is now functional + linky
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 14:55:53 -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)