Coverage for gws-app/gws/base/ows/client/cli.py: 0%

45 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-05 13:35 +0200

1"""CLI command for OWS services.""" 

2 

3from typing import Optional, cast 

4 

5import gws 

6import gws.lib.shape 

7import gws.lib.crs 

8import gws.lib.dynimport 

9import gws.lib.jsonx 

10 

11 

12from . import request 

13 

14 

15class CapsParams(gws.CliParams): 

16 """Parameters for the ``owsCaps`` command.""" 

17 

18 src: str 

19 """Service URL or XML file name.""" 

20 type: str = '' 

21 """Service type, e.g. WMS. If omitted, it is guessed from ``src``.""" 

22 out: str = '' 

23 """Output file name. If omitted, the result is printed.""" 

24 

25 

26class Object(gws.Node): 

27 """CLI commands for OWS services.""" 

28 

29 @gws.ext.command.cli('owsCaps') 

30 def caps(self, p: CapsParams): 

31 """Print the capabilities of a service in JSON format.""" 

32 

33 protocol = None 

34 

35 if p.type: 

36 protocol = p.type.lower() 

37 else: 

38 u = p.src.lower() 

39 for s in ('wms', 'wmts', 'wfs'): 

40 if s in u: 

41 protocol = s 

42 break 

43 

44 if not protocol: 

45 raise gws.Error('unknown service') 

46 

47 if p.src.startswith(('http:', 'https:')): 

48 xml = request.get_text(request.Args( 

49 url=p.src, 

50 protocol=cast(gws.OwsProtocol, protocol.upper()), 

51 verb=gws.OwsVerb.GetCapabilities)) 

52 else: 

53 xml = gws.u.read_file(p.src) 

54 

55 mod = gws.lib.dynimport.import_from_path(f'gws/plugin/ows_client/{protocol}/caps.py') 

56 res = mod.parse(xml) 

57 

58 js = gws.lib.jsonx.to_pretty_string(res, default=_caps_json) 

59 

60 if p.out: 

61 gws.u.write_file(p.out, js) 

62 gws.log.info(f'saved to {p.out!r}') 

63 else: 

64 print(js) 

65 

66 

67def _caps_json(x): 

68 """Convert an object that is not JSON-serializable for the JSON output.""" 

69 if isinstance(x, gws.lib.crs.Object): 

70 return x.epsg 

71 if isinstance(x, gws.lib.shape.Shape): 

72 return x.to_geojson() 

73 try: 

74 return vars(x) 

75 except TypeError: 

76 return repr(x)