-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature load from another package #36
Open
awesomebytes
wants to merge
8
commits into
cbandera:develop
Choose a base branch
from
awesomebytes:feature_load_from_another_package
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2b97076
Fix typo
awesomebytes f6afe40
New feature: load from another package .params file it's params and t…
awesomebytes b12863b
Add docs
awesomebytes 41f7b9e
Fix typo
awesomebytes 95432b2
Remove prints... Sorry
awesomebytes 50f3c04
Add option to add relative path in case .params file is somewhere els…
awesomebytes 45b7314
Forgot to actually apply the new param
awesomebytes 5947bfe
Merge branch 'develop' into feature_load_from_another_package
awesomebytes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# How to Import from another .params file | ||
**Description**: This tutorial will show you how to import a .params file from another ROS package instead of copy-pasting it all over. | ||
**Tutorial Level**: ADVANCED | ||
|
||
## Setup | ||
|
||
Note that your package will still need to depend on rosparam_handler and dynamic_reconfigure. | ||
|
||
You can find an example of a minimal package called [imported_rosparam_handler_test](https://github.com/awesomebytes/imported_rosparam_handler_test) which imports from [rosparam_handler_tutorial](https://github.com/cbandera/rosparam_handler_tutorial). | ||
|
||
|
||
## The params File | ||
|
||
```python | ||
#!/usr/bin/env python | ||
from rosparam_handler.parameter_generator_catkin import * | ||
gen = ParameterGenerator() | ||
# Do it at the start, as it overwrites all current params | ||
gen.initialize_from_file('rosparam_handler_tutorial', 'Demo.params') | ||
|
||
# Do your usual business | ||
gen.add("some_other_param", paramtype="int",description="Awesome int", default=2, min=1, max=10, configurable=True) | ||
gen.add("non_configurable_thing", paramtype="int",description="Im not configurable", default=2, min=1, max=10, configurable=False) | ||
|
||
# Syntax : Package, Node, Config Name(The final name will be MyDummyConfig) | ||
exit(gen.generate("imported_rosparam_handler_test", "example_node", "Example")) | ||
``` | ||
|
||
You just need to call `initialize_from_file(ros_package_name, File.params)`. Note that it will overwrite all params. Should be called at the start (that's why it's called initialize). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright (c) 2017, Sammy Pfeiffer | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above copyright | ||
# notice, this list of conditions and the following disclaimer in the | ||
# documentation and/or other materials provided with the distribution. | ||
# * Neither the name of the organization nor the | ||
# names of its contributors may be used to endorse or promote products | ||
# derived from this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY | ||
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
# | ||
# Author: Sammy Pfeiffer | ||
# | ||
# Utilities to import from another package .params file | ||
# | ||
|
||
from imp import load_source | ||
from rospkg import RosPack, ResourceNotFound | ||
from tempfile import NamedTemporaryFile | ||
import cStringIO | ||
import tokenize | ||
import re | ||
|
||
|
||
def remove_comments(source): | ||
""" | ||
Returns 'source' minus comments, based on | ||
https://stackoverflow.com/a/2962727 | ||
""" | ||
io_obj = cStringIO.StringIO(source) | ||
out = "" | ||
last_lineno = -1 | ||
last_col = 0 | ||
for tok in tokenize.generate_tokens(io_obj.readline): | ||
token_type = tok[0] | ||
token_string = tok[1] | ||
start_line, start_col = tok[2] | ||
end_line, end_col = tok[3] | ||
if start_line > last_lineno: | ||
last_col = 0 | ||
if start_col > last_col: | ||
out += (" " * (start_col - last_col)) | ||
# Remove comments: | ||
if token_type == tokenize.COMMENT: | ||
pass | ||
else: | ||
out += token_string | ||
last_col = end_col | ||
last_lineno = end_line | ||
return out | ||
|
||
|
||
def load_generator(package_name, params_file_name): | ||
""" | ||
Returns the generator created in another .params file from another package. | ||
Python does not allow to import from files without the extension .py | ||
so we need to hack a bit to be able to import from .params file. | ||
Also the .params file was never thought to be imported, so we need | ||
to do some extra tricks. | ||
""" | ||
# Get the file path | ||
rp = RosPack() | ||
try: | ||
pkg_path = rp.get_path(package_name) | ||
except ResourceNotFound: | ||
return None | ||
full_file_path = pkg_path + '/cfg/' + params_file_name | ||
# print("Loading rosparam_handler params from file: " + full_file_path) | ||
|
||
# Read the file and check for exit() calls | ||
# Look for line with exit function to not use it or we will get an error | ||
with open(full_file_path, 'r') as f: | ||
file_str = f.read() | ||
# Remove all comment lines first | ||
clean_file = remove_comments(file_str) | ||
# Find exit( calls | ||
exit_finds = [m.start() for m in re.finditer('exit\(', clean_file)] | ||
# If there are, get the last one | ||
if exit_finds: | ||
last_exit_idx = exit_finds[-1] | ||
clean_file = clean_file[:last_exit_idx] | ||
with NamedTemporaryFile() as f: | ||
f.file.write(clean_file) | ||
f.file.close() | ||
tmp_module = load_source('tmp_module', f.name) | ||
else: | ||
# Looks like the exit call is not there | ||
# or it's surrounded by if __name__ == '__main__' | ||
# so we can just load the source | ||
tmp_module = load_source('tmp_module', full_file_path) | ||
|
||
for var in dir(tmp_module): | ||
if not var.startswith('_'): | ||
module_element = getattr(tmp_module, var) | ||
type_str = str(type(module_element)) | ||
# Looks like: | ||
# <class 'PACKAGE_NAME.parameter_generator_catkin.ParameterGenerator'> | ||
if 'parameter_generator_catkin.ParameterGenerator' in type_str: | ||
return module_element | ||
|
||
return None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be handy to have the possibility to specify the relative path.
An extra param to the
load_generator
(and affiliated functions) with default value :path='/cfg/'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in the last commit. Updated docs accordingly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 👍