My python script can read a Codesys project and save in file the different POU, but the problem is that I don't able to read the project info from the obj list.
Following of this message there is my python script.
Can you help my?
Best regards
Francesco
# encoding:utf-8# We enable the new python 3 print syntaxfrom__future__importprint_functionimportosimportshutilimporttimeimportsysfromdatetimeimportdatetimeprint("--- Saving files in the project: ---")print("sys.argv: ",len(sys.argv)," elements:")forarginsys.argv:print(" - ",arg)if(len(sys.argv)>1):folderExportName=sys.argv[1]print(" folderExportName: ",folderExportName)exportPath=sys.argv[2]+sys.argv[3]+"\\"+sys.argv[1]print(" File path: ",exportPath)# git has_repo=False#save_folder=r'E:\Tmp\ControlPlugins\ControlPlugins\Export'save_folder=exportPathifnotos.path.exists(save_folder):os.makedirs(save_folder)else:a=os.listdir(save_folder)forfina:ifnotf.startswith("."):sub_path=os.path.join(save_folder,f)ifos.path.isdir(sub_path):shutil.rmtree(sub_path)else:os.remove(sub_path)eliff==".git":has_repo=Trueinfo={}type_dist={'792f2eb6-721e-4e64-ba20-bc98351056db':'pm',#property method'2db5746d-d284-4425-9f7f-2663a34b0ebc':'dut',#dut'adb5cb65-8e1d-4a00-b70a-375ea27582f3':'lib',#lib manager'f89f7675-27f1-46b3-8abb-b7da8e774ffd':'m',#method no ret'8ac092e5-3128-4e26-9e7e-11016c6684f2':'act',#action '6f9dac99-8de1-4efc-8465-68ac443b7d08':'pou',#pou'6654496c-404d-479a-aad2-8551054e5f1e':'itf',#interface '738bea1e-99bb-4f04-90bb-a7a567e74e3a':'',#folder'ffbfa93a-b94d-45fc-a329-229860183b1d':'gvl',#global var'5a3b8626-d3e9-4f37-98b5-66420063d91e':'prop',#property'2bef0454-1bd3-412a-ac2c-af0f31dbc40f':'tl',#textlist'63784cbb-9ba0-45e6-9d69-babf3f040511':'gtl',#global textlist'225bfe47-7336-4dbc-9419-4105a7c831fa':'dev',#device'ae1de277-a207-4a28-9efb-456c06bd52f3':'tc',#task configuration'f8a58466-d7f6-439f-bbb8-d4600e41d099':'m',#method with ret'261bd6e6-249c-4232-bb6f-84c2fbeef430':'gvl',#gvl_Persistent'98a2708a-9b18-4f31-82ed-a1465b24fa2d':'task',#task'085afe48-c5d8-4ea5-ab0d-b35701fa6009':'progInfo'#project information};defsave(text,path,name,tp):ifnottp:tp=''else:tp='.'+tp+'.txt'withopen(os.path.join(path,name+tp),'w')asf:f.write(text.encode('utf-8'))defprint_tree(treeobj,depth,path):globalinfo#record current Pathcurpath=pathisfolder=Falset=''#texttp=''#type# get object namename=treeobj.get_name(False)id=treeobj.type.ToString()ifidintype_dist:tp=type_dist[treeobj.type.ToString()]#Print all type of objects#print("--Name: ", tp)else:info[id]=nameiftreeobj.is_device:deviceid=treeobj.get_device_identification()t='type='+str(deviceid.type)+'\nid='+str(deviceid.id)+'\nver='+str(deviceid.version)iftp=="progInfo":print("-- There is prog info, ",tp)print("-- It has textual declaration: , ",treeobj.has_textual_declaration)print("-- It has textual implementation: , ",treeobj.has_textual_implementation)print("-- It is folder: , ",treeobj.is_folder)print("-- It is children: , ",treeobj.get_children(False))print("-- It is children len: , ",len(treeobj.get_children(False)))print("-- It is progInfo type: , ",type(treeobj.ScriptProject))#for child in treeobj.get_children(False):# print_tree(child, depth+1,curpath)try:iftreeobj.is_folder:#system.ui.prompt('folder:'+u, PromptChoice.YesNo, PromptResult.Yes)isfolder=truepassexcept:passiftreeobj.has_textual_declaration:t=t+'(*#-#-#-#-#-#-#-#-#-#---Declaration---#-#-#-#-#-#-#-#-#-#-#-#-#*)\r\n'a=treeobj.textual_declarationt=t+a.textiftreeobj.has_textual_implementation:t=t+'(*#-#-#-#-#-#-#-#-#-#---Implementation---#-#-#-#-#-#-#-#-#-#-#-#-#*)\r\n'a=treeobj.textual_implementationt=t+a.textiftreeobj.has_textual_declarationandnottreeobj.has_textual_implementation:t=t+'(*#-#-#-#-#-#-#-#-#-#---NOT Implementation visible---#-#-#-#-#-#-#-#-#-#-#-#-#*)\r\n'iftreeobj.is_task:exports=[treeobj]projects.primary.export_native(exports,os.path.join(curpath,name+'.task'))iftreeobj.is_libman:exports=[treeobj]projects.primary.export_native(exports,os.path.join(curpath,name+'.lib'))iftreeobj.is_textlist:treeobj.export(os.path.join(curpath,name+'.tl'))children=treeobj.get_children(False)ifchildrenorisfolder:iftp:curpath=os.path.join(curpath,name+'.'+tp)else:curpath=os.path.join(curpath,name)ifnotos.path.exists(curpath):os.makedirs(curpath)ift:save(t,curpath,name,tp)forchildintreeobj.get_children(False):print_tree(child,depth+1,curpath)forobjinprojects.primary.get_children():print_tree(obj,0,save_folder)withopen(os.path.join(save_folder,'ExportInfo.txt'),'w')asf:now=datetime.now()infTimeExecution=now.strftime("%Y-%m-%d %H:%M:%S")f.write("Export date and time: "+infTimeExecution+"\r\n"+str(info))print("--- Script finished. ---")#system.ui.info('save ok')projects.primary.close()
Dear all,
My python script can read a Codesys project and save in file the different POU, but the problem is that I don't able to read the project info from the obj list.
Following of this message there is my python script.
Can you help my?
Best regards
Francesco
Related
Talk.ru: 1
Talk.ru: 2
Talk.ru: 3