Salaam all. This is my first application I write in Python. Frankly, I don't know Python at all. I know C++ and Java for the desktop and PHP, and this is also my first Python writing too. I wrote it through gathering resources and examples. I have so many notes on the PyS60 project too. However I encourage this project to continue.
This code I wrote, will log and calculate the cost of every outgoing call done, and every SMS sent too.
The thread is used instead of the service() function that is part of the e32 module. I encourage using threads here.
The appswitch module must be downloaded and signed. Also I found that appswitch methods take the value of the title of the application you want to set to background. This title must be the same one under the application's icon.
I wanted to use the comma separated values file format through the csv module, but after checking this excellent forum , the csv module is not there. So, I did read the call and SMS prices/rates from the files manually/custom written. checkout the read_rates() function.
The PyS60 documentation is almost OK. However, it really needs to be like the PHP online documentation, with excess in and heaps of examples.
I wish that the PyS60 can set the printing on the screen as landscape view. I would have done it myself using the images module. but there is no rotation available. Correct me here if I am wrong.
You may use this also as skeleton or a template for any service you want to program. All you have to do is just delete the contents of the while loop in the function mythread().
Because of uncertainty in the use of the global and local variables, I had to stick as much as to global variables.
Also, use ensemble to package your python script as a SIS file (Symbian Installer File).
Hope you like it. I added comments in the code as much as possible.
For those who'd get confused in long codes, start reading/parsing the code from this line at the end of the whole code:
Thanks.PHP Code:new_accountant=Accountant()
===================================================================================================================================
PHP Code:# Mobile software. Cost monitor phone Calls and SMS messages.
import appuifw,e32,thread,logs,time,appswitch
def mythread():
#service started
print "service started...\n"
#setting log-file-pathes
filename_calls=u"E:\\call_logs.txt" #path for outgoing calls
filename_sms=u"E:\\sms_logs.txt" #path for sms
logfile_calls = open(filename_calls,"a")
logfile_sms = open(filename_sms,"a")
#starting with empty log events for both
#new_accountant.latest_outgoing_call_log=[]
#new_accountant.latest_sms_sent_log=[]
#while the service is ON do the logs
while(new_accountant.service_switch):
print "\r inside service loop.."
#logs.calls(mode='out')[0]) is the latest and the newest event happened for calls
if(new_accountant.latest_outgoing_call_log!=logs.calls(mode='out')[0]):
phoneNumber=logs.calls(mode='out')[0]["number"]
rate=new_accountant.get_call_rate(phoneNumber)
dim=time.localtime(logs.calls(mode='out')[0]["time"])
dateim= time.strftime("%Y/%m/%d-%H:%M:%S", dim)
s=str(dateim)
s=s+(" ;D:")+str(logs.calls(mode='out')[0]["duration"])
s=s+(" ;N:")+str(logs.calls(mode='out')[0]["number"])
##########################start the calclations here########################
print "call rate="+str(rate)
cost=rate*logs.calls(mode='out')[0]["duration"]
s=s+(" ; cost:$")+str(cost)
##########################end the calclations here########################
nln=" ;"
s=s+nln
#print the log line on the screen
print "calls="+str(s)+"\n"
#print the log line again on the log file
logfile_calls.write( str(s)+'\n')
new_accountant.latest_outgoing_call_log=logs.calls(mode='out')[0]
#logs.sms(mode='out')[0]) is the latest event and the newest happened for sms. mode=out is the sent messages.
if(new_accountant.latest_sms_sent_log!=logs.sms(mode='out')[0]):
phoneNumber=logs.sms(mode='out')[0]["number"]
rate=new_accountant.get_sms_rate(phoneNumber)
dim=time.localtime(logs.sms(mode='out')[0]["time"])
dateim= time.strftime("%Y/%m/%d-%H:%M:%S", dim)
s=str(dateim)
s=s+(" ; N:")+str(logs.sms(mode='out')[0]["number"])
##########################start the calclations here########################
s=s+(" ; cost:$")+str(rate)
##########################end the calclations here########################
nln=" ;"
s=s+nln
#print the log line on the screen
print "sms="+str(s)+"\n"
#print the log line again on the log file
logfile_calls.write( str(s)+'\n')
new_accountant.latest_sms_sent_log=logs.sms(mode='out')[0]
e32.ao_sleep(1)
print "closing log files"
logfile_calls.close()
logfile_sms.close()
print "service stopped\n"
return 0
def start_mythread():
new_accountant.service_switch= not new_accountant.service_switch
if(new_accountant.service_switch):
appuifw.app.menu[1]=(u"stop logging",start_mythread)
thread.start_new_thread(mythread,())
else:
appuifw.app.menu[1]=(u"start logging",start_mythread)
print "stopping service..."
def run_in_background():
print appswitch.switch_to_bg(u"Python")
def quit():
print "stopping service..."
new_accountant.service_switch= 0
print("Quitting Program")
app_lock.signal()
def read_rates():
#reads the rates of the SMS and calls. both files have a CSV file format. delimiter is the semi colon ';'
call_rates_file="E:\\Python\\call_rates.txt"
sms_rates_file="E:\\Python\\sms_rates.txt"
call_fp=open(call_rates_file,'r')
sms_fp=open(sms_rates_file,'r')
call_rate_lines=call_fp.readlines()
for every_call_rate in call_rate_lines:
call_rate_fields=every_call_rate.split(';')
new_accountant.call_rates[call_rate_fields[0]]=float(call_rate_fields[1])
sms_rate_lines=sms_fp.readlines()
for every_sms_rate in sms_rate_lines:
sms_rate_fields=every_sms_rate.split(';')
new_accountant.sms_rates[sms_rate_fields[0]]=float(sms_rate_fields[1])
call_fp.close()
sms_fp.close()
def read_discarded_numbers():
#read discarded SMS and call numbers
call_discarded_numbers_file="E:\\Python\\call_discarded_numbers.txt"
call_discarded_fp=open(call_discarded_numbers_file,'r')
new_accountant.calls_discarded_numbers=call_discarded_fp.readlines()
sms_discarded_numbers_file="E:\\Python\\sms_discarded_numbers.txt"
sms_discarded_fp=open(sms_discarded_numbers_file,'r')
new_accountant.sms_discarded_numbers=sms_discarded_fp.readlines()
class Accountant:
service_switch=False #ON/OFF switch of the logging service
latest_outgoing_call_log=[] #last call log event details used for the check in the mythread()
latest_sms_sent_log=[] #last sms log event used for the check in the mythread()
#make a list of phone numbers to be discarded the log. default is an empty list
calls_discarded_numbers=[]
sms_discarded_numbers=[]
#this is similar to a rates file. a dictionary. area_code vs rate. these are default data.
call_rates={'03':2,'04':0.6,'07':1}
sms_rates={'03':2,'04':0.6,'00':3}
def get_call_rate(self,phone_number):
if (phone_number in self.calls_discarded_numbers):
return 0
else:
area_code=str(phone_number)[0:2]
if (area_code in self.call_rates) :
return self.call_rates[area_code]
else:
return 0
def get_sms_rate(self,phone_number):
if (phone_number in self.sms_discarded_numbers):
print "phone_number "+str(phone_number)+" in discarded"
return 0
else:
area_code=str(phone_number)[0:2]
print "area code="+str(area_code)
if (area_code in self.sms_rates) :
print "reterning area code rate"
return self.call_rates[area_code]
else:
return 0
def __init__(self):
#clearing the body
appuifw.app.body.clear()
#defining the GUI on startup
appuifw.app.screen = "normal"
#setting the menu on left soft button on the mobile
appuifw.app.menu = [(u"go background",run_in_background),(u"start logging",start_mythread)]
#program exit function on the right soft button.
appuifw.app.exit_key_handler = quit
#making an instance of the application
new_accountant=Accountant()
#read the SMS and Calls rates from the respective files
read_rates()
#read discarded SMS and call numbers
read_discarded_numbers()
app_lock = e32.Ao_lock()
app_lock.wait()

Reply With Quote

