author | Edouard Tisserant |
Thu, 19 Sep 2019 15:32:36 +0200 | |
branch | svghmi |
changeset 2782 | 723e420dfe72 |
parent 2456 | 7373e3048167 |
child 3750 | f62625418bff |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
4 |
# This file is part of Beremiz, a Integrated Development Environment for |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
5 |
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
6 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
7 |
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
8 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
9 |
# See COPYING file for copyrights details. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
10 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
11 |
# This program is free software; you can redistribute it and/or |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
12 |
# modify it under the terms of the GNU General Public License |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
13 |
# as published by the Free Software Foundation; either version 2 |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
14 |
# of the License, or (at your option) any later version. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
15 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
16 |
# This program is distributed in the hope that it will be useful, |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
19 |
# GNU General Public License for more details. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
20 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
21 |
# You should have received a copy of the GNU General Public License |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
22 |
# along with this program; if not, write to the Free Software |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1390
diff
changeset
|
23 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
814 | 24 |
|
1853
47a3f39bead0
fix pylint warning "(relative-import) Relative import 'Y', should be 'X.Y'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
25 |
|
47a3f39bead0
fix pylint warning "(relative-import) Relative import 'Y', should be 'X.Y'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
26 |
from __future__ import absolute_import |
1732
94ffe74e6895
clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
27 |
import re |
1832
0f1081928d65
fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1828
diff
changeset
|
28 |
from collections import OrderedDict |
2456
7373e3048167
python3 support: pylint,W1610 # (reduce-builtin) reduce built-in referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2453
diff
changeset
|
29 |
from functools import reduce |
1832
0f1081928d65
fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1828
diff
changeset
|
30 |
|
1853
47a3f39bead0
fix pylint warning "(relative-import) Relative import 'Y', should be 'X.Y'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
31 |
from plcopen.plcopen import LoadProject |
47a3f39bead0
fix pylint warning "(relative-import) Relative import 'Y', should be 'X.Y'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
32 |
from plcopen.definitions import * |
1390 | 33 |
|
34 |
TypeHierarchy = dict(TypeHierarchy_list) |
|
35 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
36 |
|
1390 | 37 |
def IsOfType(type, reference): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
38 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
39 |
Returns true if the given data type is the same that "reference" meta-type or one of its types. |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
40 |
""" |
1390 | 41 |
if reference is None: |
42 |
return True |
|
43 |
elif type == reference: |
|
44 |
return True |
|
45 |
else: |
|
46 |
parent_type = TypeHierarchy[type] |
|
47 |
if parent_type is not None: |
|
48 |
return IsOfType(parent_type, reference) |
|
49 |
return False |
|
50 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
51 |
|
1390 | 52 |
def GetSubTypes(type): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
53 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
54 |
Returns list of all types that correspont to the ANY* meta type |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
55 |
""" |
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
56 |
return [typename for typename, _parenttype in TypeHierarchy.items() if not typename.startswith("ANY") and IsOfType(typename, type)] |
1390 | 57 |
|
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1747
diff
changeset
|
58 |
|
1390 | 59 |
DataTypeRange = dict(DataTypeRange_list) |
60 |
||
61 |
""" |
|
62 |
Ordered list of common Function Blocks defined in the IEC 61131-3 |
|
814 | 63 |
Each block have this attributes: |
64 |
- "name" : The block name |
|
65 |
- "type" : The block type. It can be "function", "functionBlock" or "program" |
|
66 |
- "extensible" : Boolean that define if the block is extensible |
|
67 |
- "inputs" : List of the block inputs |
|
68 |
- "outputs" : List of the block outputs |
|
69 |
- "comment" : Comment that will be displayed in the block popup |
|
70 |
- "generate" : Method that generator will call for generating ST block code |
|
71 |
Inputs and outputs are a tuple of characteristics that are in order: |
|
72 |
- The name |
|
73 |
- The data type |
|
74 |
- The default modifier which can be "none", "negated", "rising" or "falling" |
|
75 |
""" |
|
76 |
||
1739
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
77 |
StdBlckLibs = {libname: LoadProject(tc6fname)[0] |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
78 |
for libname, tc6fname in StdTC6Libs} |
1739
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
79 |
StdBlckLst = [{"name": libname, "list": |
1390 | 80 |
[GetBlockInfos(pous) for pous in lib.getpous()]} |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
81 |
for libname, lib in StdBlckLibs.iteritems()] |
814 | 82 |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1774
diff
changeset
|
83 |
# ------------------------------------------------------------------------------- |
814 | 84 |
# Test identifier |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1774
diff
changeset
|
85 |
# ------------------------------------------------------------------------------- |
814 | 86 |
|
965 | 87 |
IDENTIFIER_MODEL = re.compile( |
88 |
"(?:%(letter)s|_(?:%(letter)s|%(digit)s))(?:_?(?:%(letter)s|%(digit)s))*$" % |
|
89 |
{"letter": "[a-zA-Z]", "digit": "[0-9]"}) |
|
814 | 90 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
91 |
|
814 | 92 |
def TestIdentifier(identifier): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
93 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
94 |
Test if identifier is valid |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
95 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
96 |
return IDENTIFIER_MODEL.match(identifier) is not None |
814 | 97 |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1774
diff
changeset
|
98 |
# ------------------------------------------------------------------------------- |
814 | 99 |
# Standard functions list generation |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1774
diff
changeset
|
100 |
# ------------------------------------------------------------------------------- |
814 | 101 |
|
102 |
||
103 |
def csv_file_to_table(file): |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
104 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
105 |
take a .csv file and translate it it a "csv_table" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
106 |
""" |
2448
1367b796e129
python3 support: pylint, W1649 # (deprecated-string-function) Accessing a deprecated function on the string module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2441
diff
changeset
|
107 |
table = [[column.strip() |
1367b796e129
python3 support: pylint, W1649 # (deprecated-string-function) Accessing a deprecated function on the string module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2441
diff
changeset
|
108 |
for column in line.split(';')] |
1367b796e129
python3 support: pylint, W1649 # (deprecated-string-function) Accessing a deprecated function on the string module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2441
diff
changeset
|
109 |
for line in file.readlines()] |
1367b796e129
python3 support: pylint, W1649 # (deprecated-string-function) Accessing a deprecated function on the string module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2441
diff
changeset
|
110 |
return table |
814 | 111 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
112 |
|
814 | 113 |
def find_section(section_name, table): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
114 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
115 |
seek into the csv table to a section ( section_name match 1st field ) |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
116 |
return the matching row without first field |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
117 |
""" |
814 | 118 |
fields = [None] |
1828
396da88d7b5c
fix unnecessary parens after keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
119 |
while fields[0] != section_name: |
814 | 120 |
fields = table.pop(0) |
121 |
return fields[1:] |
|
122 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
123 |
|
814 | 124 |
def get_standard_funtions_input_variables(table): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
125 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
126 |
extract the standard functions standard parameter names and types... |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
127 |
return a { ParameterName: Type, ...} |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
128 |
""" |
814 | 129 |
variables = find_section("Standard_functions_variables_types", table) |
130 |
standard_funtions_input_variables = {} |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset
|
131 |
fields = [True, True] |
1828
396da88d7b5c
fix unnecessary parens after keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
132 |
while fields[1]: |
814 | 133 |
fields = table.pop(0) |
1742
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1740
diff
changeset
|
134 |
variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:]) if champ != '']) |
814 | 135 |
standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type'] |
136 |
return standard_funtions_input_variables |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
137 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
138 |
|
814 | 139 |
def csv_input_translate(str_decl, variables, base): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
140 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
141 |
translate .csv file input declaration into PLCOpenEditor interessting values |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
142 |
in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...} |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
143 |
return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")] |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
144 |
""" |
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset
|
145 |
decl = str_decl.replace('(', '').replace(')', '').replace(' ', '').split(',') |
814 | 146 |
params = [] |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
147 |
|
814 | 148 |
len_of_not_predifined_variable = len([True for param_type in decl if param_type not in variables]) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
149 |
|
814 | 150 |
for param_type in decl: |
151 |
if param_type in variables.keys(): |
|
152 |
param_name = param_type |
|
153 |
param_type = variables[param_type] |
|
154 |
elif len_of_not_predifined_variable > 1: |
|
1734
750eeb7230a1
clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1732
diff
changeset
|
155 |
param_name = "IN%d" % base |
814 | 156 |
base += 1 |
157 |
else: |
|
158 |
param_name = "IN" |
|
159 |
params.append((param_name, param_type, "none")) |
|
160 |
return params |
|
161 |
||
162 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
163 |
def get_standard_funtions(table): |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
164 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
165 |
Returns this kind of declaration for all standard functions |
814 | 166 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
167 |
[{"name" : "Numerical", 'list': [ { |
814 | 168 |
'baseinputnumber': 1, |
169 |
'comment': 'Addition', |
|
170 |
'extensible': True, |
|
171 |
'inputs': [ ('IN1', 'ANY_NUM', 'none'), |
|
172 |
('IN2', 'ANY_NUM', 'none')], |
|
173 |
'name': 'ADD', |
|
174 |
'outputs': [('OUT', 'ANY_NUM', 'none')], |
|
175 |
'type': 'function'}, ...... ] },.....] |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
176 |
""" |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
177 |
|
814 | 178 |
variables = get_standard_funtions_input_variables(table) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
179 |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset
|
180 |
fonctions = find_section("Standard_functions_type", table) |
814 | 181 |
|
182 |
Standard_Functions_Decl = [] |
|
183 |
Current_section = None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
184 |
|
814 | 185 |
translate = { |
1878
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1853
diff
changeset
|
186 |
"extensible": lambda x: {"yes": True, "no": False}[x], |
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1853
diff
changeset
|
187 |
"inputs": lambda x: csv_input_translate(x, variables, baseinputnumber), |
fb73a6b6622d
fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1853
diff
changeset
|
188 |
"outputs": lambda x: [("OUT", x, "none")]} |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
189 |
|
814 | 190 |
for fields in table: |
191 |
if fields[1]: |
|
192 |
# If function section name given |
|
193 |
if fields[0]: |
|
194 |
words = fields[0].split('"') |
|
195 |
if len(words) > 1: |
|
196 |
section_name = words[1] |
|
197 |
else: |
|
198 |
section_name = fields[0] |
|
1739
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
199 |
Current_section = {"name": section_name, "list": []} |
814 | 200 |
Standard_Functions_Decl.append(Current_section) |
201 |
if Current_section: |
|
202 |
Function_decl = dict([(champ, val) for champ, val in zip(fonctions, fields[1:]) if champ]) |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset
|
203 |
baseinputnumber = int(Function_decl.get("baseinputnumber", 1)) |
814 | 204 |
Function_decl["baseinputnumber"] = baseinputnumber |
205 |
for param, value in Function_decl.iteritems(): |
|
206 |
if param in translate: |
|
207 |
Function_decl[param] = translate[param](value) |
|
208 |
Function_decl["type"] = "function" |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
209 |
|
1739
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
210 |
if Function_decl["name"].startswith('*') or Function_decl["name"].endswith('*'): |
814 | 211 |
input_ovrloading_types = GetSubTypes(Function_decl["inputs"][0][1]) |
212 |
output_types = GetSubTypes(Function_decl["outputs"][0][1]) |
|
213 |
else: |
|
214 |
input_ovrloading_types = [None] |
|
215 |
output_types = [None] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
216 |
|
814 | 217 |
funcdeclname_orig = Function_decl["name"] |
218 |
funcdeclname = Function_decl["name"].strip('*_') |
|
219 |
fdc = Function_decl["inputs"][:] |
|
220 |
for intype in input_ovrloading_types: |
|
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
221 |
if intype is not None: |
814 | 222 |
Function_decl["inputs"] = [] |
223 |
for decl_tpl in fdc: |
|
224 |
if IsOfType(intype, decl_tpl[1]): |
|
225 |
Function_decl["inputs"] += [(decl_tpl[0], intype, decl_tpl[2])] |
|
226 |
else: |
|
227 |
Function_decl["inputs"] += [(decl_tpl)] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
228 |
|
814 | 229 |
if funcdeclname_orig.startswith('*'): |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
230 |
funcdeclin = intype + '_' + funcdeclname |
814 | 231 |
else: |
232 |
funcdeclin = funcdeclname |
|
233 |
else: |
|
234 |
funcdeclin = funcdeclname |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
235 |
|
814 | 236 |
for outype in output_types: |
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
237 |
if outype is not None: |
814 | 238 |
decl_tpl = Function_decl["outputs"][0] |
1747
6046ffa2280f
clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
239 |
Function_decl["outputs"] = [(decl_tpl[0], outype, decl_tpl[2])] |
814 | 240 |
if funcdeclname_orig.endswith('*'): |
1758
845ca626db09
clean-up: fix PEP8 E222 multiple spaces after operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
241 |
funcdeclout = funcdeclin + '_' + outype |
814 | 242 |
else: |
1758
845ca626db09
clean-up: fix PEP8 E222 multiple spaces after operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
243 |
funcdeclout = funcdeclin |
814 | 244 |
else: |
1758
845ca626db09
clean-up: fix PEP8 E222 multiple spaces after operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
245 |
funcdeclout = funcdeclin |
814 | 246 |
Function_decl["name"] = funcdeclout |
247 |
||
1390 | 248 |
# apply filter given in "filter" column |
249 |
filter_name = Function_decl["filter"] |
|
250 |
store = True |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset
|
251 |
for (InTypes, OutTypes) in ANY_TO_ANY_FILTERS.get(filter_name, []): |
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset
|
252 |
outs = reduce(lambda a, b: a or b, |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
253 |
map(lambda testtype: IsOfType( |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
254 |
Function_decl["outputs"][0][1], |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
255 |
testtype), OutTypes)) |
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset
|
256 |
inps = reduce(lambda a, b: a or b, |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
257 |
map(lambda testtype: IsOfType( |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
258 |
Function_decl["inputs"][0][1], |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
259 |
testtype), InTypes)) |
1390 | 260 |
if inps and outs and Function_decl["outputs"][0][1] != Function_decl["inputs"][0][1]: |
261 |
store = True |
|
262 |
break |
|
263 |
else: |
|
264 |
store = False |
|
1739
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
265 |
if store: |
814 | 266 |
# create the copy of decl dict to be appended to section |
267 |
Function_decl_copy = Function_decl.copy() |
|
268 |
Current_section["list"].append(Function_decl_copy) |
|
269 |
else: |
|
2453
9900a8df922f
python3 support: pylint, W1625 # (raising-string) Raising a string exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2448
diff
changeset
|
270 |
raise ValueError("First function must be in a category") |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
271 |
|
814 | 272 |
return Standard_Functions_Decl |
273 |
||
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1747
diff
changeset
|
274 |
|
1390 | 275 |
StdBlckLst.extend(get_standard_funtions(csv_file_to_table(open(StdFuncsCSV)))) |
1283
f3cfe1ff917e
More optimization attemps in type handling
Edouard Tisserant
parents:
1239
diff
changeset
|
276 |
|
f3cfe1ff917e
More optimization attemps in type handling
Edouard Tisserant
parents:
1239
diff
changeset
|
277 |
# Dictionary to speedup block type fetching by name |
1320
bb04c41cbee9
Used OrderedDict for keeping of standard function blocks library defined
Laurent Bessard
parents:
1313
diff
changeset
|
278 |
StdBlckDct = OrderedDict() |
1283
f3cfe1ff917e
More optimization attemps in type handling
Edouard Tisserant
parents:
1239
diff
changeset
|
279 |
|
f3cfe1ff917e
More optimization attemps in type handling
Edouard Tisserant
parents:
1239
diff
changeset
|
280 |
for section in StdBlckLst: |
814 | 281 |
for desc in section["list"]: |
282 |
words = desc["comment"].split('"') |
|
283 |
if len(words) > 1: |
|
284 |
desc["comment"] = words[1] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
285 |
desc["usage"] = ("\n (%s) => (%s)" % |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
286 |
(", ".join(["%s:%s" % (input[1], input[0]) |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
287 |
for input in desc["inputs"]]), |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
288 |
", ".join(["%s:%s" % (output[1], output[0]) |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
289 |
for output in desc["outputs"]]))) |
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset
|
290 |
BlkLst = StdBlckDct.setdefault(desc["name"], []) |
1283
f3cfe1ff917e
More optimization attemps in type handling
Edouard Tisserant
parents:
1239
diff
changeset
|
291 |
BlkLst.append((section["name"], desc)) |
814 | 292 |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1774
diff
changeset
|
293 |
# ------------------------------------------------------------------------------- |
814 | 294 |
# Languages Keywords |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1774
diff
changeset
|
295 |
# ------------------------------------------------------------------------------- |
814 | 296 |
|
297 |
# Keywords for Pou Declaration |
|
298 |
POU_BLOCK_START_KEYWORDS = ["FUNCTION", "FUNCTION_BLOCK", "PROGRAM"] |
|
299 |
POU_BLOCK_END_KEYWORDS = ["END_FUNCTION", "END_FUNCTION_BLOCK", "END_PROGRAM"] |
|
300 |
POU_KEYWORDS = ["EN", "ENO", "F_EDGE", "R_EDGE"] + POU_BLOCK_START_KEYWORDS + POU_BLOCK_END_KEYWORDS |
|
1283
f3cfe1ff917e
More optimization attemps in type handling
Edouard Tisserant
parents:
1239
diff
changeset
|
301 |
for category in StdBlckLst: |
814 | 302 |
for block in category["list"]: |
303 |
if block["name"] not in POU_KEYWORDS: |
|
304 |
POU_KEYWORDS.append(block["name"]) |
|
305 |
||
306 |
||
307 |
# Keywords for Type Declaration |
|
308 |
TYPE_BLOCK_START_KEYWORDS = ["TYPE", "STRUCT"] |
|
309 |
TYPE_BLOCK_END_KEYWORDS = ["END_TYPE", "END_STRUCT"] |
|
310 |
TYPE_KEYWORDS = ["ARRAY", "OF", "T", "D", "TIME_OF_DAY", "DATE_AND_TIME"] + TYPE_BLOCK_START_KEYWORDS + TYPE_BLOCK_END_KEYWORDS |
|
311 |
TYPE_KEYWORDS.extend([keyword for keyword in TypeHierarchy.keys() if keyword not in TYPE_KEYWORDS]) |
|
312 |
||
313 |
||
314 |
# Keywords for Variable Declaration |
|
315 |
VAR_BLOCK_START_KEYWORDS = ["VAR", "VAR_INPUT", "VAR_OUTPUT", "VAR_IN_OUT", "VAR_TEMP", "VAR_EXTERNAL"] |
|
316 |
VAR_BLOCK_END_KEYWORDS = ["END_VAR"] |
|
317 |
VAR_KEYWORDS = ["AT", "CONSTANT", "RETAIN", "NON_RETAIN"] + VAR_BLOCK_START_KEYWORDS + VAR_BLOCK_END_KEYWORDS |
|
318 |
||
319 |
||
320 |
# Keywords for Configuration Declaration |
|
321 |
CONFIG_BLOCK_START_KEYWORDS = ["CONFIGURATION", "RESOURCE", "VAR_ACCESS", "VAR_CONFIG", "VAR_GLOBAL"] |
|
322 |
CONFIG_BLOCK_END_KEYWORDS = ["END_CONFIGURATION", "END_RESOURCE", "END_VAR"] |
|
323 |
CONFIG_KEYWORDS = ["ON", "PROGRAM", "WITH", "READ_ONLY", "READ_WRITE", "TASK"] + CONFIG_BLOCK_START_KEYWORDS + CONFIG_BLOCK_END_KEYWORDS |
|
324 |
||
325 |
# Keywords for Structured Function Chart |
|
326 |
SFC_BLOCK_START_KEYWORDS = ["ACTION", "INITIAL_STEP", "STEP", "TRANSITION"] |
|
327 |
SFC_BLOCK_END_KEYWORDS = ["END_ACTION", "END_STEP", "END_TRANSITION"] |
|
949 | 328 |
SFC_KEYWORDS = ["FROM", "TO"] + SFC_BLOCK_START_KEYWORDS + SFC_BLOCK_END_KEYWORDS |
814 | 329 |
|
330 |
||
331 |
# Keywords for Instruction List |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
332 |
IL_KEYWORDS = [ |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
333 |
"TRUE", "FALSE", "LD", "LDN", "ST", "STN", "S", "R", "AND", "ANDN", "OR", "ORN", |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
334 |
"XOR", "XORN", "NOT", "ADD", "SUB", "MUL", "DIV", "MOD", "GT", "GE", "EQ", "NE", |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
335 |
"LE", "LT", "JMP", "JMPC", "JMPCN", "CAL", "CALC", "CALCN", "RET", "RETC", "RETCN" |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
336 |
] |
814 | 337 |
|
338 |
||
339 |
# Keywords for Structured Text |
|
340 |
ST_BLOCK_START_KEYWORDS = ["IF", "ELSIF", "ELSE", "CASE", "FOR", "WHILE", "REPEAT"] |
|
341 |
ST_BLOCK_END_KEYWORDS = ["END_IF", "END_CASE", "END_FOR", "END_WHILE", "END_REPEAT"] |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
342 |
ST_KEYWORDS = [ |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
343 |
"TRUE", "FALSE", "THEN", "OF", "TO", "BY", "DO", "DO", "UNTIL", "EXIT", |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
344 |
"RETURN", "NOT", "MOD", "AND", "XOR", "OR" |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1758
diff
changeset
|
345 |
] + ST_BLOCK_START_KEYWORDS + ST_BLOCK_END_KEYWORDS |
814 | 346 |
|
347 |
# All the keywords of IEC |
|
348 |
IEC_BLOCK_START_KEYWORDS = [] |
|
349 |
IEC_BLOCK_END_KEYWORDS = [] |
|
350 |
IEC_KEYWORDS = ["E", "TRUE", "FALSE"] |
|
351 |
for all_keywords, keywords_list in [(IEC_BLOCK_START_KEYWORDS, [POU_BLOCK_START_KEYWORDS, TYPE_BLOCK_START_KEYWORDS, |
|
352 |
VAR_BLOCK_START_KEYWORDS, CONFIG_BLOCK_START_KEYWORDS, |
|
353 |
SFC_BLOCK_START_KEYWORDS, ST_BLOCK_START_KEYWORDS]), |
|
354 |
(IEC_BLOCK_END_KEYWORDS, [POU_BLOCK_END_KEYWORDS, TYPE_BLOCK_END_KEYWORDS, |
|
355 |
VAR_BLOCK_END_KEYWORDS, CONFIG_BLOCK_END_KEYWORDS, |
|
356 |
SFC_BLOCK_END_KEYWORDS, ST_BLOCK_END_KEYWORDS]), |
|
357 |
(IEC_KEYWORDS, [POU_KEYWORDS, TYPE_KEYWORDS, VAR_KEYWORDS, CONFIG_KEYWORDS, |
|
358 |
SFC_KEYWORDS, IL_KEYWORDS, ST_KEYWORDS])]: |
|
359 |
for keywords in keywords_list: |
|
360 |
all_keywords.extend([keyword for keyword in keywords if keyword not in all_keywords]) |