Spaces:
Sleeping
Sleeping
Update tsadropboxretrieval.py
Browse files- tsadropboxretrieval.py +64 -45
tsadropboxretrieval.py
CHANGED
|
@@ -23,8 +23,14 @@ import io
|
|
| 23 |
import re
|
| 24 |
import pyarrow
|
| 25 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
"""### NEW CODE - OCTOBER 26 - Marthe"""
|
| 28 |
|
| 29 |
files_list=[]
|
| 30 |
|
|
@@ -101,6 +107,7 @@ def dropbox_upload_file(df, flag=0):
|
|
| 101 |
print('Error uploading file to Dropbox: ' + str(e))
|
| 102 |
return dbxTeam
|
| 103 |
|
|
|
|
| 104 |
def check_if_file_exists(dbxTeam,path):
|
| 105 |
try:
|
| 106 |
md = dbxTeam.files_get_metadata(path)
|
|
@@ -109,7 +116,25 @@ def check_if_file_exists(dbxTeam,path):
|
|
| 109 |
except Exception as error_response:
|
| 110 |
exists_bool = False
|
| 111 |
return exists_bool
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
def uploadanyFile(doc,pdfname,path,flag=0):
|
| 114 |
try:
|
| 115 |
dbxTeam= ADR_Access_DropboxTeam('admin')
|
|
@@ -131,11 +156,10 @@ def uploadanyFile(doc,pdfname,path,flag=0):
|
|
| 131 |
dbxTeam.files_delete(path)
|
| 132 |
meta=dbxTeam.files_upload(doc.write() ,path)
|
| 133 |
try:
|
| 134 |
-
print('hereintry')
|
| 135 |
shared_link_metadata = dbxTeam.sharing_create_shared_link_with_settings(path)
|
| 136 |
except:
|
| 137 |
-
print('hereinexcept')
|
| 138 |
shared_link_metadata=dbxTeam.sharing_create_shared_link(path)
|
|
|
|
| 139 |
return shared_link_metadata.url
|
| 140 |
except Exception as e:
|
| 141 |
print('Error uploading file to Dropbox: ' + str(e))
|
|
@@ -182,10 +206,11 @@ def GetParquetDF():
|
|
| 182 |
|
| 183 |
|
| 184 |
def getPathtoPDF_File(nameofPDF):
|
| 185 |
-
parquetDf=
|
| 186 |
nameofPDF=nameofPDF.replace('"', '')
|
| 187 |
try:
|
| 188 |
-
path=parquetDf.loc[parquetDf['name'] == nameofPDF, 'path_display'].iloc[0]
|
|
|
|
| 189 |
link=getSharedLink(path)
|
| 190 |
print(path,link)
|
| 191 |
except:
|
|
@@ -200,45 +225,39 @@ def getPDFData(path):
|
|
| 200 |
data = res.content
|
| 201 |
return data
|
| 202 |
|
| 203 |
-
def getPathtoPDF_File(nameofPDF):
|
| 204 |
-
parquetDf=GetParquetDF()
|
| 205 |
-
nameofPDF=nameofPDF.replace('"', '')
|
| 206 |
-
try:
|
| 207 |
-
path=parquetDf.loc[parquetDf['name'] == nameofPDF, 'path_display'].iloc[0]
|
| 208 |
-
link=getSharedLink(path)
|
| 209 |
-
print(path,link)
|
| 210 |
-
except:
|
| 211 |
-
return 'Project does not exist'
|
| 212 |
-
return path,link
|
| 213 |
-
# parquetDf
|
| 214 |
-
|
| 215 |
-
# getPathtoPDF_File('A5157-EBLA-V5-XX-SH-L-0004-D2-01.pdf')
|
| 216 |
-
def getPDFData(path):
|
| 217 |
-
dbxTeam= ADR_Access_DropboxTeam('admin')
|
| 218 |
-
md, res =dbxTeam.files_download(path)
|
| 219 |
-
data = res.content
|
| 220 |
-
return data
|
| 221 |
|
| 222 |
def retrieveProjects(projname):
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
|
|
|
| 23 |
import re
|
| 24 |
import pyarrow
|
| 25 |
from io import BytesIO
|
| 26 |
+
from functools import lru_cache
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@lru_cache(maxsize=1)
|
| 31 |
+
def load_parquet_df():
|
| 32 |
+
return GetParquetDF()
|
| 33 |
|
|
|
|
| 34 |
|
| 35 |
files_list=[]
|
| 36 |
|
|
|
|
| 107 |
print('Error uploading file to Dropbox: ' + str(e))
|
| 108 |
return dbxTeam
|
| 109 |
|
| 110 |
+
|
| 111 |
def check_if_file_exists(dbxTeam,path):
|
| 112 |
try:
|
| 113 |
md = dbxTeam.files_get_metadata(path)
|
|
|
|
| 116 |
except Exception as error_response:
|
| 117 |
exists_bool = False
|
| 118 |
return exists_bool
|
| 119 |
+
def uploadmarkupPDFTable(doc,pdfname,path):
|
| 120 |
+
dbxTeam= ADR_Access_DropboxTeam('admin')
|
| 121 |
+
try:
|
| 122 |
+
path=path+pdfname
|
| 123 |
+
exists_bool=check_if_file_exists(dbxTeam,path)
|
| 124 |
+
if exists_bool:
|
| 125 |
+
print("if gowa el else <3")
|
| 126 |
+
dbxTeam.files_delete(path)
|
| 127 |
+
print("abl el meta <3")
|
| 128 |
+
meta=dbxTeam.files_upload(doc.read() ,path)
|
| 129 |
+
try:
|
| 130 |
+
shared_link_metadata = dbxTeam.sharing_create_shared_link_with_settings(path)
|
| 131 |
+
except:
|
| 132 |
+
shared_link_metadata=dbxTeam.sharing_create_shared_link(path)
|
| 133 |
+
# print(shared_link_metadata.url)
|
| 134 |
+
return shared_link_metadata.url
|
| 135 |
+
except Exception as e:
|
| 136 |
+
print('Error uploading file to Dropbox: ' + str(e))
|
| 137 |
+
|
| 138 |
def uploadanyFile(doc,pdfname,path,flag=0):
|
| 139 |
try:
|
| 140 |
dbxTeam= ADR_Access_DropboxTeam('admin')
|
|
|
|
| 156 |
dbxTeam.files_delete(path)
|
| 157 |
meta=dbxTeam.files_upload(doc.write() ,path)
|
| 158 |
try:
|
|
|
|
| 159 |
shared_link_metadata = dbxTeam.sharing_create_shared_link_with_settings(path)
|
| 160 |
except:
|
|
|
|
| 161 |
shared_link_metadata=dbxTeam.sharing_create_shared_link(path)
|
| 162 |
+
# print(shared_link_metadata.url)
|
| 163 |
return shared_link_metadata.url
|
| 164 |
except Exception as e:
|
| 165 |
print('Error uploading file to Dropbox: ' + str(e))
|
|
|
|
| 206 |
|
| 207 |
|
| 208 |
def getPathtoPDF_File(nameofPDF):
|
| 209 |
+
parquetDf = load_parquet_df()
|
| 210 |
nameofPDF=nameofPDF.replace('"', '')
|
| 211 |
try:
|
| 212 |
+
# path=parquetDf.loc[parquetDf['name'] == nameofPDF, 'path_display'].iloc[0]
|
| 213 |
+
path = parquetDf.at[parquetDf.index[parquetDf['name'] == nameofPDF][0], 'path_display']
|
| 214 |
link=getSharedLink(path)
|
| 215 |
print(path,link)
|
| 216 |
except:
|
|
|
|
| 225 |
data = res.content
|
| 226 |
return data
|
| 227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
def retrieveProjects(projname):
|
| 230 |
+
print('retrieve', projname)
|
| 231 |
+
|
| 232 |
+
projname = '/' + projname.split(' ')[0] # Extract main project name
|
| 233 |
+
projname = projname.replace('/"', '') # Remove unwanted characters
|
| 234 |
+
print('projname', projname)
|
| 235 |
+
|
| 236 |
+
parquetDf = load_parquet_df()
|
| 237 |
+
documentsToMeasure = []
|
| 238 |
+
RelevantDocuments = []
|
| 239 |
+
|
| 240 |
+
# Store the original path before converting it to lowercase
|
| 241 |
+
parquetDf['original_path_display'] = parquetDf['path_display']
|
| 242 |
+
|
| 243 |
+
# Create a lowercase column for case-insensitive matching
|
| 244 |
+
parquetDf['path_display_lower'] = parquetDf['path_display'].str.lower()
|
| 245 |
+
|
| 246 |
+
# Filter using the lowercase column but retrieve the original paths
|
| 247 |
+
mask = parquetDf['path_display_lower'].apply(lambda x: '/'+projname in x and '01 project details' in x)
|
| 248 |
+
|
| 249 |
+
# Retrieve the original (case-sensitive) paths before lowering them
|
| 250 |
+
RelevantDocuments = parquetDf[mask][['name', 'original_path_display']].values.tolist()
|
| 251 |
+
documentsToMeasure = [doc for doc in RelevantDocuments if doc[0].endswith('.pdf')] # Keep only PDFs
|
| 252 |
+
|
| 253 |
+
# Extract path from the original (case-sensitive) column
|
| 254 |
+
if RelevantDocuments:
|
| 255 |
+
extracted_path = RelevantDocuments[0][1].split("01 Project Details")[0] + "01 Project Details"
|
| 256 |
+
else:
|
| 257 |
+
extracted_path = None # Handle case when no match is found
|
| 258 |
+
|
| 259 |
+
# Remove temporary columns
|
| 260 |
+
parquetDf.drop(columns=['original_path_display', 'path_display_lower'], inplace=True)
|
| 261 |
+
|
| 262 |
+
return documentsToMeasure, RelevantDocuments, extracted_path
|
| 263 |
|