Coverage for gws-app/gws/gis/cache/_test/util.py: 100%

44 statements  

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

1"""Test helpers for the cache package.""" 

2 

3import os 

4 

5import gws 

6import gws.base.grabber.box 

7import gws.base.grabber.core 

8import gws.lib.crs 

9import gws.lib.extent 

10import gws.lib.image 

11import gws.lib.mime 

12import gws.lib.osx 

13 

14RED = (255, 0, 0, 255) 

15PNG = gws.ImageFormat(name='png', mimeTypes=[gws.lib.mime.PNG], options={}) 

16 

17 

18class Grabber(gws.base.grabber.box.Object): 

19 def __init__(self, opts): 

20 super().__init__(opts) 

21 self.sourceCrs = self.targetCrs 

22 self.fetches = [] 

23 self.error = None 

24 

25 def fetch_box_as_image(self, bounds, width, height, params=None): 

26 self.fetches.append((bounds.extent, width, height)) 

27 if self.error: 

28 raise self.error 

29 return gws.lib.image.from_size((width, height), color=RED) 

30 

31 

32class Root: 

33 def __init__(self, layers): 

34 self.layers = layers 

35 

36 def find_all(self, cls): 

37 return self.layers 

38 

39 

40def system_dirs(): 

41 gws.u.ensure_dir(gws.c.LOCKS_DIR) 

42 gws.u.ensure_dir(gws.c.EPHEMERAL_DIR) 

43 gws.u.ensure_dir(gws.c.MAP_CACHE_DIR) 

44 

45 

46def cache_name(): 

47 return 'cache_' + gws.u.random_string(8) 

48 

49 

50def grabber(name=None, max_age=3600, max_level=3, srid=3857) -> Grabber: 

51 crs = gws.lib.crs.get(srid) 

52 cache = gws.MapCache( 

53 name=name or cache_name(), 

54 maxAge=max_age, 

55 maxLevel=max_level, 

56 requestBuffer=0, 

57 requestTiles=4, 

58 ) 

59 opts = gws.base.grabber.core.Options( 

60 crs=crs, 

61 cache=cache, 

62 extent=gws.lib.extent.transform_from_wgs(crs.wgsMaxExtent, crs), 

63 imageFormat=PNG, 

64 ) 

65 return Grabber(opts) 

66 

67 

68def layer(uid, grabbers, title='', ext_type='type_1'): 

69 return gws.Data(uid=uid, title=title, extType=ext_type, grabbers={gr.targetCrs.srid: gr for gr in grabbers}) 

70 

71 

72def make_old(path, age): 

73 t = gws.u.stime() - age 

74 for p in [path] if os.path.isfile(path) else gws.lib.osx.find_files(path): 

75 os.utime(p, (t, t))