def read_StoixeiaFile(filename): try: fp = xlrd.open_workbook(filename, encoding_override="cp1252") except: print "open file error" now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':Read Students' ' file' + filename + ' and add to dictionary\n') sheet = fp.sheet_by_index(0) for i in range(15, sheet.nrows, 1): """this is a solution for problem in converting unicode and ascii""" reload(sys) sys.setdefaultencoding('utf-8') am = sheet.row_values(i)[1] surname = sheet.row_values(i)[2] name = sheet.row_values(i)[4] phonenumber = sheet.row_values(i)[10] s = StudentsStoixeia(am, surname, name, phonenumber) now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':add student ' + str(s.AM) + ' ' + s.Surname + ' ' + s.Name + ' ' + s.PhoneNumber + '\n') filename_prc = filename + '.prc' os.rename(filename, filename_prc)
def sendSMS(i, to, fromSender, name, text, text1, absences): """this is for testing""" """to=toNumbers[i]""" urlsms_sender = passwd.urlsms + '&to=' + to + '&from=' + fromSender """urlsms_sender=urlsms_sender+'&text='+str(text)+str(name)+' '+str(text1)""" type_xml = '&type=xml' sms_text = str(text) + str(name) + ' ' + str(text1) + str(absences) sms_alltext = sms_text if '&' in sms_text: sms_text = sms_text.replace('&', '%26') urlsms_sender_text = urlsms_sender + '&text=' + str(sms_text) + type_xml print "sms url", urlsms_sender_text try: now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':send SMS for teacher ' + name + '\n') print "send sms to ", to """if i<=2:""" result = urllib.urlretrieve(urlsms_sender_text) except: now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':SMS for teacher ' + name + ' not send successfull\n') print "SMS for teacher ", name, "not send successfull\n"
def __init__(self, surname, specialty, listElement='', tup=''): self.surname = str(surname) self.specialty = str(specialty) self.tup = tup self.listElement = listElement if tup == '': if (self.surname, self.specialty) not in MakeInfoDict.infoDict: MakeInfoDict.infoDict[(self.surname, self.specialty)] = [self.listElement] now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ' first time teacher found ' + surname + ' ' + specialty) init.fp_log.write('\n'.join(x for x in listElement)) else: MakeInfoDict.infoDict[(self.surname, self.specialty)].append( self.listElement) now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ' more than one time teacher found ' + surname + ' ' + specialty) init.fp_log.write('\n'.join(x for x in listElement)) else: if (self.surname, self.specialty) not in MakeInfoDict.infoDict: MakeInfoDict.infoDict[(self.surname, self.specialty)] = [self.tup] else: MakeInfoDict.infoDict[(self.surname, self.specialty)].append(self.tup)
def read_AbsencesFile(filename): try: fp = xlrd.open_workbook(filename, encoding_override="cp1252") except: print "open file error" now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':Read Students' ' file ' + filename + 'and add to dictionary\n') sheet = fp.sheet_by_index(0) for i in range(15, sheet.nrows, 1): reload(sys) sys.setdefaultencoding('utf-8') am = sheet.row_values(i)[1] surname = sheet.row_values(i)[2] name = sheet.row_values(i)[3] absences = sheet.row_values(i)[5] s = StudentsAbsences(am, surname, name, absences) now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':add student ' + str(s.AM) + ' ' + s.Surname + ' ' + s.Name + ' ' + str(s.Absences) + '\n') filename_prc = filename + '.prc' os.rename(filename, filename_prc)
def checkBasicFiles(pid): print "checkBasic Files:",pid global xmlfile global xmlfileExist xmlfile,xmlfileExist=init.findFile(init.xmlDir,'xml.prc') print "check xml file existance for pid:",pid now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+'check xml file existance for pid:'+str(pid)+'\n') """xlsStoixeiaFile=False""" global xlsfileStoixeia global xlsStoixeiaFile xlsfileStoixeia,xlsStoixeiaFile=init.findFile(init.stoixeiaDir,'prc') print "check stoixeia file existance for pid:",pid now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+'check stoixeia file existance for pid:'+str(pid)+'\n') """xlsfileExist=False""" global xlsfile global xlsfileExist xlsfile,xlsfileExist=init.findFile(init.xlsDir,'xls.prc') print "check excell file existance for pid:",pid now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+'check excell file existance for pid:'+str(pid)+'\n')
def update_dict(filename): try: fp = xlrd.open_workbook(filename, encoding_override="cp1252") except: print "open file error" now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':Read absences file' + filename + ' and update dictionary\n') sheet = fp.sheet_by_index(0) for i in range(15, sheet.nrows, 1): """this is a solution for problem in converting unicode and ascii""" reload(sys) sys.setdefaultencoding('utf-8') am = sheet.row_values(i)[1] surname = sheet.row_values(i)[2] name = sheet.row_values(i)[3] absences = sheet.row_values(i)[5] if StudentsAbsences.Studentsabsences.has_key(int(am)): tmpStoixeia = StudentsAbsences.Studentsabsences.get(int(am)) if tmpStoixeia[2] == int(absences): print "no absences changed, no sms is going to be sent" now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ': no absences changed\n') continue else: StudentsAbsences.Studentsabsences.pop(int(am)) s = StudentsAbsences(am, surname, name, absences) tmpStoixeia = StudentsStoixeia.Studentstoixeia.get(s.AM) init.toSend = tmpStoixeia[2] init.studentName = s.Surname + ' ' + s.Name init.numberOfAbsences = str(s.Absences) sendingSMS.sendSMS(i, init.toSend, init.fromSender, init.studentName, init.text, init.text1, init.numberOfAbsences) now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':update student and send sms' + str(s.AM) + ' ' + s.Surname + ' ' + s.Name + ' ' + str(s.Absences) + '\n') filename_prc = filename + '.prc' os.rename(filename, filename_prc)
def sendSMSAll(): now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':start sms sending \n') tmp = 0 for key, value in TeachersStoixeia.teacherstoixeia.items(): tstoixeia = TeachersStoixeia.teacherstoixeia.get(key) now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':get name and phone number for:' + key[0] + " " + key[1] + ' \n') init.toSend = tstoixeia[2] init.teacherName = key[0] + ' ' + tstoixeia[1] if MakeInfoDict.infoDict.has_key(key): infostoixeia = MakeInfoDict.infoDict.get(key) now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':get information for name\n') efimeries = ' ' for infokey in infostoixeia: if type(infokey) == tuple: for val in infokey: efimeries = efimeries + ' ' + val efimeries = efimeries + ',' else: efimeries = efimeries + ' ' + infokey init.numberOfAbsences = str(efimeries) now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':which information is' + init.numberOfAbsences + '\n') now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':call sms function \n') sendSMS(tmp, init.toSend, init.fromSender, init.teacherName, init.text, init.text1, init.numberOfAbsences) """this is for testing""" if tmp >= 2: tmp = 0 else: tmp += 1
def read_StoixeiaFile(filename): """this function read excell file and call a class (TeacherStoixeia) to make a dictionary with this information""" try: fp = xlrd.open_workbook(filename, encoding_override="cp1252") except: print "open file error" now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':Read Teachers' ' file' + filename + ' and add to dictionary\n') sheet = fp.sheet_by_index(0) for i in range(13, sheet.nrows, 1): """this is a solution for problem in converting unicode and ascii""" reload(sys) sys.setdefaultencoding('utf-8') afm = sheet.row_values(i)[1] specialty = sheet.row_values(i)[2] surname = sheet.row_values(i)[3].strip() name = sheet.row_values(i)[4].strip() phonenumber = str(sheet.row_values(i)[6]) if surname.find(' ') != -1: surname = surname.split()[0] s = TeachersStoixeia(surname, specialty, afm, name, phonenumber) now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':add teacher ' + str(s.AFM) + ' ' + str(s.Surname) + ' ' + str(s.Specialty) + ' ' + str(s.Name) + ' ' + str(s.PhoneNumber) + '\n') """print "teachers' information\n" for key,val in TeachersStoixeia.teacherstoixeia.items(): for k in key: print k, print "==>", for t in val: print t, print "\n" print "duplicate names:",TeachersStoixeia.findDupNames""" filename_prc = filename.split('xls')[0] + 'prc' os.rename(filename, filename_prc)
def xmlStructure(pid): global xlsFormat global col global row global xlsfileExist print "xml Structure for pid:",pid now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+'xml Structure for pid:'+str(pid)+'\n') init.xmlFileName=init.xmlDir+'/'+xmlfile[0] elements=init.readXml(init.xmlFileName) init.xlsFileName=init.xlsDir+'/'+init.xlsFileName if init.xlsFileName.split('/')[2] in xlsfile: xlsfileExist=True xlsFormat=init.formatXls(elements) """print "xls format for pid:",pid,len(xlsFormat)""" if type(xlsFormat)==tuple: col=xlsFormat[0] row=xlsFormat[1] else: col=xlsFormat
def read_files(): now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':Read Students' ' information files\n') for root, dirs, filenames in os.walk(init.stoixeiaDir, topdown=True): for name in filenames: filename = os.path.join(init.stoixeiaDir, name) if 'xls' not in filename: continue read_StoixeiaFile(filename) now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':Complete Students' ' information dictionary\n')
def sendSMS(i,to,fromSender,name,text,text1,absences): urlsms_sender=init.urlsms+'to='+to urlsms_sender=urlsms_sender+'&from='+fromSender+'&text='+text+name+' '+text1+absences+"&type=xml" try: now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+':send SMS for student ' + name+'\n') print "send sms to ",to """result=urllib.urlretrieve(urlsms_sender)""" except: now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+':SMS for student ' + name+' not send successfull\n')
def read_files(): """this function checks if there is a file for teachers information""" now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':Read Teachers' ' information files\n') for root, dirs, filenames in os.walk(init.stoixeiaDir, topdown=True): for name in filenames: filename = os.path.join(init.stoixeiaDir, name) if 'xls' not in filename: continue read_StoixeiaFile(filename) now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':Complete Teachers' ' information dictionary\n')
def readXlsFile(xlsFormat, col, row): try: fp = xlrd.open_workbook(init.xlsFileName, encoding_override="cp1252") except: print "open file error" now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':Read excell file ' + init.xlsFileName + 'and create info dictionary\n') if len(MakeInfoDict.infoDict) > 0: MakeInfoDict.infoDict.clear() print init.xlsFileName sheet = fp.sheet_by_index(0) print "row and columns in excell:", sheet.nrows, sheet.ncols """start line in xls file""" for i in range(sheet.nrows): line = sheet.row_values(i) if col[i] not in line: continue start_row = i break """start row in xls file, if start_col=0 then there is no row format""" start_col = startRow(line, col) print "start reading excell from row:", start_row, " and from column:", start_col "here is the problem. To stop when the file efimeries file ends" if type(xlsFormat) == tuple: """print "process excell with row""" index = 1 readSpecificXls(index, start_row, sheet.nrows, sheet.ncols, start_col, sheet, col, row) else: """print "process file with no row""" index = 0 readSpecificXls(index, start_row, sheet.nrows, sheet.ncols, start_col, sheet, col) """for key,value in MakeInfoDict.infoDict.items():
def readSpecificXls(index, start_row, nrows, ncols, start_col, sheet, col, row=''): now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ':Read excell row \n') for i in range(start_row + 1, nrows): """Τελικά η μοναδική λύση στο encoding""" reload(sys) sys.setdefaultencoding('utf-8') line = sheet.row_values(i) """ αν βρει στοιχείο το row τότε συνεχίζει αλλιώς σταματά""" if index == 0: if line.count("") == ncols: continue else: pass k = start_col if start_col > 0: """here the file has data in row and in col""" for j in range(sheet.ncols - start_col): now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1] + ' ' + line[k] + '\n') if line[k] == "": continue rowdata = [t for t in line if line.index(t) < start_col] rowdata.append(col[j]) """parenthesis creates a generator, so it must explicitly declared""" tup = tuple(t for t in rowdata) now = init.get_datetime() init.fp_log.write(now[0] + ' ' + now[1]) init.fp_log.write('\n'.join(x for x in tup)) teachername = line[k].decode('utf-8').strip().upper() init.fp_log.write('\n' + now[0] + ' ' + now[1] + ' ' + teachername) """print "first time read teachername",teachername""" tonoExist = init.checkInTono(teachername) if tonoExist: """print "teacherName:",teachername""" teachername = init.replaceTono(teachername) """print "teachername after tono:",teachername""" init.fp_log.write('\n' + now[0] + ' ' + now[1] + 'teachername after tono ' + teachername) k += 1 if teachername.find(' ') != -1: allname = teachername.split() teachername = allname[0].strip() """print "teachername after split",teachername," ", len(teachername)""" init.fp_log.write('\n' + now[0] + ' ' + now[1] + ' teachername after split ' + teachername) specialty = allname[1].strip() init.fp_log.write('\n' + now[0] + ' ' + now[1] + ' specialty after split ' + specialty) """print "specialty teacher ",specialty," ", len(specialty)""" s = MakeInfoDict(teachername, specialty, tup) else: for j in range(sheet.ncols - start_col): if line[k] == "": continue teachername = line[k].strip().upper() teachername = init.replaceTono(teachername) teachername = teachername.decode('utf-8') """print teachername""" k += 1 if teachername.find(' ') != -1: allname = teachername.split() teachername = allname[0].strip() specialty = allname[1].strip() s = MakeInfoDict(teachername, specialty, col[j])
def mainFunction(pid): print "mainFunction for pid:",pid now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+'mainFunction for pid:'+str(pid)+'\n') if xlsStoixeiaFile: print "create teacher's dictionary for pid ",pid now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+'create teacher''s dictionary for pid:'+str(pid)+'\n') stoixeiaFile=init.stoixeiaDir+'/'+xlsfileStoixeia[0] readStoixeia.read_files() """time.sleep(4) codenum=readStoixeia.fixDupKeys()""" if not xmlfileExist and not xlsfileExist: print "there is nothing new for pid",pid exit_code=1 time.sleep(5) return exit_code if first_run==0: xmlStructure(pid) xmlname_prc=init.xmlFileName+'.prc' os.rename(init.xmlFileName,xmlname_prc) print "in first_run pid:",pid,init.xlsFileName,init.xmlFileName now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+'in first_run pid:'+str(pid)+'\n') if (not xmlfileExist and xlsfileExist) or (xmlfileExist and xlsfileExist): print "xml and xls new exist or not new xml and new xls in pid:",pid,xmlfileExist,xlsfileExist now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+'checking xml file and xls file for pid:'+str(pid)+'\n') if init.xlsFileName.split('/')[2] not in xlsfile: exit_code=1 return exit_code """print init.xlsFileName""" now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+':Read xls file:'+ init.xlsFileName+'for pid:'+str(pid)+'\n') now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+':Make information dictionary for pid:'+str(pid)+'\n') print "Read excell file for pid:",pid print "Make Information Dictionary for pid:",pid readXlsFile.readXlsFile(xlsFormat,col,row) xlsfilename_prc=init.xlsFileName+'.prc' os.rename(init.xlsFileName,xlsfilename_prc) print "rename xls file after processed for pid:",pid now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+'rename xls file after processed for pid:'+str(pid)+'\n') time.sleep(2) print "Send Sms for pid:",pid now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+':Send sms to for pid'+str(pid)+'\n') time.sleep(2) sendingSMS.sendSMSAll()
now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+':Send sms to for pid'+str(pid)+'\n') time.sleep(2) sendingSMS.sendSMSAll() if __name__== '__main__': first_run=0 child=0 pid=os.getpid() print "Start sms application for pid",pid now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+':Start application for pid'+str(pid)+'\n') checkBasicFiles(pid) if not xmlfileExist: print "how to start if there is no xml?\n" now=init.get_datetime() init.fp_log.write(now[0]+' '+now[1]+':Stop application because there is no xml file for pid'+str(pid)+'\n') print "application going to exit\n" time.sleep(5) exit(0) if xmlfileExist and not xlsStoixeiaFile: