def get_state_for_frontend(state, exploration): """Returns a representation of the given state for the frontend.""" state_repr = exp_services.export_state_to_dict(exploration.id, state.id) modified_state_dict = exp_services.export_state_internals_to_dict( exploration.id, state.id, human_readable_dests=True) # TODO(sll): The following is for backwards-compatibility and should be # deleted later. rules = {} for handler in state_repr['widget']['handlers']: rules[handler['name']] = handler['rules'] for item in rules[handler['name']]: if item['name'] == 'Default': item['rule'] = 'Default' else: item['rule'] = InteractiveWidget.get( state.widget.widget_id).get_readable_name( handler['name'], item['name'] ) state_repr['widget']['rules'] = rules state_repr['widget']['id'] = state_repr['widget']['widget_id'] state_repr['yaml'] = utils.yaml_from_dict(modified_state_dict) return state_repr
def to_yaml(self): collection_dict = self.to_dict() # The ID is the only property which should not be stored within the # YAML representation. del collection_dict['id'] return utils.yaml_from_dict(collection_dict)
def as_yaml(self): """Returns a YAML version of the exploration.""" init_dict = {} others_dict = {} for state_key in self.states: state = state_key.get() state_internals = state.internals_as_dict(human_readable_dests=True) if self.init_state.get().id == state.id: init_dict[state.name] = state_internals else: others_dict[state.name] = state_internals result = utils.yaml_from_dict(init_dict) result += utils.yaml_from_dict(others_dict) if others_dict else "" return result
def export_states_to_yaml(exploration_id, version=None, width=80): """Returns a python dictionary of the exploration, whose keys are state names and values are yaml strings representing the state contents with lines wrapped at 'width' characters.""" exploration = get_exploration_by_id(exploration_id, version=version) exploration_dict = {} for state in exploration.states: exploration_dict[state] = utils.yaml_from_dict( exploration.states[state].to_dict(), width=width) return exploration_dict
def to_yaml(self): return utils.yaml_from_dict({ 'default_skin': self.default_skin, 'init_state_name': self.init_state_name, 'param_changes': self.param_change_dicts, 'param_specs': self.param_specs_dict, 'states': {state_name: state.to_dict() for (state_name, state) in self.states.iteritems()}, 'schema_version': self.CURRENT_EXPLORATION_SCHEMA_VERSION })
def test_yaml_dict_conversion(self): """Test yaml_from_dict and dict_from_yaml methods.""" test_dicts = [{}, {'a': 'b'}, {'a': 2}, {'a': ['b', 2, {'c': 3.5}]}] for adict in test_dicts: yaml_str = utils.yaml_from_dict(adict) yaml_dict = utils.dict_from_yaml(yaml_str) self.assertEqual(adict, yaml_dict) with self.assertRaises(utils.InvalidInputException): yaml_str = utils.dict_from_yaml('{')
def to_yaml(self): """Convert the Collection domain object into YAML. Returns: str. The YAML representation of this Collection. """ collection_dict = self.to_dict() # The ID is the only property which should not be stored within the # YAML representation. del collection_dict['id'] return utils.yaml_from_dict(collection_dict)
def export_to_yaml(exploration_id): """Returns a YAML version of the exploration.""" exploration = get_exploration_by_id(exploration_id) return utils.yaml_from_dict({ 'default_skin': exploration.default_skin, 'param_changes': exploration.param_change_dicts, 'param_specs': exploration.param_specs_dict, 'states': [export_state_internals_to_dict( exploration_id, state_id, human_readable_dests=True) for state_id in exploration.state_ids], 'schema_version': CURRENT_EXPLORATION_SCHEMA_VERSION })
def export_to_yaml(exploration_id): """Returns a YAML version of the exploration.""" exploration = Exploration.get(exploration_id) params = [{ 'name': param.name, 'obj_type': param.obj_type, 'values': param.values } for param in exploration.parameters] states_list = [export_state_internals_to_dict( exploration_id, state_id, human_readable_dests=True) for state_id in exploration.state_ids] return utils.yaml_from_dict({ 'parameters': params, 'states': states_list })
def save_cron_dict(cron_dict): with open(_CRON_YAML_FILE_NAME, 'wt') as cron_yaml_file: cron_yaml_file.write(utils.yaml_from_dict(cron_dict))