Skip to content

Commit

Permalink
Merge pull request #84 from jaybythebay/use-importlib
Browse files Browse the repository at this point in the history
Use importlib
  • Loading branch information
JustinGrilli authored May 13, 2024
2 parents 6ddb312 + d5e1b5b commit 41b5b8a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
7 changes: 3 additions & 4 deletions tableau_utilities/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import shutil
from argparse import RawTextHelpFormatter
import yaml
import pkg_resources
import pkgutil
import importlib.metadata

import tableau_utilities.tableau_server.tableau_server as ts
Expand All @@ -18,7 +16,7 @@
from tableau_utilities.scripts.datasource import datasource
from tableau_utilities.scripts.csv_config import csv_config

__version__ = pkg_resources.require("tableau_utilities")[0].version
__version__ = importlib.metadata.version('tableau_utilities')

parser = argparse.ArgumentParser(
prog='tableau_utilities',
Expand All @@ -27,7 +25,7 @@
'-Manage configurations to edit datasource metadata',
formatter_class=RawTextHelpFormatter
)
parser.add_argument('--version', action='version', version=f'%(prog)s {__version__}',
parser.add_argument('-v', '--version', action='version', version=f'%(prog)s {__version__}',
help='Print the current version of the CLI')
parser.add_argument('-d', '--debugging_logs', action='store_true',
help='Print detailed logging to the console to debug CLI')
Expand Down Expand Up @@ -164,6 +162,7 @@
parser_datasource.add_argument('-fe', '--filter_extract',
help='Deletes data from the extract based on the condition string provided. '
"""E.g. "CREATED_AT" < '1/1/2024'""")
parser_datasource.add_argument('-ci', '--column_init', action='store_true', help="Adds Columns from all Metadata Records, if they don't already exist.")
parser_datasource.set_defaults(func=datasource)

# GENERATE CONFIG
Expand Down
61 changes: 54 additions & 7 deletions tableau_utilities/scripts/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@
from tableau_utilities.tableau_server.tableau_server import TableauServer


def create_column(name: str, persona: dict):
""" Creates the tfo column object with the minimum required fields to add a column
Args:
name: The name for the Column, and local_name of the Metadata Record.
persona: A dictionary showing the role, datatype, and role type
Returns:
A tfo column object
"""

column = tfo.Column(
name=name,
role=persona['role'],
datatype=persona['datatype'],
type=persona['role_type'],
)

return column

def datasource(args, server=None):
""" Updates a Tableau Datasource locally
Expand Down Expand Up @@ -45,6 +66,7 @@ def datasource(args, server=None):
calculation = args.calculation
remote_name = args.remote_name
list_objects = args.list.title() if args.list else None
column_init = args.column_init

# Datasource Connection Args
conn_type = args.conn_type
Expand Down Expand Up @@ -107,6 +129,7 @@ def datasource(args, server=None):
print(f' {symbol.arrow_r} '
f'{color.fg_yellow}caption:{color.reset} {c.caption} '
f'{color.fg_yellow}local-name:{color.reset} {c.name} '
f'{color.fg_yellow}remote-name:{color.reset} {c.name} '
f'{color.fg_yellow}persona:{color.reset} {get_persona_by_attribs(c.role, c.type, c.datatype)}')
if list_objects == 'Folders':
for f in ds.folders_common.folder:
Expand All @@ -121,6 +144,31 @@ def datasource(args, server=None):
for c in ds.connection.named_connections:
print(f' {symbol.arrow_r} {c.connection.dict()}')

# Column Init - Add columns for any column in Metadata records but not in columns
if column_init:
columns_to_add = [
m for m in ds.connection.metadata_records
if m.local_name not in [c.name for c in ds.columns]
]
print(f'{color.fg_yellow}Adding missing columns from Metadata Records:{color.reset} '
f'{[m.local_name for m in columns_to_add]}')

for m in columns_to_add:
if debugging_logs:
print(f'{color.fg_magenta}Metadata Record -> {m.local_name}:{color.reset} {m}')

persona = get_persona_by_metadata_local_type(m.local_type)
persona_dict = personas.get(persona, {})
if debugging_logs:
print(f' - {color.fg_blue}Persona -> {persona}:{color.reset} {persona_dict}')

column = create_column(m.local_name, persona_dict)

if debugging_logs:
print(f' - {color.fg_cyan}Creating Column -> {column.name}:{color.reset} {column.dict()}')
ds.enforce_column(column, remote_name=m.remote_name)


# Add / modify a specified column
if column_name and not delete:
# Column name needs to be enclosed in brackets
Expand All @@ -136,12 +184,7 @@ def datasource(args, server=None):
if not persona:
raise Exception('Column does not exist, and more args are need to add a new column.\n'
f'Minimum required args: {color.fg_yellow}--column_name --persona{color.reset}')
column = tfo.Column(
name=column_name,
role=persona['role'],
datatype=persona['datatype'],
type=persona['role_type'],
)
column = create_column(column_name, persona)
print(f'{color.fg_cyan}Creating new column for {column_name}:{color.reset} {column.dict()}')
else:
print(f'{color.fg_cyan}Updating existing column:{color.reset}\n {column.dict()}')
Expand All @@ -152,6 +195,10 @@ def datasource(args, server=None):
column.datatype = persona.get('datatype') or column.datatype
column.desc = desc or column.desc
column.calculation = calculation or column.calculation

if debugging_logs:
print(f'{color.fg_yellow}column:{color.reset}{column}')

ds.enforce_column(column, remote_name=remote_name, folder_name=folder_name)

# Add a folder if it was specified and does not exist already
Expand Down Expand Up @@ -184,7 +231,7 @@ def datasource(args, server=None):
ds.connection.update(connection)

# Save the datasource if an edit may have happened
if column_name or folder_name or delete or enforce_connection or empty_extract:
if column_name or folder_name or delete or enforce_connection or empty_extract or column_init:
start = time()
print(f'{color.fg_cyan}...Saving datasource changes...{color.reset}')
ds.save()
Expand Down

0 comments on commit 41b5b8a

Please sign in to comment.