-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a28592
commit 0ef3ac3
Showing
5 changed files
with
52 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,37 @@ | ||
import os | ||
from io import BytesIO | ||
|
||
from explorer.ee.db_connections.type_infer import get_parser, is_sqlite | ||
from explorer.ee.db_connections.utils import pandas_to_sqlite, download_local_sqlite | ||
from explorer.ee.db_connections.type_infer import get_parser | ||
from explorer.ee.db_connections.utils import pandas_to_sqlite, download_sqlite_if_needed, uploaded_db_local_path | ||
|
||
|
||
def parse_to_sqlite(file, append=None) -> (BytesIO, str): | ||
f_name = file.name | ||
def parse_to_sqlite(file, append=None, user_id=None) -> (BytesIO, str): | ||
|
||
f_bytes = file.read() | ||
table_name, _ = os.path.splitext(file.name) | ||
|
||
# If this is being uploaded as a new connection, use the file name the upload destination. | ||
# If it's being appended, then we should use the same local path as the file being appended to. | ||
if append: | ||
f_name = os.path.basename(append) | ||
else: | ||
f_name = f"{table_name}_{user_id}.db" | ||
|
||
local_path = f"{f_name}_tmp_local.db" | ||
local_path = uploaded_db_local_path(f_name) | ||
|
||
# When appending, make sure the database exists locally so that we can write to it | ||
if append: | ||
if is_sqlite(file): | ||
raise TypeError("Can't append a SQLite file to a SQLite file. Only CSV and JSON.") | ||
# Get the sqlite file we are going to append to into the local filesystem | ||
download_local_sqlite(append, local_path) | ||
download_sqlite_if_needed(append, local_path) | ||
|
||
df_parser = get_parser(file) | ||
if df_parser: | ||
df = df_parser(f_bytes) | ||
name, _ = os.path.splitext(f_name) | ||
try: | ||
f_bytes = pandas_to_sqlite(df, name, local_path, append) | ||
f_bytes = pandas_to_sqlite(df, table_name, local_path, append) | ||
except Exception as e: # noqa | ||
raise ValueError(f"Error while parsing {f_name}: {e}") from e | ||
# replace the previous extension with .db, as it is now a sqlite file | ||
f_name = f"{name}.db" | ||
else: | ||
return BytesIO(f_bytes), f_name # if it's a SQLite file already, simply cough it up as a BytesIO object | ||
# if it's a SQLite file already, simply cough it up as a BytesIO object | ||
return BytesIO(f_bytes), f_name | ||
return f_bytes, f_name | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters