@@ -46,14 +46,19 @@ def read_text(
46
46
) -> str :
47
47
"""Read text from the given URI.
48
48
49
- The source to read from can be specified
50
- as a string or a Link. If it's a string, it's the URL of the HREF from which to
51
- read. When reading links, PySTAC will pass in the entire link body.
52
- This enables implementations to utilize additional link information,
53
- e.g. the "post" information in a pagination link from a STAC API search.
49
+ The source to read from can be specified as a string or a
50
+ :class:`~pystac.Link`. If it is a string, it must be a URI or local path from
51
+ which to read. Using a :class:`~pystac.Link` enables implementations to use
52
+ additional link information, such as paging information contained in the
53
+ extended links described in the `STAC API spec
54
+ <https://github.com/radiantearth/stac-api-spec/tree/master/item-search#paging>`__.
54
55
55
56
Args:
56
57
source : The source to read from.
58
+ *args : Arbitrary positional arguments that may be utilized by the concrete
59
+ implementation.
60
+ **kwargs : Arbitrary keyword arguments that may be utilized by the concrete
61
+ implementation.
57
62
58
63
Returns:
59
64
str: The text contained in the file at the location specified by the uri.
@@ -66,10 +71,10 @@ def write_text(
66
71
) -> None :
67
72
"""Write the given text to a file at the given URI.
68
73
69
- The destination to write to from can be specified
70
- as a string or a Link. If it's a string, it's the URL of the HREF from which to
71
- read. When writing based on links links, PySTAC will pass in the entire
72
- link body .
74
+ The destination to write to from can be specified as a string or a
75
+ :class:`~pystac. Link` . If it is a string, it must be a URI or local path from
76
+ which to read. Using a :class:`~pystac.Link` enables implementations to use
77
+ additional link information .
73
78
74
79
Args:
75
80
dest : The destination to write to.
@@ -122,6 +127,21 @@ def stac_object_from_dict(
122
127
root : Optional ["Catalog_Type" ] = None ,
123
128
preserve_dict : bool = True ,
124
129
) -> "STACObject_Type" :
130
+ """Deserializes a :class:`~pystac.STACObject` sub-class instance from a
131
+ dictionary.
132
+
133
+ Args:
134
+
135
+ d : The dictionary to deserialize
136
+ href : Optional href to associate with the STAC object
137
+ root : Optional root :class:`~pystac.Catalog` to associate with the
138
+ STAC object.
139
+ preserve_dict: If ``False``, the dict parameter ``d`` may be modified
140
+ during this method call. Otherwise the dict is not mutated.
141
+ Defaults to ``True``, which results results in a deepcopy of the
142
+ parameter. Set to ``False`` when possible to avoid the performance
143
+ hit of a deepcopy.
144
+ """
125
145
if identify_stac_object_type (d ) == pystac .STACObjectType .ITEM :
126
146
collection_cache = None
127
147
if root is not None :
@@ -244,18 +264,31 @@ def default(cls) -> "StacIO":
244
264
245
265
class DefaultStacIO (StacIO ):
246
266
def read_text (
247
- self , source : Union [str , "Link_Type" ], * args : Any , ** kwargs : Any
267
+ self , source : Union [str , "Link_Type" ], * _ : Any , ** __ : Any
248
268
) -> str :
269
+ """A concrete implementation of :meth:`StacIO.read_text <pystac.StacIO.read_text>`. Converts the
270
+ ``source`` argument to a string (if it is not already) and delegates to
271
+ :meth:`DefaultStacIO.read_text_from_href` for opening and reading the file."""
249
272
href : Optional [str ]
250
273
if isinstance (source , str ):
251
274
href = source
252
275
else :
253
276
href = source .get_absolute_href ()
254
277
if href is None :
255
278
raise IOError (f"Could not get an absolute HREF from link { source } " )
256
- return self .read_text_from_href (href , * args , ** kwargs )
279
+ return self .read_text_from_href (href )
280
+
281
+ def read_text_from_href (self , href : str ) -> str :
282
+ """Reads file as a UTF-8 string.
283
+
284
+ If ``href`` has a "scheme" (e.g. if it starts with "https://") then this will
285
+ use :func:`urllib.request.urlopen` to open the file and read the contents;
286
+ otherwise, :func:`open` will be used to open a local file.
287
+
288
+ Args:
257
289
258
- def read_text_from_href (self , href : str , * args : Any , ** kwargs : Any ) -> str :
290
+ href : The URI of the file to open.
291
+ """
259
292
parsed = safe_urlparse (href )
260
293
href_contents : str
261
294
if parsed .scheme != "" :
@@ -270,20 +303,33 @@ def read_text_from_href(self, href: str, *args: Any, **kwargs: Any) -> str:
270
303
return href_contents
271
304
272
305
def write_text (
273
- self , dest : Union [str , "Link_Type" ], txt : str , * args : Any , ** kwargs : Any
306
+ self , dest : Union [str , "Link_Type" ], txt : str , * _ : Any , ** __ : Any
274
307
) -> None :
308
+ """A concrete implementation of :meth:`StacIO.write_text <pystac.StacIO.write_text>`. Converts the
309
+ ``dest`` argument to a string (if it is not already) and delegates to
310
+ :meth:`DefaultStacIO.write_text_from_href` for opening and reading the file."""
275
311
href : Optional [str ]
276
312
if isinstance (dest , str ):
277
313
href = dest
278
314
else :
279
315
href = dest .get_absolute_href ()
280
316
if href is None :
281
317
raise IOError (f"Could not get an absolute HREF from link { dest } " )
282
- return self .write_text_to_href (href , txt , * args , ** kwargs )
318
+ return self .write_text_to_href (href , txt )
283
319
284
320
def write_text_to_href (
285
- self , href : str , txt : str , * args : Any , ** kwargs : Any
321
+ self , href : str , txt : str
286
322
) -> None :
323
+ """Writes text to file using UTF-8 encoding.
324
+
325
+ This implementation uses :func:`open` and therefore can only write to the local
326
+ file system.
327
+
328
+ Args:
329
+
330
+ href : The path to which the file will be written.
331
+ txt : The string content to write to the file.
332
+ """
287
333
dirname = os .path .dirname (href )
288
334
if dirname != "" and not os .path .isdir (dirname ):
289
335
os .makedirs (dirname )
@@ -292,16 +338,16 @@ def write_text_to_href(
292
338
293
339
294
340
class DuplicateKeyReportingMixin (StacIO ):
295
- """A mixin for StacIO implementations that will report
341
+ """A mixin for :class:`pystac. StacIO` implementations that will report
296
342
on duplicate keys in the JSON being read in.
297
343
298
344
See https://github.com/stac-utils/pystac/issues/313
299
345
"""
300
346
301
347
def json_loads (self , txt : str , * _ : Any , ** __ : Any ) -> Dict [str , Any ]:
302
- """Overwrites :meth:`StacIO.json_loads` as the internal method used by
303
- :class:`DuplicateKeyReportingMixin` for deserializing a JSON string to a
304
- dictionary while checking for duplicate object keys.
348
+ """Overwrites :meth:`StacIO.json_loads <pystac.StacIO.json_loads> ` as the
349
+ internal method used by :class:`DuplicateKeyReportingMixin` for deserializing
350
+ a JSON string to a dictionary while checking for duplicate object keys.
305
351
306
352
Raises:
307
353
@@ -315,8 +361,9 @@ def json_loads(self, txt: str, *_: Any, **__: Any) -> Dict[str, Any]:
315
361
def read_json (
316
362
self , source : Union [str , "Link_Type" ], * args : Any , ** kwargs : Any
317
363
) -> Dict [str , Any ]:
318
- """Overwrites :meth:`StacIO.read_json` for deserializing a JSON file to a
319
- dictionary while checking for duplicate object keys.
364
+ """Overwrites :meth:`StacIO.read_json <pystac.StacIO.read_json>` for
365
+ deserializing a JSON file to a dictionary while checking for duplicate object
366
+ keys.
320
367
321
368
Raises:
322
369
0 commit comments