def update_markdown(file_path, record): # Key (on record type) functions that take actions on a table stub section_methods = { MetricValue: _update_metric, Watermark: _update_watermark } sections = sections_from_markdown(file_path) # The table metadata record has both a header and column details. Add # custom logic to handle both. if type(record) == metadata_model_whale.TableMetadata: table_details = re.split(COLUMN_DETAILS_DELIMITER, record.markdown_blob) header = table_details[0] column_details = "".join(table_details[1:]) sections[HEADER_SECTION] = header # Since we split on COLUMN_DETAILS_DELIMITER, reintroduce it sections[COLUMN_DETAILS_SECTION] = (COLUMN_DETAILS_DELIMITER + column_details + "\n") else: section_method = section_methods[type(record)] sections = section_method(sections, record) if hasattr(record, "record"): record.record() new_file_text = markdown_from_sections(sections) safe_write(file_path, new_file_text)
def update_markdown(file_path, record): # Key (on record type) functions that take actions on a table stub section_methods = { MetricValue: _update_metric, Watermark: _update_watermark, metadata_model_whale.TableMetadata: _update_table_metadata, metadata_model_amundsen.TableMetadata: _update_table_metadata, } sections = sections_from_markdown(file_path) # The table metadata record has both a header and column details. Add # custom logic to handle both. section_method = section_methods[type(record)] sections = section_method(sections, record) new_file_text = markdown_from_sections(sections) safe_write(file_path, new_file_text)
def execute_markdown_sql_blocks(filepath: str) -> str: """ Executes all sql, denoted by ```sql, within the file `filepath` if EXECUTION_FLAG is within. Replace the EXECUTION_FLAG with the results. """ database, _, _, _ = get_table_info_from_path(filepath) sections = sections_from_markdown(filepath) ugc_blob = sections[UGC_SECTION] def run_and_append_results(sql, warehouse_name=None) -> str: if EXECUTION_FLAG in sql: results = run(sql, warehouse_name=warehouse_name) sql_with_result = embed_results_as_comment(sql, results) return sql_with_result else: return sql new_markdown_blob = markdown_from_sections(sections) if EXECUTION_FLAG in ugc_blob: ugc_blob = find_blocks_and_process( ugc_blob, run_and_append_results, function_kwargs={"warehouse_name": database}, ) sections[UGC_SECTION] = ugc_blob new_markdown_blob = markdown_from_sections(sections) with open(filepath, "w") as f: f.write(new_markdown_blob) elif EXECUTION_FLAG.strip() in ugc_blob: LOGGER.warning(f"{EXECUTION_FLAG.strip()} must be on its own line.") return new_markdown_blob
def _get_metrics_queries_from_table_stub_path(self, table_stub_path): sections = sections_from_markdown(table_stub_path) ugc_sections = parse_ugc(sections["ugc"]) return ugc_sections[DEFINED_METRICS_SECTION]