Module prototypes_moto
[hide private]
[frames] | no frames]

Source Code for Module prototypes_moto

  1  ### BITPIM 
  2  ### 
  3  ### Copyright (C) 2006 Joe Pham <djpham@bitpim.org> 
  4  ### 
  5  ### This program is free software; you can redistribute it and/or modify 
  6  ### it under the terms of the BitPim license as detailed in the LICENSE file. 
  7  ### 
  8  ### $Id: prototypes_moto.py 3460 2006-07-08 23:55:09Z djpham $ 
  9   
 10  """Implement specific prototypes class for Motorola phones""" 
 11   
 12  import re 
 13   
 14  import prototypes 
 15   
16 -class CAL_DATE(prototypes.CSVSTRING):
17 """Dates used for Calendar Events (mm-dd-yyyy)"""
18 - def __init__(self, *args, **kwargs):
19 super(CAL_DATE, self).__init__(*args, **kwargs) 20 self._valuedate=(0, 0, 0) # y,m,d 21 if self._ismostderived(CAL_DATE): 22 self._update(args, kwargs)
23
24 - def _converttostring(self, date):
25 s='' 26 if len(date)>=3: 27 year,month,day=date[:3] 28 if month>0 or day>0 or year>0: 29 s='%2.2d-%2.2d-%4.4d'%(month, day, year) 30 return s
31
32 - def _update(self, args, kwargs):
33 for k in ('constant', 'default', 'value'): 34 if kwargs.has_key(k): 35 kwargs[k]=self._converttostring(kwargs[k]) 36 if len(args)==0: 37 pass 38 elif len(args)==1: 39 args=(self._converttostring(args[0]),) 40 else: 41 raise TypeError("expected (year,month,day) as arg") 42 super(CAL_DATE, self)._update(args, kwargs) 43 self._complainaboutunusedargs(CAL_DATE, kwargs)
44
45 - def getvalue(self):
46 s=super(CAL_DATE, self).getvalue() 47 val=s.split('-') 48 if len(val)<2: 49 year=0 50 month=0 51 day=0 52 else: 53 year=int(val[2]) 54 month=int(val[0]) 55 day=int(val[1]) 56 return (year, month, day)
57
58 -class CAL_TIME(prototypes.CSVSTRING):
59 """Times used for Calendar Events (hh:mm)"""
60 - def __init__(self, *args, **kwargs):
61 super(CAL_TIME, self).__init__(*args, **kwargs) 62 self._valuetime=(0, 0) # h,m 63 if self._ismostderived(CAL_TIME): 64 self._update(args, kwargs)
65
66 - def _converttostring(self, date):
67 s='' 68 if len(date)>=2: 69 s='%2.2d:%2.2d'%tuple(date[:2]) 70 return s
71
72 - def _update(self, args, kwargs):
73 for k in ('constant', 'default', 'value'): 74 if kwargs.has_key(k): 75 kwargs[k]=self._converttostring(kwargs[k]) 76 if len(args)==0: 77 pass 78 elif len(args)==1: 79 args=(self._converttostring(args[0]),) 80 else: 81 raise TypeError("expected (hour, min) as arg") 82 super(CAL_TIME, self)._update(args, kwargs) 83 self._complainaboutunusedargs(CAL_TIME, kwargs)
84
85 - def getvalue(self):
86 s=super(CAL_TIME, self).getvalue() 87 val=s.split(':') 88 if len(val)==2: 89 return (int(val[0]), int(val[1])) 90 return (0, 0)
91
92 -class M_SMSDATETIME(prototypes.CSVSTRING):
93 """ Represent date time with the format 'yyyy/M+/d+,h+:m+:s+' used 94 by Motorola SMS messages. 95 Currently works only 1 way: SMS Date Time -> ISO String 96 """ 97 _re_pattern='^\d\d+/\d+/\d+,\d+:\d+:\d+$' 98 _re_compiled_pattern=None
99 - def __init__(self, *args, **kwargs):
100 if M_SMSDATETIME._re_compiled_pattern is None: 101 M_SMSDATETIME._re_compiled_pattern=re.compile(M_SMSDATETIME._re_pattern) 102 super(M_SMSDATETIME, self).__init__(*args, **kwargs) 103 if self._ismostderived(M_SMSDATETIME): 104 self._update(args, kwargs)
105
106 - def _update(self, args, kwargs):
107 super(M_SMSDATETIME, self)._update(args, kwargs) 108 # strip blanks, and replace quotechar 109 if self._value: 110 self._value=self._value.strip(' ').replace('"', '') 111 if self._value and \ 112 not re.match(M_SMSDATETIME._re_compiled_pattern, self._value): 113 raise ValueError('Correct Format: [yy]yy/[M]M/[d]d,[h]h:[m]m:[s]s')
114
115 - def getvalue(self):
116 """Returns the ISO Format 'yyyyMMddThhmmss""" 117 if self._value: 118 _d,_t=self._value.strip(' ').replace('"', '').split(',') 119 _d=_d.split('/') 120 _t=_t.split(':') 121 return '%s%s%sT%s%s%s'%(_d[0].zfill(4), _d[1].zfill(2), _d[2].zfill(2), 122 _t[0].zfill(2), _t[1].zfill(2), _t[2].zfill(2))
123