#!/bin/python2.7 ''' Generate tosca definitions from Docx specfication ''' import sys import docx def is_tosca_def(table): ''' Returns true when a table contains TOSCA definitions, i.e. the table contains just one cell and text starts with an empty space ' '' ''' txt = table.rows[0].cells[0].text[0] return \ len(table.rows) == 1 and \ len(table.columns) == 1 and \ txt.startswith(' ') try: SOL001_FN = sys.argv[1] except: print 'Error: Filename missing or filename not a docx document' print 'Usage: doc2tosca ' sys.exit(1) OUT_FN = 'try-tosca-export.yaml' SOL001 = docx.Document(SOL001_FN) DEFINITIONS = [t for t in SOL001.tables if is_tosca_def(t)] F = open(OUT_FN, 'w') HDR='''tosca_definitions_version: tosca_simple_yaml_1_2 description: ETSI NFV SOL 001 nsd types definitions version 2.5.1 imports: - etsi_nfv_sol001_vnfd_2_5_1_types.yaml data_types: ''' F.write(HDR) for t in DEFINITIONS: F.write(t.rows[0].cells[0].text.encode('utf-8')) F.write('\n# -------------------- #\n') F.write('\n') F.close()