Coverage for attribuutit/cli.py: 91.18%

44 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 15:52:25 +00:00

1#! /usr/bin/env python 

2"""Scan a tree of files and extract attributes of the underlying vector or raster data.""" 

3import json 

4import pathlib 

5import sys 

6 

7import typer 

8 

9import attribuutit.shp as shp 

10import attribuutit.tif as tif 

11import attribuutit.vpf as vpf 

12 

13app = typer.Typer() 

14 

15 

16@app.command() 

17def eject(name: str) -> int: 

18 """Eject an example configuration in JSON format with stem equal to name.""" 

19 typer.echo(f'This will eject an example configuration as {name}.json') 

20 return 0 

21 

22 

23@app.command() 

24def scan(name: str, dry_run: bool = False) -> int: 

25 """Scan the file system based on the configuration given in name.json and persist the report eventually.""" 

26 if not dry_run: 

27 typer.echo(f'Real run changing the world following the configuration {name}.json - later ...') 

28 return 2 

29 

30 typer.echo(f'Dry run exposing the plan of execution when following the configuration {name}.json - later ...') 

31 return 0 

32 

33 

34@app.command() 

35def inspect(path_str: str, dry_run: bool = False) -> int: 

36 """Inspect the file at path and persist the report eventually.""" 

37 if not dry_run: 

38 typer.echo(f'Real run inspecting file at {path_str}') 

39 path = pathlib.Path(path_str) 

40 suffix = path.suffix 

41 if suffix == '.shp': 

42 error, shape = shp.load(path) 

43 if not error: 43 ↛ 46line 43 didn't jump to line 46, because the condition on line 43 was never false

44 print(shp.summary(shape)) 

45 else: 

46 print(error) 

47 elif suffix == '.tif': 

48 error, bitmap = tif.load(path) 

49 if not error: 49 ↛ 52line 49 didn't jump to line 52, because the condition on line 49 was never false

50 print(tif.summary(bitmap)) 

51 else: 

52 print(error) 

53 else: 

54 with open(path, 'rb') as source: 

55 vpf_artifact = vpf.Table(str(path), (b for b in source.read())) 

56 print(json.dumps(vpf_artifact.table, indent=2)) 

57 return 0 

58 

59 typer.echo(f'Dry run exposing the plan of execution when inspecting file at {path_str}') 

60 return 0 

61 

62 

63if __name__ == '__main__': 63 ↛ 64line 63 didn't jump to line 64, because the condition on line 63 was never true

64 sys.exit(app())