-
Notifications
You must be signed in to change notification settings - Fork 61
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
Generator refactoring #1410
Generator refactoring #1410
Conversation
@@ -0,0 +1,110 @@ | |||
# -*- coding: utf-8 -*- |
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.
This module has code common to all generator modules. Currently only one function and one constant.
@@ -0,0 +1,262 @@ | |||
# -*- coding: utf-8 -*- |
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.
This module now has the logic to extract protocol specification fields into pythonic equivalences.
It has one function extract()
with all its private sub-functions. It extracts all specification fields into python objects and encapsulates them into a PythonicProtocolSpecification
class.
I separated these code into its own module mainly to declutter the main generator and because they have a nice logical grouping.
@@ -0,0 +1,101 @@ | |||
# -*- coding: utf-8 -*- |
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.
This module has code to validate a protocol specification. It crucially no longer does validation on Pythonic conversion of specification fields. All validation is done purely on the specification.
This means it can be reused just before converting specification to other languages.
It has a single validate()
function (plus private validate sub-functions) that gets a protocol specification and indicates whether it is valid or not. I did not put validate()
into a class because I failed to see a reason for it and I think it would just complicate the code.
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.
Currently it has the existing validation logic. Other format checking logic (an outstanding generator issue) will be added in this module.
Codecov Report
@@ Coverage Diff @@
## develop #1410 +/- ##
===========================================
+ Coverage 90.10% 90.73% +0.62%
===========================================
Files 213 217 +4
Lines 15205 15242 +37
===========================================
+ Hits 13701 13830 +129
+ Misses 1504 1412 -92
Continue to review full report at Codecov.
|
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.
Looks good so far, some comments
aea/protocols/generator/generator.py
Outdated
""" | ||
Create the protocol package with Message, Serialization, __init__, protocol.yaml files. | ||
Run the generator. If in "full" mode (protobuf_only is False), it: | ||
a) validates the protocol specification. |
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.
Validation seems to be happening elsewhere, I cannot see it below.
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.
Validation is a prerequisite for extraction so extract()
already calls validate()
.
The docstring gives an overall explanation of major things that happen in the two modes.
aea/protocols/generator/generator.py
Outdated
if not protobuf_only: | ||
# Generate Python files | ||
self._generate_file(INIT_FILE_NAME, self._init_str()) | ||
self._generate_file(PROTOCOL_YAML_FILE_NAME, self._protocol_yaml_str()) | ||
self._generate_file(MESSAGE_DOT_PY_FILE_NAME, self._message_class_str()) | ||
if ( | ||
self.protocol_specification.dialogue_config is not None | ||
and self.protocol_specification.dialogue_config != {} | ||
): | ||
self._generate_file( | ||
DIALOGUE_DOT_PY_FILE_NAME, self._dialogue_class_str() | ||
) | ||
if len(self.spec.all_custom_types) > 0: | ||
self._generate_file( | ||
CUSTOM_TYPES_DOT_PY_FILE_NAME, self._custom_types_module_str() | ||
) | ||
self._generate_file( | ||
SERIALIZATION_DOT_PY_FILE_NAME, self._serialization_class_str() | ||
) |
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.
This should be wrapped in a public method generate_python_files
so it becomes a single line here
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
aea/protocols/generator/generator.py
Outdated
|
||
# Warn if specification has custom types | ||
if len(self._all_custom_types) > 0: | ||
incomplete_generation_warning_msg = "The generated protocol is incomplete, because the protocol specification contains the following custom types: {}. Update the generated '{}' file with the appropriate implementations of these custom types.".format( | ||
self._all_custom_types, CUSTOM_TYPES_DOT_PY_FILE_NAME | ||
if not protobuf_only: |
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.
Have a guard clause here:
if protobuf_only:
return
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
aea/protocols/generator/generator.py
Outdated
@@ -26,12 +26,11 @@ | |||
from datetime import date |
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.
Better to call it base.py
to align with our naming conventions.
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
aea/cli/generate.py
Outdated
@@ -38,7 +38,7 @@ | |||
PublicId, | |||
) | |||
from aea.configurations.loader import ConfigLoader | |||
from aea.protocols.generator import ProtocolGenerator | |||
from aea.protocols.generator.generator import ProtocolGenerator |
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.
Your init file below is missing header info
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
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.
LGTM
Proposed changes
This PR breaks the code in the generator module into the following modules:
validate
: containing code used to validate a protocol specificationextract_specification
: containing code to extract fields from a protocol specification into pythonic objects (all fields are encapsulated inPythonicProtocolSpecification
)common
: containing constants and functions used in more than one modulegenerator
: containing the old generator code minus the above separated codesThe PR doesn't do anything else other than breaking existing functionality into separate modules (except some minor necessary tweaks to make that happen).
Some notes:
validate
is now supposed to just take a protocol specification and indicates whether it is a valid specification or not. Crucially it no longer makes any conversion to Python code/objects. This means it can be reused before conversions of specification to other languages. Also, other format checking logic would later go into this module.extract_specification
is placed into its own module mainly because the extraction logic has many dependencies (private functions, constants, etc) which cluttered up the old generator and they could logically be grouped together in an independent way.extract()
automatically callsvalidate()
because it always needs a valid specification to do its work.All the above are now placed in a
generator
package underaea.protocols
Fixes #976
Types of changes
What types of changes does your code introduce to agents-aea?
Put an
x
in the boxes that applyChecklist
Put an
x
in the boxes that apply.develop
branch (left side). Also you should start your branch off ourdevelop
.aea cli
tool works