Skip to content

Instantly share code, notes, and snippets.

@pepicrft
Created September 30, 2013 00:19
Show Gist options
  • Save pepicrft/6757792 to your computer and use it in GitHub Desktop.
Save pepicrft/6757792 to your computer and use it in GitHub Desktop.
This script analyzes all KVO observers in the project and link them with every POST in the project to those notifications. The script output is a report with all this information.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import re
#Notification Class
class Notification:
def __init__( self, observerFile, observer, notificationSelector, notificationName, notificationObject):
self.observerFile=observerFile
self.observer=observer
self.notificationName=notificationName
self.notificationSelector=notificationSelector
self.notificationObject=notificationObject
self.postFile= []
if __name__ =='__main__':
#Notifications list
notiList = []
#Moving up to root directory
os.chdir('../..')
# Looking for observers
for dirpath, dirnames, filenames in os.walk("."):
#Reading .m files
for filename in [f for f in filenames if f.endswith('.m')]:
#Getting the path
filepath = dirpath +'/'+ filename
#Opening the file
f=open(filepath,'r')
#File content
fileContent=f.read()
regexpression="\[\[NSNotificationCenter\s+defaultCenter\]\s+addObserver\:(.*)\s+selector\:@selector\((.*)\)\s+name\:(.*)\s+object\:(.+)]"
regex=re.compile(regexpression)
r = regex.search(fileContent)
if r:
notiList.append(Notification(filename,r.groups()[0],r.groups()[1],r.groups()[2],r.groups()[3]))
#Closing the file
f.close()
#Looking for posters
for noti in notiList:
for dirpath, dirnames, filenames in os.walk("."):
#Reading .m files
for filename in [f for f in filenames if f.endswith('.m')]:
#Getting the path
filepath = dirpath +'/'+ filename
#Opening the file
f=open(filepath,'r')
#File content
fileContent=f.read()
regexpression="\[\[NSNotificationCenter\s+defaultCenter\]\s+postNotificationName\:%s\s+object:.*\]" % (noti.notificationName, )
#print regexpression
regex=re.compile(regexpression)
r = regex.search(fileContent)
if r:
noti.postFile.append(filename)
#Closing the file
f.close()
#Generating report
#Moving to script directory
os.chdir('scripts/Clean')
#Generating file
f=open('Notification_report.txt','w')
for noti in notiList:
f.write("## Notification: %s ##\n"% (noti.notificationName, ))
f.write("Post file: %s \n"%noti.postFile)
f.write("Observer file: %s \n"%noti.observerFile)
f.write("Observer: %s \n"%noti.observer)
f.write("Notification selector: %s \n"%noti.notificationSelector)
f.write("Notification object: %s \n"%noti.notificationObject)
f.write("\n\n")
#Closing file
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment