Coverage for taulukko/taulukko.py: 92.31%

49 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-05 19:15:40 +00:00

1"""Table (Finnish Taulukko) glued together to transform into hands-free living. API.""" 

2 

3import os 

4import pathlib 

5import sys 

6from typing import List, Optional, Tuple, Union 

7 

8import pytablereader as ptr # type: ignore 

9import pytablewriter as ptw 

10 

11DEBUG_VAR = 'TAULUKKO_DEBUG' 

12DEBUG = os.getenv(DEBUG_VAR) 

13 

14ENCODING = 'utf-8' 

15ENCODING_ERRORS_POLICY = 'ignore' 

16 

17STDIN, STDOUT = 'STDIN', 'STDOUT' 

18DISPATCH = { 

19 STDIN: sys.stdin, 

20 STDOUT: sys.stdout, 

21} 

22 

23MD_ROOT = pathlib.Path('taulukko-md') 

24 

25 

26def verify_request(argv: Optional[List[str]]) -> Tuple[int, str, List[str]]: 

27 """Fail with grace.""" 

28 if not argv or len(argv) != 2: 

29 return 2, 'received wrong number of arguments', [''] 

30 

31 command, inp = argv 

32 

33 if command not in ('extract',): 

34 return 2, 'received unknown command', [''] 

35 

36 if inp: 

37 in_path = pathlib.Path(str(inp)) 

38 if not in_path.is_file(): 

39 return 1, f'source ({in_path}) is no file', [''] 

40 if not ''.join(in_path.suffixes).lower().endswith('.html'): 40 ↛ 41line 40 didn't jump to line 41, because the condition on line 40 was never true

41 return 1, 'source has not .html extension', [''] 

42 

43 return 0, '', argv 

44 

45 

46def main(argv: Union[List[str], None] = None) -> int: 

47 """Drive the translation.""" 

48 error, message, strings = verify_request(argv) 

49 if error: 

50 print(message, file=sys.stderr) 

51 return error 

52 

53 command, inp = strings 

54 

55 out_root = MD_ROOT 

56 print(f'extracting html tables from ({inp if inp else STDIN}) into markdown file below {out_root}') 

57 try: 

58 loader = ptr.TableFileLoader(inp) 

59 except ptr.error.InvalidFilePathError as err: 

60 print(f'html file does not exist? detail: {err}') 

61 return 1 

62 writer = ptw.MarkdownTableWriter(margin=1) 

63 index_path = out_root / 'collected-tables.md' 

64 index_path.parent.mkdir(parents=True, exist_ok=True) 

65 with open(index_path, 'w', encoding=loader.encoding) as handle: 

66 writer.stream = handle 

67 for table_data in loader.load(): 

68 writer.from_tabledata(table_data) 

69 writer.write_table() 

70 

71 print(f'markdown tree is below ({out_root})') 

72 

73 return 0