Skip to content

Instantly share code, notes, and snippets.

@aristofor
Last active August 29, 2015 14:16
Show Gist options
  • Save aristofor/247f6d2066c226ad03a9 to your computer and use it in GitHub Desktop.
Save aristofor/247f6d2066c226ad03a9 to your computer and use it in GitHub Desktop.
Crée un poppy dans v-rep et lance un shell interactif IPython
#!/usr/bin/env python
# coding: utf-8
"""
V-REP Simulation / iPython launcher
"""
from pip.util import get_installed_distributions
from IPython.config.loader import Config as ipConfig
from IPython.terminal.embed import InteractiveShellEmbed
import re
def creature_class(pkg_name):
"""
Translates a package name to a poppy-creature class name.
Converts hyphen string to camel case.
Note:
Result should match a python class name, so weird naming is avoided,
eg. 'poppy-2-1B-surgical-droid' won't fit in creatures naming system.
"""
return ''.join( w.capitalize() for w in re.findall('[a-zA-Z0-9]+',pkg_name))
def list_creatures():
"""
Return the list of installed poppy-creature packages
"""
print("Poppy creatures :")
for pkg in sorted( get_installed_distributions() ):
if not pkg.key.startswith('poppy-') or pkg.key[6:] == 'creature':
continue
print(' {}'.format(pkg.key[6:]) )
def launch_creature(name):
"""
iPython shell launcher
"""
cls_name = creature_class('poppy-'+name)
creature_module = __import__('poppy.creatures', fromlist=[cls_name])
cls = getattr(creature_module, cls_name)
ns = { 'poppy' : cls(simulator='vrep') }
banner = 'poppy : {}'.format(cls_name)
shell = InteractiveShellEmbed(user_ns=ns, banner2=banner)
shell()
if __name__ == '__main__':
import sys
from argparse import ArgumentParser
parser = ArgumentParser(description='Poppy V-REP simulation/iPython launcher.')
parser.add_argument('-l', '--list', help="List poppy creatures", action="store_true")
parser.add_argument(dest='creature', help="Poppy creature name", action="store", nargs='?')
args = parser.parse_args()
if args.list:
list_creatures()
elif args.creature:
launch_creature(args.creature)
else:
parser.print_help()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment