2007-4-20 10:35
vhly
Python for S60 手机端
功能强筋,
支持各种操作
本代码使用手机编写 请回复后复制,
以对作者支持。
author:vhly[FR]
[code]
# SYMBIANUID=0x01234560
#This Python Script is to edit any text and save any type
#
# Author:vhly[FR]
# Date:2007/03/24
import appuifw
import sys
import e32
import os
import sysinfo
from binascii import *
import imp
import marshal
import time
import struct
if e32.s60_version_info>=(3,0):
PYTHON_PATH = '\\python'
APPMGR_PATH = 'c:\\private\\F0201512'
LIB_PATH = '\\sys\\bin'
APPS_PATH = '\\sys\\bin'
else:
PYTHON_PATH = '\\system\\apps\\python'
APPMGR_PATH = '\\system\\apps\\appmgr'
LIB_PATH = '\\system\\libs'
APPS_PATH = '\\system\\apps'
PYTHON_EXTS = ['.py', '.pyc', '.pyo', '.pyd']
PYTHON_LIB_EXTS = ['.pyc', '.pyo', '.pyd']
def python_drive():
if e32.s60_version_info>=(3,0):
return "C:"
for drive in [str(x) for x in e32.drive_list()]:
if os.path.isfile(os.path.join(drive, PYTHON_PATH, 'python.app')):
return drive
raise AssertionError, "Python not found"
def standalone_install(filename):
def reverse(L):
L.reverse()
return L
def atoi(s):
# Little-endian conversion from a 4-char string to an int.
sum = 0L
for x in reverse([x for x in s[0:4]]):
sum = (sum << 8) + ord(x)
return sum
def itoa(x):
# Little-endian conversion from an int to a 4-character string.
L=[chr(x>>24), chr((x>>16)&0xff), chr((x>>8)&0xff), chr(x&0xff)]
L.reverse()
return ''.join(L)
try:
offset = int(file(os.path.join(python_drive(), APPMGR_PATH,
'uid_offset_in_app')).read(), 16)
except:
offset = None
app_rootname = os.path.splitext(os.path.split(filename)[1])[0]
app_dir = os.path.join(python_drive(), APPS_PATH, app_rootname)
#
# read the UID from the script file
#
script = file(filename, 'r').read()
uidpos = script.find('SYMBIANUID=')
if uidpos == -1:
uid_text = appuifw.query(u'Give UID', 'text', u'0xXXXXXXXX')
if not uid_text == None:
uid = int(uid_text, 16)
else:
appuifw.note(u"Installation cancelled", "info")
return
else:
uidpos = uidpos+len('SYMBIANUID=')
while uidpos+10 < len(script) and script[uidpos].isspace():
uidpos = uidpos+1
if uidpos == len(script):
appuifw.note(u"UID not found", "error")
return
else:
uid = int(script[uidpos:uidpos+10], 16)
#
# copy the script to application's directory as default.py
#
if not os.path.isdir(app_dir):
os.mkdir(app_dir)
e32.file_copy(unicode(os.path.join(app_dir, 'default.py')),
unicode(filename))
#
# copy the template .app file to application directory with proper name
# and set the UID and checksum fields suitably
#
template_dotapp = file(os.path.join(python_drive(), APPMGR_PATH, 'pyapp_template.tmp'))
dotapp_name = app_rootname + '.app'
dotapp = file(os.path.join(app_dir, dotapp_name), 'w')
appbuf = template_dotapp.read()
csum = atoi(appbuf[24:28])
crc1 = itoa(e32._uidcrc_app(uid))
crc2 = itoa(( uid + csum ) & 0xffffffffL)
if offset:
temp = appbuf[0:8] + itoa(uid) + crc1 + appbuf[16:24] + crc2 +\
appbuf[28:offset] + itoa(uid) + appbuf[(offset+4):]
else:
temp = appbuf[0:8] + itoa(uid) + crc1 + appbuf[16:24] + crc2 + appbuf[28:]
dotapp.write(temp)
#
# copy the template .rsc file to application directory with proper name
#
rsc_name = app_rootname + '.rsc'
e32.file_copy(unicode(os.path.join(app_dir, app_rootname + '.rsc')),
unicode(os.path.join(python_drive(), APPMGR_PATH, 'pyrsc_template.tmp')))
return app_dir
# set the application screen normal
appuifw.app.screen='normal'
# create a text editor
mainEdit = appuifw.Text()
mainEdit.style=appuifw.STYLE_BOLD
#mainEdit.color=(255,0,0)
# get the app lock
app_lock = e32.Ao_lock()
# set the app screen in edit
appuifw.app.body = mainEdit
#default file path is e
target_file=u"e:\\"
def ex():
sys.exit()
app_lock.signal()
def readFile(filename):
file = open(filename,'rb')
text = file.read().decode('utf8')
file.close()
return text
def saveFile(filename,text):
file = open(filename, 'wb')
file.write(text)
file.close()
def saveAsFile(filename, text):
saveFile(filename,text)
def openFileMenu():
global target_file
filename = appuifw.query(u"Open File",'text',target_file)
if not filename:
pass
filename=filename.replace('\\\\','\\')
target_file=filename
filename=filename.encode('utf-8')
text = readFile(filename)
mainEdit.set(text)
mainEdit.set_pos(0)
def saveFileMenu():
global target_file
filename = appuifw.query(u"Save File",'text',target_file)
if not filename:
pass
filename=filename.replace('\\\\','\\')
target_file=filename
filename=filename.encode('utf-8')
text = mainEdit.get().encode('utf-8')
text=text.replace("\xE2\x80\xA9","\x0D\x0A")
saveFile(filename, text)
appuifw.note(u"File Saved",'info')
def saveAsFileMenu():
saveFileMenu()
def aboutMenu():
appuifw.note(u"This Python Editor is made by vhly[FR]",'info')
def install_file_menu():
global target_file
e32.file_copy(u"e:\\system\\apps\\Python\\my\\"+os.path.split(target_file)[1],target_file)
appuifw.note(target_file+' has installed','info')
def install_app_menu ():
# Method body
global target_file
try:
onl=standalone_install(target_file)
appuifw.note(u"Install app to:"+onl,'info')
except (Error,ValueError),err:
lens=u" "
fee=err.args
noon=len(fee)
for x in fee:
lens=lens+'\n'+x
appuifw.note(u"Error not install:"+lens,'error')
def genpyc():
global target_file
st=mainEdit.get().encode('utf-8')
st=st.replace("\xE2\x80\xA9","\x0D\x0A")
st=st.replace("\r","")
cd=compile(st,target_file,'exec')
magic=imp.get_magic()
stamp=struct.pack('<i',time.time())
base1=os.path.splitext(target_file)[0]
cfile=open(base1+".pyc",'wb')
cfile.write(magic)
cfile.write(stamp)
marshal.dump(cd,cfile)
cfile.close()
appuifw.note("Compile file to:\n"+base1+".pyc")
pass
def genpyo():
global target_file
pass
def gen_tab():
mainEdit.add(u" ")
def gen_def():
data=u"def "
defname=appuifw.query(u"Method name",'text')
if defname==None:
pass
data=data+defname+u"():\n # Method body\n \n"
mainEdit.add(data)
def regUser ():
# Method body
imei_str=sysinfo.imei()
sw_str=sysinfo.sw_version()
fields=[(u"IMEI",'text',imei_str),(u"Name",'text'),(u"Code",'text')]
frm=appuifw.Form(fields)
frm.execute()
def newFileMenu ():
# Method body
mainEdit.clear()
sup=u"# SYMBIANUID=\n# This file edited by Python Editor for S60\n# gen by vhly[FR]\n#\n# Script Name:\n# Author:\n# Description:\n# \n\nimport e32\nfrom appuifw import *\nimport sys\n\napp_lock=e32.Ao_lock()\n\ndef exitApp():\n sys.exit()\n app_lock.signal()\n\napp.exit_key_handler=exitApp\n"
mainEdit.set(sup)
def gotoEnd ():
# Method body
mainEdit.set_pos(mainEdit.len())
find_string=u"test"
def searchText ():
# Method body
global find_string
sur=appuifw.query(u"Find String",'text')
if not sur:
pass
find_string=sur
poscurrent=mainEdit.get_pos()
slen=mainEdit.len()
slen=slen-poscurrent
sur0=mainEdit.get(poscurrent,slen)
index=sur0.find(sur)
if index==-1:
appuifw.note(u"Text not found",'info')
pass
poscurrent=poscurrent+index
mainEdit.set_pos(poscurrent)
def searchNext():
global find_string
poscurrent=mainEdit.get_pos()
slen=mainEdit.len()
slen=slen-poscurrent
sur0=mainEdit.get(poscurrent,slen)
index=sur0.find(find_string)
if index==-1:
appuifw.note(u"Text not found",'info')
pass
poscurrent=poscurrent+index
mainEdit.set_pos(poscurrent)
appuifw.app.menu = [(u"File",((u"New",newFileMenu),(u"Open",openFileMenu),(u"Save",saveFileMenu),(u"SaveAs",saveAsFileMenu))),(u"Edit",((u"Tab",gen_tab),(u"New def",gen_def),(u"Goto End",gotoEnd))),(u"Search",((u"Text",searchText),(u"Search next",searchNext))),(u"Install",((u"Script",install_file_menu),(u"App",install_app_menu))),(u"Compile",((u"PYC",genpyc),(u"PYO",genpyo))),(u"About",((u"About",aboutMenu),(u"Register",regUser)))]
appuifw.app.title=u"Python Editor"
# set the exit handler
appuifw.app.exit_key_handler = ex
# wait the lock
app_lock.wait()
[/code]
[yct02]