francesco86 - 2024-04-30

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

# encoding:utf-8
# We enable the new python 3 print syntax
from __future__ import print_function
import os
import shutil
import time
import sys
from datetime import datetime

print("--- Saving files in the project: ---")

print("sys.argv: ", len(sys.argv), " elements:")
for arg in sys.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 = exportPath

if not os.path.exists(save_folder):
    os.makedirs(save_folder) 
else:
    a=os.listdir(save_folder)
    for f in a:
        if not f.startswith("."):
            sub_path= os.path.join(save_folder,f)
            if os.path.isdir(sub_path):
                shutil.rmtree(sub_path)
            else:
                os.remove(sub_path)
        elif f==".git":
            has_repo=True

info={}

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
};

def save(text,path,name,tp):
    if not tp:
        tp=''
    else:
        tp='.'+tp+'.txt'
    with open(os.path.join(path,name+tp),'w') as f:
        f.write(text.encode('utf-8'))

def print_tree(treeobj, depth, path):
    global info
    #record current Path
    curpath=path 
    isfolder=False

    t='' #text
    tp='' #type

    # get object name
    name = treeobj.get_name(False)
    id = treeobj.type.ToString()

    if id in type_dist:
        tp = type_dist[treeobj.type.ToString()]
        #Print all type of objects
        #print("--Name: ", tp)
    else:
        info[id]=name

    if treeobj.is_device:
        deviceid = treeobj.get_device_identification()
        t = 'type='+str(deviceid.type) +'\nid=' +str(deviceid.id) + '\nver='+ str(deviceid.version)

    if tp == "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:
        if treeobj.is_folder :
            #system.ui.prompt('folder:'+u, PromptChoice.YesNo, PromptResult.Yes)
            isfolder=true
            pass
    except:
        pass

    if treeobj.has_textual_declaration :
        t=t+'(*#-#-#-#-#-#-#-#-#-#---Declaration---#-#-#-#-#-#-#-#-#-#-#-#-#*)\r\n'
        a=treeobj.textual_declaration
        t=t+a.text

    if treeobj.has_textual_implementation:
        t=t+'(*#-#-#-#-#-#-#-#-#-#---Implementation---#-#-#-#-#-#-#-#-#-#-#-#-#*)\r\n'
        a=treeobj.textual_implementation
        t=t+a.text

    if treeobj.has_textual_declaration and not treeobj.has_textual_implementation:
        t=t+'(*#-#-#-#-#-#-#-#-#-#---NOT Implementation visible---#-#-#-#-#-#-#-#-#-#-#-#-#*)\r\n'

    if treeobj.is_task :
        exports=[treeobj]
        projects.primary.export_native(exports,os.path.join(curpath,name+'.task'))

    if treeobj.is_libman:
        exports=[treeobj]
        projects.primary.export_native(exports,os.path.join(curpath,name+'.lib'))

    if treeobj.is_textlist:
        treeobj.export(os.path.join(curpath,name+'.tl'))

    children = treeobj.get_children(False)

    if children or isfolder:
        if tp:
            curpath=os.path.join(curpath,name+'.'+tp)
        else:
            curpath=os.path.join(curpath,name)

        if not os.path.exists(curpath):
            os.makedirs(curpath)

    if t:
        save(t,curpath,name,tp)

    for child in treeobj.get_children(False):
        print_tree(child, depth+1,curpath)


for obj in projects.primary.get_children():
    print_tree(obj,0,save_folder)

with open(os.path.join(save_folder,'ExportInfo.txt'),'w') as f:
    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()
 

Related

Talk.ru: 1
Talk.ru: 2
Talk.ru: 3