#!/usr/bin/env python """ Create CSV spreadsheets from sponge data NetCDF files. Usage: > python query.py path/to/config/file > python query.py -t > python query.py --test Test silent import: >>> import expand """ __author__ = "Chris Calloway" __email__ = "cbc@chriscalloway.org" __copyright__ = "Copyright 2010 UNC-CH Department of Marine Science" __license__ = "GPL2" import sys import os import glob import zipfile import shutil import fileinput import doctest import unittest from StringIO import StringIO USAGE = "\n".join(__doc__.splitlines()[3:8]) TEST_PATH = os.path.join("tests", "query") def _test(): """ Run doctests as unittest suite. Test silent import: >>> from expand import _test """ suite = [] suite.append(doctest.DocTestSuite()) suite = unittest.TestSuite(suite) unittest.TextTestRunner().run(suite) return def config_path(): """ Return the configuration file path from the command line. Supply too few arguments on command line: >>> save_stdout = sys.stdout >>> temp_stdout = StringIO() >>> sys.stdout = temp_stdout >>> sys.argv = [] >>> _config_path = config_path() >>> sys.stdout = save_stdout >>> USAGE == temp_stdout.getvalue()[:-1] True Supply too many arguments on the command line: >>> save_stdout = sys.stdout >>> temp_stdout = StringIO() >>> sys.stdout = temp_stdout >>> sys.argv = ["", "", "",] >>> _config_path = config_path() >>> sys.stdout = save_stdout >>> USAGE == temp_stdout.getvalue()[:-1] True Supply non-file argument: >>> save_stdout = sys.stdout >>> temp_stdout = StringIO() >>> sys.stdout = temp_stdout >>> _config_path = os.path.join( ... os.path.dirname( ... os.path.abspath(__file__)), ... TEST_PATH) >>> sys.argv = ["", _config_path] >>> _config_path = config_path() >>> sys.stdout = save_stdout >>> USAGE == temp_stdout.getvalue()[:-1] True Supply nonexistent file argument: >>> save_stdout = sys.stdout >>> temp_stdout = StringIO() >>> sys.stdout = temp_stdout >>> _config_path = os.path.join( ... os.path.dirname( ... os.path.abspath(__file__)), ... TEST_PATH, "xxxxx") >>> sys.argv = ["", _config_path] >>> _config_path = config_path() >>> sys.stdout = save_stdout >>> USAGE == temp_stdout.getvalue()[:-1] True Supply valid config path argument: >>> _config_path = os.path.join( ... os.path.dirname( ... os.path.abspath(__file__)), ... TEST_PATH, "config.py") >>> sys.argv = ["", _config_path] >>> _config_path == config_path() True """ path = None try: if len(sys.argv) == 2: if sys.argv[1] == "-t" or sys.argv[1] == "--test": _test() else: path = sys.argv[1] if not os.path.exists(path): raise IOError(path + \ " does not exist.") elif not os.path.isfile(path): raise IOError(path + \ " is not a file.") else: raise IOError("Incorrect number of arguments supplied.") except IOError: print USAGE return path def config(path): """ Return the configuration from a file. Execute empty configuration: >>> _config_path = os.path.join( ... os.path.dirname( ... os.path.abspath(__file__)), ... TEST_PATH, "empty_config.py") >>> ncdir,csvdir, \ ncfile_pattern,csvfile_pattern, \ location,platform,packages = \ config(_config_path) >>> ncdir >>> csvdir >>> ncfile_pattern >>> csvfile_pattern >>> location >>> platform >>> packages Execute nonexistent configuration: >>> save_stdout = sys.stdout >>> temp_stdout = StringIO() >>> sys.stdout = temp_stdout >>> _config_path = os.path.join( ... os.path.dirname( ... os.path.abspath(__file__)), ... TEST_PATH, "xxxxx") >>> ncdir,csvdir, \ ncfile_pattern,csvfile_pattern, \ location,platform,packages = \ config(_config_path) >>> sys.stdout = save_stdout >>> USAGE == temp_stdout.getvalue()[:-1] True >>> ncdir >>> csvdir >>> ncfile_pattern >>> csvfile_pattern >>> location >>> platform >>> packages Execute bad configuration: >>> save_stdout = sys.stdout >>> temp_stdout = StringIO() >>> sys.stdout = temp_stdout >>> _config_path = os.path.join( ... os.path.dirname( ... os.path.abspath(__file__)), ... TEST_PATH, "bad_config.py") >>> ncdir,csvdir, \ ncfile_pattern,csvfile_pattern, \ location,platform,packages = \ config(_config_path) >>> sys.stdout = save_stdout >>> USAGE == temp_stdout.getvalue()[:-1] True >>> ncdir >>> csvdir >>> ncfile_pattern >>> csvfile_pattern >>> location >>> platform >>> packages Execute valid configuration: >>> _config_path = os.path.join( ... os.path.dirname( ... os.path.abspath(__file__)), ... TEST_PATH, "config.py") >>> ncdir,csvdir, \ ncfile_pattern,csvfile_pattern, \ location,platform,packages = \ config(_config_path) >>> ncdir == os.path.join(os.path.dirname(os.path.abspath(__file__)), ... TEST_PATH, "nc") True >>> csvdir == os.path.join(os.path.dirname(os.path.abspath(__file__)), ... TEST_PATH, "csv") True >>> ncfile_pattern == "%(location)s_%(platform)s_%(package)s_" \ "[0-9][0-9][0-9][0-9]_[0-9][0-9].nc" True >>> csvfile_pattern == "%(location)s_%(platform)s_%(package)s_" \ "[0-9][0-9][0-9][0-9]_[0-9][0-9].csv" True >>> location == "location" True >>> platform == "platform" True >>> packages == ('system', ... 'turbidity', ... 'optode_127', ... 'optode_167', ... 'optode_169', ... 'optode_170', ... 'optode_171', ... 'optode_172', ... 'optode_173', ... 'optode_175', ... 'optode_176', ... 'optode_178', ... 'optode_179', ... 'conductivity', ... 'pressure', ... 'current', ... ) True """ namespace = {} namespace["NCDIR"] = None namespace["CSVDIR"] = None namespace["NCFILE_PATTERN"] = None namespace["CSVFILE_PATTERN"] = None namespace["LOCATION"] = None namespace["PLATFORM"] = None namespace["PACKAGES"] = None try: execfile(path, globals(), namespace) except IOError: print USAGE except SyntaxError: print USAGE return (namespace["NCDIR"], namespace["CSVDIR"], namespace["NCFILE_PATTERN"], namespace["CSVFILE_PATTERN"], namespace["LOCATION"], namespace["PLATFORM"], namespace["PACKAGES"], ) def query(ncdir, csvdir, ncfile_pattern, csvfile_pattern, location, platform, packages): """ Create CSV spreadsheets from sponge data NetCDF files. """ return def _main(): """ Run module as script. Supply incomplete config file: >>> save_stdout = sys.stdout >>> temp_stdout = StringIO() >>> sys.stdout = temp_stdout >>> _config_path = os.path.join( ... os.path.dirname( ... os.path.abspath(__file__)), ... TEST_PATH, "incomplete_config.py") >>> sys.argv = ["", _config_path] >>> _main() >>> sys.stdout = save_stdout >>> USAGE == temp_stdout.getvalue()[:-1] True """ _config_path = config_path() if _config_path: _config = config(_config_path) if all(_config): query(*_config) else: print USAGE return if __name__ == "__main__": _main()