Skip to content

Commit

Permalink
updating examples - addresses PR #1345
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlooker committed Feb 27, 2024
1 parent a2ff13b commit a9c7e96
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,21 +211,21 @@ The following steps assume deployment using the Google Cloud UI Console.

```python
import looker_sdk
from looker_sdk import models40
from looker_sdk import models40 as models

config_file = "looker.ini"
sdk = looker_sdk.init40(config_file)

def restore_soft_delete_dashboard(dashboard_id):
dashboard = models40.WriteDashboard(deleted=False)
dashboard = models.WriteDashboard(deleted=False)
try:
sdk.update_dashboard(str(dashboard_id), body=dashboard)
print(f"Successfully restored dashboard {dashboard_id}")
except Exception as e:
print(f"Error: {e}")

def restore_soft_delete_look(look_id):
look = models40.WriteLookWithQuery(deleted=False)
look = models.WriteLookWithQuery(deleted=False)
try:
sdk.update_look(str(look_id), body=look)
print(f"Successfully restored look {look_id}")
Expand Down
20 changes: 10 additions & 10 deletions examples/python/cloud-function-content-cleanup-automation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
- Update NOTIFICATION_EMAIL_ADDRESS (email address for content deletion notification).
- Toggle dry run of automation off/on.
Last modified: March 2023
Last modified: Feb 27 2024
"""

import looker_sdk
from looker_sdk import models40
from looker_sdk import models40 as models
from looker_sdk import error
from google.cloud import storage
from google.cloud import exceptions
Expand Down Expand Up @@ -85,7 +85,7 @@ def get_unused_content_query_id(days: int):
""" Get a re-useable query ID for a System Activity query which returns all content that hasn't been used in at least 90 (default) days.
This query ID can be used to run the query and send a schedule with the query's results.
"""
unused_content_query = models40.WriteQuery(
unused_content_query = models.WriteQuery(
model="system__activity",
view="content_usage",
fields=[
Expand Down Expand Up @@ -128,7 +128,7 @@ def get_deleted_content_query_id(days: int):
""" Get a re-usable query ID for a System Activity query which returns all content that's been soft deleted for 90+ (default) days.
This query ID can be used to run the query and send a schedule with the query's results.
"""
deleted_query = models40.WriteQuery(
deleted_query = models.WriteQuery(
model="system__activity",
view="content_usage",
fields=[
Expand Down Expand Up @@ -180,7 +180,7 @@ def send_content_notification(query_id: str, delete_type: str, address: str):
"""
created_date = datetime.today().strftime('%Y-%m-%d')

scheduled_plan_destination_body = models40.ScheduledPlanDestination(
scheduled_plan_destination_body = models.ScheduledPlanDestination(
format="csv",
type="email",
address=address,
Expand All @@ -189,7 +189,7 @@ def send_content_notification(query_id: str, delete_type: str, address: str):
apply_formatting=False,
apply_vis=False
)
unused_content_notification = models40.WriteScheduledPlan(
unused_content_notification = models.WriteScheduledPlan(
name=f"[Looker Automation] {delete_type.capitalize()} deleted content ({created_date}).",
query_id=query_id,
scheduled_plan_destination=[
Expand Down Expand Up @@ -230,8 +230,8 @@ def get_look_ids(content: list):
def soft_delete_dashboard(dashboard_id: str):
""" Soft delete the given dashboard. """
# todo: to toggle off safe mode and soft delete dashboards, comment out `deleted=False`` line and uncomment `deleted=True` line
dashboard = models40.WriteDashboard(deleted=False)
# dashboard = models40.WriteDashboard(deleted=True)
dashboard = models.WriteDashboard(deleted=False)
# dashboard = models.WriteDashboard(deleted=True)
try:
sdk.update_dashboard(dashboard_id, body=dashboard)
print(f"Successfully soft deleted dashboard: {dashboard_id}")
Expand All @@ -242,8 +242,8 @@ def soft_delete_dashboard(dashboard_id: str):
def soft_delete_look(look_id: str):
""" Soft delete the given look. """
# todo: to toggle off safe mode and soft delete Looks, comment out `deleted=False`` line and uncomment `deleted=True` line
look = models40.WriteLookWithQuery(deleted=False)
# look = models40.WriteLookWithQuery(deleted=True)
look = models.WriteLookWithQuery(deleted=False)
# look = models.WriteLookWithQuery(deleted=True)
try:
sdk.update_look(look_id, body=look)
print(f"Successfully soft deleted Look: {look_id}")
Expand Down
2 changes: 1 addition & 1 deletion examples/python/content_validator_comparison.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import looker_sdk
from looker_sdk import models
from looker_sdk import models40 as models
import configparser
import hashlib
import csv
Expand Down
2 changes: 1 addition & 1 deletion examples/python/create_db_connections.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import looker_sdk
from looker_sdk import models
from looker_sdk import models40 as models
import base64

with open("credentials_file.json", "rb") as f:
Expand Down
5 changes: 2 additions & 3 deletions examples/python/download_dashboard_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$ python download_dashboard_pdf.py "A Test Dashboard" '{"filter1": "value1, value2", "filter2": "value3"}'
$ python download_dashboard_pdf.py "A Test Dashboard" {} "single_column"
Last modified: August 25, 2021
Last modified: Feb 27 2024
"""

import json
Expand All @@ -19,11 +19,10 @@
from typing import cast, Dict, Optional

import looker_sdk
from looker_sdk import models
from looker_sdk import models40 as models

sdk = looker_sdk.init40("../../looker.ini")


def main():
dashboard_title = sys.argv[1] if len(sys.argv) > 1 else ""
filters = json.loads(sys.argv[2]) if len(sys.argv) > 2 else None
Expand Down
4 changes: 2 additions & 2 deletions examples/python/download_look.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
$ python download_look.py "A simple look"
$ python download_look.py "A simple look" 545 842 png
Last modified: August 25, 2021
Last modified: Feb 27 2024
"""

import sys
import textwrap
import time

import looker_sdk
from looker_sdk import models
from looker_sdk import models40 as models

sdk = looker_sdk.init40("../../looker.ini")

Expand Down
2 changes: 1 addition & 1 deletion examples/python/download_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time

import looker_sdk
from looker_sdk import models
from looker_sdk import models40 as models
from looker_sdk.rtl import transport

class MyTransportOptions(transport.PTransportSettings): timeout = 300
Expand Down
3 changes: 1 addition & 2 deletions examples/python/kill_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
Authors: Lan
Last modified: July 18, 2021
Last modified: Feb 27 2024
"""

import looker_sdk
from looker_sdk import models40
sdk = looker_sdk.init40(config_file='../looker.ini', section='Looker')


Expand Down
2 changes: 1 addition & 1 deletion examples/python/logout_all_users.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import cast, Sequence

import looker_sdk
from looker_sdk import models

from looker_sdk import models40 as models

sdk = looker_sdk.init40("../../looker.ini")

Expand Down
50 changes: 25 additions & 25 deletions examples/python/manage_schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
Authors: Lan
Last modified: July 18, 2021
Last modified: Feb 27 2024
"""

import looker_sdk
from looker_sdk import models40
from looker_sdk import models40 as models
sdk = looker_sdk.init40(config_file='../looker.ini', section='Looker')


Expand All @@ -41,25 +41,25 @@ def get_schedules(id, content, user_id=None, all_users=True):

def resume_schedules(id, content, enabled, user_id=None, all_users=True):

""" Pause or resume all schedules of a Look, or a dashboard
Args:
id: id of the Looker content containing schedules
content(str): 'look', 'dashboard', or 'lookml_dashboard'
enabled (bool): set "True" to resume schedule, or "False" to pause schedule
Notes: Schedules with "enabled = False" will disappear from Admin > Schedules in Looker UI but
their data can be retrived in Looker's System Activity. Once schedules are resumed with "enabled = True",
they will be sent once and reappear in Admin > Schedules
"""
""" Pause or resume all schedules of a Look, or a dashboard
Args:
id: id of the Looker content containing schedules
content(str): 'look', 'dashboard', or 'lookml_dashboard'
enabled (bool): set "True" to resume schedule, or "False" to pause schedule
Notes: Schedules with "enabled = False" will disappear from Admin > Schedules in Looker UI but
their data can be retrived in Looker's System Activity. Once schedules are resumed with "enabled = True",
they will be sent once and reappear in Admin > Schedules
"""

"Get all schedules of a Looker content"
schedules = get_schedules(id=id, content=content)

for i in range(0, len(schedules)):
sdk.update_scheduled_plan(
scheduled_plan_id=schedules[i]['id'],
body=models40.WriteScheduledPlan(
body=models.WriteScheduledPlan(
enabled = enabled
))

Expand All @@ -70,16 +70,16 @@ def resume_schedules(id, content, enabled, user_id=None, all_users=True):

def copy_schedules(from_id, to_id, content, user_id=None, all_users=True):

""" Copy schedules from one Looker content to another content.
This script has only been tested for content of the same type (i.e.: look to look, dashboard to dashboard)
""" Copy schedules from one Looker content to another content.
This script has only been tested for content of the same type (i.e.: look to look, dashboard to dashboard)
Args:
from_id: id of the Looker content containing schedules
to_id: id of the Looker content which schedules will be copied to
content(str): 'look', 'dashboard', or 'lookml_dashboard'
user_id(int, optional): If user_id is None then schedules owned by the user calling the API will be returned
all_users(bool, optional): If all_user is True then return schedules owned by all users
"""
Args:
from_id: id of the Looker content containing schedules
to_id: id of the Looker content which schedules will be copied to
content(str): 'look', 'dashboard', or 'lookml_dashboard'
user_id(int, optional): If user_id is None then schedules owned by the user calling the API will be returned
all_users(bool, optional): If all_user is True then return schedules owned by all users
"""

"Get all schedules of a Looker content"
schedules = get_schedules(id=from_id, content=content, user_id=user_id, all_users=all_users)
Expand All @@ -88,7 +88,7 @@ def copy_schedules(from_id, to_id, content, user_id=None, all_users=True):
for i in range(0, len(schedules)):

# Write the base schedule plans with all required fields
body = models40.WriteScheduledPlan(
body = models.WriteScheduledPlan(

# Required fields for all content type
name = schedules[i]['name'],
Expand All @@ -110,7 +110,7 @@ def copy_schedules(from_id, to_id, content, user_id=None, all_users=True):
elif content == 'lookml_dashboard':
body['lookml_dashboard_id'] = to_id

"""Additional parameters can be added in the models40.WriteScheduledPlan() method for 'body',
"""Additional parameters can be added in the models.WriteScheduledPlan() method for 'body',
or through Python's dictionary syntax: body[parameter] = value """


Expand Down
2 changes: 1 addition & 1 deletion examples/python/run_look_with_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import cast, Dict, List, Union

import looker_sdk
from looker_sdk import models, error
from looker_sdk import models40 as models, error

sdk = looker_sdk.init40("../../looker.ini")

Expand Down
4 changes: 2 additions & 2 deletions examples/python/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
Notes: Connections to Looker's internal database cannot be tested.
Last modified: August 25, 2021
Last modified: Feb 27 2024
"""

from functools import reduce
import sys
from typing import cast, MutableSequence, Sequence

import looker_sdk
from looker_sdk import models
from looker_sdk import models40 as models

sdk = looker_sdk.init40("../../looker.ini")

Expand Down

0 comments on commit a9c7e96

Please sign in to comment.