Commit 043c30b5 by Shirley He

polish up file

parent a0a511b3
#!/usr/bin/env python
# other comment about how this file works, plus probably an example of implementation
#
# Create a file of course permutations based on permutations.json file configuration. A max
# of 3 fields can be chosen to build permutations on, and only the fields under
# "permutation_data" are eligible. No field input pulls from "default_data"
#
# ./generate_permutations.py --seed_data_file permutations.json --fields field1 field2 field3
import json
import argparse
......@@ -17,13 +22,14 @@ def parse_field_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--fields', nargs="*", action="append", default=None,
help="Specify which fields to generate permutations on")
parser.add_argument('filename')
parser.add_argument('--seed_data_file', help="Input a permutation configuration file")
num_args = parser.parse_args()
file = open(num_args.filename)
file = open(num_args.seed_data_file)
file_data = json.load(file)
default_data = file_data["default_data"]
permutation_data = file_data["permutation_data"]
......@@ -31,7 +37,7 @@ def parse_field_arguments():
fields = {}
# if no field arguments are given, just print out default data
# if no field arguments are given, just return default data
if not num_args.fields:
default_permutation = file_data["default_data"]
fields = default_permutation
......@@ -39,16 +45,16 @@ def parse_field_arguments():
field_length = len(num_args.fields[0])
if (field_length > 3):
raise argparse.ArgumentTypeError("--fields can only take a max of 3 values")
raise argparse.ArgumentTypeError("Only a max of 3 fields allowed")
# add each command line field to fields dict
for permutation_choices in num_args.fields:
for i in range(0, field_length):
fields[permutation_choices[i]] = permutation_data[permutation_choices[i]]
# the difference btwn all possible fields and the permutation ones
# the difference btwn all possible fields and the chosen permutation ones
default_fields = list(set(default_data_keys) - set(num_args.fields[0]))
# add non permutation fields
# add non permutation fields to dict
for j in range(0, len(default_fields)):
fields[default_fields[j]] = default_data[default_fields[j]]
......@@ -58,12 +64,11 @@ def parse_field_arguments():
def generate_permutations(fields, index, results, current, fields_dict):
all_permutations_keys = fields.keys()
permutation_option = all_permutations_keys[index]
permutations_values = fields[permutation_option]
for i in range(len(permutations_values)):
# add other required default fields to dict
current["number"] = "123" # will be generated automatically by course creation script
current["number"] = None # will be generated automatically by course creation script
current["organization"] = "RITX"
current["run"] = "3T2017"
current["user"] = "edx@example.com"
......@@ -73,10 +78,10 @@ def generate_permutations(fields, index, results, current, fields_dict):
enrollment_dict = {}
enrollment_dict["credit"] = False
enrollment_dict["credit_provider"] = "test-credit-provider"
# enrollment_dict["audit"] = True
# add permutation fields to dict
fields_dict[permutation_option] = permutations_values[i]
# generate dates
now = datetime.datetime.now(pytz.UTC)
if permutations_values[i] == "future":
future = str(now + datetime.timedelta(days=365))
......@@ -86,31 +91,22 @@ def generate_permutations(fields, index, results, current, fields_dict):
fields_dict[permutation_option] = past
if permutations_values[i] == None:
fields_dict[permutation_option] = None
# add audit and verify fields to dict
if all_permutations_keys[i] == "audit" and permutations_values[i] == True:
enrollment_dict["audit"] = permutations_values[i]
if all_permutations_keys[i] == "verify" and permutations_values[i] == True:
enrollment_dict["verify"] = True
if index + 1 < len(all_permutations_keys):
generate_permutations(fields, index + 1, results, current, fields_dict)
current["enrollment"] = enrollment_dict
current["fields"] = fields_dict.copy()
results.append(current.copy())
# results["courses"] = current.copy()
wrapper_courses_dict = {}
wrapper_courses_dict = {} # needed to match course input file creation
wrapper_courses_dict["courses"] = results
with open("test_courses.json", "w") as outfile:
json.dump(wrapper_courses_dict, outfile)
......
......@@ -26,7 +26,6 @@
false,
null
],
"mobile_available": [
true,
false,
......@@ -43,8 +42,12 @@
"display_name": [
"International Project Management"
],
"audit": [true],
"verified": [true],
"audit": [
true
],
"verified": [
true
],
"mobile_available": [
true
]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment