Skip to content

Commit

Permalink
use pathlib for concatenating paths
Browse files Browse the repository at this point in the history
  • Loading branch information
trondth committed Jan 15, 2025
1 parent 7395aaf commit a6bda19
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions tsdapiclient/fileapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hashlib
import json
import os
import pathlib

from functools import cmp_to_key
from typing import Optional, Union, Any, Iterable
Expand Down Expand Up @@ -43,7 +44,7 @@

class Bar:
"""Simple progress bar.
This class implements a progress bar compatible with (this module's usage
of) progress.bar.Bar on top of rich.progress.
Expand All @@ -58,10 +59,10 @@ class Bar:
def __init__(self, title: str, index: float = 0, max: float = 100, suffix: str = ""):
self.task_id = self.progress.add_task(title, total=max, completed=index)
self.progress.start()

def next(self):
self.progress.advance(self.task_id)

def finish(self):
self.progress.stop()

Expand Down Expand Up @@ -116,26 +117,22 @@ def format_filename(filename: str) -> str:


def upload_resource_name(filename: str, is_dir: bool, group: Optional[str] = None, remote_path: Optional[str] = None) -> str:
if not group:
group = ""
if not is_dir:
debug_step('uploading file')
resource = quote(format_filename(filename))
resource = "/" / pathlib.Path(quote(format_filename(filename)))
if remote_path:
resource = f'{quote(remote_path)}{resource}'
else:
resource = f'/{resource}'
resource = quote(remote_path) / resource
if group:
resource = f'{group}{resource}'
elif is_dir:
resource = group / resource
else:
debug_step('uploading directory (file)')
if filename.startswith('/'):
target = filename[1:]
else:
target = filename
if remote_path:
resource = f'{group}{quote(remote_path)}{quote(target)}'
resource = pathlib.Path("/") / group / quote(remote_path) / quote(filename)
else:
resource = f'{group}/{quote(target)}'
return resource
resource = pathlib.Path("/") / group / quote(filename)
return str(resource)


def lazy_reader(
Expand Down Expand Up @@ -335,10 +332,12 @@ def import_list(
"""
resource = quote(directory) if directory else ''
if not group:
group = ""
if remote_path:
endpoint=f"stream/{group}{quote(remote_path)}{resource}"
endpoint = str(pathlib.Path("stream") / group / quote(remote_path) / resource)
else:
endpoint=f"stream/{group}{resource}"
endpoint = str(pathlib.Path("stream") / group / resource)
url = f'{file_api_url(env, pnum, backend, endpoint=endpoint , page=page, per_page=per_page)}'
headers = {'Authorization': 'Bearer {0}'.format(token)}
debug_step(f'listing resources at {url}')
Expand Down Expand Up @@ -400,7 +399,7 @@ def import_delete(
api_key: Optional[str] = None,
refresh_token: Optional[str] = None,
refresh_target: Optional[int] = None,
remote_path: Optional[str] = None,
remote_path: Optional[str] = None,
) -> requests.Response:
tokens = maybe_refresh(env, pnum, api_key, token, refresh_token, refresh_target)
token = tokens.get("access_token") if tokens else token
Expand Down Expand Up @@ -453,7 +452,7 @@ def export_list(
page: Optional[str] = None,
group: Optional[str] = None,
per_page: Optional[int] = None,
remote_path: Optional[str] = None,
remote_path: Optional[str] = None,
) -> dict:
"""
Get the list of files available for export.
Expand All @@ -472,8 +471,8 @@ def export_list(
"""
resource = directory if directory else ''
if remote_path:
if remote_path:

if not resource :
# checks if remote path is a file or a directory
split_path = remote_path.split('/')
Expand Down Expand Up @@ -516,7 +515,7 @@ def export_head(
headers = {'Authorization': 'Bearer {0}'.format(token), "Accept-Encoding": "*"}
if remote_path:
endpoint = f"export{quote(remote_path)}{quote(filename)}"
else:
else:
endpoint = f'export/{quote(filename)}'
url = f'{file_api_url(env, pnum, backend, endpoint=endpoint)}'
resp = session.head(url, headers=headers)
Expand Down Expand Up @@ -731,7 +730,7 @@ def get_resumable(
api_key: Optional[str] = None,
refresh_token: Optional[str] = None,
refresh_target: Optional[int] = None,
remote_path: Optional[str] = None,
remote_path: Optional[str] = None,
) -> dict:
"""
List uploads which can be resumed.
Expand Down Expand Up @@ -785,7 +784,7 @@ def initiate_resumable(
api_key: Optional[str] = None,
refresh_token: Optional[str] = None,
refresh_target: Optional[int] = None,
remote_path: Optional[str] = None,
remote_path: Optional[str] = None,
) -> dict:
"""
Performs a resumable upload, either by resuming a partial one,
Expand Down Expand Up @@ -937,7 +936,7 @@ def _start_resumable(
api_key: Optional[str] = None,
refresh_token: Optional[str] = None,
refresh_target: Optional[int] = None,
remote_path: Optional[str] = None,
remote_path: Optional[str] = None,
) -> dict:
"""
Start a new resumable upload, reding a file, chunk-by-chunk
Expand Down Expand Up @@ -1021,7 +1020,7 @@ def _continue_resumable(
api_key: Optional[str] = None,
refresh_token: Optional[str] = None,
refresh_target: Optional[int] = None,
remote_path: Optional[str] = None,
remote_path: Optional[str] = None,
) -> dict:
"""
Continue a resumable upload, reding a file, from the
Expand Down

0 comments on commit a6bda19

Please sign in to comment.