Start a new topic

rudimentary file parser with mgWalk?

Greetings,


I believe I can edit files to keep or remove nodes based on the type of records encountered during mgWalk.


Is there an API attribute or whatnot that tracks the hierarchy as mgWalk progresses, so that if there is structure on top of the node being removed, and the node is the only child, then the entire branch can be removed instead of just the node at the end of that branch?


Or is that something I have to do myself, and if so, how would I go about doing that?

In a python list I keep track of elements by their position; how would I do this for a flt file hierarchy??


Thx


Shawn


so the crash happens here, after barrier_19:


barrier_18

o3_28

o35810_20

o35812_20

o35813_20

barr_reflector_20

barrier_19


Here's the version without delete in the same area, only a print statement. I don't know why there's a problem, esp. if the simpler file works.


barr_reflector_18

barrier_17

o3_27

o35810_19

o35812_19

o35813_19

barr_reflector_19

barrier_18

o3_28

o35810_20

o35812_20

o35813_20

barr_reflector_20

barrier_19

o3_30

o35810_22

o35812_22

o35813_22

barr_reflector_22

o57976_4

barrier_22

o3_32

o35810_24

o35812_24

o35813_23

barr_reflector_24


 

 

 

 

 

 

 

Hm.  This seems to work, although it's not quite as efficient since it's removing nodes whose parents are also removed.  Have only run one test.  Will test more ;)


I really appreciate your help Chris!


 

from mgapilib import *
import sys

mgInit (None, None)

print("\nOpening database: ",sys.argv[1],"\n")
db = mgOpenDb (sys.argv[1])
if db == None:
   print(mgGetLastError (), "\n")
   mgExit ()

# db = mgGetCurrentDb()

def gatherSwitchNodes (db, parent, rec, switchList):
   if mgGetCode (rec) == fltSwitch:
      switchList.append(rec)
   return MG_TRUE

switchList = []
mgWalk(db, gatherSwitchNodes, None, switchList, MWALK_NOREADONLY)

def isNodeParent (parentNode, testChildren):
   for possibleChild in testChildren:
      testNode = possibleChild
      while testNode and testNode != db:
         if testNode == parentNode:
            return MG_TRUE   
         testNode = mgGetParent(testNode)
   return MG_FALSE
   
switchDepth = 0
def checkSwitch (db, parent, rec, data):
   global switchDepth
   if mgGetCode (rec) == fltSwitch:
      switchDepth = switchDepth + 1
   return MG_TRUE
   

def deleteNonSwitchNodes (db, parent, rec, deleteList):
   global switchList
   global switchDepth
   if mgGetCode (rec) == fltSwitch:
      switchDepth = switchDepth -1
      
   # Switch depth tells us if we are under a switch
   # Still need to see if we have a switch as a child
   elif mgGetCode(rec) == fltGroup or mgGetCode(rec) == fltObject:
      # print ('group or object:',mgGetName(rec),'depth:',switchDepth)

      if switchDepth < 1 and not isNodeParent(rec, switchList) and not rec == db:
         deleteList.append(mgGetName(rec))

   return MG_TRUE
   
deleteList = []

mgWalk (db, checkSwitch, deleteNonSwitchNodes, deleteList, MWALK_NOREADONLY)

# print (len (deleteList))

for d in deleteList:
   record = mgGetRecByName(db, d)
   ok = mgDelete (record)
   if not ok:
      print(mgGetLastError (), "\n")

mgWriteDb (db)

ok = mgCloseDb (db)
if ok == MG_FALSE:
   print("Error closing database\n")


mgExit()

 

Login to post a comment