Skip to content

Commit

Permalink
Merge pull request #298 from textX/refactor/model-param-defs
Browse files Browse the repository at this point in the history
Refactor: change _tx_model_param_defitions -> model_param_defs
  • Loading branch information
igordejanovic committed Oct 31, 2020
2 parents 2b74257 + 08de92d commit 3911188
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ please take a look at related PRs and issues and see if the change affects you.

### Changed

- `_tx_model_param_definitions` deprecated in favor of `model_param_defs` ([#298]).
- `click` is now an optional dependency, only when CLI is needed ([#292])
- Make warning about not overwriting generated file more visible
([01341ec3](https://github.com/textX/textX/commit/01341ec381bfb4c8c27bcec5d2998a34d207f430))
Expand Down Expand Up @@ -500,6 +501,7 @@ please take a look at related PRs and issues and see if the change affects you.
- Export to dot.


[#298]: https://github.com/textX/textX/pull/298
[#294]: https://github.com/textX/textX/pull/294
[#292]: https://github.com/textX/textX/pull/292
[#288]: https://github.com/textX/textX/pull/288
Expand Down
18 changes: 9 additions & 9 deletions docs/metamodel.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,15 @@ RHS object is not matched in the input. The multiplicity assignments (`*=` and

## Optional model parameter definitions

A meta-model can define optional model parameters. Such definitions
are stored in `_tx_model_param_definitions` and define optional parameters,
which can be specified while loading/creating a model through `model_from_str`
or `model_from_file`. Details: see [tx_model_params](model.md#_tx_model_params).

`metamodel._tx_model_param_definitions` can be queried (like a dict) to
retrieve possible extra parameters and their descriptions for a meta-model.
It is also used to restrict the additional parameters passed to
`model_from_str` or `model_from_file`.
A meta-model can define optional model parameters. Such definitions are stored
in `model_param_defs` and define optional parameters, which can be specified
while loading/creating a model through `model_from_str` or `model_from_file`.
Details: see [tx_model_params](model.md#_tx_model_params).

`metamodel.model_param_defs` can be queried (like a dict) to retrieve possible
extra parameters and their descriptions for a meta-model. It is also used to
restrict the additional parameters passed to `model_from_str` or
`model_from_file`.

Default parameters are:

Expand Down
6 changes: 3 additions & 3 deletions docs/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,6 @@ instances (see textx.scoping.providers).

This attribute always exists. It holds all additional parameters passed to
`model_from_str` or `model_from_file` of a metamodel. These parameters are
restricted by the `metamodel._tx_model_param_definitions` object
([model and object processors](metamodel.md#optional-model-parameter-definitions)),
which is controlled by the metamodel designer.
restricted by the `metamodel.model_param_defs` object ([model and object
processors](metamodel.md#optional-model-parameter-definitions)), which is
controlled by the metamodel designer.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def types_dsl():
current_dir = os.path.dirname(__file__)
p = os.path.join(current_dir, 'Types.tx')
types_mm = metamodel_from_file(p, global_repository=True)
types_mm._tx_model_param_definitions.add(
types_mm.model_param_defs.add(
'type_name_check',
'enables checks on the type name (default="on" or "off")'
)
Expand Down
20 changes: 10 additions & 10 deletions tests/functional/test_metamodel/test_model_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

def test_model_params():
mm = metamodel_from_str(grammar)
mm._tx_model_param_definitions.add(
mm.model_param_defs.add(
"parameter1", "an example param (1)"
)
mm._tx_model_param_definitions.add(
mm.model_param_defs.add(
"parameter2", "an example param (2)"
)

Expand Down Expand Up @@ -53,19 +53,19 @@ def test_model_params():
with raises(TextXError, match=".*unknown parameter myerror2.*"):
mm.model_from_str(text, parameter1='P1', myerror2='P2')

assert len(mm._tx_model_param_definitions) >= 2
assert 'parameter1' in mm._tx_model_param_definitions
assert 'parameter1' in mm._tx_model_param_definitions
assert mm._tx_model_param_definitions[
assert len(mm.model_param_defs) >= 2
assert 'parameter1' in mm.model_param_defs
assert 'parameter1' in mm.model_param_defs
assert mm.model_param_defs[
'parameter1'].description == "an example param (1)"


def test_model_params_empty():
mm = metamodel_from_str(grammar)
mm._tx_model_param_definitions.add(
mm.model_param_defs.add(
"parameter1", "an example param (1)"
)
mm._tx_model_param_definitions.add(
mm.model_param_defs.add(
"parameter2", "an example param (2)"
)

Expand All @@ -79,10 +79,10 @@ def test_model_params_empty():

def test_model_params_file_based():
mm = metamodel_from_str(grammar)
mm._tx_model_param_definitions.add(
mm.model_param_defs.add(
"parameter1", "an example param (1)"
)
mm._tx_model_param_definitions.add(
mm.model_param_defs.add(
"parameter2", "an example param (2)"
)

Expand Down
16 changes: 11 additions & 5 deletions textx/metamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import codecs
import os
import sys
import warnings
from os.path import join, abspath, dirname
from collections import OrderedDict
from arpeggio import DebugPrinter
Expand Down Expand Up @@ -180,9 +181,8 @@ def __init__(self, file_name=None, classes=None, builtins=None,

super(TextXMetaModel, self).__init__(**kwargs)

self._tx_model_param_definitions = ModelParamDefinitions()
self._tx_model_param_definitions.add(
"project_root", "the project root path")
self.model_param_defs = ModelParamDefinitions()
self.model_param_defs.add("project_root", "the project root path")

self.file_name = file_name
self.rootcls = None
Expand Down Expand Up @@ -600,7 +600,7 @@ def model_from_str(self, model_str, file_name=None, debug=None,
is set while executing pre_ref_resolution_callback (see
scoping.md)
"""
self._tx_model_param_definitions.check_params('from_str', **kwargs)
self.model_param_defs.check_params('from_str', **kwargs)

if type(model_str) is not text:
raise TextXError("textX accepts only unicode strings.")
Expand Down Expand Up @@ -629,7 +629,7 @@ def kwargs_callback(other_model):

def model_from_file(self, file_name, encoding='utf-8', debug=None,
**kwargs):
self._tx_model_param_definitions.check_params(file_name, **kwargs)
self.model_param_defs.check_params(file_name, **kwargs)

return self.internal_model_from_file(
file_name, encoding, debug,
Expand Down Expand Up @@ -711,6 +711,12 @@ def register_obj_processors(self, obj_processors):
self.obj_processors = obj_processors
self.type_convertors.update(obj_processors)

@property
def _tx_model_param_definitions(self):
warnings.warn(
'_tx_model_param_definitions is deprecated in favor of model_param_defs.')
return self.model_param_defs


class TextXMetaMetaModel(object):
"""
Expand Down

0 comments on commit 3911188

Please sign in to comment.