diff --git a/setup.py b/setup.py index c729351..0334e4e 100755 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ py_modules=['taas'], entry_points={ 'console_scripts': [ - "fetch = taas:process_all_sources", + "gss2json = taas.cmdline:gss2json" ] }, diff --git a/taas/__init__.py b/taas/__init__.py index 6be59b2..1089220 100644 --- a/taas/__init__.py +++ b/taas/__init__.py @@ -225,17 +225,16 @@ def google_sheet_to_json(name, version, key, gid, mapping, directory=None): def process_source(source_name, source): """ Processes a source, generating JSON and potentially other supporting - files. Right now this is only able to flip sheets to JSON, but we may - support other services in the future. + files. - Expects a dictionary of vesions, with service descriptions underneath. + Expects a `source_name`, with which we label our output, and a dictionary + of vesions, with service descriptions underneath. """ # TODO: We should have a service class definition, rather than trusting our # config file is in the right format. for version in source: - options = source[version] google_sheet_to_json( @@ -243,16 +242,17 @@ def process_source(source_name, source): ) -def process_all_sources(config=None): - """ - Processes all sources in a given config, generating JSON output. Uses - the default config if none supplied. +def process_sources(config, sources=None): """ + Uses the config provided to process the sources given. - if config is None: - config = read_config() + Process all sources if none are provided. + """ + if sources is None: + sources = config['sources'] - sources = config['sources'] + for source in sources: + if source not in config['sources']: + raise KeyError("Asked to process source '{}', but not found in config.".format(source)) - for source_name in sources: - process_source(source_name, sources[source_name]) + process_source(source, config['sources'][source]) diff --git a/taas/cmdline.py b/taas/cmdline.py new file mode 100644 index 0000000..b85a023 --- /dev/null +++ b/taas/cmdline.py @@ -0,0 +1,38 @@ +import argparse +import taas + +"""Command-line interfaces to TAAS functions.""" + + +def gss2json(): + """Console script for processing google spreadsheet(s) to JSON.""" + + parser = argparse.ArgumentParser( + description=''' + Processes Google spreadsheet(s) to JSON. + + If no arguments are given, all sheets will be processed. + ''' + ) + + parser.add_argument('--config', metavar="FILE", help="Config file to use.") + + parser.add_argument( + 'sheets', + metavar='sheet', + nargs="*", + help="A sheet to process, as named in the config file", + ) + + args = parser.parse_args() + + # This reads the default file if none is specified. + config = taas.read_config(args.config) + + # Sheets always comes back as a list. We flip it explicitly to + # a None value if it's empty. + sheets = args.sheets + if not sheets: + sheets = None + + taas.process_sources(config, sheets)