Skip to content

Instantly share code, notes, and snippets.

@rickcnagy
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save rickcnagy/9517470 to your computer and use it in GitHub Desktop.

Select an option

Save rickcnagy/9517470 to your computer and use it in GitHub Desktop.
Data migration via the QuickSchools API - utility module.
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python
"""Data migration via the QuickSchools API - utility module."""
import qs
import api_logging
import pickle
import os
import random
import sys
filename = None
existing_filename = None
FILE_EXTENSION = '.p'
def create_file(prefix, existing_file=False):
prefix += '_' if prefix else ''
# there's already a file, so new name is prefix + old basename
if existing_file:
new_filename = '{}{}'.format(prefix, basename(get_existing_filename()))
else:
new_filename = '{}{}_{}'.format(
prefix, qs.get_schoolcode(), str(random.randint(1, 100)))
set_filename(new_filename + FILE_EXTENSION)
return get_filename()
def load(prefix):
prefix += '_' if prefix else ''
old_fullname = prefix + basename(get_existing_filename()) + FILE_EXTENSION
return pickle.load(open(old_fullname))
def save(data):
pickle.dump(data, open(get_filename(), 'w'))
api_logging.info("dumped data to file: {}".format(
get_filename()), {}, cc_print=True)
def set_filename(new_filename):
global filename
filename = new_filename
def get_existing_filename():
global existing_filename
if not existing_filename:
existing_filename = raw_input("Data filename?\n").strip("'")
return existing_filename
def get_filename():
return filename
def basename(long_name):
basename = long_name
if len(long_name.split('_')) > 2:
basename = os.path.splitext(basename[basename.find('_') + 1:])[0]
return basename
def run_id():
return get_filename()[-4:-2]
# confirm that everything's good before working with data
def check(compare_date=None):
server_warning = "**USING LIVE SERVER!**" if qs.use_live_server \
else "using backup server"
compare_date_warning = "Assignments on or after {}\n".format(compare_date) \
if compare_date is not None else ''
question = "Starting using following parameters:" if not qs.use_live_server \
else "*Does this all look correct?** (type y for yes)"
statement = ("\n{}\nSchoolcode: {}\n{}\n{}"
"".format(
question,
qs.get_schoolcode(),
server_warning,
compare_date_warning))
if (qs.use_live_server):
if raw_input(statement).lower() != 'y':
sys.exit('user aborted')
else:
api_logging.info(statement, {}, cc_print=True)
def complete():
api_logging.info("Complete. {} errors."
.format(qs.get_error_count()),{}, cc_print=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment