Skip to content
Browse files

Added --force to 'morse add'

While here, fixed some printed paths displayed after adding a component
  • Loading branch information...
1 parent cab8b51 commit 1d574d29d6747b54b710554d60dcac5ca36cfd1f Séverin Lemaignan committed with adegroote Apr 9, 2013
Showing with 70 additions and 48 deletions.
  1. +4 −1 bin/morse.in
  2. +66 −47 src/morse/environments.py
View
5 bin/morse.in
@@ -522,6 +522,7 @@ def add_component(args):
type = args.type
name = args.name
env = args.env
+ force = args.force
set_prefixes(silent = True)
@@ -535,7 +536,7 @@ def add_component(args):
env = Environment(morse_prefix, env, site)
try:
- env.add_component(type, name)
+ env.add_component(type, name, force = force)
except MorseEnvironmentError as e:
logger.error("%s" % e)
sys.exit(1)
@@ -775,6 +776,8 @@ if __name__ == '__main__':
help="the type of component you want to add.")
add_parser.add_argument('name', default="", help="the name of the new component.")
add_parser.add_argument('env', default="", help="the environment where to add.")
+ add_parser.add_argument('-f', '--force', action='store_true',
+ help='overwrite existing component with the same name.')
add_parser.set_defaults(func=add_component)
View
113 src/morse/environments.py
@@ -38,6 +38,10 @@ def pyprint(code):
###################
+## Attention! the first file of each of these lists is used to check
+## if the component already exist: do not put file in "append" mode
+## (like __init__.py) as first entry.
+
BASIC = ["default.py",
"scripts/@env@_client.py",
"src/@env@/__init__.py",
@@ -67,14 +71,14 @@ def pyprint(code):
----------------------------------------------------------
To complete the equipment of your robot, edit:
-{prefix}/src/builder/robots/{name}.py
+{prefix}/src/{env}/builder/robots/{name}.py
You can also modify its Blender mesh:
-{prefix}/data/robots/{name}.blend
+{prefix}/data/{env}/robots/{name}.blend
For advanced usage, you may also edit its internal
definition here:
-{prefix}/src/robots/{name}.py
+{prefix}/src/{env}/robots/{name}.py
----------------------------------------------------------
To use the robot in your simulation script, add the following
@@ -97,12 +101,12 @@ def pyprint(code):
added to the <{env}> environment.
----------------------------------------------------------
-Edit {prefix}/src/actuators/{name}.py to implement the
+Edit {prefix}/src/{env}/actuators/{name}.py to implement the
behaviour of your actuator.
----------------------------------------------------------
To use it on your robot, edit your robot description in
-{prefix}/src/builder/robots/
+{prefix}/src/{env}/builder/robots/
and add these lines:
""", ("""from {env}.builder.actuators import {classname}
@@ -122,12 +126,12 @@ def pyprint(code):
added to the <{env}> environment.
----------------------------------------------------------
-Edit {prefix}/src/sensors/{name}.py to implement the
+Edit {prefix}/src/{env}/sensors/{name}.py to implement the
behaviour of your sensor.
----------------------------------------------------------
To use it on your robot, edit your robot description in
-{prefix}/src/builder/robots/
+{prefix}/src/{env}/builder/robots/
and add these lines:
""", ("""from {env}.builder.sensors import {classname}
@@ -222,12 +226,14 @@ def check_env_exists(self):
if not os.access(self.path, os.F_OK):
raise MorseEnvironmentError("<%s> (expected in \"%s\") does not "
"exist! You may want to create first "
- "the environment with 'morse init "
+ "the environment with 'morse create "
"<env>'." % (self.env, self.path))
self.check_writable(self.path)
- def _install_files(self, files, **kwargs):
+ def _install_files(self, files, force = False, **kwargs):
+
+ firstfile = True
for f in files:
path, name = os.path.split(f)
@@ -240,6 +246,13 @@ def _install_files(self, files, **kwargs):
except OSError: # path already exists
pass
+ if firstfile and \
+ not force and \
+ os.path.exists(joinpth(self.path, newpath, newname)):
+ raise MorseEnvironmentError("A component called <%s> already "
+ "exist. Use 'morse add -f ...' to overwrite." % newname)
+
+ firstfile = False
self._configure(joinpth(self.tpls, path, name + ".tpl"), \
joinpth(self.path, newpath, newname), \
**kwargs)
@@ -270,7 +283,7 @@ def create(self, force = False):
self._install_files(BASIC, env = self.env)
- def add_component(self, cmpttype, name):
+ def add_component(self, cmpttype, name, force = False):
self.check_env_exists()
@@ -279,40 +292,46 @@ def add_component(self, cmpttype, name):
logger.warning("Replaced name <%s> by suitable identifier: "
"<%s>" % (name, safename))
- if cmpttype == "robot":
- self._install_files(ROBOT, \
- name = safename, \
- classname = safename.capitalize(), \
- env = self.env)
-
- self._print_info_msg(NEW_ROBOT_MSG, \
- prefix= self.abspath, \
- name = safename, \
- classname = safename.capitalize(), \
- env = self.env)
- elif cmpttype == "sensor":
- desc = input("Enter a short description for sensor <%s>: " % safename)
- self._install_files(SENSOR, \
- name = safename, \
- classname = safename.capitalize(), \
- env = self.env, \
- shortdesc = desc)
- self._print_info_msg(NEW_SNESOR_MSG, \
- prefix= self.abspath, \
- name = safename, \
- classname = safename.capitalize(), \
- env = self.env)
- elif cmpttype == "actuator":
- desc = input("Enter a short description for actuator <%s>: " % safename)
- self._install_files(ACTUATOR, \
- name = safename, \
- classname = safename.capitalize(), \
- env = self.env, \
- shortdesc = desc)
- self._print_info_msg(NEW_ACTUATOR_MSG, \
- prefix= self.abspath, \
- name = safename, \
- classname = safename.capitalize(), \
- env = self.env)
- else:
- raise MorseEnvironmentError("Unknown component type %s" % cmpttype)
+ try:
+ if cmpttype == "robot":
+ ok = self._install_files(ROBOT, \
+ force = force, \
+ name = safename, \
+ classname = safename.capitalize(), \
+ env = self.env)
+
+ self._print_info_msg(NEW_ROBOT_MSG, \
+ prefix= self.abspath, \
+ name = safename, \
+ classname = safename.capitalize(), \
+ env = self.env)
+ elif cmpttype == "sensor":
+ desc = input("Enter a short description for sensor <%s>: " % safename)
+ self._install_files(SENSOR, \
+ force = force, \
+ name = safename, \
+ classname = safename.capitalize(), \
+ env = self.env, \
+ shortdesc = desc)
+ self._print_info_msg(NEW_SNESOR_MSG, \
+ prefix= self.abspath, \
+ name = safename, \
+ classname = safename.capitalize(), \
+ env = self.env)
+ elif cmpttype == "actuator":
+ desc = input("Enter a short description for actuator <%s>: " % safename)
+ self._install_files(ACTUATOR, \
+ force = force, \
+ name = safename, \
+ classname = safename.capitalize(), \
+ env = self.env, \
+ shortdesc = desc)
+ self._print_info_msg(NEW_ACTUATOR_MSG, \
+ prefix= self.abspath, \
+ name = safename, \
+ classname = safename.capitalize(), \
+ env = self.env)
+ else:
+ raise MorseEnvironmentError("Unknown component type %s" % cmpttype)
+ except MorseEnvironmentError as mee:
+ logger.error(mee.value)

0 comments on commit 1d574d2

Please sign in to comment.
Something went wrong with that request. Please try again.