Skip to content

Fix #2276 #2294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions qiita_db/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,16 @@ def test_get_timeseries_types(self):
[10, 'mixed', 'combo intervention']]
self.assertEqual(obs, exp)

def test_get_filepath_information(self):
obs = qdb.util.get_filepath_information(1)
# This path is machine specific. Just checking that is not empty
self.assertIsNotNone(obs.pop('fullpath'))
exp = {'filepath_id': 1L, 'filepath': '1_s_G1_L001_sequences.fastq.gz',
'filepath_type': 'raw_forward_seqs', 'checksum': '852952723',
'data_type': 'raw_data', 'mountpoint': 'raw_data',
'subdirectory': False, 'active': True}
self.assertEqual(obs, exp)

def test_filepath_id_to_rel_path(self):
obs = qdb.util.filepath_id_to_rel_path(1)
exp = 'raw_data/1_s_G1_L001_sequences.fastq.gz'
Expand Down
67 changes: 60 additions & 7 deletions qiita_db/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,33 @@ def str_to_id(x):
chain.from_iterable(qdb.sql_connection.TRN.execute()[idx:])))


def _path_builder(db_dir, filepath, mountpoint, subdirectory, obj_id):
"""Builds the path of a DB stored file

Parameters
----------
db_dir : str
The DB base dir
filepath : str
The path stored in the DB
mountpoint : str
The mountpoint of the given file
subdirectory : bool
Whether the file is stored in a subdirectory in the mountpoint or not
obj_id : int
The id of the object to which the file is attached

Returns
-------
str
The full path of the given file
"""
if subdirectory:
return join(db_dir, mountpoint, str(obj_id), filepath)
else:
return join(db_dir, mountpoint, filepath)


def retrieve_filepaths(obj_fp_table, obj_id_column, obj_id, sort=None,
fp_type=None):
"""Retrieves the filepaths for the given object id
Expand All @@ -674,12 +701,6 @@ def retrieve_filepaths(obj_fp_table, obj_id_column, obj_id, sort=None,
object id
"""

def path_builder(db_dir, filepath, mountpoint, subdirectory, obj_id):
if subdirectory:
return join(db_dir, mountpoint, str(obj_id), filepath)
else:
return join(db_dir, mountpoint, filepath)

sql_sort = ""
if sort == 'ascending':
sql_sort = " ORDER BY filepath_id"
Expand Down Expand Up @@ -710,7 +731,7 @@ def path_builder(db_dir, filepath, mountpoint, subdirectory, obj_id):
results = qdb.sql_connection.TRN.execute_fetchindex()
db_dir = get_db_files_base_dir()

return [(fpid, path_builder(db_dir, fp, m, s, obj_id), fp_type_)
return [(fpid, _path_builder(db_dir, fp, m, s, obj_id), fp_type_)
for fpid, fp, fp_type_, m, s in results]


Expand Down Expand Up @@ -845,6 +866,38 @@ def move_filepaths_to_upload_folder(study_id, filepaths):
qdb.sql_connection.TRN.execute()


def get_filepath_information(filepath_id):
"""Gets the filepath information of filepath_id

Parameters
----------
filepath_id : int
The filepath id

Returns
-------
dict
The filepath information
"""
with qdb.sql_connection.TRN:
sql = """SELECT filepath_id, filepath, filepath_type, checksum,
data_type, mountpoint, subdirectory, active,
artifact_id
FROM qiita.filepath
JOIN qiita.filepath_type USING (filepath_type_id)
JOIN qiita.data_directory USING (data_directory_id)
LEFT JOIN qiita.artifact_filepath USING (filepath_id)
WHERE filepath_id = %s"""
qdb.sql_connection.TRN.add(sql, [filepath_id])
res = dict(qdb.sql_connection.TRN.execute_fetchindex()[0])

obj_id = res.pop('artifact_id')
res['fullpath'] = _path_builder(get_db_files_base_dir(),
res['filepath'], res['mountpoint'],
res['subdirectory'], obj_id)
return res


def filepath_id_to_rel_path(filepath_id):
"""Gets the relative to the base directory of filepath_id

Expand Down
Loading