Commit 1a726385 authored by carignani's avatar carignani
Browse files

intial support for TDL version 1.5

parent 1e5e99d5
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+1 −0
Original line number Diff line number Diff line
*.pyc
 No newline at end of file
+57 −0
Original line number Diff line number Diff line
@@ -2,6 +2,63 @@

A Python package to parse ETSI TDL concrete syntax and manipulate the TDL models.

## Implementation

PyTDL uses the [textX](http://textx.github.io/textX/stable/) python package to parse and manipulate TDL definitions (in TDLan syntax) into python objects.

The grammars are developed on the basis of the Xtext grammars used in [the TOP Eclipse Plugins](https://labs.etsi.org/rep/top/ide).

The syntaxes of the grammars between `Xtext` and `textX` are different. The differences are documented [here](http://textx.github.io/textX/stable/about/comparison/#difference-to-xtext-grammar-language).

More information on ETSI TDL are available at the [TDL website](https://tdl.etsi.org/).

## Install dependencies

Install requirements via `pip`:

    python3 -m pip install textx argparse 

## Usage

Use the `pytdl` script as an executable entry point to run the main script (`pytdl.py`).

The command line arguments are exaplained below:

    usage: pytdl.py [-h] {validate,python,inspect} filename
    
    TDL python utilities.
    
    positional arguments:
      {validate,python,inspect}
                        the command to be executed.
                        "inspect"    returns a list of definitions in the given package
                        "validate"   parses the file and print any syntactic errors found. Exit code is 0 if the file is valid
                        "python"     generates python objects for the definitions found (experimental)
      filename              the TDL script to manipulate

    optional arguments:
      -h, --help            show this help message and exit
	
## Test

A very small set of tests is included in the `test/` folder. 

To execute them, first install the `pytest` python package (you may need super user rights):

    python3 -m pip pytest

Then execute the following:

    cd test/
    python3 -m pytest .

## Todo

* [x]  Import new data types from Xtext plugins 
* [x]  Verify the new grammar parses Example 1 and Example 2 of 1.5
* [ ]  Support for inline parameterBinding (required for Example 3 of 1.5)
* [ ]  Support previous examples (when possible)

## Licensing

TDL examples taken from ETSI specifications are under Copyright of ETSI ([www.etsi.org](www.etsi.org)) with

pytdl

100644 → 100755
+1 −1
Original line number Diff line number Diff line
@@ -6,4 +6,4 @@

DIR="$( dirname "${BASH_SOURCE[0]}" )"

python ${DIR}/src/pytdl.py $@
 No newline at end of file
python3 ${DIR}/src/pytdl.py $@
 No newline at end of file
+39 −22
Original line number Diff line number Diff line
#!/env/python3
'''
PyTDL - validate ETSI TDL, with Python

@@ -6,6 +7,7 @@ Released under Eclipse Public License v1

import argparse
import os
import sys

from textx.metamodel import metamodel_from_file
from textx.exceptions import TextXSemanticError, TextXSyntaxError
@@ -24,13 +26,24 @@ try:
            lib.Message]
    )
except TextXSemanticError as err:
    print "Error in TDL grammar files:"
    print err
    print "Quitting."
    print("Error in TDL grammar files:")
    print(err)
    print("Quitting.")
    exit(-1)

PARSER = argparse.ArgumentParser(description='TDL python utilities.')
PARSER.add_argument('command', nargs=1, help='the command to be executed')
SUPPORTED_COMMANDS=[
    "validate",
    "python",
    "inspect"
]

CMD_HELP='''the command to be executed.
"inspect"    returns a list of definitions in the given package
"validate"   parses the file and print any syntactic errors found. Exit code is 0 if the file is valid
"python"     generates python objects for the definitions found (experimental)'''

PARSER = argparse.ArgumentParser(description='TDL python utilities.', formatter_class=argparse.RawTextHelpFormatter)
PARSER.add_argument('command', nargs=1, choices=SUPPORTED_COMMANDS, help=CMD_HELP)
PARSER.add_argument('filename', nargs=1, help='the TDL script to manipulate')

ARGS = PARSER.parse_args()
@@ -42,30 +55,34 @@ P = None
try:
    P = TDL_MM.model_from_file(FN)
except TextXSyntaxError as err:
    print "Syntax error found in file " + FN + ":"
    print ""
    print "\t" + str(err)
    print ""
    print "Quitting."
    exit(-2)
    print("Syntax error found in file " + FN + ":")
    print("")
    print("\t" + str(err))
    print("")
    print("Quitting.")
    sys.exit(-2)

if CMD == "validate":
    exit(0)
    print("File '{}' is valid.".format(FN))
    sys.exit(0)

if CMD == "python":
    print "import pytdl.interpreter"
    print ""
    print("import pytdl.interpreter")
    print("")

    P.to_python()
    exit(0)
    sys.exit(0)

print "###################"
print "Tests in the package:"
if CMD == "inspect":
    print("###################")
    print("Tests in the package:")

    lib.list_test_descriptions(P)

print "###################"
print "Everything in the package:"
    print("###################")
    print("Everything in the package:")

    for i in P.packagedElement:
    print i.name
        print(i.name)

    sys.exit(0)
+7 −7
Original line number Diff line number Diff line
#!/env/python2.7
#!/env/python3
'''
Library for Pytdl

@@ -45,14 +45,14 @@ class Package(Element):
        return self.all("TestConfiguration")

    def to_python(self, ind=0):
        print "class Package_"+self.name+"(TDL_Package):"
        print "    self.name = "+self.name
        print("class Package_"+self.name+"(TDL_Package):")
        print("    self.name = "+self.name)
        for i in self.all():
            if hasattr(i, 'to_python'):
                i.to_python(ind+1)
            else:
                print i
        print ""
                print(i)
        print("")

class TestDescription(Element):
    """docstring for TestDescription"""
@@ -67,7 +67,7 @@ class TestDescription(Element):
        self.isLocallyOrdered = isLocallyOrdered 

    def to_python(self, ind=0):
        print indent(ind) + "def test_"+self.name+"():"
        print(indent(ind) + "def test_"+self.name+"():")
        self.behaviourDescription.to_python(ind+1)

class BehaviourDescription(Element):
@@ -129,7 +129,7 @@ class Message(Element):
        self.sourceGate =  sourceGate

    def to_python(self, ind):
        print indent(ind) + "tdl_send(from="+self.sourceGate+",to="+str(self.target[0].targetGate)+")"
        print(indent(ind) + "tdl_send(from="+self.sourceGate+",to="+str(self.target[0].targetGate)+")")
            

def list_test_descriptions(package):
Loading