Example #1
0
    def test_parse_add_attributes(self):
        """Parse add attributes config items.
        """
        # Given a Munger configuration file with target xpath expression
        conf_file = os.path.join(self._conf_dir, 'baip-munger-update-attr.xml')
        xpathgen = baip_munger.XpathGen(conf_file)
        xpath = ("//table[@class='%s']/thead/tr/td/p[@class='%s']" %
                 ('TableBAHeaderRow', 'TableHeading'))
        section = xpathgen.root.xpath('//Doc/Section')

        # when I parse a sectionAddAttribute configuration element
        # section[2] is the "sectionAddAttribute" element
        received = xpathgen._parse_add_attributes(xpath, section[2])

        # then I should receive a list of dictionary structures of the
        # form
        # [{'xpath': '<xpath_expr>',
        #   'attribute': '<attr_name>'}]
        expected = [
            {
                'xpath': ("//table[@class='%s']/thead/tr/td/p[@class='%s']" %
                          ('TableBAHeaderRow', 'TableHeading')),
                'attribute':
                'style',
                'value':
                'margin-bottom:1.0pt',
                'add':
                True,
            },
        ]
        msg = 'Add attribute config items error'
        self.assertListEqual(received, expected, msg)
Example #2
0
    def test_munge_missing_input_file(self):
        """Munge a file: missing input file.
        """
        # Given a file to munge
        munge_infile = 'banana'

        # and a target munged file
        temp_dir = tempfile.mkdtemp()
        munge_outfile = os.path.join(temp_dir,
                                     'BA-NSB-GLO-1.1-combined_clean.html')

        # and a set of munging actions
        config_file = os.path.join(self._test_dir,
                                   'baip-munger-update-attr.xml')
        conf = baip_munger.XpathGen(config_file)
        actions = conf.parse_configuration()

        # when I perform a munge action
        munger = baip_munger.Munger()
        received = munger.munge(actions, munge_infile, munge_outfile)

        # then the munge should occur without error
        msg = 'Munger UI munge (missing file) should return False'
        self.assertFalse(received, msg)

        # and the munged file should not be deposited to the Munger
        # target directory
        msg = 'Munged target file not created'
        self.assertFalse(os.path.exists(munge_outfile), msg)

        # Clean up
        remove_files(get_directory_files_list(temp_dir))
        os.removedirs(temp_dir)
Example #3
0
    def test_munge_ordered_list(self):
        """Munge a file: ordered list.
        """
        # Given a file to munge
        test_file = 'BA-LEB-GAL-261-1-SWReview-v00_clean.html'
        munge_infile = os.path.join(self._test_dir, test_file)

        # and a target munged file
        temp_dir = tempfile.mkdtemp()
        munge_outfile = os.path.join(temp_dir, test_file)

        # and a set of munging actions with ordered list context
        config_file = os.path.join(self._test_dir,
                                   'baip-munger-ordered-list.xml')
        conf = baip_munger.XpathGen(config_file)
        actions = conf.parse_configuration()

        # when I perform a munge action
        munger = baip_munger.Munger()
        received = munger.munge(actions, munge_infile, munge_outfile)

        # then the munge should occur without error
        msg = 'Munger UI munge should return True'
        self.assertTrue(received, msg)

        # and the munged file deposited to the Munger target directory
        msg = 'Munged target file not created'
        self.assertTrue(os.path.exists(munge_outfile), msg)

        # Clean up
        remove_files(get_directory_files_list(temp_dir))
        os.removedirs(temp_dir)
Example #4
0
    def test_parse_replace_tag(self):
        """Parse replace tag config items.
        """
        # Given a Munger configuration file with target xpath expression
        conf_file = os.path.join(self._conf_dir, 'baip-munger-update-attr.xml')
        xpathgen = baip_munger.XpathGen(conf_file)
        xpath = "//p[@class='MsoListBullet']"
        section = xpathgen.root.xpath('//Doc/Section')

        # when I parse a sectionReplaceTag configuration element
        # section[5] is the "sectionReplaceTag" element
        received = xpathgen._parse_replace_tag(xpath, section[5])

        # then I should receive a list of dictionary structures of the
        # form
        # [{'xpath': '<xpath_expr>',
        #   'new_tag': '<new_tag>',
        #   'new_tag_attributes': [('<attr_name>', '<attr_value>'), ...]}]
        expected = [{
            'xpath': "//p[@class='MsoListBullet']",
            'new_tag_attributes': [('class', 'MsoListBullet')],
            'new_tag': 'li',
        }]
        msg = 'Replace tag config items error'
        self.assertListEqual(received, expected, msg)
Example #5
0
    def test_parse_delete_attributes(self):
        """Parse delete attributes config items.
        """
        # Given a Munger configuration file with target xpath expression
        conf_file = os.path.join(self._conf_dir, 'baip-munger-update-attr.xml')
        xpathgen = baip_munger.XpathGen(conf_file)
        xpath = "//table[@class='TableBAHeaderRow']/thead/tr/td/p"
        section = xpathgen.root.xpath('//Doc/Section')

        # when I parse a sectionDeleteAttribute configuration element
        # section[0] is the "sectionDeleteAttribute" element
        received = xpathgen._parse_delete_attributes(xpath, section[0])

        # then I should receive a list of dictionary structures of the
        # form
        # [{'xpath': '<xpath_expr>',
        #   'attribute': '<attr_name>'}]
        expected = [
            {
                'xpath': "//table[@class='TableBAHeaderRow']/thead/tr/td/p",
                'attribute': 'class',
            },
        ]
        msg = 'Delete attribute config items error'
        self.assertListEqual(received, expected, msg)
Example #6
0
    def test_parse_configuration_no_config(self):
        """Parse configuration: no config defined.
        """
        # Given a base Munger configuration instance
        xpathgen = baip_munger.XpathGen()

        # when I attempt to parse the configuration
        # then I should receive an exception
        self.assertRaises(baip_munger.exception.MungerConfigError,
                          xpathgen.parse_configuration)
Example #7
0
    def test_config_file(self):
        """Attempt config parse file.
        """
        # Given I create an XpathGen() object
        xpath_gen = baip_munger.XpathGen()

        # and a valid config file has been defined
        xpath_gen.root = self._conf_path

        # then I should receive a lxml.etree.ElementTree object
        expected = xpath_gen.root
        msg = 'Valid config file assignment should produce an ElementTree'
        self.assertIsInstance(expected, lxml.etree._ElementTree, msg)
Example #8
0
    def test_extract_xpath_no_conf_file(self):
        """Extract XPath expressions: no configuration file.
        """
        # Given an undefined Munger configuration file.
        conf_file = None

        # when I attempt to extract the XPath expressions
        xpathgen = baip_munger.XpathGen(conf_file)
        received = xpathgen.extract_xpath()

        # then I should received an empty list
        expected = []
        msg = 'XPath expression extraction should produce empty list'
        self.assertListEqual(received, expected, msg)
Example #9
0
    def test_missing_config_file(self):
        """Attempt config parse: missing config file.
        """
        # Given I create an XpathGen() object
        xpath_gen = baip_munger.XpathGen()

        # and the config file provided is missing
        conf_file_obj = tempfile.NamedTemporaryFile()
        conf_file = conf_file_obj.name
        conf_file_obj.close()

        # when I attempt to parse the configuration
        # then I should receive an exception
        self.assertRaises(baip_munger.exception.MungerConfigError,
                          xpath_gen.root, conf_file)
Example #10
0
    def test_extract_xpath_remove(self):
        """Extract XPath sections to remove from configuration.
        """
        # Given a Munger configuration file with a single tag name of
        # "table" under the "startSection" element
        conf_file = os.path.join(self._conf_dir, 'baip-munger-table-only.xml')
        conf_element_tree = lxml.etree.parse(conf_file)

        # when I parse a sectionRemover configuration element
        xpathgen = baip_munger.XpathGen()
        received = xpathgen._extract_xpath_remove(conf_element_tree)

        # then I should receive a list of tuple structures of the form
        # [('d', '//table')]
        expected = [('d', '//table')]
        msg = 'Section remove XPath expressions list error'
        self.assertListEqual(received, expected, msg)
Example #11
0
    def test_munge_unordered_list(self):
        """Munge a file: unordered list.
        """
        # Given a file to munge
        test_file = 'unordered_source.html'
        munge_infile = os.path.join(self._test_dir, test_file)

        # and a target munged file
        temp_dir = tempfile.mkdtemp()
        munge_outfile = os.path.join(temp_dir, test_file)

        # and a set of munging actions with ordered list context
        config_file = os.path.join(self._test_dir,
                                   'baip-munger-unordered-list.xml')
        conf = baip_munger.XpathGen(config_file)
        actions = conf.parse_configuration()

        # when I perform a munge action
        munger = baip_munger.Munger()
        received = munger.munge(actions, munge_infile, munge_outfile)

        # then the munge should occur without error
        msg = 'Munger UI munge should return True'
        self.assertTrue(received, msg)

        # and the munged file deposited to the Munger target directory
        msg = 'Munged target file not created'
        self.assertTrue(os.path.exists(munge_outfile), msg)

        # the resultant HTML should present with modified tag text
        result_file = 'unordered.html'
        result_fh = open(os.path.join(self._results_dir, result_file))
        expected = result_fh.read().rstrip()
        result_fh.close()
        msg = 'Unordered list generation error'
        received = munger.dump_root()
        self.assertEqual(received, expected, msg)

        # Clean up
        remove_files(get_directory_files_list(temp_dir))
        os.removedirs(temp_dir)
Example #12
0
    def test_parse_insert_tag(self):
        """Parse insert tag config items.
        """
        # Given a Munger configuration file with target xpath expression
        conf_file = os.path.join(self._conf_dir, 'baip-munger-update-attr.xml')
        xpathgen = baip_munger.XpathGen(conf_file)
        xpath = "//p[@class='MsoListBullet']"
        section = xpathgen.root.xpath('//Doc/Section')

        # when I parse a sectionInsertTag configuration element
        # section[6] is the "sectionInsertTag" element
        received = xpathgen._parse_insert_tag(xpath, section[6])

        # then I should receive a list of dictionary structures of the
        # form
        # [{'xpath': '<xpath_expr>',
        #   'new_tag': '<new_tag>'}]
        expected = [{
            'xpath': "//p[@class='MsoListBullet']",
            'new_tag': 'ul',
        }]
        msg = 'Insert tag config items error'
        self.assertListEqual(received, expected, msg)
Example #13
0
    def test_parse_strip_chars(self):
        """Parse strip characters config items.
        """
        # Given a Munger configuration file with target xpath expression
        conf_file = os.path.join(self._conf_dir, 'baip-munger-update-attr.xml')
        xpathgen = baip_munger.XpathGen(conf_file)
        xpath = "//p[@class='MsoListBullet']"
        section = xpathgen.root.xpath('//Doc/Section')

        # when I parse a sectionStripChars configuration element
        # section[4] is the "sectionStripChars" element
        received = xpathgen._parse_strip_chars(xpath, section[4])

        # then I should receive a list of dictionary structures of the
        # form
        # [{'xpath': '<xpath_expr>',
        #   'chars': '<chars>'}]
        expected = [{
            'xpath': "//p[@class='MsoListBullet']",
            'chars': u'\xa0\xb7 ',
        }]
        msg = 'Strip chars config items error'
        self.assertListEqual(received, expected, msg)
Example #14
0
"""
import flask
from flask.ext.autoindex import AutoIndex
from flask.ext.silk import Silk

from logga.log import log
import baip_munger
import baip_munger.exception

app = flask.Flask(__name__)
silk = Silk(app)
app.config.from_object('config')

# Load the Munger config.
try:
    conf_file = app.config['MUNGER_CONF_FILE']
    conf = baip_munger.XpathGen(conf_file=conf_file)
    app.config['MUNGER_ACTIONS'] = conf.parse_configuration()
except baip_munger.exception.MungerConfigError as e:
    log.error(str(e))

staging_index = AutoIndex(app,
                          browse_root=app.config['STAGING_DIR'],
                          add_url_rules=False)

ready_index = AutoIndex(app,
                        browse_root=app.config['READY_DIR'],
                        add_url_rules=False)

from baip_munger_ui import views
Example #15
0
    def test_parse_configuration(self):
        """Parse configuration.
        """
        # Given a Munger configuration file with target xpath expression
        conf_file = os.path.join(self._conf_dir, 'baip-munger-update-attr.xml')
        xpathgen = baip_munger.XpathGen(conf_file)

        # when I parse a sectionDeleteAttribute configuration element
        received = xpathgen.parse_configuration()

        # then I should receive a list of dictionary structures of the
        # form
        # [{'xpath': '<xpath_expr>',
        #   'attribute': '<attr_name>'},
        #  {...}]
        expected = {
            'attributes': [
                {
                    'xpath':
                    "//table[@class='TableBAHeaderRow']/thead/tr/td/p",
                    'attribute': 'class',
                },
                {
                    'xpath':
                    ("//table[@class='%s']/thead/tr/td/p[@class='%s']" %
                     ('TableBAHeaderRow', 'TableHeading')),
                    'attribute':
                    'class',
                    'value':
                    'TableText',
                    'old_value':
                    'TableHeading',
                },
                {
                    'xpath':
                    ("//table[@class='%s']/thead/tr/td/p[@class='%s']" %
                     ('TableBAHeaderRow', 'TableHeading')),
                    'attribute':
                    'style',
                    'value':
                    'margin-bottom:1.0pt',
                    'add':
                    True,
                },
                {
                    'xpath': "//table[@class='TableBAHeaderRow']/thead/tr/td",
                    'attribute': 'width',
                    'value': '68',
                    'add': True,
                },
                {
                    'xpath': "//table[@class='TableBAHeaderRow']/thead/tr/td",
                    'attribute': 'nowrap',
                    'add': True,
                },
                {
                    'xpath': ("//p[@class='%s']/span[@style='%s']" %
                              ('MsoBodyText', 'font-family:Symbol')),
                    'attribute':
                    'class',
                    'value':
                    'MsoListBullet',
                    'old_value':
                    'MsoBodyText',
                },
            ],
            'strip_chars': [{
                'xpath': "//p[@class='MsoListBullet']",
                'chars': u'\xa0\xb7 '
            }],
            'replace_tags': [{
                'xpath': "//ul/p[@class='MsoListBullet']",
                'new_tag_attributes': [('class', 'MsoListBullet')],
                'new_tag': 'li'
            }],
            'insert_tags': [{
                'xpath': "//p[@class='MsoListBullet']",
                'new_tag': 'ul'
            }],
        }
        msg = 'Config item error'
        self.assertDictEqual(received, expected, msg)
Example #16
0
 def test_init(self):
     """Initialise a baip_munger.XpathGen()
     """
     munger = baip_munger.XpathGen()
     msg = 'Object is not a baip_munger.XpathGen'
     self.assertIsInstance(munger, baip_munger.XpathGen, msg)