File tree Expand file tree Collapse file tree 4 files changed +79
-0
lines changed Expand file tree Collapse file tree 4 files changed +79
-0
lines changed Original file line number Diff line number Diff line change 5
5
### Added
6
6
7
7
- Add netCDF to pystac.media_type ([ #1386 ] ( https://github.com/stac-utils/pystac/pull/1386 ) )
8
+ - Add convenience method for accessing pystac_client ([ #1365 ] ( https://github.com/stac-utils/pystac/pull/1365 ) )
8
9
9
10
### Changed
10
11
Original file line number Diff line number Diff line change @@ -86,6 +86,18 @@ as to serialize and deserialize STAC object to and from JSON.
86
86
* :class: `pystac.stac_io.DefaultStacIO `: The default :class: `pystac.StacIO `
87
87
implementation used throughout the library.
88
88
89
+ Client
90
+ ------
91
+
92
+ A convenience method for accessing `pystac-client <https://github.com/stac-utils/pystac-client >`__
93
+
94
+ **Example: **
95
+
96
+ .. code-block :: python
97
+
98
+ from pystac.client import Client
99
+
100
+
89
101
Extensions
90
102
----------
91
103
Original file line number Diff line number Diff line change
1
+ # mypy: ignore-errors
2
+
3
+ _import_error_message = (
4
+ "pystac-client is not installed.\n \n "
5
+ "Please install pystac-client:\n \n "
6
+ " pip install pystac-client"
7
+ )
8
+
9
+ try :
10
+ from pystac_client import * # noqa: F403
11
+ except ImportError as e :
12
+ if e .msg == "No module named 'pystac_client'" :
13
+ raise ImportError (_import_error_message ) from e
14
+ else :
15
+ raise
16
+
17
+
18
+ def __getattr__ (value : str ):
19
+ try :
20
+ import pystac_client
21
+ except ImportError as e :
22
+ raise ImportError (_import_error_message ) from e
23
+ return getattr (pystac_client , value )
Original file line number Diff line number Diff line change
1
+ # mypy: ignore-errors
2
+ import builtins
3
+ import sys
4
+ from types import ModuleType
5
+ from typing import Any
6
+
7
+ import pytest
8
+
9
+ real_import = builtins .__import__
10
+
11
+ try :
12
+ import pystac_client # noqa:F401
13
+
14
+ HAS_PYSTAC_CLIENT = True
15
+ except ImportError :
16
+ HAS_PYSTAC_CLIENT = False
17
+
18
+
19
+ @pytest .mark .skipif (not HAS_PYSTAC_CLIENT , reason = "requires pystac_client" )
20
+ def test_import_pystac_client_when_available () -> None :
21
+ from pystac .client import Client
22
+
23
+ assert Client .__doc__
24
+
25
+
26
+ # copied from
27
+ # http://materials-scientist.com/blog/2021/02/11/mocking-failing-module-import-python/
28
+ def monkey_import_importerror (name : str , * args : Any , ** kwargs : Any ) -> ModuleType :
29
+ if name == "pystac_client" :
30
+ raise ImportError (f"No module named '{ name } '" )
31
+ return real_import (name , * args , ** kwargs )
32
+
33
+
34
+ def test_import_pystac_client_when_not_available (monkeypatch : Any ) -> None :
35
+ if HAS_PYSTAC_CLIENT :
36
+ monkeypatch .delitem (sys .modules , "pystac_client" , raising = False )
37
+ monkeypatch .setattr (builtins , "__import__" , monkey_import_importerror )
38
+
39
+ with pytest .raises (ImportError ):
40
+ from pystac_client import Client # noqa:F401
41
+
42
+ with pytest .raises (ImportError , match = "Please install pystac-client:" ):
43
+ from pystac .client import Client # noqa:F401,F811
You can’t perform that action at this time.
0 commit comments