|
1 | 1 | """Input and output. |
2 | 2 |
|
3 | 3 | In PySTAC v2.0, reading and writing STAC objects has been split into separate |
4 | | -protocols, [Read][pystac.io.Read] and [Write][pystac.io.Write] classes. This |
5 | | -should be a transparent operation for most users: |
| 4 | +protocols, [Read][pystac.io.Read] and [Write][pystac.io.Write]. This should be |
| 5 | +transparent for most users: |
6 | 6 |
|
7 | 7 | ```python |
8 | 8 | catalog = pystac.read_file("catalog.json") |
|
25 | 25 | from pathlib import Path |
26 | 26 | from typing import Any, Protocol |
27 | 27 |
|
| 28 | +from . import deprecate |
28 | 29 | from .errors import PystacError |
29 | 30 | from .stac_object import STACObject |
30 | 31 |
|
31 | 32 |
|
32 | | -def read_file(href: str | Path, reader: Read | None = None) -> STACObject: |
| 33 | +def read_file( |
| 34 | + href: str | Path, |
| 35 | + stac_io: Any = None, |
| 36 | + *, |
| 37 | + reader: Read | None = None, |
| 38 | +) -> STACObject: |
33 | 39 | """Reads a file from a href. |
34 | 40 |
|
35 | | - Uses the default [Reader][pystac.DefaultReader]. |
36 | | -
|
37 | 41 | Args: |
38 | 42 | href: The href to read |
| 43 | + reader: The [Read][pystac.Read] to use for reading |
39 | 44 |
|
40 | 45 | Returns: |
41 | 46 | The STAC object |
42 | | -
|
43 | | - Examples: |
44 | | - >>> item = pystac.read_file("item.json") |
45 | 47 | """ |
| 48 | + if stac_io: |
| 49 | + deprecate.argument("stac_io") |
46 | 50 | return STACObject.from_file(href, reader=reader) |
47 | 51 |
|
48 | 52 |
|
49 | 53 | def write_file( |
50 | | - stac_object: STACObject, |
| 54 | + obj: STACObject, |
| 55 | + include_self_link: bool | None = None, |
| 56 | + dest_href: str | Path | None = None, |
| 57 | + stac_io: Any = None, |
51 | 58 | *, |
52 | | - href: str | Path | None = None, |
53 | 59 | writer: Write | None = None, |
54 | 60 | ) -> None: |
55 | 61 | """Writes a STAC object to a file, using its href. |
56 | 62 |
|
57 | 63 | If the href is not set, this will throw and error. |
58 | 64 |
|
59 | 65 | Args: |
60 | | - stac_object: The STAC object to write |
| 66 | + obj: The STAC object to write |
| 67 | + dest_href: The href to write the STAC object to |
| 68 | + writer: The [Write][pystac.Write] to use for writing |
61 | 69 | """ |
| 70 | + if include_self_link is not None: |
| 71 | + deprecate.argument("include_self_link") |
| 72 | + if stac_io: |
| 73 | + deprecate.argument("stac_io") |
| 74 | + |
62 | 75 | if writer is None: |
63 | 76 | writer = DefaultWriter() |
64 | | - if href is None: |
65 | | - href = stac_object.href |
66 | | - if href is None: |
67 | | - raise PystacError(f"cannot write {stac_object} without an href") |
68 | | - data = stac_object.to_dict() |
69 | | - if isinstance(href, Path): |
70 | | - writer.write_json_to_path(data, href) |
| 77 | + |
| 78 | + if dest_href is None: |
| 79 | + dest_href = obj.href |
| 80 | + if dest_href is None: |
| 81 | + raise PystacError(f"cannot write {obj} without an href") |
| 82 | + d = obj.to_dict() |
| 83 | + if isinstance(dest_href, Path): |
| 84 | + writer.write_json_to_path(d, dest_href) |
71 | 85 | else: |
72 | | - url = urllib.parse.urlparse(href) |
| 86 | + url = urllib.parse.urlparse(dest_href) |
73 | 87 | if url.scheme: |
74 | | - writer.write_json_to_url(data, href) |
| 88 | + writer.write_json_to_url(d, dest_href) |
75 | 89 | else: |
76 | | - writer.write_json_to_path(data, Path(href)) |
77 | | - |
78 | | - |
79 | | -def make_absolute_href(href: str, base: str | None) -> str: |
80 | | - if urllib.parse.urlparse(href).scheme: |
81 | | - return href # TODO file:// schemes |
82 | | - |
83 | | - if base: |
84 | | - if urllib.parse.urlparse(base).scheme: |
85 | | - raise NotImplementedError("url joins not implemented yet, should be easy") |
86 | | - else: |
87 | | - if base.endswith("/"): # TODO windoze |
88 | | - return str((Path(base) / href).resolve(strict=False)) |
89 | | - else: |
90 | | - return str((Path(base).parent / href).resolve(strict=False)) |
91 | | - else: |
92 | | - raise NotImplementedError |
| 90 | + writer.write_json_to_path(d, Path(dest_href)) |
93 | 91 |
|
94 | 92 |
|
95 | 93 | class Read(Protocol): |
|
0 commit comments