Ejemplo n.º 1
0
    def modify(self, infilepath, outfilepath):
        '''modify the LookML

        Notes:
            default behavior is to match on full path when matching LookML files
            with the definitions source. 
            However, you can configure to match on LookML file basename by setting
            ``"use_basename": true`` in the config

        Args:
            infilepath (str): path to input LookML file
            outfilepath (str): path of updated LookML to wtite to

        Returns:
            nothing. Writes out modified file contents to file

        '''
        modifier = FileModifier(infilepath)

        lookml = LookML(infilepath)

        # get definitions for this file. In some cases, we might not
        # easily know the full path (such as full_auto_updater.sh which
        # uses timestamp in the git clone). Thus, just match on basename
        if 'use_basename' in self.config and self.config['use_basename']:
            logging.info("Matching files based on basename")
            defs = self.definitions[self.definitions.file == os.path.basename(infilepath)]
        else:
            defs = self.definitions[self.definitions.file == infilepath]

        for definition in defs.T.to_dict().values():
            logging.info("Processing %s: %s", definition['type'], definition['name'])

            description, has_key = self.find_description(lookml, definition['type'], definition['name'])

            num_lines = len(description.split("\n"))

            if has_key:
                logging.info("Existing description for %s.%s: '%s'", definition['type'], definition['name'], description)
            else:
                logging.info("No description for %s.%s", definition['type'], definition['name'])

            exepected_description = definition['definition']

            if description != exepected_description:
                if has_key:
                    logging.info("Update needed: %s.%s -> '%s'", definition['type'], definition['name'], exepected_description)
                    logging.info("This is %d line existing description", num_lines)
                else:
                    logging.info("Injection needed: %s.%s -> '%s'", definition['type'], definition['name'], exepected_description)

                modifier.modify(num_lines, definition['type'], definition['name'], exepected_description, has_key)

        modifier.write(outfilepath)
Ejemplo n.º 2
0
def test_write():
    modifier = FileModifier("test/minimal.lkml")
    filename = "test/test_write.lkml"
    if os.path.exists(filename):
        os.remove(filename)

    modifier.write(filename)
    assert os.path.exists(filename)

    original = open("test/minimal.lkml", 'r').readlines()
    written = open(filename, 'r').readlines()

    assert original == written

    if os.path.exists(filename):
        os.remove(filename)