Skip to content

Instantly share code, notes, and snippets.

/a_FileBrowse2.py Secret

Created Mar 4, 2013
Embed
What would you like to do?
a_FileBrowse2
from scene import *
import os, webbrowser, console
from time import gmtime
#This is needed to have dynamic input. Using console during draw()
#seems to crash Pythonista entirely. TODO: Find a solution
try: renameFileHack=console.input_alert("Rename Hack","Insert the renamed file or skip.\n"+
"To change, restart script.")
except: renameFileHack="genericName"
if not renameFileHack: renameFileHack="genericName"
#Hit test function used for clicking.
def hitBox(loc1,loc2,size2):
if loc1.x>loc2.x and loc1.x<loc2.x+size2.x:
if loc1.y>loc2.y and loc1.y<loc2.y+size2.y:
return True
#Returns the filetype name for the graph
def filetype(filename,foldername=""):
output=filename.split(".")[-1]
if os.path.isdir(filename) or os.path.isdir(foldername+"/"+filename):
output="Folder"
elif output=="py": output="Python"
elif output==filename: output="Unknown"
return output
#All the real work is done in this function
def makeScene(self, touch=False, isTouch=True):
#Delete files
if self.deleteWarning:
background(0,0,0)
fill(1,1,1,.8)
stroke(0,0,0,1)
inc=128
for i in range(2):
rect(inc,inc,self.size.w-(inc*2),self.size.h-(inc*2))
inc+=32
pointX,pointY=self.size.w/3,self.size.h/3
if isTouch:
fill(1,1,1,.5)
if hitBox(touch.location,Point(pointX-50,pointY-25),Point(100,50)):
fill(1,0,0,1)
self.deleteWarning=2
elif hitBox(touch.location,Point(pointX*2-50,pointY-25),Point(100,50)):
self.deleteWarning=0
rect((pointX)-50, (pointY)-25, 100, 50)
rect((pointX*2)-50, (pointY)-25, 100, 50)
text("Do you want to delete \""+self.deleteFile+"\"?",
'Futura', 20, self.size.w/2, pointY*2)
if self.deleteWarning==2:
text("Are you sure? This action cannot be undone.",
'Futura', 20, self.size.w/2, pointY*2-self.size.h/6)
rect((pointX*1.5)-50, (pointY)-25, 100, 50)
text("Confirm", 'Futura', 20, pointX*1.5, pointY)
if isTouch and hitBox(touch.location,
Point(pointX*1.5-50,pointY-25),
Point(100,50)):
print "Deleting: "+self.deleteFile
self.deleteWarning=0
os.remove(self.deleteFile) #This is the line that removes the file.
try: self.files=os.listdir(self.currentFolder)
except: self.files=os.listdir(os.getcwd())
text("Yes", 'Futura', 20, pointX, pointY)
text("No", 'Futura', 20, pointX*2, pointY)
self.redraw==True
return
#Rename files
if self.renameWarning:
background(0,0,0)
fill(1,1,1,.8)
stroke(0,0,0,1)
newType=self.renameFile.split(".")[-1]
if newType==self.renameFile: newType=""
else: newType="."+newType
newName=self.currentFolder+"/"+renameFileHack+newType
if newName[0]=="/": newName=newName[1:]
inc=128
for i in range(2):
rect(inc,inc,self.size.w-(inc*2),self.size.h-(inc*2))
inc+=32
pointX,pointY=self.size.w/3,self.size.h/3
text("Do you want to rename \""+self.renameFile+"\" to \""+newName+"\"?",
'Futura', 20, self.size.w/2, pointY*2)
rect((pointX*1.5)-50, (pointY)-25, 100, 50)
text("Confirm", 'Futura', 20, pointX*1.5, pointY)
if isTouch:
if hitBox(touch.location,
Point(pointX*1.5-50,pointY-25),
Point(100,50)):
print "Renaming: "+self.renameFile+" to "+newName
os.rename(self.renameFile,newName)
try: self.files=os.listdir(self.currentFolder)
except: self.files=os.listdir(os.getcwd())
self.renameWarning=0
else: self.renameWarning=0
self.redraw==True
return
#Draw background
background(1, 1, 1)
#Move files
if self.movingWarning:
text(self.movingFile, 'Futura', 20, 778, 90, 3)
text("Click box to drop", 'Futura', 20, 778, 70, 3)
fill(0,1,0)
rect(768,0,300,40)
if isTouch and hitBox(touch.location, Point(778,0), Point(300,40)):
filename=self.currentFolder+"/"+self.movingFile.split('/')[-1]
if filename[0]=="/": filename=filename[1:]
print "Moved \""+self.movingFile+"\" to \""+filename+"\""
try: os.rename(self.movingFile,filename)
except: pass #Someone tried to move a folder into itself o.O
self.movingWarning=0
try: self.files=os.listdir(self.currentFolder)
except: self.files=os.listdir(os.getcwd())
self.redraw==True
return
# Hit test for scrollbar
if isTouch and hitBox(touch.location, Point(700,0), Point(68,768)):
self.currentY=touch.location.y
#Begin drawing table
stroke(0, 0, 0, 1)
stroke_weight(1)
#This is the bottom of the table. It's often way below the scene
#TODO: This y calculation is confusing. Fix it.
y=768+(0-(len(self.files)+20)*32)-(self.currentY-768)
#Columns.
line(300,y,300,704-(self.currentY-768))
line(400,y,400,704-(self.currentY-768))
line(500,y,500,704-(self.currentY-768))
line(600,y,600,704-(self.currentY-768))
for i in range(len(self.files)+20):
#Don't make lines if they aren't visible
if y>0 and y<self.size.h:
line(0,y,768,y)
y+=32 #Lines are drawn from the bottom up.
y-=32 #Spacer from the top.
#Text
tint(0, 0, 0)
text('Current Folder: '+"\""+self.currentFolder+"\"",
'Futura', 20, 10, y, 3)
y-=32
text('Name', 'Futura', 20, 10, y, 3)
text('Type', 'Futura', 20, 310, y, 3)
text('Date', 'Futura', 20, 410, y, 3)
text('Size', 'Futura', 20, 510, y, 3)
#Sorting options.
if isTouch:
if hitBox(touch.location,Point(0,y-32),Point(300,32)): self.sort=0
elif hitBox(touch.location,Point(300,y-32),Point(100,32)): self.sort=1
elif hitBox(touch.location,Point(500,y-32),Point(100,32)): self.sort=2
elif hitBox(touch.location,Point(400,y-32),Point(100,32)): self.sort=3
#text(str(self.sort), 'Futura', 20, 620, y, 3)
#Set up the array of information for the table
fileArray=[]
for filename in self.files:
file=self.currentFolder+"/"+filename
if file[0]=="/": file=file[1:]
fileTime=os.stat(file).st_mtime
fileType=filetype(filename,self.currentFolder)
try: fileSize=str(round(os.path.getsize(file)/1024.,1))+"kb"
except:
fileSize=str(round(os.path.getsize(self.currentFolder+"/"+filename)/1024.,1))+"kb"
if os.path.isdir(file) and not self.currentFolder==filename: isPath=True
else: isPath=False
fileArray.append([filename,fileType,fileSize,fileTime,isPath])
if self.sort==0: #sort by name
fileArray=sorted(fileArray, key=lambda s: s[0].lower())
elif self.sort==1: #sort by type
fileArray=sorted(fileArray, key=lambda fileType: fileType[1])
elif self.sort==2: #sort by size
fileArray=sorted(fileArray, key=lambda fileSize: float(fileSize[2][:-2]))
elif self.sort==3: #sort by time
fileArray=sorted(fileArray, key=lambda fileTime: float(fileTime[3]), reverse=True)
else: pass
#Now that array is set up and sorted, draw it.
for i in range(len(fileArray)):
y-=32
try: hit=hitBox(touch.location,Point(0,y-32),Point(200,32))
except: hit=False
currentDir=self.currentFolder+"/"+fileArray[i][0]
if currentDir[0]=="/": currentDir=currentDir[1:] #Set up current filename
if isTouch and hit: #This means we hit one of the folders.
fill(1,0,0) #Box color = red
if os.path.isdir(currentDir) and not self.currentFolder==currentDir:
self.currentY=768
self.files=os.listdir(currentDir)
self.currentFolder=currentDir
self.redraw=True
return
elif hit: fill(1,0,0) #Hit but not isTouch
else: fill(0,0,0) #Black box
rect(10,y-26,20,20) #Draw rectangles and text..
rect(600,y-32,100,32) #Hitting these raises alert.
if isTouch and hitBox(touch.location,Point(600,y-32),Point(100,32)):
if self.opMode==0:
self.deleteWarning=1
self.deleteFile=currentDir
elif self.opMode==1:
self.renameWarning=1
self.renameFile=currentDir
else:
self.movingWarning=1
self.movingFile=currentDir
self.redraw=True
return
s=gmtime(fileArray[i][3])
s=str(s.tm_mon)+'/'+str(s.tm_mday)+'/'+str(s.tm_year)[2:]
text(str(fileArray[i][0]), 'Futura', 20, 30, y, 3)
text(str(fileArray[i][1]), 'Futura', 20, 310, y, 3)
text(str(fileArray[i][2]), 'Futura', 20, 510, y, 3)
text(s, 'Futura', 20, 405, y, 3)
if fileArray[i][4]==True: #If the file is a directory, draw children
for filesInFolder in os.listdir(currentDir):
y-=32
rect(50,y-26,20,20)
text(str(filesInFolder), 'Futura', 20, 70, y, 3)
#Go up one directory
fill(.5,.5,.3)
rect(778,700,50,50)
if isTouch and hitBox(touch.location,Point(778,700),Point(50,50)) and self.currentFolder:
self.currentFolder='/'.join(self.currentFolder.split('/')[:-1])
self.currentY=768
try: self.files=os.listdir(self.currentFolder)
except: self.files=os.listdir(os.getcwd())
self.redraw=True
return
#Draw scrollbar
fill(.9,.9,.9)
rect(700,0,68,1028)
fill(1,0,1)
scrollSize=50#768/len(fileArray)
y=self.currentY-scrollSize/2
rect(700,y,68,scrollSize)
#Draw opMode buttons
#rect()
for i in range(3):
fill(0,0,0,1)
if isTouch:
if hitBox(touch.location,Point(778+i*75,600),Point(70,70)):
self.opMode=i
self.redraw=True
return
if self.opMode==i: fill(1,0,0,1)
rect(778+i*75,600,70,70)
if i==0: txt="delete"
elif i==1: txt="rename"
else: txt="move"
text(txt,'Futura',15,(778+(i*75)+35),590)
class MyScene (Scene):
def setup(self):
self.opMode=1 #Mode for operations. 0=del, 1=rename, 2=move
self.deleteWarning=0 #Activates when deleting a file.
self.renameWarning=0 #Activates when renaming a file.
self.movingWarning=0 #Activates when moving a file.
self.deleteFile="ERROR"
self.renameFile="ERROR"
self.movingFile="ERROR"
self.sort=0 #Mode for sorting.
self.currentY=768 #This is associated with the scrollbar.
self.currentFolder=""
self.files=os.listdir(os.getcwd())
self.redraw=False
makeScene(self,isTouch=False)
def draw(self):
pass
def touch_began(self, touch):
pass
def touch_moved(self, touch):
pass
def touch_ended(self, touch):
makeScene(self,touch,True)
if self.redraw==True: #Many updates need to be redrawn.
makeScene(self,touch,False)
run(MyScene(), LANDSCAPE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.