|
10 | 10 | import os
|
11 | 11 | from tableauserverclient import ConnectionCredentials, ConnectionItem
|
12 | 12 | from tableaudocumentapi import Workbook
|
13 |
| - |
| 13 | +from dateutil.parser import parse |
| 14 | +import platform |
| 15 | +import datetime |
| 16 | +import time |
14 | 17 |
|
15 | 18 | ############################################################
|
16 | 19 | # Step 3) Use a database list (in CSV), loop thru and
|
@@ -54,85 +57,106 @@ def main():
|
54 | 57 | count = 1
|
55 | 58 | overwrite_true = TSC.Server.PublishMode.Overwrite
|
56 | 59 |
|
57 |
| - |
| 60 | + # Open the workbook |
| 61 | + source = "Source" |
| 62 | + zip = "Zip" |
| 63 | + export = "Export" |
| 64 | + columns = ['Workbook', 'Format', 'Project', 'Branch', 'Directory', 'PublishDate','ModifiedDate' ] |
58 | 65 |
|
59 | 66 | with server.auth.sign_in(tableau_auth):
|
60 |
| - with open('databases.csv') as csvfile: |
| 67 | + with open('databases.csv',mode='r') as csvfile: |
61 | 68 | total = sum(1 for line in open('databases.csv'))
|
62 |
| - print("Total file count: " + str(total)) |
63 |
| - databases = csv.DictReader(csvfile, delimiter=',', quotechar='"') |
64 |
| - for row in databases: |
65 |
| - |
66 |
| - print ("working on item: "+ str(count)+" \n") |
67 |
| - # Open the workbook |
68 |
| - source = "Source" |
69 |
| - zip = "Zip" |
70 |
| - export = "Export" |
71 |
| - count = count +1 |
72 |
| - |
73 |
| - if(row['Format'] == ".twbx"): |
74 |
| - originaltableauworkbook = zipfile.ZipFile(os.path.join(source, row['Workbook'] + ".twbx")) |
75 |
| - originaltableauworkbook.extractall(zip) |
76 |
| - test = os.listdir(zip) |
77 |
| - |
78 |
| - for item in test: |
79 |
| - if item.endswith(".twb") : |
80 |
| - sourceWB = Workbook(os.path.join(zip, item)) |
| 69 | + print("Total file count: " + str(total-1)) |
| 70 | + databases = csv.DictReader(csvfile, delimiter=',', quotechar='"') |
| 71 | + outfile = open('databases_new.csv',mode='wb') |
| 72 | + writer = csv.DictWriter(outfile,fieldnames=columns) |
| 73 | + writer.writeheader() |
| 74 | + for row in databases: |
| 75 | + if any(row): |
| 76 | + print ("working on item: "+ str(count)+" \n") |
| 77 | + mtime = parse(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.path.getmtime(os.path.join(source, row['Workbook'] + ".twbx"))))) |
| 78 | + ptime = parse(row['PublishDate']) |
| 79 | + count = count +1 |
| 80 | + if(mtime>ptime): |
| 81 | + if(row['Format'] == ".twbx"): |
| 82 | + originaltableauworkbook = zipfile.ZipFile(os.path.join(source, row['Workbook'] + ".twbx")) |
| 83 | + originaltableauworkbook.extractall(zip) |
| 84 | + test = os.listdir(zip) |
| 85 | + |
| 86 | + for item in test: |
| 87 | + if item.endswith(".twb") : |
| 88 | + sourceWB = Workbook(os.path.join(zip, item)) |
| 89 | + for datasource in reversed(sourceWB.datasources): |
| 90 | + # Update the filters |
| 91 | + for children in datasource._datasourceXML._children: |
| 92 | + if "column" in children.attrib and "class" in children.attrib: |
| 93 | + if children.attrib["column"] == "[Branch]" and children.attrib["class"] == "categorical": |
| 94 | + for subchildren in children._children: |
| 95 | + if "member" in subchildren.attrib: |
| 96 | + subchildren.attrib["member"] = '"' + row['Branch'] + '"' |
| 97 | + |
| 98 | + |
| 99 | + # Save our newly created workbook with the new file name |
| 100 | + outputpath = os.path.join(export, row['Workbook'] + ' - ' + row['Branch'] + ".twb") |
| 101 | + sourceWB.save_as(outputpath) |
| 102 | + |
| 103 | + if(row['Format'] == ".twbx"): |
| 104 | + z = zipfile.ZipFile(os.path.join(export, row['Workbook'] + ' - ' + row['Branch'] + row['Format']),mode='w',compression=zipfile.ZIP_DEFLATED) |
| 105 | + z.write(os.path.join(export, row['Workbook'] + ' - ' + row['Branch'] + ".twb"),item.replace("Export","")) |
| 106 | + for item in list_files(zip): |
| 107 | + if item.endswith(".twb") != True: |
| 108 | + z.write(item,item.replace("Zip","")) |
| 109 | + |
| 110 | + z.close() |
| 111 | + |
| 112 | + elif (row['Format'] ==".twb"): |
| 113 | + sourceWB = Workbook(os.path.join(source, row['Workbook'] + ".twb")) |
| 114 | + # Update the filters |
81 | 115 | for datasource in reversed(sourceWB.datasources):
|
82 |
| - # Update the filters |
83 |
| - for children in datasource._datasourceXML._children: |
| 116 | + for children in datasource._datasourceTree._root._children: |
84 | 117 | if "column" in children.attrib and "class" in children.attrib:
|
85 | 118 | if children.attrib["column"] == "[Branch]" and children.attrib["class"] == "categorical":
|
86 | 119 | for subchildren in children._children:
|
87 | 120 | if "member" in subchildren.attrib:
|
88 |
| - subchildren.attrib["member"] = '"' + row['Branch'] + '"' |
89 |
| - |
| 121 | + subchildren.attrib["member"] = '"' + row['Branch'] + '"' |
90 | 122 |
|
91 |
| - # Save our newly created workbook with the new file name |
92 |
| - outputpath = os.path.join(export, row['Workbook'] + ' - ' + row['Branch'] + ".twb") |
93 |
| - sourceWB.save_as(outputpath) |
94 |
| - |
95 |
| - if(row['Format'] == ".twbx"): |
96 |
| - z = zipfile.ZipFile(os.path.join(export, row['Workbook'] + ' - ' + row['Branch'] + row['Format']),mode='w',compression=zipfile.ZIP_DEFLATED) |
97 |
| - z.write(os.path.join(export, row['Workbook'] + ' - ' + row['Branch'] + ".twb"),item.replace("Export","")) |
98 |
| - for item in list_files(zip): |
99 |
| - if item.endswith(".twb") != True: |
100 |
| - z.write(item,item.replace("Zip","")) |
101 |
| - |
102 |
| - z.close() |
103 |
| - |
104 |
| - elif (row['Format'] ==".twb"): |
105 |
| - sourceWB = Workbook(os.path.join(source, row['Workbook'] + ".twb")) |
106 |
| - # Update the filters |
107 |
| - for datasource in reversed(sourceWB.datasources): |
108 |
| - for children in datasource._datasourceTree._root._children: |
109 |
| - if "column" in children.attrib and "class" in children.attrib: |
110 |
| - if children.attrib["column"] == "[Branch]" and children.attrib["class"] == "categorical": |
111 |
| - for subchildren in children._children: |
112 |
| - if "member" in subchildren.attrib: |
113 |
| - subchildren.attrib["member"] = '"' + row['Branch'] + '"' |
114 |
| - |
115 |
| - # Save our newly created workbook with the new file name |
116 |
| - outputpath = os.path.join(export, row['Workbook'] + ' - ' + row['Branch'] + ".twb") |
117 |
| - sourceWB.save_as(outputpath) |
118 |
| - |
119 |
| - |
120 |
| - all_projects, pagination_item = server.projects.get() |
121 |
| - default_project = next((project for project in all_projects if project.name == row['Project']), None) |
122 |
| - |
123 |
| - if default_project is not None: |
124 |
| - new_workbook = TSC.WorkbookItem(default_project.id,show_tabs= True) |
125 |
| - if args.as_job: |
126 |
| - new_job = server.workbooks.publish(new_workbook,os.path.join(export, row['Workbook'] + ' - ' + row['Branch'] + row['Format']), overwrite_true) |
127 |
| - print("Workbook published. JOB ID: {0}".format(new_job.id)) |
128 |
| - else: |
129 |
| - new_workbook = server.workbooks.publish(new_workbook, os.path.join(export, row['Workbook'] + ' - ' + row['Branch'] + row['Format']), overwrite_true) |
130 |
| - print("Workbook published. ID: {0}".format(new_workbook.id)) |
131 |
| - else: |
132 |
| - error = "The default project could not be found." |
133 |
| - raise LookupError(error) |
| 123 | + # Save our newly created workbook with the new file name |
| 124 | + outputpath = os.path.join(export, row['Workbook'] + ' - ' + row['Branch'] + ".twb") |
| 125 | + sourceWB.save_as(outputpath) |
| 126 | + |
| 127 | + |
| 128 | + all_projects, pagination_item = server.projects.get() |
| 129 | + default_project = next((project for project in all_projects if project.name == row['Project']), None) |
| 130 | + |
| 131 | + if default_project is not None: |
| 132 | + new_workbook = TSC.WorkbookItem(default_project.id,show_tabs= True) |
| 133 | + if args.as_job: |
| 134 | + new_job = server.workbooks.publish(new_workbook,os.path.join(export, row['Workbook'] + ' - ' + row['Branch'] + row['Format']), overwrite_true) |
| 135 | + row['PublishDate'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 136 | + writer.writerow(row) |
| 137 | + print("Workbook published. JOB ID: {0}".format(new_job.id)) |
| 138 | + else: |
| 139 | + row['PublishDate'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 140 | + writer.writerow(row) |
| 141 | + new_workbook = server.workbooks.publish(new_workbook, os.path.join(export, row['Workbook'] + ' - ' + row['Branch'] + row['Format']), overwrite_true) |
| 142 | + print("Workbook published. ID: {0}".format(new_workbook.id)) |
| 143 | + |
| 144 | + else: |
| 145 | + error = "The default project could not be found." |
| 146 | + raise LookupError(error) |
134 | 147 | print ("Done!!!!!!!!!!!!!!!!!!")
|
135 |
| - |
| 148 | + outfile.close() |
| 149 | + statinfo = os.stat('databases_new.csv') |
| 150 | + if(statinfo.st_size>67): |
| 151 | + os.remove('databases.csv') |
| 152 | + os.rename('databases_new.csv','databases.csv') |
| 153 | + else: |
| 154 | + os.remove('databases_new.csv') |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
136 | 160 |
|
137 | 161 | if __name__ == '__main__':
|
138 | 162 | main()
|
|
0 commit comments