Skip to content

Commit

Permalink
Merge pull request #87 from girder/upload-left-atrium
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeezley authored Jun 7, 2021
2 parents e3f052c + 76d7a1c commit 9378e7d
Show file tree
Hide file tree
Showing 3 changed files with 1,146 additions and 167 deletions.
5 changes: 5 additions & 0 deletions swcc/swcc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import requests
from requests_toolbelt.sessions import BaseUrlSession
from s3_file_field_client import S3FileFieldClient
from urllib3.util.retry import Retry

from . import __version__

Expand Down Expand Up @@ -41,6 +42,10 @@ def __init__(
base_url = f'{base_url.rstrip("/")}/' # tolerate input with or without trailing slash
super().__init__(base_url=base_url, **kwargs)

retry = Retry()
adapter = requests.adapters.HTTPAdapter(max_retries=retry)
self.mount(base_url, adapter)

self.headers.update(
{
'User-agent': f'swcc/{__version__}',
Expand Down
25 changes: 17 additions & 8 deletions swcc/swcc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pathlib import Path
from typing import Any, Dict, Generic, Iterator, Literal, Optional, Type, TypeVar, Union, get_args
from urllib.parse import unquote

from pydantic import (
AnyHttpUrl,
Expand Down Expand Up @@ -98,8 +99,6 @@ def upload(self):
return self.field_value

def download(self, path: Union[Path, str]) -> Path:
session = current_session()

if self.url is None:
raise Exception('Cannot download a local file')

Expand All @@ -114,14 +113,24 @@ def download(self, path: Union[Path, str]) -> Path:
path.mkdir(parents=True, exist_ok=True)

path = path / self.url.path.split('/')[-1]
r = session.get(self.url, stream=True)
r = requests.get(self.url, stream=True)
raise_for_status(r)

with path.open('wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
return path

@property
def name(self) -> str:
if self.path:
return self.path.name
elif self.url:
if self.url.path is None:
raise Exception('Invalid file url')
return unquote(self.url.path.split('/')[-1])
raise Exception('Invalid file object')


ModelType = TypeVar('ModelType', bound='ApiModel')

Expand Down Expand Up @@ -285,16 +294,16 @@ def add_groomed_segmentation(
self,
file: Path,
segmentation: Segmentation,
pre_cropping: Optional[Path],
pre_alignment: Optional[Path],
pre_cropping: Optional[Path] = None,
pre_alignment: Optional[Path] = None,
) -> GroomedSegmentation:
return GroomedSegmentation(
file=file,
segmentation=segmentation,
project=self,
pre_cropping=pre_cropping,
pre_alignment=pre_alignment,
)
).create()

@property
def shape_models(self) -> Iterator[OptimizedShapeModel]:
Expand All @@ -312,8 +321,8 @@ class GroomedSegmentation(ApiModel):
_endpoint = 'groomed-segmentations'

file: FileType[Literal['core.GroomedSegmentation.file']]
pre_cropping: Optional[FileType[Literal['core.GroomedSegmentation.pre_cropping']]]
pre_alignment: Optional[FileType[Literal['core.GroomedSegmentation.pre_alignment']]]
pre_cropping: Optional[FileType[Literal['core.GroomedSegmentation.pre_cropping']]] = None
pre_alignment: Optional[FileType[Literal['core.GroomedSegmentation.pre_alignment']]] = None

segmentation: Segmentation
project: Project
Expand Down
Loading

0 comments on commit 9378e7d

Please sign in to comment.