Exemplo n.º 1
0
 def test_constants_file_contains_valid_json(self):
     """Test if the constants file is valid json file."""
     with python_utils.open_file(os.path.join('assets', 'constants.ts'),
                                 'r') as f:
         json = constants.parse_json_from_js(f)
         self.assertTrue(isinstance(json, dict))
         self.assertEqual(json['TESTING_CONSTANT'], 'test')
Exemplo n.º 2
0
class BaseRteComponent(python_utils.OBJECT):
    """Base Rte Component class.

    This is the superclass for rich text components in Oppia, such as
    Image and Video.
    """

    with python_utils.open_file(feconf.RTE_EXTENSIONS_DEFINITIONS_PATH,
                                'r') as f:
        rich_text_component_specs = constants.parse_json_from_js(f)

    obj_types_to_obj_classes = {
        'unicode': objects.UnicodeString,
        'html': objects.Html,
        'Filepath': objects.Filepath,
        'SanitizedUrl': objects.SanitizedUrl,
        'MathExpressionContent': objects.MathExpressionContent,
        'ListOfTabs': objects.ListOfTabs,
        'SvgFilename': objects.SvgFilename,
        'int': objects.Int,
        'bool': objects.Boolean,
        'SkillSelector': objects.SkillSelector
    }

    @classmethod
    def validate(cls, value_dict):
        """Validates customization args for a rich text component.

        Raises:
            TypeError. If any customization arg is invalid.
        """
        arg_names_to_obj_classes = {}
        customization_arg_specs = cls.rich_text_component_specs[
            cls.__name__]['customization_arg_specs']
        for customization_arg_spec in customization_arg_specs:
            arg_name = '%s-with-value' % customization_arg_spec['name']
            schema = customization_arg_spec['schema']
            if schema['type'] != 'custom':
                obj_type = schema['type']
            else:
                obj_type = schema['obj_type']
            obj_class = cls.obj_types_to_obj_classes[obj_type]
            arg_names_to_obj_classes[arg_name] = obj_class

        required_attr_names = list(arg_names_to_obj_classes.keys())
        attr_names = list(value_dict.keys())

        if set(attr_names) != set(required_attr_names):
            missing_attr_names = list(
                set(required_attr_names) - set(attr_names))
            extra_attr_names = list(set(attr_names) - set(required_attr_names))
            raise Exception(
                'Missing attributes: %s, Extra attributes: %s' %
                (', '.join(missing_attr_names), ', '.join(extra_attr_names)))

        for arg_name in required_attr_names:
            arg_obj_class = arg_names_to_obj_classes[arg_name]
            arg_obj_class.normalize(value_dict[arg_name])
 def _refresh(cls):
     """Repopulate the registry."""
     cls._rte_components.clear()
     with python_utils.open_file(feconf.RTE_EXTENSIONS_DEFINITIONS_PATH,
                                 'r') as f:
         cls._rte_components = constants.parse_json_from_js(f)