Skip to content

Instantly share code, notes, and snippets.

@drewgillson
Created November 20, 2019 21:22
Show Gist options
  • Save drewgillson/e243e356c494c38d8b22ef49d4aee893 to your computer and use it in GitHub Desktop.
Save drewgillson/e243e356c494c38d8b22ef49d4aee893 to your computer and use it in GitHub Desktop.
This Python script automatically adds labels and descriptions saved in a CSV file to fields within LookML project files.
import csv
import argparse
import lkml
import os
"""
This script automatically adds labels and descriptions saved in a CSV file to fields within LookML project files.
Warning! Since this script doesn't check for fully-scoped field names, if you specify an update to a field with
a common name, like "count", every instance of a field called count in your LookML project will be updated with
your new label and description. This is probably not what you want.
The purpose of this script is to update uniquely-named fields in bulk, from a list that originates from a master
data management or data governance system. In this scenario, duplicate names are not usually found in a project.
Usage: python main.py --filename=fields_to_update.csv --directory=/lookml_project_dir/
Example contents of 'fields_to_update.csv':
field,new label,new description
super_industry_name,"Super Industry Name","The super industry name yo"
parent_aggregate_merchant_id2,"Parent Aggregate Merchant ID","The merchant ID of the parent aggregate"
"""
parser = argparse.ArgumentParser()
parser.add_argument("-f","--filename", help="Fully-qualified path to a CSV file containing three columns: Field Name, Label, Description",type=str)
parser.add_argument("-d","--directory", help="Path to a directory containing LookML project files",type=str)
args = parser.parse_args()
def walk_directory(dir_name, field_to_update):
"""Walk the directory specified in the command line argument recursively to find LookML files to parse"""
dir_name = os.path.abspath(dir_name)
for f in os.listdir(dir_name):
f = os.path.join(dir_name, f)
baseName, ext = os.path.splitext(f)
if ext == '.lkml':
update_fields(f, field_to_update)
elif os.path.isdir(f):
walk_directory(os.path.join(dir_name, f), field_to_update)
def update_fields(filename, field_to_update):
"""Apply label and description from the CSV file to a field in a LookML file that has a matching field name"""
with open(filename, 'r') as file:
try:
parsed = lkml.load(file)
except SyntaxError:
parsed = dict()
for key in parsed:
if key == 'views':
for idx, view in enumerate(parsed[key]):
fields = list()
try:
fields = fields + parsed['views'][idx]['dimension_groups']
except KeyError:
pass
try:
fields = fields + parsed['views'][idx]['dimensions']
except KeyError:
pass
try:
fields = fields + parsed['views'][idx]['measures']
except KeyError:
pass
for field in fields:
if field['name'] == field_to_update[0]:
field['label'] = field_to_update[1]
field['description'] = field_to_update[2]
print(field)
with open(filename, 'w+') as file:
lkml.dump(parsed, file)
if __name__ == "__main__":
f = open(args.filename, 'r', encoding='utf-8-sig')
csv_reader = csv.reader(f, delimiter=',')
for field_to_update in csv_reader:
walk_directory(args.directory, field_to_update)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment