PyXR

c:\projects\bitpim\src \ prototypes_moto.py



0001 ### BITPIM
0002 ###
0003 ### Copyright (C) 2006 Joe Pham <djpham@bitpim.org>
0004 ###
0005 ### This program is free software; you can redistribute it and/or modify
0006 ### it under the terms of the BitPim license as detailed in the LICENSE file.
0007 ###
0008 ### $Id: prototypes_moto.py 3460 2006-07-08 23:55:09Z djpham $
0009 
0010 """Implement specific prototypes class for Motorola phones"""
0011 
0012 import re
0013 
0014 import prototypes
0015 
0016 class CAL_DATE(prototypes.CSVSTRING):
0017     """Dates used for Calendar Events (mm-dd-yyyy)"""
0018     def __init__(self, *args, **kwargs):
0019         super(CAL_DATE, self).__init__(*args, **kwargs)
0020         self._valuedate=(0, 0, 0) # y,m,d
0021         if self._ismostderived(CAL_DATE):
0022             self._update(args, kwargs)
0023 
0024     def _converttostring(self, date):
0025         s=''
0026         if len(date)>=3:
0027             year,month,day=date[:3]
0028             if month>0 or day>0 or year>0:
0029                 s='%2.2d-%2.2d-%4.4d'%(month, day, year)
0030         return s
0031 
0032     def _update(self, args, kwargs):
0033         for k in ('constant', 'default', 'value'):
0034             if kwargs.has_key(k):
0035                 kwargs[k]=self._converttostring(kwargs[k])
0036         if len(args)==0:
0037             pass
0038         elif len(args)==1:
0039             args=(self._converttostring(args[0]),)
0040         else:
0041             raise TypeError("expected (year,month,day) as arg")
0042         super(CAL_DATE, self)._update(args, kwargs)
0043         self._complainaboutunusedargs(CAL_DATE, kwargs)
0044 
0045     def getvalue(self):
0046         s=super(CAL_DATE, self).getvalue()
0047         val=s.split('-')
0048         if len(val)<2:
0049             year=0
0050             month=0
0051             day=0
0052         else:
0053             year=int(val[2])
0054             month=int(val[0])
0055             day=int(val[1])
0056         return (year, month, day)
0057 
0058 class CAL_TIME(prototypes.CSVSTRING):
0059     """Times used for Calendar Events (hh:mm)"""
0060     def __init__(self, *args, **kwargs):
0061         super(CAL_TIME, self).__init__(*args, **kwargs)
0062         self._valuetime=(0, 0) # h,m
0063         if self._ismostderived(CAL_TIME):
0064             self._update(args, kwargs)
0065 
0066     def _converttostring(self, date):
0067         s=''
0068         if len(date)>=2:
0069             s='%2.2d:%2.2d'%tuple(date[:2])
0070         return s
0071 
0072     def _update(self, args, kwargs):
0073         for k in ('constant', 'default', 'value'):
0074             if kwargs.has_key(k):
0075                 kwargs[k]=self._converttostring(kwargs[k])
0076         if len(args)==0:
0077             pass
0078         elif len(args)==1:
0079             args=(self._converttostring(args[0]),)
0080         else:
0081             raise TypeError("expected (hour, min) as arg")
0082         super(CAL_TIME, self)._update(args, kwargs)
0083         self._complainaboutunusedargs(CAL_TIME, kwargs)
0084 
0085     def getvalue(self):
0086         s=super(CAL_TIME, self).getvalue()
0087         val=s.split(':')
0088         if len(val)==2:
0089             return (int(val[0]), int(val[1]))
0090         return (0, 0)
0091 
0092 class M_SMSDATETIME(prototypes.CSVSTRING):
0093     """ Represent date time with the format 'yyyy/M+/d+,h+:m+:s+' used
0094     by Motorola SMS messages.
0095     Currently works only 1 way: SMS Date Time -> ISO String
0096     """
0097     _re_pattern='^\d\d+/\d+/\d+,\d+:\d+:\d+$'
0098     _re_compiled_pattern=None
0099     def __init__(self, *args, **kwargs):
0100         if M_SMSDATETIME._re_compiled_pattern is None:
0101             M_SMSDATETIME._re_compiled_pattern=re.compile(M_SMSDATETIME._re_pattern)
0102         super(M_SMSDATETIME, self).__init__(*args, **kwargs)
0103         if self._ismostderived(M_SMSDATETIME):
0104             self._update(args, kwargs)
0105 
0106     def _update(self, args, kwargs):
0107         super(M_SMSDATETIME, self)._update(args, kwargs)
0108         # strip blanks, and replace quotechar
0109         if self._value:
0110             self._value=self._value.strip(' ').replace('"', '')
0111         if self._value and \
0112            not re.match(M_SMSDATETIME._re_compiled_pattern, self._value):
0113             raise ValueError('Correct Format: [yy]yy/[M]M/[d]d,[h]h:[m]m:[s]s')
0114 
0115     def getvalue(self):
0116         """Returns the ISO Format 'yyyyMMddThhmmss"""
0117         if self._value:
0118             _d,_t=self._value.strip(' ').replace('"', '').split(',')
0119             _d=_d.split('/')
0120             _t=_t.split(':')
0121             return '%s%s%sT%s%s%s'%(_d[0].zfill(4), _d[1].zfill(2), _d[2].zfill(2),
0122                                     _t[0].zfill(2), _t[1].zfill(2), _t[2].zfill(2))
0123 

Generated by PyXR 0.9.4