"""Create a synthetic XLSX workflows worksheet using only the standard library."""
import argparse
from pathlib import Path
from xml.sax.saxutils import escape
from zipfile import ZipFile, ZIP_DEFLATED
HEADERS = ['Name','Machine','Project','Ops Segment','Mon','Tues','Wed','Thu','Fri','Sat','Sun','Other','Notes']
ROWS = [
['Demo report','demo-worker','Demo analytics','Marketing',0.375,None,None,None,None,None,None,'','Synthetic report'],
['Demo backup','demo-worker','Demo maintenance','Systems',None,0.5,None,None,None,None,None,'','Synthetic backup'],
['Demo review','demo-worker','Demo planning','Strategy',None,None,'Hourly',None,None,None,None,'','Synthetic review'],
]
def create_workbook(path, rows=None, headers=None):
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
matrix = [HEADERS if headers is None else headers] + (ROWS if rows is None else rows)
xml_rows = []
for rownum, row in enumerate(matrix, 1):
cells=[]
for col,value in enumerate(row):
if value is None: continue
ref=f'{chr(65+col)}{rownum}'
if isinstance(value,(int,float)):
cells.append(f'{value}')
else:
cells.append(f'{escape(str(value))}')
xml_rows.append(f'{"".join(cells)}
')
ns='http://schemas.openxmlformats.org/spreadsheetml/2006/main'
rel='http://schemas.openxmlformats.org/package/2006/relationships'
docrel='http://schemas.openxmlformats.org/officeDocument/2006/relationships'
with ZipFile(path,'w',ZIP_DEFLATED) as z:
z.writestr('[Content_Types].xml','')
z.writestr('_rels/.rels',f'')
z.writestr('xl/workbook.xml',f'')
z.writestr('xl/_rels/workbook.xml.rels',f'')
z.writestr('xl/worksheets/sheet1.xml',f'{"".join(xml_rows)}')
if __name__=='__main__':
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('--output',type=Path,default=Path(__file__).resolve().parent.parent/'local/workflows.xlsx')
args=parser.parse_args()
if args.output.exists(): parser.error('Output already exists; choose a new path.')
create_workbook(args.output)
print(f'Created synthetic workbook: {args.output}')