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

import os
import shutil


class FileHandle:
	'''
	import filehandle
	files_list = filehandle.FileHandle().listAllFiles(path)
	filehandle.FileHandle().replaceFileContent(path,primary,replace))

	'''
	def __init__(self,*args):
		if len(args)==1:
			if args[0].strip()=="safeoff":self.mode="safeoff"
			else:self.mode="safeon"
		else:
			self.mode="safeon"
		self.allfileslist = []

	def copyfile(self,pri,tar):	
		shutil.copyfile(pri,tar)

	def movefile(self,pri,tar):
		shutil.move(pri,tar)

	def deletefile(self,path):
		os.remove(path)

	def getExtension(self,path):
		L = os.path.split(path)[1].split(".")
		return L[len(L)-1]

	def getFolder(self,path):
		return os.path.split(path)[0]

	def getFilename(self,path):
		return os.path.split(path)[1]

	def listAllFiles(self,path):
		'''
		List = os.listdir(path)
		for f in List:
			tp = path + f
			if os.path.isfile(tp):self.allfileslist.append(tp)
			elif os.path.isdir(tp):self.listAllFiles(tp+"/")# '/' very important!!!

		return self.allfileslist
		
		'''
		List = []
		[List.extend(map(lambda f:os.path.join(r,f), fs) for r,ds,fs in os.walk(path))]

		return List



	def headAdd(self,path,content,checkrepeat="off"):
		f = open(path)
		primaryContent = f.read()
		f.close()
		if checkrepeat == 'on' and primaryContent.strip().find(content.strip()) == 0:
			print "Repeat! passed! File: ",path
		else:
			backupPath = path + ".backup." + self.getExtension(path)
			if self.mode == "safeon":
				self.copyfile(path,backupPath)#copy file
			try:
				f = open(path,"w")
				f.write(content + os.linesep*2 +primaryContent)
				f.close()
			except:
				try:self.copyfile(backupPath,path)
				except:pass#no backup if users safeoff
				print "Failed:",path



	def replaceFileContent(self,path,primary,replace):
		f = open(path)
		primaryContent = f.read()
		f.close()

		Content = primaryContent.replace(primary,replace)

		backupPath = path + ".backup." + self.getExtension(path)
		if self.mode == "safeon":
			self.copyfile(path,backupPath)#copy file

		try:
			f = open(path,"w")
			f.write(Content)
			f.close()
		except:
			try:self.copyfile(backupPath,path)#copy file back
			except:pass#no backup if users safeoff
			print "Failed:",path

		print self.runningTips


if __name__ == "__main__":
	#print Folder().listAllFiles("/home/tu/try/")
	FileHandle().headAdd("/home/tu/views.py","import settings"+os.linesep+"rootpath = settings.RootPath")
	FileHandle().replaceFileContent("/home/tu/views.py",'"/home/tu/','rootpath + "')
	FileHandle().replaceFileContent("/home/tu/views.py","'/home/tu/","rootpath + '")


#end
@@ -51,17 +51,16 @@
 shutil%0A%0A
-%0A
 class Fi
@@ -383,33 +383,22 @@
 on%22%0A
-%09%09self.allfileslist = %5B%5D%0A
+%0A%09@classmethod
 %0A%09de
@@ -448,24 +448,38 @@
 e(pri,tar)%0A%0A
+%09@classmethod%0A
 %09def movefil
@@ -519,16 +519,30 @@
 i,tar)%0A%0A
+%09@classmethod%0A
 %09def del
@@ -580,16 +580,30 @@
 (path)%0A%0A
+%09@classmethod%0A
 %09def get
@@ -682,24 +682,38 @@
 %5Blen(L)-1%5D%0A%0A
+%09@classmethod%0A
 %09def getFold
@@ -760,16 +760,30 @@
 th)%5B0%5D%0A%0A
+%09@classmethod%0A
 %09def get
@@ -836,16 +836,30 @@
 th)%5B1%5D%0A%0A
+%09@classmethod%0A
 %09def lis
@@ -881,306 +881,168 @@
 path
-):%0A%09%09'''%0A%09%09List = os.listdir(path)%0A%09%09for f in List:%0A%09%09%09tp = path + f%0A%09%09%09if os.path.isfile(tp):self.allfileslist.append(tp)%0A%09%09%09elif os.path.isdir(tp):self.listAllFiles(tp+%22/%22)# '/' very important!!!%0A%0A%09%09return self.allfileslist%0A%09%09%0A%09%09'''%0A%09%09List = %5B%5D%0A%09%09%5BList.extend(map(lambda f:os.path.join(r,f
+,extension=''):%0A%09%09List = %5B%5D%0A%09%09# map(getFullPath, extensionFiles)%0A%09%09%5Bmap(lambda f:List.append(os.path.join(r,f)), filter(lambda f:f.endswith(extension
 ),
- 
 fs)
- 
+)%5C%0A%09%09%09
 for 
@@ -1065,17 +1065,16 @@
 lk(path)
-)
 %5D%0A%0A%09%09ret
@@ -1083,17 +1083,29 @@
 n List%0A%0A
-%0A
+%09@classmethod
 %0A%09def he
@@ -1682,17 +1682,29 @@
 %22,path%0A%0A
-%0A
+%09@classmethod
 %0A%09def re

add list all files with extension