Code:
### Pylattyplus.py -- An On-The-Go Basic Editor For The Pys60
# modules
from graphics import *
from sensor import *
import os,appuifw,e32,sensor,sys,graphics,codecs,sysinfo,time,re,dir_iter,key_codes,math,random
trk_on=1
img=None
main_loop=1
settxt=0
# [[][][]] are Nested Lists
# (()()()) are Nested Tuples
def handle_redraw(rect):
if img: ### Skips only at first pass as 'None'
canvas.blit(img)
def gfx_on(mode): # actually took me a long while to figure out how to come up with this
global canvas,txt
if mode == 1:
appuifw.app.body=canvas
else:
txt=appuifw.Text()
appuifw.app.body=txt
appuifw.app.directional_pad=False
txt=appuifw.Text()
appuifw.app.body=txt
appuifw.app.title=u"Pylattyplus Pys60 Editor"
canvas=appuifw.Canvas(redraw_callback=handle_redraw)
appuifw.app.body=canvas
img=Image.new(canvas.size)
timer=e32.Ao_timer()
gfx_on(0)
intro=u"\nThank You for taking an interest in one of my first attempts at creating a very basic pys60 Editor that is a work in progress.\n\n\
A Hobby Project by\n\
Kirk Lawrence\n\n"
txt.clear()
txt.set(unicode(intro))
process=u'\n\n\nThis might take quite a while to process...'
COLOR_BLACK=(0,0,0)
COLOR_GRAY=(128,128,128)
COLOR_WHITE=(255,255,255)
COLOR_GREEN=(0,204,0)
COLOR_YELLOW=(255,255,0)
COLOR_RED=(255,0,0)
COLOR_BLOODRED=(102,0,0)
class Movement(object):
x=0
y=0
slide=0.97654321
vx=Movement()
vy=Movement()
mdet=0
# screen boundary
vx,vy=0,0
xm,ym=0,60
view_xx,view_yy=1208,4105
accel=0,0
exitmark=2
def exit():
global exitmark
exitmark=1
appuifw.app.exit_key_handler=exit
def z_quit():
global main_loop
quit_x=appuifw.query(u'To Confirm Exiting... Select The OK','query')
if quit_x == True:
main_loop=0
def z_info():
appuifw.note(u'ZZZ','info')
def z_msg(msg):
appuifw.note(unicode(msg),'info')
def z_xy(pos):
global xm,ym,vx,vy,accel
aa=abs(pos[0])-xm
bb=abs(pos[1])-ym
vx,vy=(vx+aa,vy+bb)
if vx < -border[0]:
vx=-border[0]
elif vx > 0:
vx=0
if vy < -border[1]:
vy=-border[1]
elif vy > 0:
vy=0
if not ym-vy < 40 or not ym-vy > 40*(maxline+1):
accel=0,bb
if abs(accel[1]) < 5:
accel=0,0
xm,ym=pos[0],pos[1]
def z_dot(pos):
global xm,ym,accel,mdet
mdet=1
xm,ym=pos[0],pos[1]
accel=0,0
def z_notouch(pos):
global mdet
mdet=0
canvas.bind(key_codes.EButton1Up,z_notouch)
canvas.bind(key_codes.EButton1Down,z_dot)
canvas.bind(key_codes.EDrag,z_xy)
def filewr(selected):
gfx_on(0)
txt.clear()
txt.set(unicode(process))
appuifw.app.title=u'Sel:' + selected;
f=open('e:\\data\\python\\' + selected,'r+')
f2=f.read()
f.close()
txt.clear()
f2=unicode(f2)
txt.set(unicode(f2))
TEMP_FILE='e:\\data\\python\\temp.py'
f=open(unicode(TEMP_FILE),'w')
f.write('e:\\data\\python\\' + selected)
f.close()
tr(1)
def flist():
global settxt
filenames=[]
for filename in os.listdir('e:\data\python'):
if filename.endswith('.py'):
filename=filename.decode('utf-8')
filenames.append(filename)
for filename in os.listdir('c:\data\python'):
if filename.endswith('.py'):
filename=filename.decode('utf-8')
filenames.append(filename)
filenames.reverse()
index=appuifw.selection_list(filenames,1)
tr()
m=settxt
try:
if appuifw.query(u'Open ' + filenames[index] + '?','query') == True:
settxt=1
selected=filenames[index]
go(selected)
except:
settxt=m
def opentxt():
global settxt
filenames=[]
for filename in os.listdir('e:\data\python'):
if filename.endswith('.txt'):
filename=filename.decode('utf-8')
filenames.append(filename)
filenames.reverse()
index=appuifw.selection_list(filenames,1)
tr()
m=settxt
try:
if appuifw.query(u'Open ' + filenames[index] + '?','query') == True:
settxt=1
selected=filenames[index]
go(selected)
except:
settxt=m
def sel2canv():
tr("sel2canv")
selected=linesoftxt[inline]
gfx_on(0)
exitmark=0
txt.clear()
txt.set(unicode(selected))
def canv2sel():
global settxt
txt=appuifw.app.body.get()
txt=txt.replace(u'\u2029','\n').encode('utf8') # fix newline characters
txt=txt.replace("\t"," ") ###ZZZ substitute whitespace
linesoftxt.pop(inline)
linesoftxt.insert(inline,txt)
gfx_on(1)
z_mainmenu()
def z_save2db(TEMP_FILE):
linesoftxt2=linesoftxt[1:maxline]
try:
f=open(unicode(TEMP_FILE),'w')
for txt in linesoftxt2:
#txt=txt.replace(" ","\t") ###ZZZ substitute whitespace
txt=txt+"\n"
f.write(txt)
f.close()
appuifw.note(u'Saved','conf')
except:
appuifw.note(u'Unable To Save!','error')
def z_save_01():
TEMP_FILE=u'c:\\data\\python\\000_000_000_temp.py'
z_save2db(TEMP_FILE)
def z_save_02():
TEMP_FILE=u'c:\\data\\python\\100_000_000_temp.py'
z_save2db(TEMP_FILE)
def z_saveas():
newname=appuifw.query(u'Enter The New Filename','text')
TEMP_FILE=unicode('e:\\data\\python\\'+newname+'.py')
z_save2db(TEMP_FILE)
def z_fileopen01():
CONFIG_DIR='c:\\data\python'
CONFIG_FILE=os.path.join(CONFIG_DIR,'000_000_000_temp.py')
appuifw.app.title=u'Sel: Temp 01';
z_db2canvas(CONFIG_FILE)
def z_fileopen02():
CONFIG_DIR='c:\\data\python'
CONFIG_FILE=os.path.join(CONFIG_DIR,'100_000_000_temp.py')
appuifw.app.title=u'Sel: Temp 02';
z_db2canvas(CONFIG_FILE)
def z_template():
gfx_on(0)
txt.set(unicode(process))
CONFIG_FILE=os.path.join(CONFIG_DIR,'pyup_lib.py')
f=open(unicode(CONFIG_FILE),'r')
z_libr=unicode(f.read())
f.close
appuifw.app.title=u'New: Template';
txt.clear()
txt.set(unicode(z_libr))
def z_db2canvas(CONFIG_FILE):
gfx_on(0)
txt.clear()
txt.set(unicode(process))
f=open(unicode(CONFIG_FILE),'r')
f2=unicode(f.read())
f.close
def canv2list(): ###ZZZ will reference body.get elsewhere
global linesoftxt
linesoftxt=[]
txt=appuifw.app.body.get()
txt=txt.replace(u'\u2029', '\n')
txt=txt.decode('utf8')
linesoftxt.append(u'**** HEAD OF ENTRY ****')
for z_line in txt.split('\n'):
linesoftxt.append(z_line)
linesoftxt.append(u'**** LAST OF ENTRY ****')
gfx_on(1)
img.clear(COLOR_BLOODRED)
canvas.blit(img)
tr(1)
def z_find(reverse=False,beginning=False):
'''find text within a document'''
string=appuifw.query(u'Find text:','text',unicode(last_find))
saved_string=string[:]
if (string != None):
try:
text=text.get()
if settings.config['casesensitive'][0] == 'no':
text=text.lower()
string=string.lower()
if beginning:
if reverse:
cur_pos=text.rindex(string)
else:
cur_pos=text.index(string)
else:
if reverse:
cur_pos=text.rindex(string,0,(text.get_pos() -1))
else:
cur_pos=text.index(string,(text.get_pos() +1))
text.set_pos(cur_pos)
except:
appuifw.note(u'Search string not found.','info')
last_find=saved_string
def z_replace():
'''find and replace all instances of a given string'''
replace=appuifw.multi_query(u'Replace :',u'with : ')
if replace != None:
text=text.get()
cur_pos=text.get_pos()
text.set(text.replace(replace[0],replace[1]))
text.set_pos(cur_pos + text[0:cur_pos].count(replace[0])*(len(replace[1])-len(replace[0])))
appuifw.note(unicode(text.count(replace[0])) + ' instances replaced')
def z_line():
global ym,vy,inline
'''move the cursor to a particular line no.'''
inline=appuifw.query(u'Line number:','number',maxline)
tr((int(ym/40)*40,int(vy/40)*40))
if (inline == None):
pass
elif (inline <= maxline):
if inline < 4:
vy=0
ym=inline*40+20
tr((ym,vy))
else:
ym=120
vy=inline*40*-1+ym
tr((ym,vy))
if settxt == 0:
sel2canv()
else:
appuifw.note(u'Line does not exist!','info')
def _rename():
'''rename a file/folder'''
path=dir_iter.entry(browse.current())
filename=unicode(os.path.basename(path))
if appuifw.query(u'Rename ' + filename + ' ?','query'):
newname=appuifw.query(u'Rename ' + filename,'text',filename)
if newname != None:
try:
os.rename(path,os.path.dirname(path) + str(newname))
appuifw.note(u'File renamed','info')
except:
appuifw.note(u'Error renaming file!','error')
def z_font(): ### ???
'''change the display font'''
fonts = appuifw.available_fonts()
fonts.sort()
index=appuifw.selection_list(choices=fonts,search_field=1)
fontsize=appuifw.query(u'Font Size: 9 to 36','number',16)
if fontsize < 9 or fontsize > 36:
fonsize = 16
if index != None:
txt.font=(unicode(fonts[index]),fontsize)
else:
pass
def z_mainmenu():
appuifw.app.menu=[(u'Open/Save',
((u'Open File...',z_canv2list),
(u'Open TXT',opentxt),
(u'Save 01',z_save_01),
(u'Save 02',z_save_02),
(u'Save As...',z_saveas),
(u'Open Temp 1',z_fileopen01),
(u'Open Temp 2',z_fileopen02),
(u'Open Template',z_template))),
(u'Edit Script',z_writemenu),
(u'Change Font',z_font),
(u'Info',z_info),
(u'Quit',z_quit)]
def z_canv2list():
flist() # sets to def go
def go(selected):
global accel,vx,vy,ym
filewr(selected)
canv2list()
ym=60
accel=0,0
vx,vy=0,0
def z_writemenu(): ### ZZZ a whole lot more to be added here
appuifw.app.menu=[(u'Select Line...',z_line),
(u'*** Main Menu',z_mainmenu),
(u'Quit',z_quit)]
CONFIG_DIR='e:\\data\python'
def tr(msg=None):
import inspect
trk = str(inspect.currentframe().f_back.f_lineno)
# in case there is no trk_on variable
try:
if trk_on == 1 or trk_on == True:
print 'Line Number: ' + trk
if msg!=None: # or msg!='': <- didn't work ???
if msg!='':
if msg!=1:
print str(msg)
else:
print "***"
except:
pass
if __name__ == '__main__':
pass