# Copyright 2017 Red Hat, Inc. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import os from yaml import load, dump try: from yaml import CLoader as Loader, CDumper as Dumper except ImportError: from yaml import Loader, Dumper # all files needs to be processed files = ['capabilities-map.yaml'] # the number of title or description pair incnum = 1 # the pair of title and description strings = [] def collect_all_files(data): if isinstance(data, dict): for item in data.values(): collect_all_files(item) elif isinstance(data, list) or isinstance(data, tuple): for item in data: collect_all_files(item) elif isinstance(data, str): if data.endswith('.yaml') and data not in files: files.append(data) else: pass def collect_all_strings(data): if isinstance(data, dict): for item in data.values(): collect_all_strings(item) title = None if 'title' in data: title = data['title'] if title: title = title.rstrip(os.linesep) description = None if 'description' in data: description = data['description'] if description: description = description.rstrip(os.linesep) if title or description: strings.append((title, description)) elif isinstance(data, list) or isinstance(data, tuple): for item in data: collect_all_strings(item) else: pass # loop all files for onefile in files: # load yaml file try: stream = open(onefile, "r") except: continue data = load(stream, Loader=Loader) collect_all_files(data) collect_all_strings(data) stream.close() # unique string strings = list(set(strings)) def format_item(kind, num, string): item = ''' {0}: {{ id: '{1}', defaultMessage: '{1}' }}''' return item.format(kind + num, string) def get_strings(): global incnum items = [] for (title, description) in strings: if title: item = format_item('title', str(incnum), title) items.append(item) if description: item = format_item('description', str(incnum), description) items.append(item) incnum += 1 return ",\n".join(items) def expand_file(filename): infile = open(filename, "r") for line in infile.readlines(): line = line.rstrip(os.linesep) if line == '@STRINGS@': print(get_strings()) else: print(line) ### main function ### if __name__ == "__main__": expand_file("tripleo-heat-templates.js.in")