8000 TSC-152 #time 3h enabling showtabs on workbook view · onware/document-api-python@3daff18 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3daff18

Browse files
author
Sam Bao
committed
TSC-152 #time 3h enabling showtabs on workbook view
1 parent a764b5c commit 3daff18

File tree

6 files changed

+111
-22560
lines changed

6 files changed

+111
-22560
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.

samples/filter-and-replicate/Zip/Job Time.twb

Lines changed: 0 additions & 22473 deletions
This file was deleted.
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
Workbook,Format,Project,Branch,Directory
2-
Job Time,.twbx,Operations - Edmonton,Edmonton,/thorpespecialtyservicescorporation/projects/113334/workbooks
3-
Job Time,.twbx,Operations - Abbotsford-Cor,Abbotsford-Cor,/thorpespecialtyservicescorporation/projects/113330/workbooks
4-
Job Time,.twbx,Operations - Abbotsford-Ref,Abbotsford-Ref,/thorpespecialtyservicescorporation/projects/113331/workbooks
5-
Job Time,.twbx,Operations - Birmingham,Birmingham,/thorpespecialtyservicescorporation/projects/113347/workbooks
6-
Job Time,.twbx,Operations - Corpus Christi,Corpus Christi,/thorpespecialtyservicescorporation/projects/113333/workbooks
7-
Job Time,.twbx,Operations - Engineered Products,Engineered Products,/thorpespecialtyservicescorporation/projects/113335/workbooks
8-
Job Time,.twbx,Operations - Flooring,Flooring,/thorpespecialtyservicescorporation/projects/113336/workbooks
9-
Job Time,.twbx,Operations - Harrison,Harrison,/thorpespecialtyservicescorporation/projects/113337/workbooks
10-
Job Time,.twbx,Operations - Houston-Cor,Houston-Cor,/thorpespecialtyservicescorporation/projects/113338/workbooks
11-
Job Time,.twbx,Operations - Houston-Ref,Houston-Ref,/thorpespecialtyservicescorporation/projects/113339/workbooks
12-
Job Time,.twbx,Operations - International,International,/thorpespecialtyservicescorporation/projects/113340/workbooks
13-
Job Time,.twbx,Operations - JT-Beaumont,JT-Beaumont,/thorpespecialtyservicescorporation/projects/113356/workbooks
14-
Job Time,.twbx,Operations - JT-Houston,JT-Houston,/thorpespecialtyservicescorporation/projects/113341/workbooks
15-
Job Time,.twbx,Operations - St. Gabriel-Cor,St. Gabriel-Cor,/thorpespecialtyservicescorporation/projects/113342/workbooks
16-
Job Time,.twbx,Operations - St. Gabriel-Ref,St. Gabriel-Ref,/thorpespecialtyservicescorporation/projects/113343/workbooks
17-
Job Time,.twbx,Operations - Tulsa,Tulsa,/thorpespecialtyservicescorporation/projects/113344/workbooks
1+
Workbook,Format,Project,Branch,Directory,PublishDate,ModifiedDate
2+
Job Time,.twbx,Operations - Edmonton,Edmonton,/thorpespecialtyservicescorporation/projects/113334/workbooks,2018-08-29 15:20:34,
3+
Job Time,.twbx,Operations - Abbotsford-Cor,Abbotsford-Cor,/thorpespecialtyservicescorporation/projects/113330/workbooks,2018-08-29 15:20:53,
4+
Job Time,.twbx,Operations - Abbotsford-Ref,Abbotsford-Ref,/thorpespecialtyservicescorporation/projects/113331/workbooks,2018-08-29 15:21:11,
5+
Job Time,.twbx,Operations - Birmingham,Birmingham,/thorpespecialtyservicescorporation/projects/113347/workbooks,2018-08-29 15:21:27,
6+
Job Time,.twbx,Operations - Corpus Christi,Corpus Christi,/thorpespecialtyservicescorporation/projects/113333/workbooks,2018-08-29 15:21:43,
7+
Job Time,.twbx,Operations - Engineered Products,Engineered Products,/thorpespecialtyservicescorporation/projects/113335/workbooks,2018-08-29 15:22:00,
8+
Job Time,.twbx,Operations - Flooring,Flooring,/thorpespecialtyservicescorporation/projects/113336/workbooks,2018-08-29 15:22:15,
9+
Job Time,.twbx,Operations - Harrison,Harrison,/thorpespecialtyservicescorporation/projects/113337/workbooks,2018-08-29 15:22:34,
10+
Job Time,.twbx,Operations - Houston-Cor,Houston-Cor,/thorpespecialtyservicescorporation/projects/113338/workbooks,2018-08-29 15:22:52,
11+
Job Time,.twbx,Operations - Houston-Ref,Houston-Ref,/thorpespecialtyservicescorporation/projects/113339/workbooks,2018-08-29 15:23:09,
12+
Job Time,.twbx,Operations - International,International,/thorpespecialtyservicescorporation/projects/113340/workbooks,2018-08-29 15:23:26,
13+
Job Time,.twbx,Operations - JT-Beaumont,JT-Beaumont,/thorpespecialtyservicescorporation/projects/113356/workbooks,2018-08-29 15:23:44,
14+
Job Time,.twbx,Operations - JT-Houston,JT-Houston,/thorpespecialtyservicescorporation/projects/113341/workbooks,2018-08-29 15:24:02,
15+
Job Time,.twbx,Operations - St. Gabriel-Cor,St. Gabriel-Cor,/thorpespecialtyservicescorporation/projects/113342/workbooks,2018-08-29 15:24:18,
16+
Job Time,.twbx,Operations - St. Gabriel-Ref,St. Gabriel-Ref,/thorpespecialtyservicescorporation/projects/113343/workbooks,2018-08-29 15:24:34,
17+
Job Time,.twbx,Operations - Tulsa,Tulsa,/thorpespecialtyservicescorporation/projects/113344/workbooks,2018-08-29 15:24:51,

samples/filter-and-replicate/filter-and-replicate.py

Lines changed: 94 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
import os
1111
from tableauserverclient import ConnectionCredentials, ConnectionItem
1212
from tableaudocumentapi import Workbook
13-
13+
from dateutil.parser import parse
14+
import platform
15+
import datetime
16+
import time
1417

1518
############################################################
1619
# Step 3) Use a database list (in CSV), loop thru and
@@ -54,85 +57,106 @@ def main():
5457
count = 1
5558
overwrite_true = TSC.Server.PublishMode.Overwrite
5659

57-
60+
# Open the workbook
61+
source = "Source"
62+
zip = "Zip"
63+
export = "Export"
64+
columns = ['Workbook', 'Format', 'Project', 'Branch', 'Directory', 'PublishDate','ModifiedDate' ]
5865

5966
with server.auth.sign_in(tableau_auth):
60-
with open('databases.csv') as csvfile:
67+
with open('databases.csv',mode='r') as csvfile:
6168
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
81115
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:
84117
if "column" in children.attrib and "class" in children.attrib:
85118
if children.attrib["column"] == "[Branch]" and children.attrib["class"] == "categorical":
86119
for subchildren in children._children:
87120
if "member" in subchildren.attrib:
88-
subchildren.attrib["member"] = '"' + row['Branch'] + '"'
89-
121+
subchildren.attrib["member"] = '"' + row['Branch'] + '"'
90122

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)
134147
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+
136160

137161
if __name__ == '__main__':
138162
main()

0 commit comments

Comments
 (0)
0