Skip to content

Commit

Permalink
Custom paths in the TSV path field instead of labouriously arranging …
Browse files Browse the repository at this point in the history
…files
  • Loading branch information
plesubc committed Sep 13, 2024
1 parent 3b36981 commit 694326f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/dataverse_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
'''
from dataverse_utils.dataverse_utils import *

VERSION = (0,12,0)
VERSION = (0,13,0)
__version__ = '.'.join([str(x) for x in VERSION])
28 changes: 25 additions & 3 deletions src/dataverse_utils/dataverse_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def _make_info(dv_url, study, apikey) -> tuple:
def make_tsv(start_dir, in_list=None, def_tag='Data', # pylint: disable=too-many-arguments
inc_header=True,
mime=False,
quotype=csv.QUOTE_MINIMAL) -> str:
quotype=csv.QUOTE_MINIMAL,
**kwargs) -> str:
'''
Recurses the tree for files and produces tsv output with
with headers 'file', 'description', 'tags'.
Expand Down Expand Up @@ -93,6 +94,11 @@ def make_tsv(start_dir, in_list=None, def_tag='Data', # pylint: disable=too-many
csv.QUOTE_NONNUMERIC / 2
csv.QUOTE_NONE / 3
path: bool
If true include a 'path' field so that you can type
in a custom path instead of actually structuring
your data
'''
if start_dir.endswith(os.sep):
#start_dir += os.sep
Expand All @@ -109,6 +115,8 @@ def make_tsv(start_dir, in_list=None, def_tag='Data', # pylint: disable=too-many
headers = ['file', 'description', 'tags']
if mime:
headers.append('mimetype')
if kwargs.get('path'):
headers.insert(1, 'path')
outf = io.StringIO(newline='')
tsv_writer = csv.writer(outf, delimiter='\t',
quoting=quotype
Expand Down Expand Up @@ -169,9 +177,10 @@ def dump_tsv(start_dir, filename, in_list=None,
def_tag= kwargs.get('def_tag', 'Data')
inc_header =kwargs.get('inc_header', True)
mime = kwargs.get('mime', False)
path = kwargs.get('path', False)
quotype = kwargs.get('quotype', csv.QUOTE_MINIMAL)

dumper = make_tsv(start_dir, in_list, def_tag, inc_header, mime, quotype)
dumper = make_tsv(start_dir, in_list, def_tag, inc_header, mime, quotype, path=path)
with open(filename, 'w', newline='', encoding='utf-8') as tsvfile:
tsvfile.write(dumper)

Expand Down Expand Up @@ -371,15 +380,23 @@ def upload_file(fpath, hdl, **kwargs):
Mimetype of file. Useful if using File Previewers. Mimetype for zip files
(application/zip) will be ignored to circumvent Dataverse's automatic
unzipping function.
label : str
OPTIONAL
If included in kwargs, this value will be used for the label
timeout : int
OPTIONAL
Timeout in seconds
override : bool
OPTIONAL
Ignore NOTAB (ie, NOTAB = [])
timeout = int
OPTIONAL
Timeout in seconds
'''
#Why are SPSS files getting processed anyway?
#Does SPSS detection happen *after* upload
Expand Down Expand Up @@ -556,7 +573,12 @@ def upload_from_tsv(fil, hdl, **kwargs):
if num == 0:
continue
#dirlabel = file_path(row[0], './')
dirlabel = file_path(row['file'], kwargs.get('trunc', ''))
if row.get('path'):
#Explicit separate path because that way you can organize
#on upload
dirlabel = row.get('path')
else:
dirlabel = file_path(row['file'], kwargs.get('trunc', ''))
tags = row['tags'].split(',')
tags = [x.strip() for x in tags]
descr = row['description']
Expand Down
11 changes: 8 additions & 3 deletions src/dataverse_utils/scripts/dv_manifest_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import dataverse_utils as du

VERSION = (0, 4, 0)
VERSION = (0, 5, 0)
__version__ = '.'.join([str(x) for x in VERSION])

def parse() -> argparse.ArgumentParser():
Expand Down Expand Up @@ -71,6 +71,9 @@ def parse() -> argparse.ArgumentParser():
parser.add_argument('-m', '--mime',
help=('Include autodetected mimetypes'),
action='store_true')
parser.add_argument('-p', '--path',
help=('Include an optional path column for custom file paths'),
action='store_true')
parser.add_argument('--version', action='version',
version='%(prog)s '+__version__,
help='Show version number and exit')
Expand Down Expand Up @@ -124,13 +127,15 @@ def main() -> None:
def_tag=args.tag,
inc_header=args.inc_header,
quotype=args.quote,
mime=args.mime)
mime=args.mime,
path=args.path)
else:
print(du.make_tsv(os.getcwd(), in_list=f_list,
def_tag=args.tag,
inc_header=args.inc_header,
quotype=args.quote,
mime=args.mime))
mime=args.mime,
path=args.path))

if __name__ == '__main__':
main()

0 comments on commit 694326f

Please sign in to comment.