|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright 1996-2020 Cyberbotics Ltd. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import os |
| 18 | +import re |
| 19 | +import subprocess |
| 20 | +import collections |
| 21 | +from glob import glob |
| 22 | +import yaml |
| 23 | + |
| 24 | + |
| 25 | +def dict_merge(dct, merge_dct): |
| 26 | + """ Recursive dict merge. Inspired by :meth:``dict.update()``, instead of |
| 27 | + updating only top-level keys, dict_merge recurses down into dicts nested |
| 28 | + to an arbitrary depth, updating keys. The ``merge_dct`` is merged into |
| 29 | + ``dct``. |
| 30 | + :param dct: dict onto which the merge is executed |
| 31 | + :param merge_dct: dct merged into dct |
| 32 | + :return: None |
| 33 | + """ |
| 34 | + # Reference: https://gist.github.com/angstwad/bf22d1822c38a92ec0a9 |
| 35 | + for k in merge_dct.keys(): |
| 36 | + if (k in dct and isinstance(dct[k], dict) |
| 37 | + and isinstance(merge_dct[k], collections.abc.Mapping)): |
| 38 | + dict_merge(dct[k], merge_dct[k]) |
| 39 | + else: |
| 40 | + dct[k] = merge_dct[k] |
| 41 | + |
| 42 | + |
| 43 | +def load_config(): |
| 44 | + default_config_dir = os.path.join( |
| 45 | + os.path.dirname(os.path.realpath(__file__)), 'config') |
| 46 | + |
| 47 | + # Load user's config |
| 48 | + user_config = {} |
| 49 | + if os.path.isfile('webots.yaml'): |
| 50 | + with open('webots.yaml', 'r') as f: |
| 51 | + user_config = yaml.load(f.read(), Loader=yaml.FullLoader) or {} |
| 52 | + |
| 53 | + # Load default config |
| 54 | + config = None |
| 55 | + with open(os.path.join(default_config_dir, 'webots.yaml'), 'r') as f: |
| 56 | + config = yaml.load(f.read(), Loader=yaml.FullLoader) |
| 57 | + |
| 58 | + # Put user's config on top of default config |
| 59 | + dict_merge(config, user_config) |
| 60 | + return config |
| 61 | + |
| 62 | + |
| 63 | +def generate_animation_recorder_vrml(duration, output): |
| 64 | + return ( |
| 65 | + f'Robot {{\n' |
| 66 | + f' name "supervisor"\n' |
| 67 | + f' controller "animation_recorder"\n' |
| 68 | + f' controllerArgs [\n' |
| 69 | + f' "--duration={duration}"\n' |
| 70 | + f' "--output={output}"\n' |
| 71 | + f' ]\n' |
| 72 | + f' supervisor TRUE\n' |
| 73 | + f'}}\n' |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def get_world_name_from_path(path): |
| 78 | + return os.path.splitext(os.path.basename(path))[0] |
| 79 | + |
| 80 | + |
| 81 | +def generate_animation_list(animation_config): |
| 82 | + template = None |
| 83 | + template_dir = os.path.dirname(os.path.realpath(__file__)) |
| 84 | + with open(os.path.join(template_dir, 'template.html'), 'r') as f: |
| 85 | + template = f.read() |
| 86 | + |
| 87 | + worlds = [] |
| 88 | + for world in animation_config['worlds']: |
| 89 | + for world_file in glob(world['file']): |
| 90 | + description = '' |
| 91 | + title = '' |
| 92 | + with open(world_file, 'r') as f: |
| 93 | + world_content = f.read() |
| 94 | + |
| 95 | + # Parse `title` |
| 96 | + title_expr = re.compile(r'title\s\"(.*?)\"', re.MULTILINE | re.DOTALL) |
| 97 | + title_re = re.findall(title_expr, world_content) |
| 98 | + if title_re: |
| 99 | + title = title_re[0] |
| 100 | + |
| 101 | + # Parse `info` |
| 102 | + info_expr = re.compile(r'info\s\[(.*?)\]', re.MULTILINE | re.DOTALL) |
| 103 | + info_re = re.findall(info_expr, world_content) |
| 104 | + if info_re: |
| 105 | + description = ' '.join([x.strip().strip('"') for x in info_re[0].split('\n') if x.strip().strip('"')]) |
| 106 | + |
| 107 | + worlds.append({ |
| 108 | + 'title': title, |
| 109 | + 'description': description, |
| 110 | + 'name': get_world_name_from_path(world_file) |
| 111 | + }) |
| 112 | + |
| 113 | + template = template.replace('{ WORLD_LIST_PLACEHOLDER }', str(worlds)) |
| 114 | + |
| 115 | + with open(os.path.join('/tmp/animation', 'index.html'), 'w') as f: |
| 116 | + f.write(template) |
| 117 | + |
| 118 | + |
| 119 | +def generate_animation(animation_config): |
| 120 | + generate_animation_list(animation_config) |
| 121 | + |
| 122 | + for world in animation_config['worlds']: |
| 123 | + for world_file in glob(world['file']): |
| 124 | + world_content = None |
| 125 | + world_name = get_world_name_from_path(world_file) |
| 126 | + animation_recorder_vrml = generate_animation_recorder_vrml( |
| 127 | + duration=world['duration'], |
| 128 | + output=os.path.join(os.path.abspath('.'), '/tmp/animation', world_name + '.html') |
| 129 | + ) |
| 130 | + |
| 131 | + with open(world_file, 'r') as f: |
| 132 | + world_content = f.read() |
| 133 | + |
| 134 | + with open(world_file, 'w') as f: |
| 135 | + f.write(world_content + animation_recorder_vrml) |
| 136 | + |
| 137 | + out = subprocess.check_output(['xvfb-run', 'webots', '--stdout', '--stderr', '--batch', '--mode=fast', world_file]) |
| 138 | + print(out.decode('utf-8')) |
| 139 | + |
| 140 | + with open(world_file, 'w') as f: |
| 141 | + f.write(world_content) |
| 142 | + |
| 143 | + |
| 144 | +def main(): |
| 145 | + config = load_config() |
| 146 | + generate_animation(config['animation']) |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + main() |
0 commit comments