Beispiel #1
0
    def load_default_widgets(cls):
        """Loads the default widgets.

        Assumes that everything is valid (directories exist, widget config files
        are formatted correctly, etc.).
        """
        widget_ids = os.listdir(feconf.INTERACTIVE_WIDGETS_DIR)

        for widget_id in widget_ids:
            widget_dir = os.path.join(feconf.INTERACTIVE_WIDGETS_DIR, widget_id)
            widget_conf_filename = '%s.config.yaml' % widget_id
            with open(os.path.join(widget_dir, widget_conf_filename)) as f:
                conf = utils.dict_from_yaml(f.read().decode('utf-8'))

            conf['id'] = '%s-%s' % (feconf.INTERACTIVE_PREFIX, widget_id)
            conf['params'] = [Parameter(**param) for param in conf['params']]
            conf['handlers'] = [AnswerHandler(**ah) for ah in conf['handlers']]
            conf['template'] = utils.get_file_contents(
                os.path.join(widget_dir, '%s.html' % widget_id))
            conf['static_template'] = ''
            static_path = os.path.join(widget_dir, '%s.static.html' % widget_id)
            if os.path.exists(static_path):
                conf['static_template'] = utils.get_file_contents(static_path)

            widget = cls(**conf)
            widget.put()
 def get_full_html(cls):
     """Returns the HTML bodies for all visualizations."""
     js_directives = utils.get_file_contents(os.path.join(
         feconf.VISUALIZATIONS_DIR, 'visualizations.js'))
     html_templates = utils.get_file_contents(os.path.join(
         feconf.VISUALIZATIONS_DIR, 'visualizations.html'))
     return '<script>%s</script>\n%s' % (js_directives, html_templates)
    def test_default_rte_components_are_valid(self):
        """Test that the default RTE components are valid."""

        for component_id in feconf.ALLOWED_RTE_EXTENSIONS:
            # Check that the component id is valid.
            self.assertTrue(self._is_camel_cased(component_id))

            # Check that the component directory exists.
            component_dir = os.path.join(
                feconf.RTE_EXTENSIONS_DIR, component_id)
            self.assertTrue(os.path.isdir(component_dir))

            # In this directory there should be a config .py file, an
            # html file, a JS file, a .png file and a protractor.js file.
            dir_contents = self._listdir_omit_ignored(component_dir)
            self.assertLessEqual(len(dir_contents), 5)

            py_file = os.path.join(component_dir, '%s.py' % component_id)
            html_file = os.path.join(component_dir, '%s.html' % component_id)
            js_file = os.path.join(component_dir, '%s.js' % component_id)
            png_file = os.path.join(component_dir, '%s.png' % component_id)
            protractor_file = os.path.join(component_dir, 'protractor.js')

            self.assertTrue(os.path.isfile(py_file))
            self.assertTrue(os.path.isfile(html_file))
            self.assertTrue(os.path.isfile(js_file))
            self.assertTrue(os.path.isfile(png_file))
            self.assertTrue(os.path.isfile(protractor_file))

            js_file_content = utils.get_file_contents(js_file)
            html_file_content = utils.get_file_contents(html_file)
            self.assertIn(
                'oppiaNoninteractive%s' % component_id, js_file_content)
            self.assertIn(
                '<script type="text/ng-template" '
                'id="richTextComponent/%s"' % component_id,
                html_file_content)
            self.assertNotIn('<script>', js_file_content)
            self.assertNotIn('</script>', js_file_content)

            component = rte_component_registry.Registry.get_rte_component(
                component_id)

            # Check that the specified component id is the same as the class
            # name.
            self.assertTrue(component_id, component.__class__.__name__)

            # Check that the configuration file contains the correct
            # top-level keys, and that these keys have the correct types.
            for item, item_type in _COMPONENT_CONFIG_SCHEMA:
                self.assertTrue(isinstance(
                    getattr(component, item), item_type))
                # The string attributes should be non-empty.
                if item_type == basestring:
                    self.assertTrue(getattr(component, item))

            self._validate_customization_arg_specs(
                component._customization_arg_specs)  # pylint: disable=protected-access
Beispiel #4
0
 def html_body(self):
     """The HTML code containing directives and templates for the
     gadget. This contains everything needed to display the gadget
     once the necessary attributes are supplied.
     """
     js_directives = utils.get_file_contents(os.path.join(
         feconf.GADGETS_DIR, self.type, '%s.js' % self.type))
     html_templates = utils.get_file_contents(os.path.join(
         feconf.GADGETS_DIR, self.type, '%s.html' % self.type))
     return '<script>%s</script>\n%s' % (js_directives, html_templates)
Beispiel #5
0
def analyse_paths(host):

    directory = utils.test_host_directory(host)
    filters = utils.get_url_path_filters(host)
    if os.path.isfile(directory+"paths_filtered"):
        print "paths_filtered exists"
        paths = utils.get_file_contents(host, "paths_filtered")
    else:
        print "paths_filtered doesn't exists"
        paths = utils.get_file_contents(host, "paths")
    paths.sort()
    return render_template('analyse_paths.html', paths=paths, host=host, num_all_paths=len(paths), filters=filters)
Beispiel #6
0
    def test_wrapper_name_rule(self):
        rule = tar_file_string.ChecksWrapperDirName('myproject-0.1')

        file_name = 'wrong-wrapper-name.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True, mode='rb'))
        self.assertTrue(rule.eval(encoded_content))

        file_name = 'good.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True, mode='rb'))
        self.assertFalse(rule.eval(encoded_content))
Beispiel #7
0
    def html_body(self):
        """The HTML code containing directives and templates for the component.

        This contains everything needed to display the component once the
        necessary attributes are supplied. For rich-text components, this
        consists of a single directive/template pair.
        """
        js_directives = utils.get_file_contents(os.path.join(
            feconf.RTE_EXTENSIONS_DIR, self.id, '%s.js' % self.id))
        html_templates = utils.get_file_contents(os.path.join(
            feconf.RTE_EXTENSIONS_DIR, self.id, '%s.html' % self.id))
        return '<script>%s</script>\n%s' % (js_directives, html_templates)
Beispiel #8
0
    def test_wrapper_presence_rule(self):
        rule = tar_file_string.ChecksWrapperDirPresence()

        file_name = 'no-wrapper-dir.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True, mode='rb'))
        self.assertTrue(rule.eval(encoded_content))

        file_name = 'good.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True, mode='rb'))
        self.assertFalse(rule.eval(encoded_content))
Beispiel #9
0
    def test_apple_double_file_rule(self):
        rule = tar_file_string.HasAppleDoubleFile()

        file_name = 'apple-double.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True, mode='rb'))
        self.assertTrue(rule.eval(encoded_content))

        file_name = 'good.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True, mode='rb'))
        self.assertFalse(rule.eval(encoded_content))
Beispiel #10
0
    def test_thumbnail_icons_exist_for_each_category(self):
        all_categories = feconf.CATEGORIES_TO_COLORS.keys()

        # Test that an icon exists for each default category.
        for category in all_categories:
            utils.get_file_contents(os.path.join(
                'static', 'images', 'gallery', 'thumbnails',
                '%s.svg' % category.replace(' ', '')))

        # Test that the default icon exists.
        utils.get_file_contents(os.path.join(
            'static', 'images', 'gallery', 'thumbnails',
            '%s.svg' % feconf.DEFAULT_THUMBNAIL_ICON))
Beispiel #11
0
    def html_body(self):
        """The HTML code containing directives and templates for the
        interaction. This contains everything needed to display the interaction
        once the necessary attributes are supplied.

        Each interaction has two directive/template pairs, one for the
        interaction itself and the other for displaying the learner's response
        in a read-only view after it has been submitted.
        """
        js_directives = utils.get_file_contents(os.path.join(
            feconf.INTERACTIONS_DIR, self.id, '%s.js' % self.id))
        html_templates = utils.get_file_contents(os.path.join(
            feconf.INTERACTIONS_DIR, self.id, '%s.html' % self.id))
        return '<script>%s</script>\n%s' % (js_directives, html_templates)
Beispiel #12
0
    def test_missing_expected_file_rule(self):
        rule = tar_file_string.MissingExpectedFile(
            ["myproject-0.1", "myproject-0.1/hello.c",
             "myproject-0.1/Makefile"]
        )

        file_name = 'missing-expected-file.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True, mode='rb'))
        self.assertTrue(rule.eval(encoded_content))

        file_name = 'good.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True, mode='rb'))
        self.assertFalse(rule.eval(encoded_content))
    def test_unexpected_content_rule(self):

        TEST_DATA_DIR = 'extensions/rules/testdata'
        rule = tar_file_string.HasUnexpectedContent(
            ['hello.c', 'Makefile'])

        file_name = 'incorrect-contents.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True))
        self.assertTrue(rule.eval(encoded_content))

        file_name = 'good.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True))
        self.assertFalse(rule.eval(encoded_content))
Beispiel #14
0
    def html_body(self):
        """The HTML code containing directives and templates for the
        interaction. This contains everything needed to display the interaction
        once the necessary attributes are supplied.

        Each interaction has two directive/template pairs, one for the
        interaction itself and the other for displaying the learner's response
        in a read-only view after it has been submitted.
        """
        js_directives = utils.get_file_contents(
            os.path.join(feconf.INTERACTIONS_DIR, self.id, '%s.js' % self.id))
        html_templates = utils.get_file_contents(
            os.path.join(feconf.INTERACTIONS_DIR, self.id,
                         '%s.html' % self.id))
        return '<script>%s</script>\n%s' % (js_directives, html_templates)
Beispiel #15
0
    def test_thumbnail_icons_exist_for_each_category(self):
        all_categories = list(constants.CATEGORIES_TO_COLORS.keys())

        # Test that an icon exists for each default category.
        for category in all_categories:
            utils.get_file_contents(
                os.path.join(self.get_static_asset_filepath(), 'assets',
                             'images', 'subjects',
                             '%s.svg' % category.replace(' ', '')))

        # Test that the default icon exists.
        utils.get_file_contents(
            os.path.join(self.get_static_asset_filepath(), 'assets', 'images',
                         'subjects',
                         '%s.svg' % constants.DEFAULT_THUMBNAIL_ICON))
    def test_unexpected_file_rule(self):

        TEST_DATA_DIR = 'extensions/rules/testdata'
        rule = tar_file_string.HasUnexpectedFile(
            ["myproject-0.1", "myproject-0.1/hello.c", "myproject-0.1/Makefile"]
        )

        file_name = 'unexpected-file.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True))
        self.assertTrue(rule.eval(encoded_content))

        file_name = 'good.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True))
        self.assertFalse(rule.eval(encoded_content))
Beispiel #17
0
 def get_dependency_html(cls, dependency_id):
     """Returns the HTML template needed to inject this dependency in the
     client webpage.
     """
     return utils.get_file_contents(
         os.path.join(feconf.DEPENDENCIES_TEMPLATES_DIR,
                      '%s.html' % dependency_id))
def load_demo(exploration_id):
    """Loads a demo exploration."""
    if not (0 <= int(exploration_id) < len(feconf.DEMO_EXPLORATIONS)):
        raise Exception('Invalid demo exploration id %s' % exploration_id)

    exploration = feconf.DEMO_EXPLORATIONS[int(exploration_id)]

    if len(exploration) == 3:
        (exp_filename, title, category) = exploration
        image_filename = None
    elif len(exploration) == 4:
        (exp_filename, title, category, image_filename) = exploration
    else:
        raise Exception('Invalid demo exploration: %s' % exploration)

    image_id = None
    if image_filename:
        image_filepath = os.path.join(
            feconf.SAMPLE_IMAGES_DIR, image_filename)
        image_id = image_models.Image.create(utils.get_file_contents(
            image_filepath, raw_bytes=True))

    yaml_content = utils.get_sample_exploration_yaml(exp_filename)
    exploration_id = create_from_yaml(
        yaml_content, ADMIN_COMMITTER_ID, title, category,
        exploration_id=exploration_id, image_id=image_id)

    exploration = get_exploration_by_id(exploration_id)
    exploration.is_public = True
    save_exploration(ADMIN_COMMITTER_ID, exploration)

    logging.info('Exploration with id %s was loaded.' % exploration_id)
Beispiel #19
0
 def get_js_template(cls):
     # NB: These generators should use only Angular templating. The
     # variables they have access to are generatorId, initArgs,
     # customizationArgs and objType.
     return utils.get_file_contents(
         os.path.join(os.getcwd(), feconf.VALUE_GENERATORS_DIR, 'templates',
                      '%s.js' % cls.__name__))
Beispiel #20
0
def load_demos():
    """Initializes the demo explorations."""
    for index, exploration in enumerate(feconf.DEMO_EXPLORATIONS):
        if len(exploration) == 3:
            (exp_filename, title, category) = exploration
            image_filename = None
        elif len(exploration) == 4:
            (exp_filename, title, category, image_filename) = exploration
        else:
            raise Exception('Invalid demo exploration: %s' % exploration)

        image_id = None
        if image_filename:
            image_filepath = os.path.join(
                feconf.SAMPLE_IMAGES_DIR, image_filename)
            image_id = Image.create(utils.get_file_contents(
                image_filepath, raw_bytes=True))

        yaml_content = utils.get_sample_exploration_yaml(exp_filename)
        exploration_id = create_from_yaml(
            yaml_content, None, title, category, exploration_id=str(index),
            image_id=image_id)

        exploration = Exploration.get(exploration_id)
        exploration.is_public = True
        exploration.put()
Beispiel #21
0
def load_demo(collection_id):
    """Loads a demo collection.

    The resulting collection will have version 2 (one for its initial
    creation and one for its subsequent modification).

    Args:
        collection_id: str. ID of the collection to be loaded.
    """
    delete_demo(collection_id)

    demo_filepath = os.path.join(feconf.SAMPLE_COLLECTIONS_DIR,
                                 feconf.DEMO_COLLECTIONS[collection_id])

    yaml_content = utils.get_file_contents(demo_filepath)

    collection = save_new_collection_from_yaml(feconf.SYSTEM_COMMITTER_ID,
                                               yaml_content, collection_id)

    system_user = user_services.get_system_user()
    publish_collection_and_update_user_profiles(system_user, collection_id)

    index_collections_given_ids([collection_id])

    # Now, load all of the demo explorations that are part of the collection.
    for collection_node in collection.nodes:
        exp_id = collection_node.exploration_id
        # Only load the demo exploration if it is not yet loaded.
        if exp_services.get_exploration_by_id(exp_id, strict=False) is None:
            exp_services.load_demo(exp_id)

    logging.info('Collection with id %s was loaded.' % collection_id)
Beispiel #22
0
 def _extract_keys_from_html_file(self, filename):
     # The \b is added at the start to ensure that keys ending with
     # '_I18N_IDS' do not get matched. Instances of such keys can be found
     # in learner_dashboard.html.
     regex_pattern = r'(\bI18N_[A-Z/_\d]*)'
     return re.findall(regex_pattern, utils.get_file_contents(
         filename))
Beispiel #23
0
def analyse_paths(host):

    directory = utils.test_host_directory(host)
    filters = utils.get_url_path_filters(host)
    if os.path.isfile(directory + "paths_filtered"):
        print "paths_filtered exists"
        paths = utils.get_file_contents(host, "paths_filtered")
    else:
        print "paths_filtered doesn't exists"
        paths = utils.get_file_contents(host, "paths")
    paths.sort()
    return render_template('analyse_paths.html',
                           paths=paths,
                           host=host,
                           num_all_paths=len(paths),
                           filters=filters)
Beispiel #24
0
def api_split_urls(host):

    if request.headers['Content-Type'] == 'application/json':
        if request.json["urls"] == '<all>':
            urls = utils.get_file_contents(host)
            urls = utils.divide_in_out_urls(host, urls)['in_urls']
            return Response(json.dumps({
                'status': 'OK',
                'urls': utils.parse_urls(urls)
            }),
                            status=200,
                            mimetype='application/json')
        else:
            return Response(json.dumps({
                'status': 'error',
                'reason': 'not implemented'
            }),
                            status=200,
                            mimetype='application/json')
    else:
        return Response(json.dumps({
            'status': 'error',
            'reason': 'Unsupported Media Type'
        }),
                        status=415,
                        mimetype='application/json')
    def test_get_training_data(self):
        """Test retrieval of training data."""
        exploration_id = 'eid'
        test_exp_filepath = os.path.join(feconf.SAMPLE_EXPLORATIONS_DIR,
                                         'classifier_demo_exploration.yaml')
        yaml_content = utils.get_file_contents(test_exp_filepath)
        assets_list = []
        exp_services.save_new_exploration_from_yaml_and_assets(
            feconf.SYSTEM_COMMITTER_ID, yaml_content, exploration_id,
            assets_list)

        exploration = exp_services.get_exploration_by_id(exploration_id)
        state = exploration.states['text']

        expected_training_data = [{
            'answer_group_index':
            1,
            'answers': [
                u'cheerful', u'merry', u'ecstatic', u'glad', u'overjoyed',
                u'pleased', u'thrilled', u'smile'
            ]
        }]

        observed_training_data = state.get_training_data()

        self.assertEqual(observed_training_data, expected_training_data)
Beispiel #26
0
    def load_demo_explorations(cls):
        """Initializes the demo explorations."""
        for index, exploration in enumerate(feconf.DEMO_EXPLORATIONS):
            assert len(exploration) in [3, 4], "Invalid format for demo exploration: %s" % exploration

            yaml_filename = "%s.yaml" % exploration[0]
            yaml_file = utils.get_file_contents(os.path.join(feconf.SAMPLE_EXPLORATIONS_DIR, yaml_filename))

            title = exploration[1]
            category = exploration[2]
            image_filename = exploration[3] if len(exploration) == 4 else None

            image_id = None
            if image_filename:
                with open(os.path.join(feconf.SAMPLE_IMAGES_DIR, image_filename)) as f:
                    raw_image = f.read()
                image_id = Image.create(raw_image)

            exploration = cls.create_from_yaml(
                yaml_file=yaml_file,
                user=None,
                title=title,
                category=category,
                exploration_id=str(index),
                image_id=image_id,
            )
            exploration.is_public = True
            exploration.put()
Beispiel #27
0
def load_demo(collection_id):
    """Loads a demo collection.

    The resulting collection will have version 2 (one for its initial
    creation and one for its subsequent modification.)
    """
    delete_demo(collection_id)

    if not collection_domain.Collection.is_demo_collection_id(collection_id):
        raise Exception('Invalid demo collection id %s' % collection_id)

    demo_filepath = os.path.join(feconf.SAMPLE_COLLECTIONS_DIR,
                                 feconf.DEMO_COLLECTIONS[collection_id])

    if demo_filepath.endswith('yaml'):
        yaml_content = utils.get_file_contents(demo_filepath)
    else:
        raise Exception('Unrecognized file path: %s' % demo_filepath)

    collection = save_new_collection_from_yaml(feconf.SYSTEM_COMMITTER_ID,
                                               yaml_content, collection_id)

    publish_collection_and_update_user_profiles(feconf.SYSTEM_COMMITTER_ID,
                                                collection_id)

    index_collections_given_ids([collection_id])

    # Now, load all of the demo explorations that are part of the collection.
    for collection_node in collection.nodes:
        exp_id = collection_node.exploration_id
        # Only load the demo exploration if it is not yet loaded.
        if exp_services.get_exploration_by_id(exp_id, strict=False) is None:
            exp_services.load_demo(exp_id)

    logging.info('Collection with id %s was loaded.' % collection_id)
Beispiel #28
0
    def test_apple_double_file_rule(self):
        rule = tar_file_string.HasAppleDoubleFile()

        file_name = 'apple-double.tar.gz'
        encoded_content = base64.b64encode(
            utils.get_file_contents(os.path.join(TEST_DATA_DIR, file_name),
                                    raw_bytes=True,
                                    mode='rb'))
        self.assertTrue(rule.eval(encoded_content))

        file_name = 'good.tar.gz'
        encoded_content = base64.b64encode(
            utils.get_file_contents(os.path.join(TEST_DATA_DIR, file_name),
                                    raw_bytes=True,
                                    mode='rb'))
        self.assertFalse(rule.eval(encoded_content))
Beispiel #29
0
    def test_wrapper_name_rule(self):
        rule = tar_file_string.ChecksWrapperDirName('myproject-0.1')

        file_name = 'wrong-wrapper-name.tar.gz'
        encoded_content = base64.b64encode(
            utils.get_file_contents(os.path.join(TEST_DATA_DIR, file_name),
                                    raw_bytes=True,
                                    mode='rb'))
        self.assertTrue(rule.eval(encoded_content))

        file_name = 'good.tar.gz'
        encoded_content = base64.b64encode(
            utils.get_file_contents(os.path.join(TEST_DATA_DIR, file_name),
                                    raw_bytes=True,
                                    mode='rb'))
        self.assertFalse(rule.eval(encoded_content))
Beispiel #30
0
 def _stats_log_template(self):
     """The template for reader responses in the stats log."""
     try:
         return utils.get_file_contents(os.path.join(
             feconf.INTERACTIONS_DIR, self.id, 'stats_response.html'))
     except IOError:
         return '{{answer}}'
Beispiel #31
0
 def _stats_log_template(self):
     """The template for reader responses in the stats log."""
     try:
         return utils.get_file_contents(os.path.join(
             feconf.INTERACTIONS_DIR, self.id, 'stats_response.html'))
     except IOError:
         return '{{answer}}'
Beispiel #32
0
 def validator_html(self):
     """The HTML code containing validators for the interaction's
     customization_args and submission handler.
     """
     return ('<script>%s</script>\n' % utils.get_file_contents(
         os.path.join(feconf.INTERACTIONS_DIR, self.id,
                      '%sValidationService.js' % self.id)))
Beispiel #33
0
 def get_js_template(cls):
     # NB: These generators should use only Angular templating. The
     # variables they have access to are generatorId, initArgs,
     # customizationArgs and objType.
     return utils.get_file_contents(os.path.join(
         os.getcwd(), feconf.VALUE_GENERATORS_DIR, 'templates',
         '%s.js' % cls.__name__))
Beispiel #34
0
    def test_wrapper_presence_rule(self):
        rule = tar_file_string.ChecksWrapperDirPresence()

        file_name = 'no-wrapper-dir.tar.gz'
        encoded_content = base64.b64encode(
            utils.get_file_contents(os.path.join(TEST_DATA_DIR, file_name),
                                    raw_bytes=True,
                                    mode='rb'))
        self.assertTrue(rule.eval(encoded_content))

        file_name = 'good.tar.gz'
        encoded_content = base64.b64encode(
            utils.get_file_contents(os.path.join(TEST_DATA_DIR, file_name),
                                    raw_bytes=True,
                                    mode='rb'))
        self.assertFalse(rule.eval(encoded_content))
Beispiel #35
0
    def test_rte_components_are_valid(self):
        """Test that the default RTE components are valid."""

        rte_components = (
            rte_component_registry.Registry.get_all_rte_components())

        for (component_id, component_specs) in rte_components.items():
            # Check that the component id is valid.
            hyphenated_component_id = utils.camelcase_to_hyphenated(
                component_id)
            self.assertTrue(self._is_camel_cased(component_id))

            # Check that the component directory exists.
            component_dir = os.path.join(
                feconf.RTE_EXTENSIONS_DIR, component_id)
            self.assertTrue(os.path.isdir(component_dir))

            # In this directory there should be a /directives directory, an
            # an icon .png file and a protractor.js file, and an optional
            # preview .png file.
            # In /directives directory should be HTML file, a JS file,
            # there could be multiple JS and HTML files.
            dir_contents = self._listdir_omit_ignored(component_dir)
            self.assertLessEqual(len(dir_contents), 4)

            directives_dir = os.path.join(component_dir, 'directives')
            png_file = os.path.join(component_dir, '%s.png' % component_id)
            protractor_file = os.path.join(component_dir, 'protractor.js')

            self.assertTrue(os.path.isdir(directives_dir))
            self.assertTrue(os.path.isfile(png_file))
            self.assertTrue(os.path.isfile(protractor_file))

            main_ts_file = os.path.join(
                directives_dir, 'oppia-noninteractive-%s.component.ts'
                % hyphenated_component_id)
            main_html_file = os.path.join(
                directives_dir, '%s.component.html'
                % hyphenated_component_id)
            self.assertTrue(os.path.isfile(main_ts_file))
            self.assertTrue(os.path.isfile(main_html_file))

            ts_file_content = utils.get_file_contents(main_ts_file)
            self.assertIn(
                'oppiaNoninteractive%s' % component_id, ts_file_content)
            self.assertNotIn('<script>', ts_file_content)
            self.assertNotIn('</script>', ts_file_content)

            # Check that the configuration file contains the correct
            # top-level keys, and that these keys have the correct types.
            for item, item_type in _COMPONENT_CONFIG_SCHEMA:
                self.assertTrue(isinstance(
                    component_specs[item], item_type))
                # The string attributes should be non-empty.
                if item_type == python_utils.BASESTRING:
                    self.assertTrue(component_specs[item])

            self._validate_customization_arg_specs(
                component_specs['customization_arg_specs'])  # pylint: disable=protected-access
Beispiel #36
0
def api_get_urls(host):
    urls = utils.get_file_contents(host, "in_urls")
    return Response(json.dumps({
        'status': 'OK',
        'urls': urls
    }),
                    status=200,
                    mimetype='application/json')
    def test_unexpected_file_rule(self):

        TEST_DATA_DIR = 'extensions/rules/testdata'
        rule = tar_file_string.HasUnexpectedFile(
            ["myproject-0.1", "myproject-0.1/hello.c",
             "myproject-0.1/Makefile"]
        )

        file_name = 'unexpected-file.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True))
        self.assertTrue(rule.eval(encoded_content))

        file_name = 'good.tar.gz'
        encoded_content = base64.b64encode(utils.get_file_contents(
            os.path.join(TEST_DATA_DIR, file_name), raw_bytes=True))
        self.assertFalse(rule.eval(encoded_content))
Beispiel #38
0
 def get_editor_html_template(cls):
     if cls.edit_html_filename is None:
         raise Exception(
             'There is no editor template defined for objects of type %s' %
             cls.__name__)
     return utils.get_file_contents(os.path.join(
         os.getcwd(), feconf.OBJECT_TEMPLATES_DIR,
         '%s.html' % cls.edit_html_filename))
Beispiel #39
0
 def _extract_keys_from_json_file(self, filename):
     """Returns the extracted keys from the json file corresponding to the
     given filename.
     """
     return sorted(json.loads(utils.get_file_contents(
         os.path.join(os.getcwd(), self.get_static_asset_filepath(),
                      'assets', 'i18n', filename)
     )).keys())
Beispiel #40
0
 def html_body(self):
     """The HTML code containing directives and templates for the
     gadget. This contains everything needed to display the gadget
     once the necessary attributes are supplied.
     """
     html_templates = utils.get_file_contents(os.path.join(
         feconf.GADGETS_DIR, self.type, '%s.html' % self.type))
     return jinja_utils.interpolate_cache_slug('%s' % html_templates)
Beispiel #41
0
 def validator_html(self):
     """The HTML code containing validators for the interaction's
     customization_args and submission handler.
     """
     return (
         '<script>%s</script>\n' %
         utils.get_file_contents(os.path.join(
             feconf.INTERACTIONS_DIR, self.id, 'validator.js')))
Beispiel #42
0
    def _response_template(self):
        """The template that generates the html to display reader responses."""
        if not self.is_interactive:
            raise Exception(
                'This method should only be called for interactive widgets.')

        return utils.get_file_contents(os.path.join(
            feconf.WIDGETS_DIR, feconf.INTERACTIVE_PREFIX,
            self.id, 'response.html'))
Beispiel #43
0
def api_test_filter_paths(host):
    if request.headers['Content-Type'] == 'application/json':
        
        print request.json["filter"]
        paths = []
        directory = utils.test_host_directory(host)
        if os.path.isfile(directory+"paths_filtered"):
            paths = utils.get_file_contents(host, "paths_filtered")
        else:
            paths = utils.get_file_contents(host, "paths")

        pre_filter_num = len(paths)
        paths = utils.filter_url_paths(paths, request.json["filter"])
        post_filter_num = len(paths)

        return Response(json.dumps({'status': 'OK', 'filters': utils.get_url_path_filters(host), 'pre_filter_num': pre_filter_num, "post_filter_num": post_filter_num}), status=200, mimetype='application/json')
    else:
        return Response(json.dumps({'status': 'error', 'reason': 'Unsupported Media Type'}), status=415, mimetype='application/json')
Beispiel #44
0
    def test_missing_expected_file_rule(self):
        rule = tar_file_string.MissingExpectedFile([
            "myproject-0.1", "myproject-0.1/hello.c", "myproject-0.1/Makefile"
        ])

        file_name = 'missing-expected-file.tar.gz'
        encoded_content = base64.b64encode(
            utils.get_file_contents(os.path.join(TEST_DATA_DIR, file_name),
                                    raw_bytes=True,
                                    mode='rb'))
        self.assertTrue(rule.eval(encoded_content))

        file_name = 'good.tar.gz'
        encoded_content = base64.b64encode(
            utils.get_file_contents(os.path.join(TEST_DATA_DIR, file_name),
                                    raw_bytes=True,
                                    mode='rb'))
        self.assertFalse(rule.eval(encoded_content))
Beispiel #45
0
    def _response_template(self):
        """The template that generates the html to display reader responses."""
        if not self.is_interactive:
            raise Exception(
                'This method should only be called for interactive widgets.')

        return utils.get_file_contents(
            os.path.join(feconf.WIDGETS_DIR, feconf.INTERACTIVE_PREFIX,
                         self.id, 'response.html'))
Beispiel #46
0
    def get_html_template(cls):
        """Returns the HTML template for the class.

        Returns:
            str. The HTML template corresponding to the class.
        """
        return utils.get_file_contents(
            os.path.join(os.getcwd(), feconf.VALUE_GENERATORS_DIR, 'templates',
                         '%s.html' % cls.__name__))
Beispiel #47
0
    def rules_dict(self):
        """A dict of rule names to rule properties."""
        if self._cached_rules_dict is not None:
            return self._cached_rules_dict

        rules_index_dict = json.loads(
            utils.get_file_contents(feconf.RULES_DESCRIPTIONS_FILE_PATH))
        self._cached_rules_dict = rules_index_dict[self.id]

        return self._cached_rules_dict
    def get_full_html(cls):
        """Returns the HTML bodies for all visualizations."""
        js_directives = ''
        for visualization_class in cls.get_all_visualization_ids():
            filename = ('OppiaVisualization%sDirective.js' %
                        (visualization_class))
            js_directives += (utils.get_file_contents(
                os.path.join(feconf.VISUALIZATIONS_DIR_FOR_JS, filename)))

        return '<script>%s</script>\n' % (js_directives)
Beispiel #49
0
    def rules_dict(self):
        """A dict of rule names to rule properties."""
        if self._cached_rules_dict is not None:
            return self._cached_rules_dict

        rules_index_dict = json.loads(
            utils.get_file_contents(feconf.RULES_DESCRIPTIONS_FILE_PATH))
        self._cached_rules_dict = rules_index_dict[self.id]

        return self._cached_rules_dict
Beispiel #50
0
    def _init_classify_inputs(self, exploration_id):
        test_exp_filepath = os.path.join(feconf.TESTS_DATA_DIR, "string_classifier_test.yaml")
        yaml_content = utils.get_file_contents(test_exp_filepath)
        assets_list = []
        exp_services.save_new_exploration_from_yaml_and_assets(
            feconf.SYSTEM_COMMITTER_ID, yaml_content, exploration_id, assets_list
        )

        self.exp_id = exploration_id
        self.exp_state = exp_services.get_exploration_by_id(exploration_id).states["Home"]
Beispiel #51
0
def api_split_urls(host):

    if request.headers['Content-Type'] == 'application/json':
        if request.json["urls"] == '<all>':
            urls = utils.get_file_contents(host)
            urls = utils.divide_in_out_urls(host, urls)['in_urls']
            return Response(json.dumps({'status': 'OK', 'urls': utils.parse_urls(urls)}), status=200, mimetype='application/json')
        else:
            return Response(json.dumps({'status': 'error', 'reason': 'not implemented'}), status=200, mimetype='application/json')
    else:
        return Response(json.dumps({'status': 'error', 'reason': 'Unsupported Media Type'}), status=415, mimetype='application/json')
Beispiel #52
0
    def js_code(self):
        """The JS code containing directives and templates for the widget."""
        js_directives = utils.get_file_contents(os.path.join(
            feconf.WIDGETS_DIR, self.type, self.id, '%s.js' % self.id))

        html_template = utils.get_file_contents(os.path.join(
            feconf.WIDGETS_DIR, self.type, self.id, '%s.html' % self.id))
        if '<script>' in html_template or '</script>' in html_template:
            raise Exception(
                'Unexpected script tag in HTML template for widget ' % self.id)

        widget_type = (
            'interactiveWidget' if self.is_interactive
            else 'noninteractiveWidget')
        js_template = ("""
            <script type="text/ng-template" id="%s/%s">
              %s
            </script>""" % (widget_type, self.id, html_template))

        return '<script>%s</script>\n%s' % (js_directives, js_template)
Beispiel #53
0
    def html_body(self):
        """The HTML code containing directives and templates for the component.

        This contains everything needed to display the component once the
        necessary attributes are supplied. For rich-text components, this
        consists of a single directive/template pair.
        """
        html_templates = utils.get_file_contents(
            os.path.join(feconf.RTE_EXTENSIONS_DIR, self.id,
                         '%s.html' % self.id))
        return jinja_utils.interpolate_cache_slug('%s' % html_templates)
Beispiel #54
0
 def compile(self):
     utils.ensure_dir(self._output_prefix + self.output_path)
     string = utils.get_file_contents(self._source).format(
         student_name=self.student.printed_name,
         title='{student_name} - {course_name}'.format(
             student_name=self.student.printed_name,
             course_name=self.course_name))
     weasyprint.HTML(
         string=string,
         base_url=self._base_path).write_pdf(self._output_prefix +
                                             self.output_file)
Beispiel #55
0
    def _init_classify_inputs(self, exploration_id):
        test_exp_filepath = os.path.join(feconf.TESTS_DATA_DIR,
                                         'string_classifier_test.yaml')
        yaml_content = utils.get_file_contents(test_exp_filepath)
        assets_list = []
        exp_services.save_new_exploration_from_yaml_and_assets(
            feconf.SYSTEM_COMMITTER_ID, yaml_content,
            'Testing String Classifier', 'Test', exploration_id, assets_list)

        self.exp_id = exploration_id
        self.exp_state = (
            exp_services.get_exploration_by_id(exploration_id).states['Home'])
Beispiel #56
0
    def get_static_asset_filepath(self):
        """Returns filepath for referencing static files on disk.
        examples: '' or 'build/1234'
        """
        cache_slug_filepath = ''
        if feconf.IS_MINIFIED or not feconf.DEV_MODE:
            yaml_file_content = utils.dict_from_yaml(
                utils.get_file_contents('cache_slug.yaml'))
            cache_slug = yaml_file_content['cache_slug']
            cache_slug_filepath = os.path.join('build', cache_slug)

        return cache_slug_filepath
Beispiel #57
0
    def _stats_log_template(self):
        """The template for reader responses in the stats log."""
        if not self.is_interactive:
            raise Exception(
                'This method should only be called for interactive widgets.')

        try:
            return utils.get_file_contents(os.path.join(
                feconf.WIDGETS_DIR, feconf.INTERACTIVE_PREFIX, self.id,
                'stats_response.html'))
        except IOError:
            return '{{answer}}'
Beispiel #58
0
    def _stats_log_template(self):
        """The template for reader responses in the stats log."""
        if not self.is_interactive:
            raise Exception(
                'This method should only be called for interactive widgets.')

        try:
            return utils.get_file_contents(
                os.path.join(feconf.WIDGETS_DIR, feconf.INTERACTIVE_PREFIX,
                             self.id, 'stats_response.html'))
        except IOError:
            return '{{answer}}'