Package londiste :: Module file_write
[frames] | no frames]

Source Code for Module londiste.file_write

 1   
 2  """Writes events into file.""" 
 3   
 4  import sys, os, skytools 
 5  from cStringIO import StringIO 
 6  from playback import * 
 7   
 8  __all__ = ['FileWrite'] 
 9   
10 -class FileWrite(Replicator):
11 """Writes events into file. 12 13 Incomplete implementation. 14 """ 15 16 last_successful_batch = None 17
18 - def load_state(self, batch_id):
19 # maybe check if batch exists on filesystem? 20 self.cur_tick = self.cur_batch_info['tick_id'] 21 self.prev_tick = self.cur_batch_info['prev_tick_id'] 22 return 1
23
24 - def process_batch(self, db, batch_id, ev_list):
25 pass
26
27 - def save_state(self, do_commit):
28 # nothing to save 29 pass
30
31 - def sync_tables(self, dst_db):
32 # nothing to sync 33 return 1
34
35 - def interesting(self, ev):
36 # wants all of them 37 return 1
38
39 - def handle_data_event(self, ev):
40 fmt = self.sql_command[ev.type] 41 sql = fmt % (ev.ev_extra1, ev.data) 42 row = "%s -- txid:%d" % (sql, ev.txid) 43 self.sql_list.append(row) 44 ev.tag_done()
45
46 - def handle_system_event(self, ev):
47 row = "-- sysevent:%s txid:%d data:%s" % ( 48 ev.type, ev.txid, ev.data) 49 self.sql_list.append(row) 50 ev.tag_done()
51
52 - def flush_sql(self):
53 self.sql_list.insert(0, "-- tick:%d prev:%s" % ( 54 self.cur_tick, self.prev_tick)) 55 self.sql_list.append("-- end_tick:%d\n" % self.cur_tick) 56 # store result 57 dir = self.cf.get("file_dst") 58 fn = os.path.join(dir, "tick_%010d.sql" % self.cur_tick) 59 f = open(fn, "w") 60 buf = "\n".join(self.sql_list) 61 f.write(buf) 62 f.close()
63 64 if __name__ == '__main__': 65 script = Replicator(sys.argv[1:]) 66 script.start() 67