Nolan Zandi commited on
Commit ·
260593e
1
Parent(s): 960febc
add check for file availability before opening
Browse files- data_sources/upload_file.py +19 -2
data_sources/upload_file.py
CHANGED
|
@@ -2,9 +2,20 @@ import pandas as pd
|
|
| 2 |
import sqlite3
|
| 3 |
import csv
|
| 4 |
import json
|
| 5 |
-
import os
|
| 6 |
import time
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def get_delimiter(file_path, bytes = 4096):
|
| 9 |
sniffer = csv.Sniffer()
|
| 10 |
data = open(file_path, "r").read(bytes)
|
|
@@ -19,7 +30,6 @@ def read_file(file):
|
|
| 19 |
contents = json.load(f)
|
| 20 |
df = pd.json_normalize(contents)
|
| 21 |
elif file.endswith('.ndjson'):
|
| 22 |
-
time.sleep(5)
|
| 23 |
with open(file, 'r') as f:
|
| 24 |
contents = f.read()
|
| 25 |
data = [json.loads(str(item)) for item in contents.strip().split('\n')]
|
|
@@ -33,6 +43,13 @@ def read_file(file):
|
|
| 33 |
return df
|
| 34 |
|
| 35 |
def process_data_upload(data_file, session_hash):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
df = read_file(data_file)
|
| 37 |
|
| 38 |
# Read each sheet and store data in a DataFrame
|
|
|
|
| 2 |
import sqlite3
|
| 3 |
import csv
|
| 4 |
import json
|
|
|
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
def is_file_done_saving(file_path):
|
| 8 |
+
try:
|
| 9 |
+
with open(file_path, 'r') as f:
|
| 10 |
+
contents = f
|
| 11 |
+
|
| 12 |
+
if contents:
|
| 13 |
+
return True
|
| 14 |
+
else:
|
| 15 |
+
return False
|
| 16 |
+
except PermissionError:
|
| 17 |
+
return False
|
| 18 |
+
|
| 19 |
def get_delimiter(file_path, bytes = 4096):
|
| 20 |
sniffer = csv.Sniffer()
|
| 21 |
data = open(file_path, "r").read(bytes)
|
|
|
|
| 30 |
contents = json.load(f)
|
| 31 |
df = pd.json_normalize(contents)
|
| 32 |
elif file.endswith('.ndjson'):
|
|
|
|
| 33 |
with open(file, 'r') as f:
|
| 34 |
contents = f.read()
|
| 35 |
data = [json.loads(str(item)) for item in contents.strip().split('\n')]
|
|
|
|
| 43 |
return df
|
| 44 |
|
| 45 |
def process_data_upload(data_file, session_hash):
|
| 46 |
+
total_time = 0
|
| 47 |
+
while not is_file_done_saving(data_file):
|
| 48 |
+
total_time += .5
|
| 49 |
+
time.sleep(.5)
|
| 50 |
+
if total_time > 10:
|
| 51 |
+
break
|
| 52 |
+
|
| 53 |
df = read_file(data_file)
|
| 54 |
|
| 55 |
# Read each sheet and store data in a DataFrame
|