Coverage for putki/things.py: 0.00%
83 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-04 22:07:46 +00:00
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-04 22:07:46 +00:00
1"""Identification of parts given an area and perspectives from the declaration of structures."""
3import os
4import pathlib
5import sys
6from typing import Any, Union
8import yaml
10# server
11PIPE_TOOL_BASE = pathlib.Path('/opt/pipe')
12PIPE_NAME = 'pipe'
13PIPE_SRC = PIPE_TOOL_BASE / PIPE_NAME
14PIPE_PERMS = 0o755
15LOG_HEAD_NAME = 'log_head.html'
16LOG_HEAD_SRC = PIPE_TOOL_BASE / LOG_HEAD_NAME
17LOG_TAIL_NAME = 'log_tail.html'
18LOG_TAIL_SRC = PIPE_TOOL_BASE / LOG_TAIL_NAME
19LOG_PERMS = 0o644
21# client
22STRUCTURE = 'structure'
23STRUCTURES = f'{STRUCTURE}s'
26def read_structures(path: Union[str, pathlib.Path]) -> Union[dict[str, dict[str, str]], Any]:
27 """Return the data of the structures."""
28 with open(path, 'rt', encoding='utf-8') as handle:
29 return yaml.safe_load(handle)
32def elucidate(area: str, perspectives: list[str]) -> int:
33 """Discover and verify the parts of the given area and perspectives."""
34 if not area:
35 return 2
36 if not perspectives:
37 return 2
38 d = read_structures(f'{STRUCTURES}.yml')
39 s = d[STRUCTURES]
40 parts = []
41 for k, v in s.items():
42 val = v.strip()
43 if val.startswith(f'{area}/') and val.endswith(f'/{STRUCTURES}.yml'):
44 p = val.replace(f'{area}/', '').replace(f'/{STRUCTURES}.yml', '')
45 if p not in parts:
46 parts.append(p)
47 parts.sort()
49 for part in parts:
50 print(f'{area} {part}:', file=sys.stderr)
51 part_path = pathlib.Path(area) / part
52 if not part_path.is_dir():
53 print('- folder is missing in file system', file=sys.stderr)
54 else:
55 print('+ folder present in file system', file=sys.stderr)
56 part_structure_path = part_path / f'{STRUCTURES}.yml'
57 if not part_structure_path.is_file():
58 print(f' - folder is missing the {STRUCTURES}.yml file', file=sys.stderr)
59 else:
60 print(f' + folder provides a {STRUCTURES}.yml file', file=sys.stderr)
61 print(f' perspectives of {part}:', file=sys.stderr)
62 cd = read_structures(part_structure_path)
63 cs = cd[STRUCTURES]
64 for k, v in cs.items():
65 val = v.strip()
66 vp = part_path / val
67 if not vp.is_file():
68 print(f' - {STRUCTURE} path ({vp}) does not exist - skipping target ({k})', file=sys.stderr)
69 continue
70 known = False
71 for perspective in perspectives:
72 if val == f'{perspective}/{STRUCTURE}.yml':
73 known = True
74 print(f' + target ({k}) -> {perspective}', file=sys.stderr)
75 perspective_pdf_path = part_path / perspective / 'render/pdf'
76 if not perspective_pdf_path.is_dir():
77 print(f' - is missing {perspective} render folder', file=sys.stderr)
78 render_path = perspective_pdf_path
79 render_path.mkdir(parents=True, exist_ok=True)
81 src = PIPE_SRC
82 dest = render_path / PIPE_NAME
83 dest.write_text(src.read_text())
84 os.chmod(dest, PIPE_PERMS)
86 print(f' * created {perspective} pipe script at ({dest})', file=sys.stderr)
88 src = LOG_HEAD_SRC
89 dest = render_path / LOG_HEAD_NAME
90 dest.write_text(src.read_text())
91 os.chmod(dest, LOG_PERMS)
93 src = PIPE_SRC
94 dest = render_path / LOG_TAIL_NAME
95 dest.write_text(src.read_text())
96 os.chmod(dest, LOG_PERMS)
97 else:
98 print(f' + has {perspective} render folder', file=sys.stderr)
99 if not known:
100 print(f' ? ignored target ({k}) --> ({val})', file=sys.stderr)
102 print(' '.join(parts))
104 return 0
107if __name__ == '__main__':
108 sys.exit(elucidate(sys.argv[1], sys.argv[2:]))