Marthee commited on
Commit
0730fbe
·
1 Parent(s): 5dfece0

Delete db.py

Browse files
Files changed (1) hide show
  1. db.py +0 -255
db.py DELETED
@@ -1,255 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """dropbox.ipynb
3
-
4
- Automatically generated by Colaboratory.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/11JdwIfblSKRelJJa7rllER8HwbSl_0Nu
8
- """
9
-
10
-
11
- import dropbox
12
- import dropbox.exceptions as AuthError
13
- import cv2
14
- import pandas as pd
15
- import os
16
-
17
- """### Lists of project names, parts, sections --
18
- *Will get from MC*
19
- """
20
-
21
- prjname1=['2340 United Living Low Rise Building','2341 Bouygues-Cotswold','2342 GF Tomlinson - Hardingstone Primary School']
22
- prjname1parts=['Block O Pumphouse','Hawthorn','Ling','Red Oak']
23
- prjname2parts=['Block B','Block G']
24
-
25
- prjname1sec1=['1.0 Substructure','5.1 Sanitary','8.2 Roads, Paths, Pavings','8.4 Fencing, Railing, Walls','8.5 External Fixtures','8.6 External Drainage','8.7 External Services','8.8 Minor Buildings']
26
- prjname1sec2=['1.0 Substructure','2.1 Frames','2.3 Roof','2.4 Stairs','2.5 External Walls','2.6 External Openings','2.7 Internal Walls','2.8 Internal Openings','3.1 Wall Finishes','3.2 Floor Finishes','3.3 Ceiling Finishes','4.0 Fittings, Furnishings and Equipments','7.0 Works to exisitng buildings','8.1 Site Preparation Works','8.2 Roads, Paths, Pavings','8.3 Soft Landscaping','8.5 External Fixtures','8.6 External Drainage','8.7 External Services','8.8 Minor Buildings']
27
- prjname1sec3=['1.0 Substructure','2.3 Roof','2.4 Stairs','2.5 External Walls','2.6 External Openings','2.7 Internal Walls','3.1 Wall Finishes','3.2 Floor Finishes','3.3 Ceiling Finishes', '4.0 Fittings, Furnishings and Equipments','7.0 Works to existing buildings','8.1 Site Preparation Works','8.2 Roads, Paths, Pavings','8.3 Soft Landscaping','8.5 External Fixtures','8.6 External Drainage']
28
- prjname1sec4=['1.0 Substructure','2.3 Roof','2.4 Stairs','2.5 External Walls','2.6 External Openings','2.7 Internal Walls','3.1 Wall Finishes','3.2 Floor Finishes','3.3 Ceiling Finishes', '4.0 Fittings, Furnishings and Equipments','7.0 Works to existing buildings','8.1 Site Preparation Works','8.2 Roads, Paths, Pavings','8.3 Soft Landscaping','8.5 External Fixtures','8.6 External Drainage']
29
- prj1secs=[prjname1sec1,prjname1sec2,prjname1sec3,prjname1sec4]
30
- prjname2parts=['Block B','Block G']
31
- prjname2sec=['3.3 Ceiling Finishes']
32
- prjname2sec2=['3.3 Ceiling Finishes']
33
- prj2secs=[prjname2sec,prjname2sec2]
34
- prjname3parts=['Community Center building','External Works','Primary School']
35
- prjname3sec1=['1.0 Substructure','2.3 Roof','3.1 Wall Finishes','3.2 Floor Finishes','3.3 Ceiling Finishes','4.0 Fittings, Furnishings and Equipments','5.1 Sanitary']
36
- prjname3sec2=['8.1 Site Preparation Works','8.4 Fencing, Railing, Walls','8.5 External Fixtures', ' 8.6 External Drainage']
37
- prjname3sec3=['1.0 Substructure','2.3 Roof','2.4 Stairs','3.2 Floor Finishes','3.3 Ceiling Finishes','4.0 Fittings, Furnishings and Equipments','5.1 Sanitary']
38
- prj3secs=[prjname3sec1,prjname3sec2,prjname3sec3]
39
-
40
- """### Main Dropbox Functions :connect - list- download- upload"""
41
-
42
- access_token='sl.BiJ1Bte91KheyVoQI95qEQs9fVRR9Chq7U6RBtu73oJPWSrCaMUiNhRjwRzcCHbK4OrQsUTh9hPCJDJ5Ht7KASbAp-RffAqzZLIFMu6ZAHoWNygK2dShvKwznFF4eHGgp4AqD2RG'
43
- ###################################################################################################################
44
- def dropbox_connect():
45
- """Create a connection to Dropbox."""
46
- print('connecy')
47
- try:
48
- # print('ayhaga')
49
- dbx = dropbox.Dropbox(
50
- app_key='67w6ibpa9d2b60x',
51
- app_secret='d3ecz8g1604fu04',
52
- oauth2_refresh_token = 'Oc-2umxnIFsAAAAAAAAAAXQlSUH2EutbUiUa-cZE-iVhe96p4qHLAE2J2ZZ8NqjW',
53
-
54
- )
55
- # dbx=dropbox.Dropbox(access_token)
56
-
57
- except AuthError as e:
58
- print('Error connecting to Dropbox with access token: ' + str(e))
59
- return dbx
60
-
61
-
62
- ################################################################################################################
63
- def dropbox_list_files(path):
64
- """Return a Pandas dataframe of files in a given Dropbox folder path in the Apps directory.
65
- """
66
-
67
- dbx = dropbox_connect()
68
-
69
- try:
70
- files = dbx.files_list_folder(path).entries
71
- files_list = []
72
- for file in files:
73
- if isinstance(file, dropbox.files.FileMetadata):
74
- metadata = {
75
- 'name': file.name,
76
- 'path_display': file.path_display,
77
- 'client_modified': file.client_modified,
78
- 'server_modified': file.server_modified
79
- }
80
- files_list.append(metadata)
81
-
82
- df = pd.DataFrame.from_records(files_list)
83
- return df.sort_values(by='server_modified', ascending=False)
84
-
85
- except Exception as e:
86
- print('Error getting list of files from Dropbox: ' + str(e))
87
-
88
- #####################################################################################################################
89
-
90
- def dropbox_download_file(dropbox_file_path, local_file_path):
91
- """Download a file from Dropbox to the local machine."""
92
-
93
- try:
94
- dbx = dropbox_connect()
95
-
96
- with open(local_file_path, 'wb') as f:
97
- metadata, result = dbx.files_download(path=dropbox_file_path)
98
- f.write(result.content)
99
- except Exception as e:
100
- print('Error downloading file from Dropbox: ' + str(e))
101
-
102
-
103
-
104
- #########################################################################################################################
105
- def dropbox_shareableLink(path):
106
- dbx= dropbox_connect()
107
- shared_link_metadata = dbx.sharing_create_shared_link_with_settings(path)
108
- return shared_link_metadata.url
109
-
110
- #########################################################################################################################
111
-
112
- def dropbox_upload_file( doc, pdfname, pdfpath):
113
- try:
114
- dbx = dropbox_connect()
115
- path= pdfpath + pdfname
116
- meta=dbx.files_upload(doc.write(),path,mode=dropbox.files.WriteMode("overwrite"))
117
- shared_link_metadata = dbx.sharing_create_shared_link_with_settings(path)
118
- print( shared_link_metadata.url)
119
- return shared_link_metadata.url
120
- except Exception as e:
121
- print('Error uploading file to Dropbox: ' + str(e))
122
-
123
- #########################################################################################################################
124
- """### Create Dictionaries (relation between values)"""
125
-
126
- projectsdictionary={} # prj name: parts
127
- sectionsdict={} #each prj part:sections
128
- #allprj parts
129
- parts=[prjname1parts,prjname2parts,prjname3parts]
130
- #allprjsections
131
- sections=[prj1secs,prj2secs,prj3secs]
132
-
133
- for i in range(len(prjname1)):
134
- projectsdictionary.update({prjname1[i]:parts[i]})
135
- for j in range(len(parts[i]) ):
136
- sectionsdict.update({parts[i][j]:sections[i][j]})
137
- print(projectsdictionary)
138
- print(sectionsdict)
139
-
140
-
141
-
142
- """### Create prj from scratch or prj name with all subfolders in it"""
143
-
144
- def create_folder(projectsdictionary={},sectionsdict={}):
145
- dbx = dropbox_connect()
146
- try:
147
- if len(projectsdictionary)==0 and len(sectionsdict)==0:
148
- print(len(projectsdictionary))
149
-
150
- else:
151
- names=[]
152
- secondnames=[]
153
- for item in projectsdictionary.items():
154
- for secondItem in item[1]:
155
- name='/'+item[0]
156
- secondname='/'+secondItem
157
- name+=secondname
158
- names.append(name)
159
- for item1 in sectionsdict.items():
160
- for ii in item1[1]:
161
- if secondItem== item1[0]:
162
- final =name+'/'+ii
163
- print(final)
164
- dbx.files_create_folder(path=final)
165
- except Exception as e:
166
- print('eeeeeerorrrrrrrrrr',e)
167
- # create_folder(projectsdictionary,sectionsdict)
168
-
169
- def dropbox_list_files(path):
170
- """Return a Pandas dataframe of files in a given Dropbox folder path in the Apps directory.
171
- """
172
-
173
- dbx = dropbox_connect()
174
-
175
- try:
176
- files = dbx.files_list_folder(path).entries
177
- files_list = []
178
- folders_list=[]
179
- for file in files:
180
- if isinstance(file, dropbox.files.FileMetadata):
181
-
182
- metadata = {
183
- 'name': file.name,
184
- 'path_display': file.path_display,
185
- # 'client_modified': file.client_modified,
186
- # 'server_modified': file.server_modified
187
- }
188
- files_list.append(metadata)
189
- else:#folder
190
- metadata = {
191
- 'name': file.name,
192
- 'path_display': file.path_display,
193
- }
194
- folders_list.append(metadata)
195
- df = pd.DataFrame.from_records(files_list)
196
- df2 = pd.DataFrame.from_records(folders_list)
197
- return df , df2
198
-
199
- except Exception as e:
200
- print('Error getting list of files from Dropbox: ' + str(e))
201
-
202
- def prjNameCheckandCreate():
203
- df1,df2=dropbox_list_files('')
204
-
205
- if len(df2)!=0:
206
- df2Values=list(df2['name'].values)
207
- difference = list(set(prjname1) - set(df2Values))
208
- if len(difference)==0:
209
- print(df2Values)
210
- else:
211
- newdict={}
212
- newsecdict={}
213
- print('else')
214
- for i in difference:
215
- newdict.update({i:projectsdictionary.get(i)})
216
- for key,value in newdict.items():
217
- for ii in value:
218
- newsecdict.update({ii:sectionsdict.get(ii)})
219
- create_folder(projectsdictionary=newdict,sectionsdict=newsecdict)
220
- return df2
221
-
222
- def prjPartSectionCheckandCreate():
223
- df2= prjNameCheckandCreate()
224
- for i in range(len(df2['path_display'].values)):
225
- #look for the same path
226
- df11,df21=dropbox_list_files(df2['path_display'].values[i])
227
-
228
- if len(df21)!=0:
229
- df2Values=list(df21['name'].values)
230
- difference = list(set(parts[i]) - set(df2Values))
231
- if len(difference)==0:
232
- print('samee')
233
- else:
234
- for j in difference:
235
- dbx = dropbox_connect()
236
- dbx.files_create_folder_v2(df2['path_display'].values[i]+'/'+j) #nopart - create part folder
237
- for k in sectionsdict.get(j):
238
- dbx.files_create_folder(df2['path_display'].values[i]+'/'+j+'/'+k) #create sections folders
239
- #check if section is missing and get prj name and part for path
240
- for arr in df2Values:
241
- df111,df211=dropbox_list_files(df2['path_display'].values[i]+'/'+arr)
242
- if len(df211)!=0:
243
- df211Values=list(df211['name'].values)
244
- for key,value in sectionsdict.items():
245
- if key== arr:
246
- if value == df211Values:
247
- print('s')
248
- else:
249
- difference1 = list(set(value) - set(df211Values))
250
- dbx = dropbox_connect()
251
- for d in difference1:
252
- print(arr,d)
253
- dbx.files_create_folder_v2(df2['path_display'].values[i]+'/'+arr+'/'+d) #no section - create section folder
254
-
255
-