|
1 | 1 | import json |
2 | 2 | import time |
3 | 3 | from uuid import UUID, uuid4 |
| 4 | +import functools |
4 | 5 |
|
5 | 6 | import logging |
6 | 7 | from pathlib import Path |
@@ -114,35 +115,58 @@ class BulkImportRequest(DbObject): |
114 | 115 | created_by = Relationship.ToOne("User", False, "created_by") |
115 | 116 |
|
116 | 117 | @property |
117 | | - def inputs(self): |
| 118 | + def inputs(self) -> Optional[List[Dict[str, str]]]: |
118 | 119 | """ |
119 | 120 | Inputs for each individual annotation uploaded. |
120 | | - * This should match the ndjson annotations that you have uploaded. |
121 | | - * This information will expire after 24 hours. |
122 | | - """ |
123 | | - return self._fetch_remote_ndjson(self.input_file_url) |
| 121 | + This should match the ndjson annotations that you have uploaded. |
124 | 122 | |
| 123 | + Returns: |
| 124 | + Uploaded ndjsons. |
| 125 | +
|
| 126 | + * This information will expire after 24 hours. |
| 127 | + """ |
| 128 | + return self._fetch_remote_ndjson(self.input_file_url) |
| 129 | + |
125 | 130 | @property |
126 | | - def errors(self): |
| 131 | + def errors(self) -> Optional[List[Dict[str, str]]]: |
127 | 132 | """ |
128 | 133 | Errors for each individual annotation uploaded. |
129 | | - * Returns an empty list if there are no errors and None if the update is still running. |
| 134 | +
|
| 135 | + Returns: |
| 136 | + Empty list if there are no errors and None if the update is still running. |
| 137 | + If there are errors, and the job has completed then a list of dicts containing the error messages will be returned. |
| 138 | +
|
130 | 139 | * This information will expire after 24 hours. |
131 | 140 | """ |
132 | | - return self._fetch_remote_ndjson(self.error_file_url) |
| 141 | + return self._fetch_remote_ndjson(self.error_file_url) |
133 | 142 |
|
134 | 143 | @property |
135 | | - def statuses(self): |
| 144 | + def statuses(self) -> Optional[List[Dict[str, str]]]: |
136 | 145 | """ |
137 | 146 | Status for each individual annotation uploaded. |
138 | | - * Returns a status for each row if the upload is done running and was successful. Otherwise it returns None. |
| 147 | +
|
| 148 | + Returns: |
| 149 | + A status for each annotation if the upload is done running and was successful. Otherwise it returns None. |
| 150 | +
|
139 | 151 | * This information will expire after 24 hours. |
140 | 152 | """ |
141 | | - return self._fetch_remote_ndjson(self.status_file_url) |
| 153 | + return self._fetch_remote_ndjson(self.status_file_url) |
| 154 | + |
| 155 | + @functools.lru_cache() |
| 156 | + def _fetch_remote_ndjson( |
| 157 | + self, url: Optional[str]) -> Optional[List[Dict[str, str]]]: |
| 158 | + """ |
| 159 | + Fetches the remote ndjson file and caches the results. |
142 | 160 |
|
143 | | - def _fetch_remote_ndjson(self, url): |
| 161 | + Args: |
| 162 | + url (str): either the input_file_url, error_file_url, status_file_url, or None |
| 163 | + urls are None when the file is unavailable. |
| 164 | + Returns: |
| 165 | + None if the url is None or the ndjson as a list of dicts. |
| 166 | + """ |
144 | 167 | if url is not None: |
145 | 168 | return ndjson.loads(requests.get(url).text) |
| 169 | + return None |
146 | 170 |
|
147 | 171 | def refresh(self) -> None: |
148 | 172 | """Synchronizes values of all fields with the database. |
|
0 commit comments