-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_export_schemas.py
More file actions
106 lines (83 loc) · 4.37 KB
/
main_export_schemas.py
File metadata and controls
106 lines (83 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import requests
import json
import os
from methods import AnalyticsUIRequestHelper
from methods import recursively_truncate_enumeration, recursively_get_parameter_names_in_blockquote
from methods import check_for_prefix
# step 1 - get the token
# step 2 - get the project IDs (can we convert IDs to names?)
# step 2 - get the environment names
with open("token.auth", "r+") as f:
token = f.read().strip()
rh = AnalyticsUIRequestHelper("https://services.unity.com", token)
try:
with open("settings.json", "r+") as f:
settings = json.load(f)
org_id = settings["srcOrgId"]
project_id = settings["srcProjectId"]
environment_id = settings["srcEnvId"]
if not project_id or not environment_id:
raise ValueError("Project name and environment name must be set in settings.json")
except FileNotFoundError:
out_str = { "srcOrgId": "",
"srcProjectId": "",
"srcEnvId": "",
"trgtOrgId": "",
"trgtProjectId": "",
"trgtEnvId": ""
}
with open("settings.json", "w") as f:
json.dump(out_str, f, indent=4)
print("settings.json file not found. Created a new one with empty values.")
print("Please fill in the values for trgtOrgId, trgtProjectId, trgtEnvId.")
exit(1)
src_project = rh.get_project_metadata(org_id, project_id)
if not src_project:
print("Source project not found. Please check your settings.json file.")
exit(1)
project_name = src_project.get("name", "Unknown Project")
print(" EXPORT SCHEMAS AS MARKDOWN ")
print("-------------------------------------------------")
print(f"Here's a link to the source project {project_name}:")
print(f"https://cloud.unity.com/home/organizations/{org_id}/projects/{project_id}/environments/{environment_id}")
input("If this is correct, press enter to continue... If not, terminate the script and change the settings.json file.")
print("thanks! beginning...")
newpath = ".\outputs\\"
if not os.path.exists(newpath):
os.makedirs(newpath)
#------------------------------get the source schemas and parameters--------------------
src_schemas = rh.get_schemas(org_id, project_id, environment_id)
if not src_schemas:
print("No schemas found in source project. This probably means your token is expired. Look in the cookie for https://cloud.unity.com/, under 'token'.")
print("Store the latest token in token.auth and re-run the script.")
print("Exiting...")
exit(1)
schema_names = [x["name"] for x in src_schemas ]
src_schemas = {x["name"]: x for x in src_schemas}
# read token from token.auth
#-------------------------------get the target schemas and parameters--------------------
#-------------------------------these already exist, and should not be created--------------------
with (open(f"outputs/{project_name}_{project_id}_schemas.md", "w+", encoding="UTF-8")) as f:
f.write(f"# Schemas for project {project_name} [{project_id}](https://cloud.unity.com/home/organizations/{org_id}/projects/{project_id}/environments/{environment_id})")
f.write("\n\n")
f.write("The following is an output of **all** event schemas for the project, including the standard Unity Analytics events. \n")
for event_name, event in src_schemas.items():
event:dict
# convert the event from GET to POST format
original_event_desc = str.strip(event.get("description", ""))
compiled_schema = json.loads(event["schema"])
compiled_event, list_of_truncated_enums = recursively_truncate_enumeration("",compiled_schema,[])
is_enabled = event.get("isEnabled", True)
basic_output = recursively_get_parameter_names_in_blockquote("", compiled_event)
fancy_output = json.dumps(compiled_schema, indent=4)
f.write(f"## {event_name}\n")
f.write(f"**Description:** {original_event_desc} \n")
f.write(f"**Event Version:** {event.get('version', 'Unknown')} \n")
f.write(f"**Enabled:** {event.get('isEnabled')} \n")
f.write(f"**Restricted:** {event.get('isRestricted', False)} \n")
f.write(f"**Predefined:** {event.get('isPredefined', False)} \n")
f.write("**Parameter summary:**\n")
f.write(basic_output)
f.write("\n\n")
f.write("**Actual Event schema:**\n")
f.write(f"```json\n{fancy_output}\n```\n")