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

46 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +0200

1"""CLI utilty for OWS services""" 

2 

3from typing import Optional, cast 

4 

5import gws 

6import gws.base.shape 

7import gws.gis.crs 

8import gws.lib.importer 

9import gws.lib.jsonx 

10 

11 

12from . import request 

13 

14 

15class CapsParams(gws.CliParams): 

16 src: str 

17 """service URL or an XML file name""" 

18 type: str = '' 

19 """service type, e.g. WMS""" 

20 out: str = '' 

21 """output filename""" 

22 

23 

24class Object(gws.Node): 

25 

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

27 def caps(self, p: CapsParams): 

28 """Print the capabilities of a service in JSON format""" 

29 

30 protocol = None 

31 

32 if p.type: 

33 protocol = p.type.lower() 

34 else: 

35 u = p.src.lower() 

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

37 if s in u: 

38 protocol = s 

39 break 

40 

41 if not protocol: 

42 raise gws.Error('unknown service') 

43 

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

45 xml = request.get_text(request.Args( 

46 url=p.src, 

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

48 verb=gws.OwsVerb.GetCapabilities)) 

49 else: 

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

51 

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

53 res = mod.parse(xml) 

54 

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

56 

57 if p.out: 

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

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

60 else: 

61 print(js) 

62 

63 

64def _caps_json(x): 

65 if isinstance(x, gws.gis.crs.Crs): 

66 return x.epsg 

67 if isinstance(x, gws.base.shape.Shape): 

68 return x.to_geojson() 

69 try: 

70 return vars(x) 

71 except TypeError: 

72 return repr(x)