""" Turn a wadl doc into the base for a gabbi test. Limited functionality as yet: Some of the WADL files on the api-site just plain do not work. Works okay on ceilometer though. """ import yaml import sys from io import BytesIO import requests from lxml import etree as ET def parse_for_paths(wadl_string): paths = [] whole_paths = {} for event, element in ET.iterparse(BytesIO(wadl_string.encode('UTF-8')), tag='{*}resource', events=('start', 'end'), recover=True): if event == 'start': path = element.attrib.get('path') paths.append(path) if event == 'end': full_path = '/'.join(paths) methods = [method.attrib.get('href').lstrip('#') for method in element.findall('{*}method')] if len(methods) > 0: whole_paths[full_path] = methods else: whole_paths[full_path] = ['generic'] paths.pop() return whole_paths def get_method_map(wadl_string): method_info = {} for event, element in ET.iterparse(BytesIO(wadl_string.encode('UTF-8')), tag='{*}method', recover=True): method_id = element.attrib.get('id') if method_id is None: continue method_method = element.attrib.get('name') try: desc = ' '.join(element.find('.//{*}para').text.split()) except AttributeError: desc = 'No desc' method_info[method_id] = (method_method, desc) return method_info def get_wadl(): try: source = sys.argv[1] except IndexError: source = 'https://raw.githubusercontent.com/openstack/api-site/master/api-ref/src/wadls/telemetry-api/src/v2/os-telemetry-api-2.0.wadl' return requests.get(source).text if __name__ == '__main__': wadl_string = get_wadl() tests = [] whole_paths = parse_for_paths(wadl_string) methods = get_method_map(wadl_string) for path in sorted(whole_paths.keys()): for action in whole_paths[path]: if action == 'generic': continue name = action method = methods[action][0] desc = methods[action][1] url = '/' + path.lstrip('/') tests.append({ 'name': name, 'method': method, 'desc': desc, 'url': url }) print yaml.dump({'tests': tests}, default_flow_style=False)