示例#1
0
    def setUp(self) -> None:
        tag_regex_delim_text = (r'[*', r'*]')
        tag_regex_delim_note = (r'(-', r'-)')

        self.outline = Outline.from_opml(
            os.path.join(tcfg.test_resources_root, 'opml-test-valid-01.opml'),
            tag_regex_delim_text, tag_regex_delim_note)
示例#2
0
    def test_data_node_extract_04(self, index, key1, key2, non_key1, non_key2,
                                  non_key3):
        """
        Args:
            index:
            key1:
            key2:
            non_key1:
            non_key2:
            non_key3:
        """
        data_node_index = 31

        outline = Outline.from_opml(
            os.path.join(tcfg.test_resources_root,
                         'opml_data_extraction_test_02.opml'))

        outline_node_list = list(outline.list_all_nodes())
        data_node = outline_node_list[data_node_index].node()

        extracted_data_records = data_node.extract_data_node(
            test_data_node_specifier_03x)
        test_record = extracted_data_records[index]

        self.assertEqual(key1, test_record['key_field_1'])
        self.assertEqual(key2, test_record['key_field_2'])
        self.assertEqual(non_key1, test_record['data_field_1'])
        self.assertEqual(non_key2, test_record['data_field_2'])
        self.assertEqual(non_key3, test_record['data_field_3'])
示例#3
0
    def test_data_node_extract_07(self, index, category, note, date):
        """
        Args:
            index:
            category:
            note:
            date:
        """
        data_node_index = 1

        outline = Outline.from_opml(os.path.join(
            tcfg.test_resources_root, 'OutlinesUnleashed-Examples.opml'),
                                    tag_text_delimiter=('', ':'))

        outline_node_list = list(outline.list_all_nodes())
        data_node = outline_node_list[data_node_index].node()

        extracted_data_records = data_node.extract_data_node(
            test_data_node_specifier_07)
        if category is None:  # Signals end of list and that the test is just to check number of records
            self.assertEqual(index, len(extracted_data_records),
                             "Wrong number of records")
        else:
            test_record = extracted_data_records[index]
            self.assertEqual(category, test_record['category'])
            self.assertEqual(note, test_record['item'])
            self.assertEqual(date, test_record['date_due'])
    def test_version(self):
        """Just check that version number is correct (at top level of
        outline).
        """
        outline = Outline.from_opml(
            os.path.join(tcfg.test_resources_root, 'opml-test-valid-01.opml'))
        version = outline.version

        self.assertEqual('2.0', version)
示例#5
0
    def setUp(self) -> None:
        tag_delimiters_text = ('[*', '*]')

        outline = Outline.from_opml(
            os.path.join(tcfg.test_resources_root,
                         'opml_data_extraction_test_01.opml'),
            tag_delimiters_text)

        # Create list of all nodes (plus ancestry) to allow acess to nodes by index.
        self.node_list = list(outline.list_all_nodes())
    def test_head_data_from_opml(self, field_name, expected_value):
        """
        Args:
            field_name:
            expected_value:
        """
        outline = Outline.from_opml(
            os.path.join(tcfg.test_resources_root, 'opml-test-valid-01.opml'))

        # Access field by attribute name to allow data driven approach for test
        value = getattr(outline, field_name)
        self.assertEqual(expected_value, value)
示例#7
0
    def test_data_node_freeform_notes(self):
        data_node_index = 1

        outline = Outline.from_opml(os.path.join(tcfg.test_resources_root,
                                                 'FreeFormNotesExample.opml'),
                                    tag_text_delimiter=('', ':'))

        outline_node_list = list(outline.list_all_nodes())
        data_node = outline_node_list[data_node_index].node()

        extracted_data_records = data_node.extract_data_node(
            test_data_node_specifier_freeform_notes)
        pass
    def test_confirm_identity(self):
        """Note this test will not pass until re-factoring is complete to ensure
        that same OutlineNode object is returned for same outline element. Some
        thinking to do before we can do this. :return:
        """
        outline = Outline.from_opml(os.path.join(tcfg.test_resources_root, 'opml-test-valid-01.opml'))

        top_level_node = outline.outline  # Access the top level OutlineNode object

        node_01_01 = top_level_node[0][0]
        node_01_01_01a = node_01_01[0]

        node_01_01_01b = top_level_node[0][0][0]
示例#9
0
    def test_invalid_node_01(self, filename):
        """Tests that if there is a node which is invalid it generates and
        exception and aborts the walk. :return:

        Args:
            filename:
        """

        outline = Outline.from_opml(
            os.path.join(tcfg.test_resources_root, filename))
        #node_list = list(outline.list_all_nodes())

        func = functools.partial(list, outline.list_all_nodes())
        self.assertRaises(MalformedOutline, func)
示例#10
0
    def test_data_node_extract_05(self):
        data_node_index = 46

        outline = Outline.from_opml(os.path.join(
            tcfg.test_resources_root, 'opml_data_extraction_test_02.opml'),
                                    tag_text_delimiter=('', ':'))

        outline_node_list = list(outline.list_all_nodes())
        data_node = outline_node_list[data_node_index].node()

        extracted_data_records = data_node.extract_data_node(
            test_data_node_specifier_05x)

        pass
示例#11
0
    def test_data_node_extract_01(self):
        outline = Outline.from_opml(
            os.path.join(tcfg.test_resources_root,
                         'opml_data_extraction_test_02.opml'))

        outline_node_list = list(outline.list_all_nodes())
        data_node = outline_node_list[1].node()

        extracted_data_records = data_node.extract_data_node(
            test_data_node_specifier_ppt_01)

        expected_num_records = 21

        self.assertEqual(expected_num_records, len(extracted_data_records))
示例#12
0
    def test_data_node_extract_03(self):
        data_node_index = 31

        outline = Outline.from_opml(
            os.path.join(tcfg.test_resources_root,
                         'opml_data_extraction_test_02.opml'))

        outline_node_list = list(outline.list_all_nodes())
        data_node = outline_node_list[data_node_index].node()

        extracted_data_records = data_node.extract_data_node(
            test_data_node_specifier_03x)

        self.assertEqual(3, len(extracted_data_records))
    def test_child_access(self):
        outline = Outline.from_opml(os.path.join(tcfg.test_resources_root, 'opml-test-valid-01.opml'))

        top_level_node = outline.outline

        num_child_nodes = len(top_level_node)
        self.assertEqual(3, num_child_nodes)

        child_outline_nodes = list(top_level_node)
        self.assertEqual(3, len(child_outline_nodes))

        for index, child in enumerate(top_level_node):
            self.assertIsInstance(child, OutlineNode)
            self.assertIsInstance(child_outline_nodes[index], OutlineNode)
            self.assertEqual(child, child_outline_nodes[index])
示例#14
0
    def setUp(self) -> None:
        data_node_index = 1
        data_node_descriptor = data_node_specifier_test_driver[0]

        tag_delimiters_text = ('[*', '*]')

        outline = Outline.from_opml(
            os.path.join(tcfg.test_resources_root,
                         'opml_data_extraction_test_01.opml'),
            tag_delimiters_text)

        # Create list of all nodes (plus ancestry) to allow acess to nodes by index.
        self.node_list = list(outline.list_all_nodes())

        test_data_node = self.node_list[data_node_index].node()
        self.extracted_data_table = test_data_node.extract_data_node(
            data_node_descriptor)
    def test_invalid_version_01(self, file, fail_type):
        """As version is a mandatory field and the version of the OPML file
        drives how it is parsed, only outlines with version of '2.0' will be
        excepted, and anything else should raise an exception.

        I'm not 100% sure this is right as the version really applies to the
        OPML XML file format, so if the outline is being initialised from
        something else there is no XML. However if the version ever changes it
        may change other features which are meaningful beyond just the XML
        format (e.g. may add other elements or attributes) which is still valid
        regardless of how the outline was generated.

        Args:
            file:
            fail_type:
        """

        if fail_type == 'value':
            outline = Outline.from_opml(file)
            version = outline.version
            self.assertNotEqual('2.0', version)
        elif fail_type == 'exception':
            partial_from_opml = partial(Outline.from_opml, file)
            self.assertRaises(MalformedOutline, partial_from_opml)
    def test_field_access(self):
        outline = Outline.from_opml(
            os.path.join(tcfg.test_resources_root, 'opml-test-valid-01.opml'),
            ('(-', '-)'),
            ('[*', '*]')
        )

        top_level_node = outline.outline  # Access the top level OutlineNode object

        # Check that accessing child node gets the right one
        node_01 = top_level_node[0]
        self.assertEqual('H1:Heading A', node_01.text)
        self.assertEqual('Notes for Heading A', node_01.note)

        # Check that accessing sub-nodes from top level works ok.
        node_01_01 = top_level_node[0][0]
        self.assertEqual('H2: Heading B', node_01_01.text)
        self.assertEqual('Notes for Heading C', node_01_01.note)

        # Check that two ways of getting to the same node reveal the same one.
        node_01_01_01a = node_01_01[0]
        node_01_01_01b = top_level_node[0][0][0]

        self.assertEqual(node_01_01_01a, node_01_01_01b)
示例#17
0
    def test_data_node_extract_02(self, index, section, slide, bullet):
        """
        Args:
            index:
            section:
            slide:
            bullet:
        """
        data_node_index = 1

        outline = Outline.from_opml(
            os.path.join(tcfg.test_resources_root,
                         'opml_data_extraction_test_02.opml'))

        outline_node_list = list(outline.list_all_nodes())
        data_node = outline_node_list[data_node_index].node()

        extracted_data_records = data_node.extract_data_node(
            test_data_node_specifier_ppt_01)

        test_record = extracted_data_records[index]
        self.assertEqual(section, test_record['section_name'])
        self.assertEqual(slide, test_record['slide_name'])
        self.assertEqual(bullet, test_record['bullet'])
示例#18
0
def unleash_outline(request):
    if request.method == 'POST':
        form = TransformationForm(request.POST, request.FILES)
        if form.is_valid():
            transformation_instance = form.save(commit=False)
            transformation_instance.file = request.FILES['file']
            file_type = transformation_instance.file.url.split('.')[-1]
            file_type = file_type.lower()
            if file_type != 'opml':
                return render(request, 'ou_app/error.html')

            transformation_instance.save()

            # We have saved the file.  Let's process it now.
            # ToDo: Check whether this should be BASE_DIR or MEDIA_DIR (Media duplicates media folder)
            outline_full_path = os.path.join(settings.BASE_DIR,
                                             transformation_instance.file.url)

            # Get file name and trans type chosen by user (not actual filename saved) to display on result
            file = form.cleaned_data['file']
            descriptor = form.cleaned_data['transformation']

            if descriptor.name == "PPT-01":
                # For now this is hard-coded to route through create ppt treatment.
                # This will be changed to be more generic.
                # ToDo: Remove hard-coding of PPT processing and replace with more general approach.

                outline = Outline.from_opml(outline_full_path,
                                            tag_text_delimiter=("", ":"),
                                            tag_note_delimiter=("", ":"))

                # For testing assume that the data node is in node 1 (first node below root)
                outline_node_list = list(outline.list_all_nodes())
                data_node = outline_node_list[1].node()

                # Generate PowerPoint file from table
                output_generator = PowerPointGenerator()
                output_generator.create_power_point_skeleton(
                    data_node, template_ppt_filepath, output_ppt_filepath)
                return render(request, 'ou_app/ppt_download.html')
            else:
                # descriptor.name is a key into the data node descriptor table, so extract the record and use the JSON
                # as the descriptor.  Exception is that if descriptor is PPT - hard-code to process as PPT file
                # (for now).

                descriptor_json = descriptor.json
                descriptor_object = OutlineNode.from_json(descriptor_json)

                tag_text_delimiter = tuple(
                    descriptor_object['header']['tag_delimiters']
                    ['text_delimiters'])
                tag_note_delimiter = tuple(
                    descriptor_object['header']['tag_delimiters']
                    ['note_delimiters'])

                outline = Outline.from_opml(
                    outline_full_path,
                    tag_text_delimiter=tag_text_delimiter,
                    tag_note_delimiter=tag_note_delimiter)

                # For testing assume that the data node is in node 1 (first node below root)
                outline_node_list = list(outline.list_all_nodes())
                data_node = outline_node_list[1].node()

                # Convert data fields into lists of fields to make easier processing by template
                extracted_data_records = data_node.extract_data_node(
                    descriptor_object)
                fields, data_records = data_node_output.extract_data_fields(
                    extracted_data_records)

                return render(
                    request, 'ou_app/result.html', {
                        'transformation': descriptor.name,
                        'file': file,
                        'data_records': data_records,
                        'fields': fields
                    })
    elif request.method == "GET":
        form = TransformationForm
        context = {
            "form": form,
        }
        return render(request, 'ou_app/unleash_outline.html', context)
    else:
        raise ValueError(f'Unexpected HTTP method {request.method}')
示例#19
0
 def setUp(self) -> None:
     self.outline = Outline.from_opml(
         os.path.join(tcfg.test_resources_root, 'opml-test-valid-01.opml'))