Skip to content

Commit 01f5f71

Browse files
committed
remove bugs due to leading slash
1 parent fa83891 commit 01f5f71

File tree

3 files changed

+38
-43
lines changed

3 files changed

+38
-43
lines changed

tsdapiclient/fileapi.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,23 @@ def format_filename(filename: str) -> str:
117117

118118

119119
def upload_resource_name(filename: str, is_dir: bool, group: Optional[str] = None, remote_path: Optional[str] = None) -> str:
120-
if not group:
121-
group = ""
122120
if not is_dir:
123121
debug_step('uploading file')
124-
resource = "/" / pathlib.Path(quote(format_filename(filename)))
122+
resource = pathlib.Path(quote(format_filename(filename)))
125123
if remote_path:
126124
resource = quote(remote_path) / resource
127125
if group:
128126
resource = group / resource
129127
else:
130128
debug_step('uploading directory (file)')
131-
if remote_path:
132-
resource = pathlib.Path("/") / group / quote(remote_path) / quote(filename)
129+
if filename.startswith('/'):
130+
resource = pathlib.Path(filename[1:])
133131
else:
134-
resource = pathlib.Path("/") / group / quote(filename)
132+
resource = pathlib.Path(filename)
133+
if remote_path:
134+
resource = quote(remote_path) / resource
135+
if group:
136+
resource = group / resource
135137
return str(resource)
136138

137139

@@ -253,7 +255,7 @@ def streamfile(
253255
tokens = maybe_refresh(env, pnum, api_key, token, refresh_token, refresh_target)
254256
token = tokens.get('access_token') if tokens else token
255257
resource = upload_resource_name(filename, is_dir, group=group, remote_path=remote_path)
256-
endpoint = str(pathlib.Path("stream") / f"{resource}?group={group}")
258+
endpoint=f"stream/{resource}?group={group}"
257259
url = f'{file_api_url(env, pnum, backend, endpoint=endpoint)}'
258260
headers = {'Authorization': f'Bearer {token}'}
259261
debug_step(f'streaming data to {url}')
@@ -335,9 +337,9 @@ def import_list(
335337
if not group:
336338
group = ""
337339
if remote_path:
338-
endpoint = str(pathlib.Path("stream") / group / quote(remote_path) / quote(resource))
340+
endpoint = str(pathlib.Path("stream") / group / quote(remote_path) / resource)
339341
else:
340-
endpoint = str(pathlib.Path("stream") / group / quote(resource))
342+
endpoint = str(pathlib.Path("stream") / group / resource)
341343
url = f'{file_api_url(env, pnum, backend, endpoint=endpoint , page=page, per_page=per_page)}'
342344
headers = {'Authorization': 'Bearer {0}'.format(token)}
343345
debug_step(f'listing resources at {url}')
@@ -376,7 +378,7 @@ def survey_list(
376378
per_page: number of files to list per page
377379
378380
"""
379-
endpoint = str(pathlib.Path(directory or "") / "attachments")
381+
endpoint=f"{directory}/attachments"
380382
url = f'{file_api_url(env, pnum, backend, endpoint=endpoint, page=page, per_page=per_page)}'
381383
headers = {'Authorization': 'Bearer {0}'.format(token)}
382384
debug_step(f'listing resources at {url}')
@@ -403,12 +405,10 @@ def import_delete(
403405
) -> requests.Response:
404406
tokens = maybe_refresh(env, pnum, api_key, token, refresh_token, refresh_target)
405407
token = tokens.get("access_token") if tokens else token
406-
endpoint = pathlib.Path("stream")
407-
if group:
408-
endpoint = endpoint / group
409408
if remote_path:
410-
endpoint = endpoint / quote(remote_path)
411-
endpoint = str(endpoint / quote(filename))
409+
endpoint = f'stream/{group}{quote(remote_path)}{quote(filename)}'
410+
else:
411+
endpoint = f'stream/{group}{quote(filename)}'
412412
url = f'{file_api_url(env, pnum, "files", endpoint=endpoint)}'
413413
headers = {'Authorization': f'Bearer {token}'}
414414
print(f'deleting: {filename}')
@@ -431,10 +431,10 @@ def export_delete(
431431
) -> requests.Response:
432432
tokens = maybe_refresh(env, pnum, api_key, token, refresh_token, refresh_target)
433433
token = tokens.get("access_token") if tokens else token
434-
endpoint = pathlib.Path("export")
435434
if remote_path:
436-
endpoint = endpoint / quote(remote_path)
437-
endpoint = str(endpoint / quote(filename))
435+
endpoint = f'export{quote(remote_path)}{quote(filename)}'
436+
else:
437+
endpoint = f'export/{quote(filename)}'
438438
url = f'{file_api_url(env, pnum, "files", endpoint=endpoint)}'
439439
headers = {'Authorization': f'Bearer {token}'}
440440
print(f'deleting: {filename}')
@@ -491,10 +491,9 @@ def export_list(
491491
sys.exit(f'{remote_path} is a file, not a directory')
492492
if not exists:
493493
sys.exit(f'{remote_path} does not exist')
494-
endpoint = pathlib.Path("export")
495-
if remote_path:
496-
endpoint = endpoint / quote(remote_path)
497-
endpoint = str(endpoint / resource)
494+
endpoint = f"export{quote(remote_path)}{resource}"
495+
else:
496+
endpoint = f'export/{resource}'
498497
url = f'{file_api_url(env, pnum, backend, endpoint=endpoint, page=page, per_page=per_page)}'
499498
headers = {'Authorization': 'Bearer {0}'.format(token)}
500499
debug_step(f'listing resources at {url}')
@@ -516,10 +515,10 @@ def export_head(
516515
remote_path: Optional[str] = None,
517516
) -> requests.Response:
518517
headers = {'Authorization': 'Bearer {0}'.format(token), "Accept-Encoding": "*"}
519-
endpoint = pathlib.Path("export")
520518
if remote_path:
521-
endpoint = endpoint / quote(remote_path)
522-
endpoint = str(endpoint / quote(filename))
519+
endpoint = f"export{quote(remote_path)}{quote(filename)}"
520+
else:
521+
endpoint = f'export/{quote(filename)}'
523522
url = f'{file_api_url(env, pnum, backend, endpoint=endpoint)}'
524523
resp = session.head(url, headers=headers)
525524
return resp
@@ -589,12 +588,13 @@ def export_get(
589588
url = dev_url
590589
else:
591590
if backend == 'survey':
592-
urlpath = pathlib.Path("")
591+
urlpath = ''
593592
else:
594-
urlpath = pathlib.Path("export")
595593
if remote_path:
596-
urlpath = urlpath / quote(remote_path)
597-
endpoint = str(urlpath / filename)
594+
urlpath = f"export{quote(remote_path)}"
595+
else:
596+
urlpath = 'export/'
597+
endpoint = f'{urlpath}{filename}'
598598
# make provision for unsatisfactory semantics
599599
if backend in ['export', 'files']:
600600
service = 'files'
@@ -671,7 +671,7 @@ def _resumable_url(
671671
) -> str:
672672
resource = upload_resource_name(filename, is_dir, group=group, remote_path=remote_path)
673673
if not dev_url:
674-
endpoint = str(pathlib.Path("stream") / resource)
674+
endpoint = f"stream/{resource}"
675675
url = f'{file_api_url(env, pnum, backend, endpoint=endpoint)}'
676676
else:
677677
url = dev_url
@@ -1124,8 +1124,8 @@ def delete_resumable(
11241124
if dev_url:
11251125
url = dev_url
11261126
else:
1127-
filename = quote(format_filename(filename)) if filename else ''
1128-
endpoint = str(pathlib.Path('resumables') / f'{filename}?id={upload_id}')
1127+
filename = f'/{quote(format_filename(filename))}' if filename else ''
1128+
endpoint = f'resumables{filename}?id={upload_id}'
11291129
url = f'{file_api_url(env, pnum, backend, endpoint=endpoint)}'
11301130
debug_step(f'deleting {filename} using: {url}')
11311131
resp = session.delete(url, headers={'Authorization': 'Bearer {0}'.format(token)})

tsdapiclient/sync.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import pathlib
32
import time
43
import shutil
54
import sqlite3
@@ -354,7 +353,7 @@ def _find_local_resources(self, path: str) -> list:
354353
break
355354
if ignore_suffix:
356355
continue
357-
target = os.path.normpath(f'{directory}/{file}')
356+
target = f'{directory}/{file}'
358357
if self.sync_mtime:
359358
integrity_reference = str(os.stat(target).st_mtime)
360359
if self.target_dir:
@@ -399,15 +398,15 @@ def _find_remote_resources(self, path: str) -> list:
399398
group=self.group,
400399
backend=list_funcs[self.remote_key]['backend'],
401400
per_page=10000, # for better sync performance
402-
remote_path=self.remote_path,
401+
remote_path=self.remote_path,
403402
)
404403
found = out.get('files')
405404
next_page = out.get('page')
406405
if found:
407406
for entry in found:
408407
import os
409408
subdir_and_resource = os.path.basename(entry.get("href"))
410-
ref = str(pathlib.Path(path) / subdir_and_resource)
409+
ref = f'{path}/{subdir_and_resource}'
411410
ignore_prefix = False
412411
# check if we should ignore it
413412
for prefix in self.ignore_prefixes:
@@ -459,7 +458,7 @@ def _transfer_local_to_remote(
459458
print(f'WARNING: could not find {resource} on local disk')
460459
return resource
461460
if os.stat(resource).st_size > self.chunk_threshold:
462-
print(f'initiating resumable upload for {resource} {self.remote_path}')
461+
print(f'initiating resumable upload for {resource} {self.remote_path}')
463462
resp = initiate_resumable(
464463
self.env,
465464
self.pnum,
@@ -535,7 +534,7 @@ def _transfer_remote_to_local(
535534
refresh_token=self.refresh_token,
536535
refresh_target=self.refresh_target,
537536
public_key=self.public_key,
538-
remote_path=self.remote_path,
537+
remote_path=self.remote_path,
539538
)
540539
if resp.get('tokens'):
541540
self.token = resp.get('tokens').get('access_token')

tsdapiclient/tacl.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -707,14 +707,10 @@ def cli(
707707

708708
token_path = get_claims(token).get('path', None)
709709
if token_path:
710-
if not token_path.startswith('/'):
711-
token_path = f'/{token_path}'
712710
if not token_path.endswith('/'):
713711
token_path = f'{token_path}/'
714712

715713
if remote_path:
716-
if not remote_path.startswith('/'):
717-
remote_path = f"/{remote_path}"
718714
if not remote_path.endswith('/'):
719715
remote_path = f'{remote_path}/'
720716
if token_path and remote_path.startswith(token_path):
@@ -835,7 +831,7 @@ def cli(
835831
token,
836832
etag=download_id,
837833
public_key=public_key,
838-
remote_path=remote_path,
834+
remote_path=remote_path,
839835
)
840836
elif download_list:
841837
debug_step('listing export directory')

0 commit comments

Comments
 (0)