Package conduit :: Package datatypes :: Module DataType
[hide private]

Source Code for Module conduit.datatypes.DataType

  1  import logging 
  2  log = logging.getLogger("datatypes.DataType") 
  3   
  4  import conduit.datatypes 
  5   
  6  CHANGE_UNMODIFIED = 0 
  7  CHANGE_ADDED = 1 
  8  CHANGE_MODIFIED = 2 
  9  CHANGE_DELETED = 3 
 10   
11 -class DataType(object):
12 """ 13 Base DataType which represents any thing 14 which can be synchronized between two DataProviders 15 16 @cvar _name_: The name of the type 17 @type _name_: C{string} 18 @ivar URI: A URI which uniquely represents the location of the datatype. 19 @type URI: C{string} 20 @ivar UID: A Unique identifier for this type. This is particuarly 21 neccessary on types that are used in two-way sync. 22 @type UID: C{string} 23 """ 24 25 _name_ = "" 26
27 - def __init__(self):
28 self.change_type = CHANGE_UNMODIFIED 29 30 self._original_URI = None 31 self._mtime = None 32 self._UID = None 33 self._tags = ()
34
35 - def get_type(self):
36 """ 37 @returns: The type (name) of this datatype 38 @rtype: C{string} 39 """ 40 return self._name_
41
42 - def compare(self, B):
43 """ 44 Comparison function to be overridden by datatypes who support two 45 way synchronisation. 46 47 This funcion should compare self with B. All answers 48 are from the perspective of the me (the instance) 49 50 - C{conduit.datatypes.COMPARISON_NEWER} This means the I am newer than B 51 - C{conduit.datatypes.COMPARISON_EQUAL} This means the we are equal 52 - L{conduit.datatypes.COMPARISON_OLDER} This means the I am older than B 53 - L{conduit.datatypes.COMPARISON_UNKNOWN} This means we were unable to determine 54 which was newer than the other so its up to the user to decide 55 """ 56 log.debug("COMPARE: %s <----> %s " % (self.get_UID(), B.get_UID())) 57 58 if self.get_rid() == B.get_rid(): 59 return conduit.datatypes.COMPARISON_EQUAL 60 61 mtime1 = self.get_mtime() 62 mtime2 = B.get_mtime() 63 64 if mtime1 == None or mtime2 == None: 65 return conduit.datatypes.COMPARISON_UNKNOWN 66 67 if mtime1 > mtime2: 68 return conduit.datatypes.COMPARISON_NEWER 69 else: 70 return conduit.datatypes.COMPARISON_OLDER
71
72 - def get_hash(self):
73 raise NotImplementedError
74
75 - def get_UID(self):
76 """ 77 Returns the UID of the datatype 78 """ 79 return self._UID
80
81 - def set_UID(self, UID):
82 """ 83 Sets the UID of the datatype 84 @type UID: C{string} 85 """ 86 self._UID = UID
87
88 - def set_mtime(self, mtime):
89 """ 90 Sets the modification time of the datatype. 91 @type mtime: C{datetime.datetime} 92 """ 93 self._mtime = mtime
94
95 - def get_mtime(self):
96 """ 97 @returns: The file modification time (or None) as a python datetime object 98 @rtype: C{datetime.datetime} 99 """ 100 return self._mtime
101
102 - def get_tags(self):
103 """ 104 @returns: the current list of tags 105 """ 106 return self._tags
107
108 - def set_tags(self, tags):
109 """ 110 Sets the tags of the datatype 111 """ 112 self._tags = tags
113
114 - def get_open_URI(self):
115 """ 116 @returns: The URI that can be opened through gnome-open (or None) 117 """ 118 return self._original_URI
119
120 - def set_open_URI(self, URI):
121 """ 122 Saves the URI that can be opened through gnome-open 123 """ 124 self._original_URI = URI
125
126 - def get_snippet(self):
127 """ 128 Returns a small representation of the data that may be shown to the 129 user. Derived types may override this function. 130 """ 131 s = "" 132 uri = self.get_open_URI() 133 mtime = self.get_mtime() 134 135 if uri != None: 136 s += "%s" % uri 137 if mtime != None: 138 s += " (%s)" % mtime.strftime("%c") 139 140 if s == "": 141 s += "%s" % str(self) 142 else: 143 s += "\n%s" % str(self) 144 145 return s
146
147 - def get_rid(self):
148 """ 149 @returns: The record identifier (Rid) for this data 150 """ 151 log.debug("Getting Rid for %s" % self.get_UID()) 152 rid = conduit.datatypes.Rid( 153 uid=self.get_UID(), 154 mtime=self.get_mtime(), 155 hash=self.get_hash() 156 ) 157 return rid
158
159 - def __getstate__(self):
160 """ 161 Store the object state in a dict for pickling 162 """ 163 data = {} 164 data['mtime'] = self.get_mtime() 165 data['uid'] = self.get_UID() 166 data['open_uri'] = self.get_open_URI() 167 data['tags'] = self.get_tags() 168 return data
169
170 - def __setstate__(self, data):
171 """ 172 Set object state from dict (after unpickling) 173 """ 174 self.set_mtime(data['mtime']) 175 self.set_UID(data['uid']) 176 self.set_open_URI(data['open_uri']) 177 self.set_tags(data['tags'])
178