Created
June 12, 2015 14:19
-
-
Save pathcl/e71ec6aeef33bb26decc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import csv | |
import sys | |
import os | |
try: | |
import gdata.spreadsheet.service | |
except ImportError, e: | |
errMsg = "please install gdata see http://goo.gl/Eg22wF :C" | |
print >> sys.stderr, errMsg | |
sys.exit(2) | |
try: | |
from argparse import ArgumentParser | |
import argparse | |
except ImportError, e: | |
errMsg = "please install argparser see http://goo.gl/qg0KCw :C" | |
print >> sys.stderr, errMsg | |
sys.exit(2) | |
try: | |
import csv | |
except ImportError, e: | |
errMsg = "please install csv see http://goo.gl/MQ4DzI :C" | |
print >> sys.stderr, errMsg | |
sys.exit(2) | |
if (len(sys.argv) != 2): #very ugly validation | |
errMsg = ("%s %s %s") % ("usage", sys.argv[0], "-csv /tmp/some.csv") | |
print >> sys.stderr, errMsg | |
sys.exit(3) | |
def parseArgs(): | |
thaWord = 'spreadSHIT' | |
parser = ArgumentParser(description=("%s %s") % ('Arguments for this auto filler of google ', thaWord)) | |
parser.add_argument('-e', '--email', | |
type=str, default='sysreporter@domain.tld', | |
help=("%s %s") % ('email that could write on some', thaWord)) | |
parser.add_argument('-p', '--password', | |
type=str, default='somePass', | |
help=("%s %s") % ('email that could write on some', thaWord)) | |
parser.add_argument('-k', '--key', | |
type=str, default='aASDasdASDASZZAsdasda4234234234', | |
help=("%s %s %s") % ('the key of the ', thaWord, 'such as ASDadasdSdasdaSDZZZzzzzzz8766z')) | |
parser.add_argument('-wid', '--worksheet_id', | |
type=str, default='od6', | |
help=("%s %s %s") % ('the key of the ', thaWord, 'i mean the key of the id,the first one is od6')) | |
parser.add_argument('-csv','--data_on_csv',type=argparse.FileType('r'), | |
help=("%s %s %s") % ('the file with data to upload on', thaWord, 'such as /tmp/some.csv') | |
) | |
args = parser.parse_args() | |
return args | |
def main(args): | |
client = gdata.spreadsheet.service.SpreadsheetsService() | |
client.debug = True | |
client.email = args.email | |
client.password = args.password | |
client.source = 'someClient' | |
client.ProgrammaticLogin() | |
csvFh = args.data_on_csv | |
csvReader = csv.reader(csvFh) | |
fullRow = {} | |
rows = [] | |
row = {} | |
#here we put the headers of the columns, they SHOULD BE THE SAME as on the spreadShit | |
headings = ['hypervisor', 'powerstatus', 'vmwaretoolsver', 'vmwaretoolsstat', 'numcpus','memoryinmb', 'disksizesingbs', 'uuid', 'name', 'hostname', 'ip', 'kind', 'completename', 'pool'] | |
#all this tags should have LESS than 15 chars | |
#and be the same on the spreadSHIT and DO NOT call as any reserved word on gdata api, such as "guest" | |
try: | |
for row in csvReader: | |
h = 0 | |
for col in row: | |
index = headings[h] | |
fullRow[index] = col | |
h += 1 | |
rows.append(fullRow) | |
try: | |
client.InsertRow(fullRow, args.key, args.worksheet_id) | |
except Exception as e: | |
print e | |
finally: | |
csvFh.close() | |
return | |
if __name__ == '__main__': | |
args = parseArgs() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment