Coverage for gws-app/gws/plugin/qgis/cli.py: 0%

51 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-16 23:09 +0200

1"""CLI utility for qgis""" 

2 

3import re 

4 

5import gws 

6import gws.config 

7import gws.base.shape 

8import gws.lib.crs 

9import gws.lib.importer 

10import gws.lib.jsonx 

11 

12from . import project 

13 

14 

15class CapsParams(gws.CliParams): 

16 """Parameters for the caps command.""" 

17 

18 src: str 

19 """Source path or postgres address `postgres:<dbUid>/<schema>/<projectName>`""" 

20 out: str = '' 

21 """Output filename""" 

22 

23 

24class CopyParams(gws.CliParams): 

25 """Parameters for the copy command.""" 

26 

27 src: str 

28 """Source path or postgres address `postgres:<dbUid>/<schema>/<projectName>`""" 

29 dst: str 

30 """Source path or postgres address `postgres:<dbUid>/<schema>/<projectName>`""" 

31 

32 

33class Object(gws.Node): 

34 @gws.ext.command.cli('qgisCopy') 

35 def do_copy(self, p: CopyParams): 

36 """Copy a qgis project.""" 

37 

38 root = gws.config.load() 

39 src_prj = project.from_store(root, _addr_to_store(p.src)) 

40 src_prj.to_store(root, _addr_to_store(p.dst)) 

41 

42 @gws.ext.command.cli('qgisCaps') 

43 def do_caps(self, p: CapsParams): 

44 """Print the capabilities of a document in JSON format""" 

45 

46 root = gws.config.load() 

47 src_prj = project.from_store(root, _addr_to_store(p.src)) 

48 caps = src_prj.caps() 

49 

50 js = gws.lib.jsonx.to_pretty_string(caps, default=_caps_json) 

51 

52 if p.out: 

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

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

55 else: 

56 print(js) 

57 

58 

59def _addr_to_store(addr): 

60 m = re.match(r'^postgres:(.*?)/(.*?)/(.+?)$', addr) 

61 if m: 

62 return project.Store(type=project.StoreType.postgres, dbUid=m.group(1), schema=m.group(2), projectName=m.group(3)) 

63 m = re.match(r'^(/.+)$', addr) 

64 if m: 

65 return project.Store( 

66 type=project.StoreType.file, 

67 path=m.group(1), 

68 ) 

69 raise ValueError('invalid path or postgres address') 

70 

71 

72def _caps_json(x): 

73 if isinstance(x, gws.Crs): 

74 return x.epsg 

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

76 return x.to_geojson() 

77 try: 

78 return vars(x) 

79 except TypeError: 

80 return repr(x)