Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for line in lines:
key, value = line.split(':')
parameters[key] = value[:-1]
spreadsheet_name = "Sign up form for INF3331/INF43331 (2016) (Responses)"
# or use
# spreadsheet_name = parameters["course"]
# Log on to disk
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_file, scope)
gc = gspread.authorize(credentials)
try:
wks = gc.open(spreadsheet_name).sheet1
except gspread.SpreadsheetNotFound:
json_key = json.load(open(json_file))
print "The spreadsheet document '{}' not found. Maybe it does not exist?".format(spreadsheet_name)
print "Otherwise, make sure that you shared the spreadsheet with {} and try again.".format(json_key['client_email'])
sys.exit(1)
# Store file in ../Attendance/
attendance_location = os.path.join(os.path.dirname(__file__), '..',
*parameters["filepath"].split(os.path.sep)[:-1])
# Create ../Attendance/ if it does not exist
if not os.path.exists(attendance_location):
os.makedirs(attendance_location)
filename = os.path.join(attendance_location, "%s-students_base.txt" % parameters['course'])
if os.path.isfile(filename):
answ = input("The student_base file exists, are you " + \
def run_query(self, query, user):
logger.debug("Spreadsheet is about to execute query: %s", query)
values = query.split("|")
key = values[0] #key of the spreadsheet
worksheet_num = 0 if len(values) != 2 else int(values[1])# if spreadsheet contains more than one worksheet - this is the number of it
try:
spreadsheet_service = self._get_spreadsheet_service()
spreadsheet = spreadsheet_service.open_by_key(key)
data = parse_spreadsheet(spreadsheet, worksheet_num)
json_data = json.dumps(data, cls=JSONEncoder)
error = None
except gspread.SpreadsheetNotFound:
error = "Spreadsheet ({}) not found. Make sure you used correct id.".format(key)
json_data = None
return json_data, error
async def load_character(self, ctx, args):
"""
Downloads and parses the character data, returning a fully-formed Character object.
:raises ExternalImportError if something went wrong during the import that we can expect
:raises Exception if something weirder happened
"""
owner_id = str(ctx.author.id)
try:
await self.get_character()
except (KeyError, SpreadsheetNotFound, APIError):
raise ExternalImportError("Invalid character sheet. Make sure you've shared it with me at "
"`avrae-320@avrae-bot.iam.gserviceaccount.com`!")
except Exception:
raise
return await asyncio.get_event_loop().run_in_executor(None, self._load_character, owner_id, args)
def run_query(self, query, user):
logger.debug("Spreadsheet is about to execute query: %s", query)
key, worksheet_num = parse_query(query)
try:
spreadsheet_service = self._get_spreadsheet_service()
if is_url_key(key):
spreadsheet = spreadsheet_service.open_by_url(key)
else:
spreadsheet = spreadsheet_service.open_by_key(key)
data = parse_spreadsheet(spreadsheet, worksheet_num)
return json_dumps(data), None
except gspread.SpreadsheetNotFound:
return None, "Spreadsheet ({}) not found. Make sure you used correct id.".format(key)
except APIError as e:
return None, parse_api_error(e)
def _open_doc(url):
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
settings.GOOGLE_SERVICE_ACCOUNT_KEYFILE_PATH, scope)
gc = gspread.authorize(credentials)
try:
return gc.open_by_url(url)
except gspread.SpreadsheetNotFound:
raise SpreadsheetNotFound
def download_google_spreadsheet(name, filename=None):
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# Get password and username
json_file = input("Path to Google credentials JSON file (see" \
" http://gspread.readthedocs.org/en/latest/oauth2.html): ")
# Log on to disk
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_file, scope)
gc = gspread.authorize(credentials)
try:
wks = gc.open(name).sheet1.export()
except gspread.SpreadsheetNotFound:
json_key = json.load(open(json_file))
print("The spreadsheet document '{}' not found. Maybe it does not exist?".format(name))
print("Otherwise, make sure that you shared the spreadsheet with {} and try again.".format(
json_key['client_email']))
return None
if filename is not None:
with open(filename, "wb") as f:
f.write(wks.encode("utf-8"))
return wks.decode("utf-8")
def open_url(self, url):
# TODO: make this work with docs as well (only spreadsheets work at the moment)
# ^^ (this is very hard :o) ^^
if self._client is None:
logger.debug("OAuthHandler doesn't have _client yet")
return None
try:
logger.debug("OAuthHandler.open_url")
return self._client.open_by_url(url)
except gspread.SpreadsheetNotFound:
logger.error("open_url: spreadsheet not found %s", url)
self.authorized = False
return None
except gspread.NoValidUrlKeyFound:
logger.error("open_url: no valid url found %s", url)
self.authorized = False
return None