-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path__init__.py
72 lines (60 loc) · 2.6 KB
/
__init__.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
import os
from collections import OrderedDict
from typing import Callable, Dict, List
import config
from jobs.base.job import Job
from util.files import Files, assert_within
from .elife import pull_elife
from .gdoc import pull_gdoc
from .gdrive import pull_gdrive
from .github import pull_github
from .gsheet import pull_gsheet
from .http import pull_http
from .plos import pull_plos
from .upload import pull_upload
# Functions for pulling individual source types
PULL_FUNCS: Dict[str, Callable[..., Files]] = {
"elife": pull_elife,
"github": pull_github,
"googledocs": pull_gdoc,
"googlesheets": pull_gsheet,
"googledrive": pull_gdrive,
"http": pull_http,
"plos": pull_plos,
"upload": pull_upload,
"url": pull_http, # URL is used as an alias for HTTP e.g `UrlSource`
}
class Pull(Job):
"""
A job that pulls a source to disk.
This class delegates to the functions for each type of
source defined in `PULL_FUNCS`. Each of those functions
should have the same call signature as this class's `do`
method (except that project is a path string, rather than an int).
"""
name = "pull"
def do(self, source: dict, path: str, secrets: Dict = {}, **kwargs): # type: ignore
"""
Pull `source` to `path` within `project`.
:param source: A dictionary with `type` and any other keys required to
pull the source (e.g. urls, authentication tokens).
:param path: The path, within the project, to pull the source to;
could be the path of a directory or file; may not yet exist.
:param secrets: Authentication credentials, API keys and other secrets needed
to pull the source. Secrets are not displayed in job listings.
:returns: A list of paths, with the project, created by the pull.
"""
assert isinstance(source, dict), "source must be a dictionary"
assert "type" in source, "source must have a type"
assert isinstance(path, str), "path must be a string"
assert_within(".", path) if path else True
assert isinstance(secrets, dict), "secrets must be a dictionary"
# Resolve the pull function based on the source type
typ = source["type"].lower()
if typ not in PULL_FUNCS:
raise ValueError("Unknown source type: {}".format(typ))
pull_func = PULL_FUNCS[typ]
# Call the function to get a dictionary of files
files = pull_func(source=source, path=path, secrets=secrets)
# Sort the dictionary by file path
return OrderedDict(sorted(files.items()))