-
Notifications
You must be signed in to change notification settings - Fork 4
Erb template #40
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
base: master
Are you sure you want to change the base?
Erb template #40
Changes from all commits
1582ed5
e95debc
2d35534
4301929
5739d8c
9548488
d23b7fe
2fd84f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "erb" | ||
| require "sdf/loader" | ||
|
|
||
| module SDF | ||
| # class to load SDF and ERB templated SDF files | ||
| class ERBLoader < Loader | ||
| # Parses an ERB string and returns the raw rendered string | ||
| # | ||
| # @param [String] erb_content ERB template file content as string | ||
| # @param [Hash] erb_args the configuration arguments to evaluate | ||
| # @return [String] the raw rendered XML string representing the model | ||
| def self.parse_erb_as_str(erb_content, **erb_args) | ||
| erb_engine = ::ERB.new(erb_content, trim_mode: "-") | ||
|
|
||
| # Render the ERB template with the passed hash arguments | ||
| erb_engine.result_with_hash(erb_args) | ||
| end | ||
|
|
||
| # Renders an ERB template and returns it as a REXML::Document | ||
| # | ||
| # @return [REXML::Document] the rendered sdf model | ||
| def self.render_erb_sdf_model(path, **erb_args) | ||
| erb_content = File.read(path) | ||
| solved_erb_as_sdf_str = parse_erb_as_str(erb_content, **erb_args) | ||
|
|
||
| REXML::Document.new(solved_erb_as_sdf_str) | ||
| end | ||
|
|
||
| def initialize(erb_args: {}) | ||
| super() | ||
| @erb_args = erb_args | ||
| end | ||
|
|
||
| def parse_sdf_document(sdf_file) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should make a difference between erb and non-erb files, that is parse ERB only when the extension is
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I told him to do it like this. If you mean for having an explicit error when a non-erb file is given to an ERBLoader, I feel this is overkill and it would painful in the Robot level to constantly juggle between loaders when the model file changes (I dont think you mean this, just getting it out there). In the case you want to split the functionality between
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with @jhonasiv on this one, but in this case I would simply remove the base In case we enforce the files to end with But anyway, I don't have a strong opinion on this, so I would happily go with any
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A part from this comment, the review was addressed. I didn't address this one as it seems there is no consensus yet, let me know what to do about this one |
||
| ERBLoader.render_erb_sdf_model(sdf_file, **@erb_args) | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,24 @@ | ||
| module SDF | ||
| class InternalError < RuntimeError; end | ||
|
|
||
| module XML | ||
| # Exception raised when trying to load a model URI, but the model does | ||
| # not contain a SDF entry for the required SDF version | ||
| class UnavailableSDFVersionInModel < ArgumentError; end | ||
| # Exception raised when trying to load a file that is not a SDF file | ||
| class NotSDF < ArgumentError; end | ||
| # Exception raised when trying to load a malformed XML file | ||
| class InvalidXML < ArgumentError; end | ||
|
|
||
| # Exception raised when trying to resolve a model that cannot be found | ||
| # in {model_path} | ||
| class NoSuchModel < ArgumentError | ||
| attr_reader :model_name | ||
|
|
||
| def initialize(model_name) | ||
| super | ||
| @model_name = model_name | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # frozen_string_literal: true | ||
|
Rezenders marked this conversation as resolved.
|
||
|
|
||
| module SDF | ||
| # class to load SDF and ERB templated SDF files | ||
| class Loader | ||
| # Open a SDF file SDF file and returns its XML representation. | ||
| # | ||
| # @param [String] sdf_file the path to the SDF file | ||
| # @raise [Errno::ENOENT] if the files does not exist | ||
| # @raise [NotSDF] if the file is not a SDF file | ||
| # @raise [InvalidXML] if the file is not a valid XML file | ||
| # @return [REXML::Element] sdf_file's content as a REXML::Element instance | ||
| def load_sdf_raw(sdf_file) | ||
| sdf = begin | ||
| parse_sdf_document(sdf_file) | ||
| rescue REXML::ParseException => e | ||
| raise SDF::XML::InvalidXML, | ||
| "Cannot load #{sdf_file}: #{e.message}" | ||
| end | ||
| validate_sdf_root(sdf, sdf_file) | ||
|
|
||
| sdf | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def parse_sdf_document(sdf_file) | ||
| File.open(sdf_file) do |io| | ||
| REXML::Document.new(io) | ||
| end | ||
| end | ||
|
|
||
| def validate_sdf_root(sdf, sdf_file) | ||
| return if sdf.root.name == "sdf" | ||
|
|
||
| raise SDF::XML::NotSDF, "#{sdf_file} is not a SDF file" | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1 @@ | ||
| <?xml version="1.0"?> | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?xml version="1.0"?> | ||
| <model> | ||
| <name>simple_model</name> | ||
| <sdf version="1.5">model.sdf.erb</sdf> | ||
| </model> |
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.
I'm missing the purpose or advantage of having separate loader and ERBLoader classes. This stuff is so simple, why not a single class ?
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.
If I remember correctly, @jhonasiv requested me to do it so we could enforce that when the
SDF::Loaderis configured only.sdffiles are loaded