def test_modify2(): modifier = FileModifier("test/minimal_multiline.view.lkml") assert len(modifier.lines) == 19 modifier.modify(6, "dimension", "city_code", "a description", True) assert len(modifier.lines) == 14 assert modifier.lines[ 5] == " description: \"a description\" # programmatically added by LookML modifier\n"
def test_modify1(): modifier = FileModifier("test/minimal.lkml") assert len(modifier.lines) == 13 # add a description to city_code modifier.modify(1, "dimension", "city_code", "a description", False) assert len(modifier.lines) == 14 assert modifier.lines[4] == " description: \"a description\" # programmatically added by LookML modifier\n"
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)