-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
146 lines (112 loc) · 3.93 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from cwl_utils.parser import load_document_by_uri
import cwl_utils
import os
from typing import Any, Dict, Optional, List
from pydantic import BaseModel, DirectoryPath
from pydantic.fields import ModelField
import json
import requests
import pystac
import stac_asset
import asyncio
from pathlib import PosixPath
def get_release_assets(
user="Terradue", repo="app-package-training-bids23", token="", page=1
):
pat = token
result = {}
response = requests.get(
f"https://api.github.com/repos/{user}/{repo}/releases?per_page=100&page={page}",
headers={"Authorization": "token " + pat},
)
for release in response.json():
local_assets = []
for asset in release["assets"]:
cwl_obj = load_document_by_uri(asset["browser_download_url"])
local_assets.append(
{
"url": asset["browser_download_url"],
"cwl": cwl_obj,
"label": cwl_obj.label,
"doc": cwl_obj.doc,
}
)
result[release["tag_name"]] = local_assets
return result
def my_dumps(v, *, default):
for key, value in v.items():
if isinstance(value, PosixPath):
v[key] = {"class": "Directory", "path": str(value)}
else:
v[key] = value
return json.dumps(v)
class Params(BaseModel):
@classmethod
def set_fields(cls, **field_definitions: Any):
cls.__fields__ = {}
new_fields: Dict[str, ModelField] = {}
new_annotations: Dict[str, Optional[type]] = {}
for f_name, f_def in field_definitions.items():
if isinstance(f_def, tuple):
try:
f_annotation, f_value = f_def
except ValueError as e:
raise Exception(
"field definitions should either be a tuple of (<type>, <default>) or just a "
"default value, unfortunately this means tuples as "
"default values are not allowed"
) from e
else:
f_annotation, f_value = None, f_def
if f_annotation:
new_annotations[f_name] = f_annotation
new_fields[f_name] = ModelField.infer(
name=f_name,
value=f_value,
annotation=f_annotation,
class_validators=None,
config=cls.__config__,
)
cls.__fields__.update(new_fields)
@classmethod
def clear_fields(cls):
cls.__fields__ = {}
@classmethod
def get_fields(cls):
return cls.__fields__
class Config:
json_dumps = my_dumps
def get_param_model_fields(cwl_obj):
fields = {}
for inp in cwl_obj.inputs:
key = os.path.basename(inp.id)
if inp.type_ == "string":
input_type = str
if inp.type_ == "Directory":
input_type = DirectoryPath
if isinstance(inp.type_, cwl_utils.parser.cwl_v1_0.InputArraySchema):
if inp.type_.items == "string":
input_type = List[str]
else:
input_type = List
if not inp.default:
fields[key] = (input_type, ...)
else:
fields[key] = (input_type, inp.default)
return fields
async def stage_in(stac_item, target_dir="."):
config = stac_asset.Config(warn=True)
os.makedirs(os.path.join(target_dir, stac_item.id), exist_ok=True)
cwd = os.getcwd()
os.chdir(os.path.join(target_dir, stac_item.id))
item = await stac_asset.download_item(item=stac_item, directory=".", config=config)
os.chdir(cwd)
cat = pystac.Catalog(
id="catalog",
description=f"catalog with staged {item.id}",
title=f"catalog with staged {item.id}",
)
cat.add_item(item)
cat.normalize_hrefs(target_dir)
cat.save(catalog_type=pystac.CatalogType.SELF_CONTAINED)
return cat