Zim Zim Python Python Fix Fix

I recently upgraded to Ubuntu/Kubuntu 10.04 and it comes with some changes. Zim wiki is now completely python and much faster and clearly cleaner and more attractive. Thunderbird 3 is also very nice. Upgrade went easily, but some questions were difficult, like: "The quantum entanglement of multiple simultaneous realities has caused a Gamma Ray Burst, continue, Yes or No".

It seems that the equation references in Zim are slightly different and so I devised this little tidbit to bring harmony back to the universe of Zim, Zimpy, and perhaps python itself. The point of this is to take {{./Dir/equation_xx}} and convert it to {{./equation_xx}}. I could have used grep, sed, awk or C, but python is a clean language and I think it is more understandable when viewed by others.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os.path
import sys

# If there is more than one reference on a line in the Zim it will require that the script be run once for each level. 
#I could just iterate a few times, but it is easy enough to hit up arrow in terminal and run again
#I am also limiting the location to within media just to make sure it doesn't go awry in my root directory
#This is GPL code if you have any question what that means : 
#         Return to your home planet and never visit Earth again!!
#Use at your own risk, code provided as example only
fileScan=0

def doall(aDir,anExt):
 name=os.listdir(aDir)
 allScan=0
 global fileScan
 for named in name:
  if named.find(anExt) != -1:
   (jbase,jext)= os.path.splitext(named)
   allScan=allScan+fixequations(jbase,aDir) #t or not determines if it really does something
   fileScan=fileScan+1
 return allScan
 
#Just list the files that would be changed
def fixequationst(thebase,adir):
 print "Fixing ",adir," file ",thebase+".txt"
 return 1
 
def fixequations(thebase,adir):
 allScan=0
 thename1=thebase+".txt"
 thename4=thebase+"UNIQUEOLD.txt"
 thename2=thebase+"UNIQUENEW.txt"
 handle=open(adir+"/"+thename1,"r")
 handlew=open(adir+"/"+thename2,"w")
 aline="something"
 while aline:
  aline=handle.readline()
  if aline.find("media") != -1 and aline.find("{{") != -1 and aline.find("}}") != -1 and aline.find("equation") != -1:
   token4s=aline[aline.find('{{/media')+2:aline.find('equation}}')+8]
   if token4s != -1  and  aline.find('{{/media')+2 < aline.find('equation}}')+8 : 
    (jbase,jext)= os.path.splitext(token4s)
    jext2=jext.split("?")
    newr= aline.replace(token4s,"./"+os.path.basename(jbase)+jext2[0][0:]+"?type=equation")
#    print newr,token4s,thebase,adir #This would allow progress monitoring of changes
    handlew.write(newr)
    allScan=allScan+1
   else:
    handlew.write(aline)
  else:
   handlew.write(aline)
 handle.close()
 handlew.close()
 os.rename(adir+"/"+thename1,adir+"/"+thename4)
 os.rename(adir+"/"+thename2,adir+"/"+thename1)
 os.remove(adir+"/"+thename4)
 return allScan
 
def scanDirs(ipath,theExt):
 filesChecked=0
 thisext=theExt
 thisdir=ipath
 filesChecked=filesChecked+doall(thisdir,thisext)
 return filesChecked
 
#------- This is where it does something ------
pathsScanned=0
allScan=0
filesChecked=0
if len(sys.argv) > 2:
 InputDir=sys.argv[1]
 x=os.path.exists(InputDir+"/Home"+sys.argv[2])
 y=os.path.isfile(InputDir+"/Home"+sys.argv[2])
 if x==True and y==True:
  print "Scanning"
  for root, dirs, files in os.walk(InputDir):
   for onedir in dirs:
    path = os.path.join(InputDir,root, onedir)
    filesChecked=filesChecked+scanDirs(path,sys.argv[2])
    pathsScanned=pathsScanned+1
 else:
  if x==False:
   print InputDir+" does not exist and returns "+str(x)+" when checked."
  if y==False:
   print InputDir+"/Home.txt is not a file and thus "+InputDir+" is not a Zim Wiki location. And when checked returns "+str(y)
else:
 print "Input arguments::"
 print sys.argv
 print "This requires base directory containing Home.txt and a limit on file types like: .txt"
 print "It must be invoked with python also as it requires 3 arguments."
 print "Example would be:"
 print "\"python FixZim.py /someplace .txt \""

print "Number of paths scanned is "+str(pathsScanned)+" and references changed is "+str(filesChecked)+" in "+str(fileScan)+" files."

0 comments:

Automated Intelligence

Automated Intelligence
Auftrag der unendlichen LOL katzen