def random_phonebook_csv(minlength=0, maxlength=2000, maxfields=400): """Generate a random phonebook in CSV format @param minlength: minimum number of entries @param maxlength: maximum number of entries @returns: a list of lists of the data. the first row is the headers """ possfields=["First Name", "Last Name", "Middle Name", "Name", "Nickname", "Email Address", "Web Page", "Fax", "Home Street", "Home City", "Home Postal Code", "Home State", "Home Country/Region", "Home Phone", "Home Fax", "Mobile Phone", "Home Web Page", "Business Street", "Business City", "Business Postal Code", "Business State", "Business Country/Region", "Business Web Page", "Business Phone", "Business Fax", "Pager", "Company", "Notes", "Private", "Category", "Categories"] # some fields use different types mostly types={ 'Fax': phonenumber, "Home Phone": phonenumber, "Home Fax": phonenumber, "Mobile Phone": phonenumber, "Business Phone": phonenumber, "Business Fax": phonenumber, "Pager": phonenumber, "Email Address": email, "Categories": categories, "Private": boolean } print "Generating phonebook", headers=[random.choice(possfields) for i in range(random.randrange(1,maxfields))] numrows=random.randrange(minlength, maxlength) print "with",len(headers),"columns and",numrows,"rows" op=[ headers ] for i in range(numrows): op.append([gen_string(choices=types.get(col, words)) for col in headers]) print " ... generation completed" return DSV.exportDSV(op, quoteall=random.choice([0,1]))
def splitandunescape(self, line): """Split fields and unescape double quote and right brace""" col=line.find(": ") print line[col+2:] e=DSV.importDSV([line[col+2:]])[0] i=0 while i<len(e): item=e[i] item=item.replace("}\002",'"') item=item.replace("}]","}") e[i]=item i+=1 return e
def random_phonebook_csv(minlength=0, maxlength=2000, maxfields=400): """Generate a random phonebook in CSV format @param minlength: minimum number of entries @param maxlength: maximum number of entries @returns: a list of lists of the data. the first row is the headers """ possfields = [ "First Name", "Last Name", "Middle Name", "Name", "Nickname", "Email Address", "Web Page", "Fax", "Home Street", "Home City", "Home Postal Code", "Home State", "Home Country/Region", "Home Phone", "Home Fax", "Mobile Phone", "Home Web Page", "Business Street", "Business City", "Business Postal Code", "Business State", "Business Country/Region", "Business Web Page", "Business Phone", "Business Fax", "Pager", "Company", "Notes", "Private", "Category", "Categories" ] # some fields use different types mostly types = { 'Fax': phonenumber, "Home Phone": phonenumber, "Home Fax": phonenumber, "Mobile Phone": phonenumber, "Business Phone": phonenumber, "Business Fax": phonenumber, "Pager": phonenumber, "Email Address": email, "Categories": categories, "Private": boolean } print "Generating phonebook", headers = [ random.choice(possfields) for i in range(random.randrange(1, maxfields)) ] numrows = random.randrange(minlength, maxlength) print "with", len(headers), "columns and", numrows, "rows" op = [headers] for i in range(numrows): op.append( [gen_string(choices=types.get(col, words)) for col in headers]) print " ... generation completed" return DSV.exportDSV(op, quoteall=random.choice([0, 1]))
from DSV import DSV import matplotlib.pyplot as plt from numpy import * csvfile='2.csv' data = file(csvfile).read() data = DSV.organizeIntoLines(data, textQualifier=None) data = DSV.importDSV(data, delimiter=';', textQualifier=None) # skip the headers data=data[1:] def dat(no): return array([float(d[no]) for d in data]) (t, tin, tout, door) = (dat(0), dat(1), dat(2), dat(3)) plt.figure(1) plt.subplot(211) plt.plot(t, tout, 'b-', t, tin, 'r-', door, 'g-') plt.subplot(212) plt.plot(t, tin-tout, 'c-') plt.show()
def splitandunescape(self, line): """Split fields and unescape double quote and right brace""" col=line.find(": ") print line[col+2:] e=DSV.importDSV([line[col+2:]])[0] i=0 while i<len(e): item=e[i] item=item.replace("}\002",'"') item=item.replace("}]","}") e[i]=item i+=1 return e def samsungescape(self, s): """Escape double quotes and }'s in a string""" return s def defrell(self, s, acol, ncol): """Fixes up phonebook responses with the alias field. The alias field does not have quotes around it, but can still contain commas""" if acol<0 or acol>=ncol: return s e=s.split(",") i=0 while i<len(e): if len(e[i]) and e[i][0]=='"' and e[i][-1]!='"': while i+1<len(e) and (len(e[i+1])==0 or e[i+1][-1]!='"'): e[i] += ","+e[i+1] del e[i+1] else: if i+1<len(e): e[i] += ","+e[i+1] del e[i+1] i+=1 if len(e)<=ncol: return s for k in range(len(e)-ncol): e[acol]+=","+e[acol+1] del e[acol+1] e[acol]='"'+e[acol]+'"' res=e[0] for item in e[1:]: res+=","+item return res def csvsplit(self, line): """Parse a Samsung comma separated list.""" e=line.split(",") i=0 print len(e) result=[] while i<len(e): if len(e[i]) and e[i][0]=='"' and e[i][-1]!='"': while i+1<len(e) and (len(e[i+1])==0 or e[i+1][-1]!='"'): e[i] = e[i]+","+e[i+1] del e[i+1] else: if i+1<len(e): e[i] = e[i]+","+e[i+1] del e[i+1] item=e[i] if len(item)==0: t=0 elif item[0]=='"' or item[-1]=='"': mo=re.match('^"?(.*?)"?$',item) item=mo.group(1) item=item.replace("}\002",'"') item=item.replace("}]","}") t='string' elif re.match('^\d+T\d+$',item): t='timestamp' elif re.match('^[\dPT]+$',item): t='number' elif re.match('^\(\d+-\d+\)',item): t='range' elif re.match('^\d\d?/\d\d?/\d\d(\d\d)?$',item): t='date' else: t='other' if t: result.append({'type':t, 'value':item}) else: result.append(0) i+=1 return result def getcalendar(self, result): self.log("Getting calendar entries") self.setmode(self.MODEPHONEBOOK) res={} l=len(self._cal_entries_range) cal_cnt=0 for k in self._cal_entries_range: r=self.get_calendar_entry(k) if not len(r): self.progress(k+1, l, "Getting blank entry: %d"%k) continue self.progress(k+1, l, "Getting "+r[self._cal_read_name]) entry=bpcalendar.CalendarEntry() entry.start=self.extract_timedate(r[self._cal_start_datetime]) if self._cal_end_datetime_value is None: entry.end=self.extract_timedate(r[self._cal_end_datetime]) else: entry.end=entry.start entry.description=r[self._cal_read_name] try: alarm=self._cal_alarm_values[r[self._cal_alarm_type]] except: alarm=None entry.alarm=alarm res[entry.id]=entry cal_cnt += 1 result['calendar']=res self.setmode(self.MODEMODEM) return result def process_calendar(self, dict): """ Optimize and expand calendar data suitable for phone download """ r={} rp=[] today=datetime.date.today() last_date=today if __debug__: print 'original calendar:' for k,e in dict.items(): if __debug__: print e.description,':',e.start sd=datetime.date(*e.start[:3]) ed=datetime.date(*e.end[:3]) if ed>last_date: last_date=ed if e.repeat is None: if sd>=today: r.setdefault(e.start[:3], []).append(Samsung_Calendar(e)) else: if ed>=today: rp.append(e) delta_1=datetime.timedelta(1) for n in rp: current_date=today end_date=datetime.date(*n.end[:3]) cnt=0 while current_date<=end_date: if n.is_active(current_date.year, current_date.month, current_date.day): cd_l=(current_date.year, current_date.month, current_date.day) r.setdefault(cd_l, []).append(\ Samsung_Calendar(n, cd_l)) cnt+=1 if cnt>self._cal_max_events: break current_date+=delta_1 res=[] keys=r.keys() keys.sort() for k in keys: r[k].sort() if len(r[k])>self._cal_max_events_per_day: res+=r[k][:self._cal_max_events_per_day] else: res+=r[k] if len(res)>self._cal_max_events: res=res[:self._cal_max_events] return res def savecalendar(self, dict, merge): self.log("Sending calendar entries") cal=self.process_calendar(dict['calendar']) if __debug__: print 'processed calendar: ', len(cal), ' items' for c in cal: print c.description,':', c.start self.setmode(self.MODEPHONEBOOK) self.log("Saving calendar entries") cal_cnt=0 l=self._cal_max_events for c in cal: e=['']*self._cal_num_of_write_fields e[self._cal_entry]=`cal_cnt` e[self._cal_start_datetime]=self.encode_timedate(c.start) if self._cal_end_datetime_value is None: e[self._cal_end_datetime]=self.encode_timedate(c.end) else: e[self._cal_end_datetime]=self._cal_end_datetime_value e[self._cal_datetime_stamp]=self.get_time_stamp() e[self._cal_alarm_type]=c.alarm name=c.description.replace('"', '') if len(name)>self._cal_max_name_len: name=name[:self._cal_max_name_len] e[self._cal_write_name]='"'+name+'"' self.progress(cal_cnt+1, l, "Updating "+name) if not self.save_calendar_entry(",".join(e)): self.log("Failed to save item: "+name) else: cal_cnt += 1 self.log('Deleting unused entries') for k in range(cal_cnt, l): self.progress(k, l, "Deleting entry %d" % k) self.save_calendar_entry(`k`) self.setmode(self.MODEMODEM) return dict def _getmemo(self, result): self.setmode(self.MODEPHONEBOOK) m=MemoList(self) m.read() m_dict=m.get() result['memo']=m_dict self.setmode(self.MODEMODEM) return m_dict def _savememo(self, result, merge): self.setmode(self.MODEPHONEBOOK) m=MemoList(self) r=result.get('memo', {}) m.set(r) m.write() self.setmode(self.MODEMODEM) return r def _gettodo(self, result): self.log("Getting todo entries") self.setmode(self.MODEPHONEBOOK) td_l=TodoList(self) td_l.read() result['todo']=td_l.get() self.setmode(self.MODEMODEM) return result def _savetodo(self, result, merge): self.log("Saving todo entries") self.setmode(self.MODEPHONEBOOK) td_l=TodoList(self, result.get('todo', {})) td_l.validate() td_l.write() self.setmode(self.MODEMODEM) return result def _getsms(self, result): self.log("Getting SMS entries") self.setmode(self.MODEPHONEBOOK) sms_l=SMSList(self) sms_l.read() result['sms']=sms_l.get() sms_canned=CannedMsgList(self) sms_canned.read() result['canned_msg']=sms_canned.get() self.setmode(self.MODEMODEM) return result def _savesms(self, result, merge): self.log("Saving SMS Canned Messages") self.setmode(self.MODEPHONEBOOK) canned_msg=CannedMsgList(self, result.get('canned_msg', {})) canned_msg.write() self.setmode(self.MODEMODEM) return result def _getphoneinfo(self, phone_info): self.log('Getting Phone Info') self.setmode(self.MODEPHONEBOOK) for e in phoneinfo.PhoneInfo.standard_keys: f=getattr(self, 'get_'+e[0]) setattr(phone_info, e[0], f()) phone_info.append('Analog/Digital:', self.get_analog_digital()) self.setmode(self.MODEMODEM) def _send_at_and_get(self, cmd): try: resp=self.comm.sendatcommand(cmd) return ': '.join(resp[0].split(': ')[1:]) except: return None def is_mode_modem(self): try: resp=self.comm.sendatcommand('E0V1') return True except: return False def get_detect_data(self, r): r['manufacturer']=self._send_at_and_get('+GMI') r['model']=self._send_at_and_get('+GMM') r['firmware_version']=self._send_at_and_get('+GMR') r['esn']=self._send_at_and_get('+GSN') def _detectphone(coms, likely_ports, res, _module, _log): if not len(likely_ports): return None for port in likely_ports: if not res.has_key(port): res[port]={ 'mode_modem': None, 'mode_brew': None, 'manufacturer': None, 'model': None, 'firmware_version': None, 'esn': None, 'firmwareresponse': None } try: if res[port]['mode_modem']==False or \ res[port]['model']: continue p=Phone(_log, commport.CommConnection(_log, port, timeout=1)) if p.is_mode_modem(): res[port]['mode_modem']=True p.get_detect_data(res[port]) else: res[port]['mode_modem']=False except: if __debug__: raise _detectphone=staticmethod(_detectphone) "Talk to a Samsung phone using AT commands" class Profile (com_phone.Profile) : BP_Calendar_Version=3 serialsname='samsung' usbids=( ( 0x04e8, 0x6601, 1), ) deviceclasses=("modem", "serial") _supportedsyncs=() def __init__(self): com_phone.Profile.__init__(self) class Samsung_Calendar : _cal_alarm_values={ '0': -1, '1': 0, '2': 10, '3': 30, '4': 60 } def __init__(self, calendar_entry, new_date=None): self._start=self._end=self._alarm=self._desc=None self._extract_cal_info(calendar_entry, new_date) def _extract_cal_info(self, cal_entry, new_date): s=cal_entry.start if new_date is not None: s=new_date[:3]+s[3:] self._start=s self._end=cal_entry.end self._desc=cal_entry.description self._alarm='0' alarm=cal_entry.alarm _keys=self._cal_alarm_values.keys() _keys.sort() _keys.reverse() for k in _keys: if alarm>=self._cal_alarm_values[k]: self._alarm=k break def __lt__(self, rhs): return self.start<rhs.start def __le__(self, rhs): return self.start<=rhs.start def __eq__(self, rhs): return self.start==rhs.start def __ne__(self, rhs): return self.start!=rhs.start def __gt__(self, rhs): return self.start>rhs.start def __ge__(self, rhs): return self.start>=rhs.start def _get_start(self): return self._start start=property(fget=_get_start) def _get_end(self): return self._end end=property(fget=_get_end) def _get_desc(self): return self._desc description=property(fget=_get_desc) def _get_alarm(self): return self._alarm alarm=property(fget=_get_alarm) class MemoList (object) : _max_num_entries=10 _range_entries=xrange(_max_num_entries) _max_text_len=60 _max_subject_len=12 _max_num_of_fields=4 _text_index=3 _date_index=1 _max_write_fields=3 _write_entry_index=0 _write_date_index=1 _write_text_index=2 _continuation_char='-' def __init__(self, phone): self._phone=phone self._data={} def get(self): return copy.deepcopy(self._data, {}) def read(self): self._data={} text='' for i in self._range_entries: try: self._phone.progress(i, self._max_num_entries, 'Reading Memo Entry: '+str(i)) s=self._phone.get_memo_entry(i) if len(s)!=self._max_num_of_fields: continue t=s[self._text_index] if len(t)==self._max_text_len and \ t[-1]==self._continuation_char: text+=t[:len(t)-1] continue text+=t m=memo.MemoEntry() m.text=text m.set_date_isostr(s[self._date_index]) self._data[m.id]=m text='' except: if __debug__: raise def write(self): keys=self._data.keys() keys.sort() count=0 for k in keys: if count>=self._max_num_entries: self._phone.log('Max number of memos sent') break n=self._data[k] text=n.text subj=n.subject l=min(self._max_subject_len, len(text)) if subj[:l]!=text[:l]: text=subj+':'+text text.replace('"', '') while len(text) and count<self._max_num_entries: if len(text)>self._max_text_len: sub_text=text[:self._max_text_len-1]+self._continuation_char text=text[self._max_text_len-1:] else: sub_text=text text='' entry_str=['']*self._max_write_fields entry_str[self._write_entry_index]=`count` entry_str[self._write_date_index]=self._phone.get_time_stamp() entry_str[self._write_text_index]='"'+sub_text+'"' self._phone.progress(count, self._max_num_entries, 'Writing Memo Entry: '+str(count)) if self._phone.save_memo_entry(','.join(entry_str)): self._phone.log('Sent memo %s to the phone'%subj) count+=1 else: self._phone.log("Failed to send memo"+subj) for k in xrange(count, self._max_num_entries): self._phone.progress(k, self._max_num_entries, 'Deleing Memo Entry: '+str(k)) self._phone.save_memo_entry(`k`) def set(self, data): self._data={} self._data.update(data) class TodoList (object) : _td_max_read_fields=6 _td_max_write_fields=5 _td_max_entries=20 _td_entry=0 _td_priority=1 _td_due_datetime=2 _td_datetime_stamp=3 _td_status=4 _td_subject=5 _td_write_subject=4 _td_max_len_name=32 def __init__(self, phone, data={}): self._phone=phone self._data=data def get(self): return copy.deepcopy(self._data, {}) def _extract_fields(self, s): entry=todo.TodoEntry() i=int(s[self._td_priority]) if i: entry.priority=1 else: entry.priority=10 entry.due_date=s[self._td_due_datetime][:8] entry.summary=s[self._td_subject] return entry def read(self): self._data={} cnt=0 for i in xrange(self._td_max_entries): s=self._phone.get_todo_entry(i) if not len(s): self._phone.progress(i+1, self._td_max_entries, 'Getting blank entry: '+str(i)) continue self._phone.progress(i+1, self._td_max_entries, s[self._td_subject]) e=self._extract_fields(s) self._data[e.id]=e def _encode_fields(self, i, entry): e=['']*self._td_max_write_fields e[self._td_entry]=`i` if entry.priority is not None and entry.priority<5: e[self._td_priority]='1' else: e[self._td_priority]='0' s=entry.due_date if s is None or not len(s): s=self._phone.get_time_stamp() else: s+='T000000' e[self._td_due_datetime]=s e[self._td_datetime_stamp]=self._phone.get_time_stamp() e[self._td_write_subject]='"'+entry.summary+'"' return ','.join(e) def _write_entry(self, i, entry): return self._phone.save_todo_entry(self._encode_fields(i, entry)) def validate(self): for k,n in self._data.items(): name=n.summary.replace('"', '') if len(name)>self._td_max_len_name: name=name[:self._td_max_len_name] n.summary=name def write(self): keys=self._data.keys() keys.sort() cnt=0 for k in keys: n=self._data[k] if cnt>self._td_max_entries: break if self._write_entry(cnt, n): cnt += 1 else: self._phone.log('Failed to save todo entry '+str(k)) self._phone.progress(cnt, self._td_max_entries, 'Saving entry: '+n.summary) for i in xrange(cnt, self._td_max_entries): self._phone.progress(i, self._td_max_entries, 'Deleting entry: '+str(i)) self._phone.save_todo_entry(`i`) class SMS_Generic_List (object) : def __init__(self, phone): self._phone=phone self._data={} def get(self): return self._data.copy() def read(self): raise NotImplementedError class SMS_Inbox_List (SMS_Generic_List) : _max_entries=100 _valid_range=xrange(_max_entries) _datetime_index=3 _body_index=4 _callback_index=5 _field_num=6 def __init__(self, phone): super(SMS_Inbox_List, self).__init__(phone) def read(self): for i in self._valid_range: self._phone.progress(i, self._max_entries, 'Reading SMS Inbox Entry '+str(i)) s=self._phone.get_sms_inbox(i) if len(s)==self._field_num: e=sms.SMSEntry() e.folder=e.Folder_Inbox e.datetime=s[self._datetime_index] e._from, e.subject, txt=self._extract_body(s[self._body_index]) e.text=unicode(txt, errors='ignore') e.callback=s[self._callback_index] self._data[e.id]=e def _extract_body(self, s): try: _from=None l=s.split(' ') ss=l[0] if ss.find('@') != -1: _from=ss l=l[1:] ss=l[0] _subj=[] if ss[0]=='(': while l: _subj.append(ss) l=l[1:] if ss[-1]==')': break if l: ss=l[0] if l: return (_from, ' '.join(_subj), ' '.join(l)) else: return (_from, '', ' '.join(_subj)) except: return (None, '', s) class SMS_Saved_List (SMS_Generic_List) : _max_entries=20 _valid_range=xrange(_max_entries) _field_num=5 _datetime_index=1 _from_index=2 _body_index=4 def __init__(self, phone): super(SMS_Saved_List, self).__init__(phone) def read(self): for i in self._valid_range: self._phone.progress(i, self._max_entries, 'Reading SMS Saved Entry '+str(i)) s=self._phone.get_sms_saved(i) if len(s)==self._field_num: e=sms.SMSEntry() e.folder=e.Folder_Saved e.datetime=s[self._datetime_index] e._from=s[self._from_index] e.subject, txt=self._extract_body(s[self._body_index]) e.text=unicode(txt, errors='ignore') self._data[e.id]=e def _extract_body(self, s): try: l=s.split(' ') ss=l[0] _subj=[] if ss[0]=='(': while l: _subj.append(ss) l=l[1:] if ss[-1]==')': break if l: ss=l[0] if l: return (' '.join(_subj), ' '.join(l)) else: return ('', ' '.join(_subj)) except: return ('', s) class SMS_Sent_List (SMS_Generic_List) : _max_entries=100 _valid_range=xrange(_max_entries) _field_num=5 _datetime_index=1 _to_index=2 _from_index=3 _text_index=4 def __init__(self, phone): super(SMS_Sent_List, self).__init__(phone) def read(self): for i in self._valid_range: self._phone.progress(i, self._max_entries, 'Reading SMS Sent Entry '+str(i)) s=self._phone.get_sms_sent(i) if len(s)==self._field_num: e=sms.SMSEntry() e.folder=e.Folder_Sent e.datetime=s[self._datetime_index] e._to=s[self._to_index] e._from=s[self._from_index] e.text=unicode(s[self._text_index], errors='ignore') self._data[e.id]=e class SMSList (object) : def __init__(self, phone): self._phone=phone self._inbox=SMS_Inbox_List(phone) self._saved=SMS_Saved_List(phone) self._sent=SMS_Sent_List(phone) self._data={} def get(self): return self._data.copy() def read(self): self._inbox.read() self._data.update(self._inbox.get()) self._saved.read() self._data.update(self._saved.get()) self._sent.read() self._data.update(self._sent.get()) class CannedMsgList (SMS_Generic_List) : _max_entries=20 _valid_range=xrange(_max_entries) _field_num=4 _text_index=3 _data_key='canned_msg' _max_write_fields=3 _count_index=0 _timestamp_index=1 _write_text_index=2 def __init__(self, phone, data={}): super(CannedMsgList, self).__init__(phone) self._data=data def read(self): msg_list=[] for i in self._valid_range: self._phone.progress(i, self._max_entries, 'Reading SMS Canned Msg '+str(i)) s=self._phone.get_canned_msg(i) if len(s)==self._field_num: msg_list.append({'text': s[self._text_index], 'type': sms.CannedMsgEntry.user_type }) self._data=msg_list def get(self): return copy.deepcopy(self._data, _nil=[]) def validate(self): pass def write(self): msg_lst=[x['text'] for x in self._data if x['type']==sms.CannedMsgEntry.user_type] k=None for k,n in enumerate(msg_lst): if k>=self._max_entries: break n=n.replace('"', '') self._phone.progress(k, self._max_entries, 'Writing SMS Canned Msg '+str(k)) s=`k`+','+self._phone.get_time_stamp()+',"'+n+'"' if not self._phone.save_canned_msg(s): self._phone.log('Failed to write SMS Canned Msg entry: '+str(k)) if k is None: k=0 else: k+=1 for i in xrange(k, self._max_entries): self._phone.progress(i, self._max_entries, 'Deleting SMS Canned Msg entry: '+str(i)) self._phone.save_canned_msg(`i`)