예제 #1
0
def build_from_template(path, destination, course_name, ignore):
    if isinstance(course_name, str):
        course = Course(destination, course_name)
    else:
        course = course_name
    # Find the YAML file
    search_path = os.path.join(destination, Page.canonical_category, '**',
                               path)
    potentials = glob(search_path, recursive=True)
    if not potentials:
        raise WaltzException("File not found: " + path)
    elif len(potentials) > 1:
        raise WaltzException("Too many files found: " + '\n'.join(potentials))
    yaml_path = potentials[0]
    with open(yaml_path) as yaml_file:
        yaml_data = yaml.load(yaml_file)
    # Figure out template
    template_name = yaml_data['_template']
    # Render the template
    markdown_page = course.render(template_name, yaml_data)
    # Figure out where we should store it
    path, currently = os.path.splitext(yaml_path)
    output_path = path + '.md'
    # And store it
    with open(output_path, 'w') as output_file:
        output_file.write(markdown_page)
예제 #2
0
 def load_all(course):
     category_folder = os.path.join(course.root_directory,
                                    Outcome.canonical_category, '**',
                                    '*' + Outcome.extension)
     Outcome.CACHE[course.course_name] = {}
     for bank in glob(category_folder, recursive=True):
         with open(bank) as bank_file:
             outcomes = yaml.load(bank_file)
             for name, outcome in outcomes.items():
                 new_outcome = Outcome.from_disk(course, {'body': outcome},
                                                 None)
                 new_outcome.bank_source = os.path.dirname(bank)
                 Outcome.CACHE[course.course_name][name] = new_outcome
예제 #3
0
def to_markdown(yaml_path, template_path):
    path, currently = os.path.splitext(yaml_path)
    output_path = path + '.md'

    with open(yaml_path) as yaml_file:
        yaml_data = yaml.load(yaml_file)

    with open(template_path) as template_file:
        raw_template = template_file.read()

    template = Template(raw_template)
    markdown_page = template.render(**yaml_data)

    with open(output_path, 'w') as output_file:
        output_file.write(markdown_page)
예제 #4
0
 def load_bank(course):
     ''' TODO: I don't think this works anymore. We need a better picture
     of how question banks should work in Waltz. '''
     category_folder = os.path.join(course.root_directory,
                                    QuizQuestion.canonical_category, '**',
                                    '*.yaml')
     QuizQuestion.CACHE[course.course_name] = {}
     for bank in glob(category_folder, recursive=True):
         with open(bank) as bank_file:
             questions = yaml.load(bank_file)
             for question in questions:
                 question_name = question['question_name']
                 new_question = QuizQuestion.from_disk(question)
                 new_question.bank_source = os.path.dirname(bank)
                 QuizQuestion.CACHE[
                     course.course_name][question_name] = new_question
예제 #5
0
 def from_disk(self, resource_id):
     '''
     Args:
         resource_id (ResourceID): The resource ID to load in.
     Returns:
         Resource: The formatted resource object
     '''
     if not os.path.exists(resource_id.path):
         return None
     if resource_id.path.endswith('.yaml'):
         with open(resource_id.path) as resource_file:
             resource_yaml = yaml.load(resource_file)
     else:
         with open(resource_id.path) as resource_file:
             resource_yaml = resource_file.read()
     return resource_id.resource_type.from_disk(self, resource_yaml,
                                                resource_id)
예제 #6
0
 def update_bank(cls, course, question_name, bank_source, new_question):
     course.backup_bank(bank_source)
     course_cache = cls.CACHE[course.course_name]
     course_cache[question_name] = new_question
     # Grab the names of the old questions
     kept_question_names = []
     with open(bank_source) as bank_file:
         questions = yaml.load(bank_file)
         for question in questions:
             question_name = question['question_name']
             kept_question_names.append(['question_name'])
     # Get the actual up-to-date questions
     questions = [
         course_cache[name].to_disk(force=True)
         for name in kept_question_names
     ]
     # Dump them back into the file
     walk_tree(questions)
     with open(bank_source, 'w') as bank_file:
         yaml.dump(questions, out)
예제 #7
0
def yaml_load(path):
    with open(path) as settings_file:
        return yaml.load(settings_file)