import csv
import datetime
import logging
+import os
+import sys
import time
from collections import OrderedDict
log.addHandler(logging.handlers.SysLogHandler())
parser = OptionParser()
-parser.add_option("--config", dest="config", metavar="FILE", help="location of config file")
+parser.add_option("--config",
+ dest="config",
+ metavar="FILE",
+ help="location of config file")
+parser.add_option("--dry-run",
+ action="store_true",
+ dest="dry_run",
+ default=False,
+ help="fetch data but don't upload to InfoEx")
(options, args) = parser.parse_args()
config = configparser.ConfigParser(allow_no_value=False)
+
+if not options.config:
+ print("Please specify a configuration file via --config")
+ sys.exit(1)
+
config.read(options.config)
log.debug('STARTING UP')
try:
infoex = {
- 'host': config['ftp']['host'],
- 'uuid': config['ftp']['uuid'],
- 'api_key': config['ftp']['api_key'],
- 'location_uuid': config['wxsite']['location_uuid'],
+ 'host': config['infoex']['host'],
+ 'uuid': config['infoex']['uuid'],
+ 'api_key': config['infoex']['api_key'],
+ 'location_uuid': config['infoex']['location_uuid'],
'wx_data': {}, # placeholder key, values to come later
- 'csv_filename': config['wxsite']['csv_filename']
+ 'csv_filename': config['infoex']['csv_filename']
}
- station_triplet = config['wxsite']['station_triplet']
+ station_triplet = config['nrcs']['station_triplet']
try:
- desired_data = config['wxsite']['desired_data'].split(',')
+ desired_data = config['nrcs']['desired_data'].split(',')
except:
# desired_data malformed or missing, setting default
desired_data = [
writer.writerow(final_data)
f.close()
-with open(infoex['csv_filename'], 'rb') as f:
- log.debug("uploading FTP file '%s'" % (infoex['host']))
- ftp = FTP(infoex['host'], infoex['uuid'], infoex['api_key'])
- ftp.storlines('STOR ' + infoex['csv_filename'], f)
- ftp.close()
- f.close()
+if not options.dry_run:
+ # not a dry run
+ with open(infoex['csv_filename'], 'rb') as f:
+ log.debug("uploading FTP file '%s'" % (infoex['host']))
+ ftp = FTP(infoex['host'], infoex['uuid'], infoex['api_key'])
+ ftp.storlines('STOR ' + infoex['csv_filename'], f)
+ ftp.close()
+ f.close()
+ os.remove(infoex['csv_filename'])
log.debug('DONE')