# HG changeset patch # User etisserant # Date 1181145273 -7200 # Node ID fc897b7bfa7b1ba766799de4bf552685754711a4 # Parent cd0133ed377b449acf8a7b8bbee3a9601ff551cf Enhanced standard function decalration fr cvs diff -r cd0133ed377b -r fc897b7bfa7b plcopen/structures.py --- a/plcopen/structures.py Tue Jun 05 14:32:01 2007 +0200 +++ b/plcopen/structures.py Wed Jun 06 17:54:33 2007 +0200 @@ -178,8 +178,7 @@ } """ -Function that returns if the given data type is the same that "reference" or one -of its children types +returns true if the given data type is the same that "reference" meta-type or one of its types. """ def IsOfType(test, reference): @@ -189,23 +188,32 @@ test = TypeHierarchy[test] return False +""" +returns list of all types that correspont to the ANY* meta type +""" def GetSubTypes(reference): return [ typename for typename in TypeHierarchy.iterkeys() if typename[:3] != "ANY" and IsOfType(typename, reference)] - - - - +""" +take a .csv file and translate it it a "csv_table" +""" def csv_file_to_table(file): return [ map(string.strip,line.split(';')) for line in file.xreadlines()] +""" +seek into the csv table to a section ( section_name match 1st field ) +return the matching row without first field +""" def find_section(section_name, table): fields = [None] while(fields[0] != section_name): fields = table.pop(0) return fields[1:] - +""" +extract the standard functions standard parameter names and types... +return a { ParameterName: Type, ...} +""" def get_standard_funtions_input_variables(table): variables = find_section("Standard_functions_variables_types", table) standard_funtions_input_variables = {} @@ -216,7 +224,11 @@ standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type'] return standard_funtions_input_variables -#"(ANY_NUM, ANY_NUM)" ---> [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")] +""" +translate .csv file input declaration into PLCOpenEditor interessting values +in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...} +return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")] +""" def csv_input_translate(str_decl, variables, base): decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',') param_types = [] @@ -239,6 +251,11 @@ modifiers = ["none"]*len(param_types) return zip(param_names,param_types,modifiers) +""" +Fillin the PLCOpenEditor standard function dictionnary +translate input and output declaration to something more pythonesque +and add interface description to comment +""" def decl_function(dico_from_table, variables): Function_decl = { "type" : "function" } for field, val in dico_from_table: @@ -252,7 +269,19 @@ Function_decl.pop("overloaded") return Function_decl - +""" +Returns this kind of declaration for all standard functions + + [{"name" : "Numerical", 'list': [ { + 'baseinputnumber': 1, + 'comment': 'Addition', + 'extensible': True, + 'inputs': [ ('IN1', 'ANY_NUM', 'none'), + ('IN2', 'ANY_NUM', 'none')], + 'name': 'ADD', + 'outputs': [('OUT', 'ANY_NUM', 'none')], + 'type': 'function'}, ...... ] },.....] +""" def get_standard_funtions(table): variables = get_standard_funtions_input_variables(table) @@ -296,17 +325,19 @@ Function_decl["outputs"] = [decl_tpl[:1] + (outype,) + decl_tpl[2:]] + Function_decl["outputs"][1:] funcdeclout = funcdeclin.replace("_**", '_' + outype) Function_decl["name"] = funcdeclout - Current_section["list"].append(Function_decl.copy()) + + # create the copy of decl dict to be appended to section + Function_decl_copy = Function_decl.copy() + # Have to generate type description in comment with freshly redefined types + Function_decl_copy["comment"] += ("\n (" + + str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["inputs"]]).strip("[]").replace("'",'') + + " ) => (" + + str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["outputs"]]).strip("[]").replace("'",'') + + " )") + Current_section["list"].append(Function_decl_copy) return Standard_Functions_Decl - -to_append=get_standard_funtions(csv_file_to_table(open(os.path.join(sys.path[0], "plcopen/iec_std.csv")))) -import pprint -pp = pprint.PrettyPrinter(indent=4) -pp.pprint(to_append) -BlockTypes.extend(to_append) -pp.pprint(BlockTypes) #------------------------------------------------------------------------------- # Test identifier #------------------------------------------------------------------------------- @@ -384,3 +415,12 @@ IEC_KEYWORDS.extend([keyword for keyword in IL_KEYWORDS if keyword not in IEC_KEYWORDS]) IEC_KEYWORDS.extend([keyword for keyword in ST_KEYWORDS if keyword not in IEC_KEYWORDS]) +if __name__ == '__main__': + import pprint + pp = pprint.PrettyPrinter(indent=4) + std_decl = get_standard_funtions(csv_file_to_table(open("iec_std.csv"))) + pp.pprint(std_decl) +else: + # Put standard functions declaration in Bloktypes + BlockTypes.extend(get_standard_funtions(csv_file_to_table(open(os.path.join(sys.path[0], "plcopen/iec_std.csv"))))) + \ No newline at end of file