Skip to content

Commit 85c706c

Browse files
authored
Merge pull request #182 from lanl/issue181
updated dsi interactions with user
2 parents f2641b0 + b97d1c4 commit 85c706c

File tree

7 files changed

+287
-329
lines changed

7 files changed

+287
-329
lines changed

docs/python_api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Example 5: Update data
106106
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107107
Updating data from the edited output of ``find()``. Users must NOT modify metadata columns starting with **`dsi_`** even when adding new rows.
108108

109-
The input can be the output of either ``find()``, ``search()``, ``query()``, or ``get_table()``.
109+
The input can be the output of either ``find()``, ``query()``, or ``get_table()``.
110110

111111
.. literalinclude:: ../examples/user/5.update.py
112112

dsi/core.py

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import re
1414
import tarfile
1515
import subprocess
16+
from contextlib import redirect_stdout
1617

1718
class Terminal():
1819
"""
@@ -1460,45 +1461,56 @@ def index(self, local_loc, remote_loc, isVerbose=False):
14601461
# Open and validate local DSI data store
14611462
t = Terminal()
14621463

1464+
f = self.project_name+".db"
14631465
try:
14641466
#f = os.path.join((local_loc, str(self.project_name+".db") ))
14651467
#f = local_loc+"/"+self.project_name+".db"
1466-
f = self.project_name+".db"
14671468
if isVerbose:
14681469
print("trying db: ", f)
1469-
if os.path.exists(f):
1470+
assert os.path.exists(f)
1471+
1472+
fnull = open(os.devnull, 'w')
1473+
with redirect_stdout(fnull):
14701474
t.load_module('backend','Sqlite','back-write', filename=f)
14711475

14721476
except Exception as err:
1473-
print(f"Unexpected {err=}, {type(err)=}")
1477+
print(f"Database {f} not found")
14741478
raise
14751479

14761480
# See if filesystem exists
14771481
fs_t = t.get_table("filesystem")
14781482
if fs_t.empty:
14791483
if isVerbose:
1480-
print( "Creating new fs table")
1481-
# Create new filesystem collection with origin and remote locations
1482-
# Stage data for ingest
1483-
# Transpose the OrderedDict to a list of row dictionaries
1484-
num_rows = len(next(iter(st_dict.values()))) # Assume all columns are of equal length
1485-
rows = []
1486-
1487-
for i in range(num_rows):
1488-
row = {col: values[i] for col, values in st_dict.items()}
1489-
rows.append(row)
1490-
1491-
# Temporary csv to ingest
1492-
output_file = '.fs.csv'
1493-
with open(output_file, mode='w', newline='') as csvfile:
1494-
writer = csv.DictWriter(csvfile, fieldnames=st_dict.keys())
1495-
writer.writeheader()
1496-
writer.writerows(rows)
1484+
print("Creating new Filesystem table")
1485+
1486+
fnull = open(os.devnull, 'w')
1487+
with redirect_stdout(fnull):
1488+
t.load_module('plugin', "Dict", "reader", collection=st_dict)
1489+
t.artifact_handler(interaction_type='ingest')
1490+
1491+
# # Create new filesystem collection with origin and remote locations
1492+
# # Stage data for ingest
1493+
# # Transpose the OrderedDict to a list of row dictionaries
1494+
# num_rows = len(next(iter(st_dict.values()))) # Assume all columns are of equal length
1495+
# rows = []
1496+
1497+
# for i in range(num_rows):
1498+
# row = {col: values[i] for col, values in st_dict.items()}
1499+
# rows.append(row)
1500+
1501+
# # Temporary csv to ingest
1502+
# output_file = '.fs.csv'
1503+
# with open(output_file, mode='w', newline='') as csvfile:
1504+
# writer = csv.DictWriter(csvfile, fieldnames=st_dict.keys())
1505+
# writer.writeheader()
1506+
# writer.writerows(rows)
14971507

1498-
# Add filesystem table
1499-
t.load_module('plugin', 'Csv', 'reader', filenames=".fs.csv", table_name="filesystem")
1500-
#t.load_module('plugin', 'collection_reader', 'reader', st_dict )
1501-
t.artifact_handler(interaction_type='ingest')
1508+
# # Add filesystem table
1509+
# t.load_module('plugin', 'Csv', 'reader', filenames=".fs.csv", table_name="filesystem")
1510+
# #t.load_module('plugin', 'collection_reader', 'reader', st_dict )
1511+
# t.artifact_handler(interaction_type='ingest')
1512+
1513+
t.close()
15021514

15031515
self.file_list = file_list
15041516
self.rfile_list = rfile_list
@@ -1518,21 +1530,26 @@ def copy(self, tool="copy", isVerbose=False, **kwargs):
15181530
try:
15191531
#f = os.path.join((local_loc, str(self.project_name+".db") ))
15201532
#f = self.local_location+"/"+self.project_name+".db"
1521-
assert os.path.exists(f)
15221533
if isVerbose:
1523-
print("db: ", f)
1524-
t.load_module('backend','Sqlite','back-read', filename=f)
1534+
print("trying db: ", f)
1535+
assert os.path.exists(f)
1536+
1537+
fnull = open(os.devnull, 'w')
1538+
with redirect_stdout(fnull):
1539+
t.load_module('backend','Sqlite','back-read', filename=f)
15251540
except Exception:
1526-
print(f"Databaase {f} not found")
1541+
print(f"Database {f} not found")
15271542
raise
15281543

15291544
# See if filesystem exists
15301545
fs_t = t.get_table("filesystem")
15311546
if fs_t.empty:
1532-
print( " Filesystem table not found. Try running Index first.")
1533-
print( " Data copy failed. ")
1547+
print(" Filesystem table not found. Try running Index first.")
1548+
print(" Data copy failed.")
15341549
return
15351550

1551+
t.close()
1552+
15361553
# Future: have movement service handle type (cp,scp,ftp,rsync,etc.)
15371554
if tool == "copy":
15381555
#print(self.file_list)
@@ -1561,7 +1578,7 @@ def copy(self, tool="copy", isVerbose=False, **kwargs):
15611578
#shutil.copy2(os.path.join(self.local_location, str(self.project_name+".db") ), os.path.join(self.remote_location, self.project_name, self.project_name+".db" ) )
15621579
shutil.copy2(str(self.project_name+".db"), os.path.join(self.remote_location, self.project_name, self.project_name+".db" ) )
15631580

1564-
print( " Data Copy Complete! ")
1581+
print(" Data Copy Complete!")
15651582
elif tool == "scp":
15661583
# Data movement via SCP
15671584
remote_user = os.getlogin()
@@ -1605,7 +1622,6 @@ def copy(self, tool="copy", isVerbose=False, **kwargs):
16051622
raise TypeError(f"Data movement format not supported:, Type: {tool}")
16061623

16071624

1608-
16091625
def dircrawl(self,filepath):
16101626
"""
16111627
Crawls the root 'filepath' directory and returns files
@@ -1756,10 +1772,13 @@ def index(self, local_files, remote_dir, tar_name, isVerbose=False):
17561772

17571773
f = self.project_name+".db"
17581774
try:
1759-
assert os.path.exists(f)
17601775
if isVerbose:
1761-
print("db: ", f)
1762-
t.load_module('backend','Sqlite','back-read', filename=f)
1776+
print("trying db: ", f)
1777+
assert os.path.exists(f)
1778+
1779+
fnull = open(os.devnull, 'w')
1780+
with redirect_stdout(fnull):
1781+
t.load_module('backend','Sqlite','back-read', filename=f)
17631782
except Exception:
17641783
print(f"Databaase {f} not found")
17651784
raise
@@ -1768,7 +1787,7 @@ def index(self, local_files, remote_dir, tar_name, isVerbose=False):
17681787
fs_t = t.get_table("filesystem_hpss")
17691788
if fs_t.empty:
17701789
if isVerbose:
1771-
print( "Creating new hpss fs table")
1790+
print("Creating new hpss Filesystem table")
17721791
# Create new filesystem collection with origin and remote locations
17731792
# Stage data for ingest
17741793
# Transpose the OrderedDict to a list of row dictionaries
@@ -1788,6 +1807,8 @@ def index(self, local_files, remote_dir, tar_name, isVerbose=False):
17881807
# Add filesystem table
17891808
t.load_module('plugin', 'Csv', 'reader', filenames=".fs.csv", table_name="filesystem_hpss")
17901809
t.artifact_handler(interaction_type='ingest')
1810+
1811+
t.close()
17911812

17921813
def move(self, tool="copy", isVerbose=False, **kwargs):
17931814
self.copy(tool,isVerbose,kwargs)
@@ -1800,14 +1821,17 @@ def copy(self, tool="copy", isVerbose=False, **kwargs):
18001821
# See if FS table has been created
18011822
t = Terminal()
18021823

1824+
f = self.project_name+".db"
18031825
try:
1804-
f = self.project_name+".db"
18051826
if isVerbose:
1806-
print("db: ", f)
1807-
if os.path.exists(f):
1827+
print("trying db: ", f)
1828+
assert os.path.exists(f)
1829+
1830+
fnull = open(os.devnull, 'w')
1831+
with redirect_stdout(fnull):
18081832
t.load_module('backend','Sqlite','back-read', filename=f)
18091833
except Exception as err:
1810-
print(f"Unexpected {err=}, {type(err)=}")
1834+
print(f"Database {f} not found")
18111835
raise
18121836

18131837
# See if filesystem exists
@@ -1816,6 +1840,8 @@ def copy(self, tool="copy", isVerbose=False, **kwargs):
18161840
print( " Filesystem table not found. Try running Index first.")
18171841
print( " Data copy failed. ")
18181842
return
1843+
1844+
t.close()
18191845

18201846
hpss_files = {}
18211847
for f in self.tar_files:

0 commit comments

Comments
 (0)