plcopen/structures.py
changeset 1736 7e61baa047f0
parent 1734 750eeb7230a1
child 1739 ec153828ded2
equal deleted inserted replaced
1735:c02818d7e29f 1736:7e61baa047f0
    28 from collections import OrderedDict
    28 from collections import OrderedDict
    29 from definitions import *
    29 from definitions import *
    30 
    30 
    31 TypeHierarchy = dict(TypeHierarchy_list)
    31 TypeHierarchy = dict(TypeHierarchy_list)
    32 
    32 
    33 """
    33 
    34 returns true if the given data type is the same that "reference" meta-type or one of its types.
       
    35 """
       
    36 def IsOfType(type, reference):
    34 def IsOfType(type, reference):
       
    35     """
       
    36     Returns true if the given data type is the same that "reference" meta-type or one of its types.
       
    37     """
    37     if reference is None:
    38     if reference is None:
    38         return True
    39         return True
    39     elif type == reference:
    40     elif type == reference:
    40         return True
    41         return True
    41     else:
    42     else:
    42         parent_type = TypeHierarchy[type]
    43         parent_type = TypeHierarchy[type]
    43         if parent_type is not None:
    44         if parent_type is not None:
    44             return IsOfType(parent_type, reference)
    45             return IsOfType(parent_type, reference)
    45     return False
    46     return False
    46 
    47 
    47 """
    48 
    48 returns list of all types that correspont to the ANY* meta type
       
    49 """
       
    50 def GetSubTypes(type):
    49 def GetSubTypes(type):
       
    50     """
       
    51     Returns list of all types that correspont to the ANY* meta type
       
    52     """
    51     return [typename for typename, parenttype in TypeHierarchy.items() if not typename.startswith("ANY") and IsOfType(typename, type)]
    53     return [typename for typename, parenttype in TypeHierarchy.items() if not typename.startswith("ANY") and IsOfType(typename, type)]
    52 
    54 
    53 DataTypeRange = dict(DataTypeRange_list)
    55 DataTypeRange = dict(DataTypeRange_list)
    54 
    56 
    55 """
    57 """
    80 
    82 
    81 IDENTIFIER_MODEL = re.compile(
    83 IDENTIFIER_MODEL = re.compile(
    82     "(?:%(letter)s|_(?:%(letter)s|%(digit)s))(?:_?(?:%(letter)s|%(digit)s))*$" %
    84     "(?:%(letter)s|_(?:%(letter)s|%(digit)s))(?:_?(?:%(letter)s|%(digit)s))*$" %
    83     {"letter": "[a-zA-Z]", "digit": "[0-9]"})
    85     {"letter": "[a-zA-Z]", "digit": "[0-9]"})
    84 
    86 
    85 # Test if identifier is valid
    87 
    86 def TestIdentifier(identifier):
    88 def TestIdentifier(identifier):
    87      return IDENTIFIER_MODEL.match(identifier) is not None
    89     """
       
    90     Test if identifier is valid
       
    91     """
       
    92     return IDENTIFIER_MODEL.match(identifier) is not None
    88 
    93 
    89 #-------------------------------------------------------------------------------
    94 #-------------------------------------------------------------------------------
    90 #                        Standard functions list generation
    95 #                        Standard functions list generation
    91 #-------------------------------------------------------------------------------
    96 #-------------------------------------------------------------------------------
    92 
    97 
    93 
    98 
    94 """
       
    95 take a .csv file and translate it it a "csv_table"
       
    96 """
       
    97 def csv_file_to_table(file):
    99 def csv_file_to_table(file):
       
   100     """
       
   101     take a .csv file and translate it it a "csv_table"
       
   102     """
    98     return [ map(string.strip,line.split(';')) for line in file.xreadlines()]
   103     return [ map(string.strip,line.split(';')) for line in file.xreadlines()]
    99 
   104 
   100 """
   105 
   101 seek into the csv table to a section ( section_name match 1st field )
       
   102 return the matching row without first field
       
   103 """
       
   104 def find_section(section_name, table):
   106 def find_section(section_name, table):
       
   107     """
       
   108     seek into the csv table to a section ( section_name match 1st field )
       
   109     return the matching row without first field
       
   110     """
   105     fields = [None]
   111     fields = [None]
   106     while(fields[0] != section_name):
   112     while(fields[0] != section_name):
   107         fields = table.pop(0)
   113         fields = table.pop(0)
   108     return fields[1:]
   114     return fields[1:]
   109 
   115 
   110 """
   116 
   111 extract the standard functions standard parameter names and types...
       
   112 return a { ParameterName: Type, ...}
       
   113 """
       
   114 def get_standard_funtions_input_variables(table):
   117 def get_standard_funtions_input_variables(table):
       
   118     """
       
   119     extract the standard functions standard parameter names and types...
       
   120     return a { ParameterName: Type, ...}
       
   121     """
   115     variables = find_section("Standard_functions_variables_types", table)
   122     variables = find_section("Standard_functions_variables_types", table)
   116     standard_funtions_input_variables = {}
   123     standard_funtions_input_variables = {}
   117     fields = [True,True]
   124     fields = [True,True]
   118     while(fields[1]):
   125     while(fields[1]):
   119         fields = table.pop(0)
   126         fields = table.pop(0)
   120         variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:]) if champ!=''])
   127         variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:]) if champ!=''])
   121         standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type']
   128         standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type']
   122     return standard_funtions_input_variables
   129     return standard_funtions_input_variables
   123 
   130 
   124 """
   131 
   125 translate .csv file input declaration into PLCOpenEditor interessting values
       
   126 in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...}
       
   127 return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")]
       
   128 """
       
   129 def csv_input_translate(str_decl, variables, base):
   132 def csv_input_translate(str_decl, variables, base):
       
   133     """
       
   134     translate .csv file input declaration into PLCOpenEditor interessting values
       
   135     in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...}
       
   136     return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")]
       
   137     """
   130     decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',')
   138     decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',')
   131     params = []
   139     params = []
   132 
   140 
   133     len_of_not_predifined_variable = len([True for param_type in decl if param_type not in variables])
   141     len_of_not_predifined_variable = len([True for param_type in decl if param_type not in variables])
   134 
   142 
   143             param_name = "IN"
   151             param_name = "IN"
   144         params.append((param_name, param_type, "none"))
   152         params.append((param_name, param_type, "none"))
   145     return params
   153     return params
   146 
   154 
   147 
   155 
   148 """
   156 def get_standard_funtions(table):
   149 Returns this kind of declaration for all standard functions
   157     """
       
   158     Returns this kind of declaration for all standard functions
   150 
   159 
   151             [{"name" : "Numerical", 'list': [   {
   160             [{"name" : "Numerical", 'list': [   {
   152                 'baseinputnumber': 1,
   161                 'baseinputnumber': 1,
   153                 'comment': 'Addition',
   162                 'comment': 'Addition',
   154                 'extensible': True,
   163                 'extensible': True,
   155                 'inputs': [   ('IN1', 'ANY_NUM', 'none'),
   164                 'inputs': [   ('IN1', 'ANY_NUM', 'none'),
   156                               ('IN2', 'ANY_NUM', 'none')],
   165                               ('IN2', 'ANY_NUM', 'none')],
   157                 'name': 'ADD',
   166                 'name': 'ADD',
   158                 'outputs': [('OUT', 'ANY_NUM', 'none')],
   167                 'outputs': [('OUT', 'ANY_NUM', 'none')],
   159                 'type': 'function'}, ...... ] },.....]
   168                 'type': 'function'}, ...... ] },.....]
   160 """
   169     """
   161 def get_standard_funtions(table):
       
   162 
   170 
   163     variables = get_standard_funtions_input_variables(table)
   171     variables = get_standard_funtions_input_variables(table)
   164 
   172 
   165     fonctions = find_section("Standard_functions_type",table)
   173     fonctions = find_section("Standard_functions_type",table)
   166 
   174