Coverage for attribuutit/wbk.py: 0.00%

59 statements  

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

1"""Dump report into fixed format worksheets of a workbook from Redmond near Seattle, WA, USA.""" 

2 

3# import json 

4# import pathlib 

5# import sys 

6# from datetime import date 

7# from time import time 

8from typing import no_type_check 

9 

10import pandas as pd # type: ignore 

11import xlsxwriter # type: ignore 

12 

13COL_SEP = ';' 

14ENCODING = 'utf-8' 

15ROW_INDEX_HEADERS = 0 

16ROW_INDEX_CONTENT_START = 1 

17 

18 

19@no_type_check 

20def dump_row(sheet, row, entries=None): 

21 """Dump data into indicated row.""" 

22 for col, entry in enumerate([] if entries is None else entries, start=0): 

23 sheet.write(row, col, entry) 

24 

25 

26@no_type_check 

27def add_sheet(workbook, name, headers): 

28 """Add a sheet, fill in headers, and return the handle.""" 

29 sheet = workbook.add_worksheet(name) 

30 dump_row(sheet, row=ROW_INDEX_HEADERS, entries=headers) 

31 return sheet 

32 

33 

34@no_type_check 

35def fill_sheet(sheet, matrix): 

36 """Dump data into the content area of the sheet#s table.""" 

37 if not matrix: 

38 return 

39 

40 for row, entries in enumerate(matrix, start=ROW_INDEX_CONTENT_START): 

41 dump_row(sheet, row, entries=entries) 

42 

43 

44@no_type_check 

45def update_totals_table(total_worksheet, folder_count, files_count, aspect_counts): 

46 """Update the worksheet total with a table containing the key-value pairs per file type and folder.""" 

47 dump_row(total_worksheet, ROW_INDEX_HEADERS, ['key', 'value']) 

48 key_col_index = 0 

49 value_col_index = 1 

50 

51 row = ROW_INDEX_CONTENT_START 

52 total_worksheet.write(row, key_col_index, 'folder') 

53 total_worksheet.write(row, value_col_index, folder_count) 

54 

55 row += 1 

56 total_worksheet.write(row, key_col_index, 'file') 

57 total_worksheet.write(row, value_col_index, files_count) 

58 

59 row += 1 

60 for kv_row, (key, value) in enumerate(aspect_counts.items(), start=row): 

61 total_worksheet.write(kv_row, key_col_index, f'file.{key}') 

62 total_worksheet.write(kv_row, value_col_index, aspect_counts.get(value, 0)) 

63 

64 

65@no_type_check 

66def create_book(path): 

67 """Create a workbook at path and return the handle.""" 

68 return xlsxwriter.Workbook(path) 

69 

70 

71@no_type_check 

72def add_sheets(book, facets): 

73 """Add sheets for the facets and return a dict of sheet handles with the facets and a special total key.""" 

74 return { 

75 **{facet: add_sheet(book, facet) for facet in facets}, 

76 'total': book.add_worksheet('total'), 

77 } 

78 

79 

80@no_type_check 

81def close_book(handle): 

82 """Attempt to close the book per handle and assume any exception is due to file locking.""" 

83 try: 

84 handle.close() 

85 except Exception: # pylint: disable=broad-except 

86 print( 

87 'The spreadsheet file is locked - possibly in use by another application.' 

88 ' Please close the other app and execute again.' 

89 ) 

90 return 1 

91 

92 

93@no_type_check 

94def derive_csv(counter, path, out_folder): 

95 """Derive a single CSV file for counter from workbook at path writing to out folder and per convention.""" 

96 table = pd.read_excel(path, counter) 

97 table.to_csv(out_folder / f'{counter}.csv', COL_SEP) 

98 

99 

100@no_type_check 

101def derive_csvs(counters, path, out_folder): 

102 """Somehow clumsy derivation of CSV files from counters and main workbook at path.""" 

103 for counter in counters: 

104 if counter not in ('folder', 'other') and counters[counter]: 

105 derive_csv(counter, path, out_folder)