plcopen/structures.py
changeset 1740 b789b695b5c6
parent 1739 ec153828ded2
child 1742 92932cd370a4
equal deleted inserted replaced
1739:ec153828ded2 1740:b789b695b5c6
    98 
    98 
    99 def csv_file_to_table(file):
    99 def csv_file_to_table(file):
   100     """
   100     """
   101     take a .csv file and translate it it a "csv_table"
   101     take a .csv file and translate it it a "csv_table"
   102     """
   102     """
   103     return [ map(string.strip,line.split(';')) for line in file.xreadlines()]
   103     return [ map(string.strip, line.split(';')) for line in file.xreadlines()]
   104 
   104 
   105 
   105 
   106 def find_section(section_name, table):
   106 def find_section(section_name, table):
   107     """
   107     """
   108     seek into the csv table to a section ( section_name match 1st field )
   108     seek into the csv table to a section ( section_name match 1st field )
   119     extract the standard functions standard parameter names and types...
   119     extract the standard functions standard parameter names and types...
   120     return a { ParameterName: Type, ...}
   120     return a { ParameterName: Type, ...}
   121     """
   121     """
   122     variables = find_section("Standard_functions_variables_types", table)
   122     variables = find_section("Standard_functions_variables_types", table)
   123     standard_funtions_input_variables = {}
   123     standard_funtions_input_variables = {}
   124     fields = [True,True]
   124     fields = [True, True]
   125     while(fields[1]):
   125     while(fields[1]):
   126         fields = table.pop(0)
   126         fields = table.pop(0)
   127         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!=''])
   128         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']
   129     return standard_funtions_input_variables
   129     return standard_funtions_input_variables
   133     """
   133     """
   134     translate .csv file input declaration into PLCOpenEditor interessting values
   134     translate .csv file input declaration into PLCOpenEditor interessting values
   135     in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...}
   135     in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...}
   136     return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")]
   136     return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")]
   137     """
   137     """
   138     decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',')
   138     decl = str_decl.replace('(', '').replace(')', '').replace(' ', '').split(',')
   139     params = []
   139     params = []
   140 
   140 
   141     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])
   142 
   142 
   143     for param_type in decl:
   143     for param_type in decl:
   168                 'type': 'function'}, ...... ] },.....]
   168                 'type': 'function'}, ...... ] },.....]
   169     """
   169     """
   170 
   170 
   171     variables = get_standard_funtions_input_variables(table)
   171     variables = get_standard_funtions_input_variables(table)
   172 
   172 
   173     fonctions = find_section("Standard_functions_type",table)
   173     fonctions = find_section("Standard_functions_type", table)
   174 
   174 
   175     Standard_Functions_Decl = []
   175     Standard_Functions_Decl = []
   176     Current_section = None
   176     Current_section = None
   177 
   177 
   178     translate = {
   178     translate = {
   179             "extensible": lambda x: {"yes":True, "no":False}[x],
   179             "extensible": lambda x: {"yes": True, "no": False}[x],
   180             "inputs": lambda x:csv_input_translate(x,variables,baseinputnumber),
   180             "inputs":     lambda x: csv_input_translate(x, variables, baseinputnumber),
   181             "outputs":lambda x:[("OUT",x,"none")]}
   181             "outputs":    lambda x: [("OUT", x, "none")]}
   182 
   182 
   183     for fields in table:
   183     for fields in table:
   184         if fields[1]:
   184         if fields[1]:
   185             # If function section name given
   185             # If function section name given
   186             if fields[0]:
   186             if fields[0]:
   192                 Current_section = {"name": section_name, "list": []}
   192                 Current_section = {"name": section_name, "list": []}
   193                 Standard_Functions_Decl.append(Current_section)
   193                 Standard_Functions_Decl.append(Current_section)
   194                 Function_decl_list = []
   194                 Function_decl_list = []
   195             if Current_section:
   195             if Current_section:
   196                 Function_decl = dict([(champ, val) for champ, val in zip(fonctions, fields[1:]) if champ])
   196                 Function_decl = dict([(champ, val) for champ, val in zip(fonctions, fields[1:]) if champ])
   197                 baseinputnumber = int(Function_decl.get("baseinputnumber",1))
   197                 baseinputnumber = int(Function_decl.get("baseinputnumber", 1))
   198                 Function_decl["baseinputnumber"] = baseinputnumber
   198                 Function_decl["baseinputnumber"] = baseinputnumber
   199                 for param, value in Function_decl.iteritems():
   199                 for param, value in Function_decl.iteritems():
   200                     if param in translate:
   200                     if param in translate:
   201                         Function_decl[param] = translate[param](value)
   201                         Function_decl[param] = translate[param](value)
   202                 Function_decl["type"] = "function"
   202                 Function_decl["type"] = "function"
   240                         Function_decl["name"] = funcdeclout
   240                         Function_decl["name"] = funcdeclout
   241 
   241 
   242                         # apply filter given in "filter" column
   242                         # apply filter given in "filter" column
   243                         filter_name = Function_decl["filter"]
   243                         filter_name = Function_decl["filter"]
   244                         store = True
   244                         store = True
   245                         for (InTypes, OutTypes) in ANY_TO_ANY_FILTERS.get(filter_name,[]):
   245                         for (InTypes, OutTypes) in ANY_TO_ANY_FILTERS.get(filter_name, []):
   246                             outs = reduce(lambda a,b: a or b,
   246                             outs = reduce(lambda a, b: a or b,
   247                                        map(lambda testtype: IsOfType(
   247                                        map(lambda testtype: IsOfType(
   248                                            Function_decl["outputs"][0][1],
   248                                            Function_decl["outputs"][0][1],
   249                                            testtype), OutTypes))
   249                                            testtype), OutTypes))
   250                             inps = reduce(lambda a,b: a or b,
   250                             inps = reduce(lambda a, b: a or b,
   251                                        map(lambda testtype: IsOfType(
   251                                        map(lambda testtype: IsOfType(
   252                                            Function_decl["inputs"][0][1],
   252                                            Function_decl["inputs"][0][1],
   253                                            testtype), InTypes))
   253                                            testtype), InTypes))
   254                             if inps and outs and Function_decl["outputs"][0][1] != Function_decl["inputs"][0][1]:
   254                             if inps and outs and Function_decl["outputs"][0][1] != Function_decl["inputs"][0][1]:
   255                                 store = True
   255                                 store = True
   278         desc["usage"] = ("\n (%s) => (%s)" %
   278         desc["usage"] = ("\n (%s) => (%s)" %
   279             (", ".join(["%s:%s" % (input[1], input[0])
   279             (", ".join(["%s:%s" % (input[1], input[0])
   280                         for input in desc["inputs"]]),
   280                         for input in desc["inputs"]]),
   281              ", ".join(["%s:%s" % (output[1], output[0])
   281              ", ".join(["%s:%s" % (output[1], output[0])
   282                         for output in desc["outputs"]])))
   282                         for output in desc["outputs"]])))
   283         BlkLst = StdBlckDct.setdefault(desc["name"],[])
   283         BlkLst = StdBlckDct.setdefault(desc["name"], [])
   284         BlkLst.append((section["name"], desc))
   284         BlkLst.append((section["name"], desc))
   285 
   285 
   286 #-------------------------------------------------------------------------------
   286 #-------------------------------------------------------------------------------
   287 #                            Languages Keywords
   287 #                            Languages Keywords
   288 #-------------------------------------------------------------------------------
   288 #-------------------------------------------------------------------------------