Module ExampleModule
[hide private]

Source Code for Module ExampleModule

  1  """ 
  2  An Example DataSource and DataType implementation. 
  3  """ 
  4  import random 
  5  import logging 
  6  log = logging.getLogger("modules.Example") 
  7   
  8  import conduit 
  9  import conduit.Exceptions as Exceptions 
 10  import conduit.Utils as Utils 
 11  import conduit.datatypes.DataType as DataType 
 12  import conduit.dataproviders.DataProvider as DataProvider 
 13   
 14  MODULES = { 
 15      "ExampleDataProviderTwoWay" :   { "type": "dataprovider" }, 
 16      "ExampleConverter"          :   { "type": "converter" } 
 17  } 
 18   
19 -class ExampleDataProviderTwoWay(DataProvider.TwoWay):
20 """ 21 An example dataprovider demonstrating how to partition 22 funtionality in such a way 23 """ 24 25 _name_ = "Example Dataprovider" 26 _description_ = "Demonstrates a Twoway Dataprovider" 27 _category_ = conduit.dataproviders.CATEGORY_MISC 28 _module_type_ = "twoway" 29 _in_type_ = "exampledata" 30 _out_type_ = "exampledata" 31 _icon_ = "applications-internet" 32 33 DEFAULT_FOO_VALUE = 42 34
35 - def __init__(self):
36 """ 37 Constructor should call the base constructor and initialize 38 all variables that are restored from configuration 39 """ 40 DataProvider.TwoWay.__init__(self) 41 self.data = [] 42 self.foo = 0
43
44 - def _data_exists(self, LUID):
45 """ 46 @returns: True if data at the LUID exists 47 """ 48 return random.randint(0,1)
49
50 - def _get_data(self, LUID):
51 """ 52 @returns: A ExampleDataType with the specified LUID 53 """ 54 data = ExampleDataType( 55 uri=LUID, 56 data=self.foo*random.randint(1,100) 57 ) 58 return data
59
60 - def _put_data(self, data):
61 """ 62 @returns: Rid 63 """ 64 data = ExampleDataType( 65 uri=random.randint(1,100), 66 data=self.foo*random.randint(1,100) 67 ) 68 return data.get_rid()
69
70 - def _replace_data(self, LUID, data):
71 """ 72 Some dataproviders assign a new LUID when data is replaced. This 73 is the purpose of having replace in a different function to _put 74 """ 75 data.set_UID(random.randint(1,100)) 76 return data.get_rid()
77
78 - def configure(self, window):
79 """ 80 Uses the L{conduit.DataProvider.DataProviderSimpleConfigurator} class 81 to show a simple configuration dialog which is just a gtk.Enry 82 where the user can enter one or more GNOME wiki pages names, 83 seperated by commas 84 85 @param window: The parent window (used for modal dialogs) 86 @type window: C{gtk.Window} 87 """ 88 #lazy import gtk so if conduit is run from command line arg, or 89 #a non gtk system, this dp will still load. There should be no need 90 #to use gtk outside of this function 91 import gtk 92 import conduit.gtkui.SimpleConfigurator as SimpleConfigurator 93 94 def set_foo(param): 95 self.foo = int(param)
96 97 items = [ 98 { 99 "Name" : "Value of Foo:", 100 "Widget" : gtk.Entry, 101 "Callback" : set_foo, 102 "InitialValue" : str(self.foo) 103 } 104 ] 105 #We just use a simple configuration dialog 106 dialog = SimpleConfigurator.SimpleConfigurator(window, self._name_, items) 107 #This call blocks 108 dialog.run()
109
110 - def refresh(self):
111 """ 112 The refresh method should do whatever is needed to ensure that a 113 subseqent call to get_all returns the correct result. 114 115 The refresh method is always called before the sync step. DataSources 116 should always call the base classes refresh() method. 117 """ 118 DataProvider.TwoWay.refresh(self) 119 self.data = [str(random.randint(1,100)) for i in range(10)]
120
121 - def get_all(self):
122 """ 123 Returns the LUIDs of all items to synchronize. 124 DataSources should