2014-05-10 08:26:51 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
#ConvertYCoordFlipped.py
|
|
|
|
|
|
|
|
import plistlib
|
|
|
|
import os.path
|
|
|
|
import argparse
|
|
|
|
import glob
|
2014-05-10 11:07:26 +08:00
|
|
|
import shutil
|
2014-05-10 08:26:51 +08:00
|
|
|
|
|
|
|
#keys in dictionary
|
|
|
|
metaDataKey = 'metaData'
|
|
|
|
yCoordFlippedConvertedKey = 'yCoordFlippedConverted'
|
|
|
|
yCoordFlippedKey = 'yCoordFlipped'
|
|
|
|
|
|
|
|
#check if the particle file has been converted
|
|
|
|
def checkFlippedConvertFlag(plistDict):
|
|
|
|
if(not plistDict.has_key(metaDataKey)):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
metaDict = plistDict.get(metaDataKey)
|
|
|
|
if(not metaDict.has_key(yCoordFlippedConvertedKey)):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return metaDict.get(yCoordFlippedConvertedKey) is 1
|
|
|
|
|
|
|
|
#write flag to indicate to file has been converted
|
|
|
|
def writeFlippedConvertFlag(plistDict):
|
|
|
|
metaDict = dict()
|
|
|
|
metaDict.update(yCoordFlippedConverted = 1)
|
|
|
|
plistDict.update(metaData = metaDict)
|
|
|
|
|
|
|
|
#process file
|
|
|
|
def processConvertFile(filename):
|
2015-04-25 05:02:39 +08:00
|
|
|
#print a line to separate files
|
2014-05-10 08:26:51 +08:00
|
|
|
print ('')
|
|
|
|
if(not os.path.isfile(filename)):
|
|
|
|
print(filename + ' dose not exist!')
|
|
|
|
return
|
|
|
|
print('Begin process particle file: ' + filename)
|
|
|
|
fp = open(filename, 'r')
|
|
|
|
pl = plistlib.readPlist(fp)
|
|
|
|
|
|
|
|
if (not pl.has_key(yCoordFlippedKey)):
|
|
|
|
print('Skip plist file: ' + filename + ' for there is no key for yCoordFlipped,')
|
|
|
|
else:
|
|
|
|
if(not checkFlippedConvertFlag(pl)):
|
2014-05-10 11:07:26 +08:00
|
|
|
backupFileName = filename+'.backup'
|
|
|
|
print('Write backup file to ' + backupFileName)
|
|
|
|
shutil.copyfile(filename,backupFileName)
|
2014-05-10 08:26:51 +08:00
|
|
|
print('converting...')
|
|
|
|
pl[yCoordFlippedKey] = -pl[yCoordFlippedKey]
|
|
|
|
writeFlippedConvertFlag(pl)
|
|
|
|
print('converted...')
|
|
|
|
print('Write new plist file to ' + filename)
|
|
|
|
plistlib.writePlist(pl,filename)
|
|
|
|
else:
|
|
|
|
print('Skip a converted file ' + filename)
|
|
|
|
|
|
|
|
# -------------- entrance --------------
|
|
|
|
if __name__ == '__main__':
|
|
|
|
argparser = argparse.ArgumentParser()
|
|
|
|
argparser.add_argument("file", nargs = "+",help = "specify a file or a patten")
|
|
|
|
#argparser.add_argument("-r", "--recursive",action = "store_true", help = "recursive folder or not")
|
|
|
|
args = argparser.parse_args()
|
|
|
|
|
|
|
|
for file in args.file:
|
|
|
|
processConvertFile(file)
|