/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

faker.py
Go to the documentation of this file.
00001 import os, logging, time
00002 from datetime import datetime
00003 
00004 log = logging.getLogger(__name__)
00005 
00006 
00007 class Faker(list):
00008     """
00009     create fake source instances and insert them 
00010     """
00011     def __init__(self, srcs, cfg ):
00012         self.cfg = cfg
00013         log.info("faking srcs %s from %s  " % ( srcs, srcs[0].db ) )
00014         for src in srcs:
00015             self.append(src)
00016         #
00017         self.sleep = cfg.pop('sleep') 
00018         self.interval = cfg.pop('interval') 
00019         self.timestart = cfg.pop('faker_timestart')
00020 
00021         self.maxiter = cfg.pop('maxiter')
00022         self.profile = cfg.pop('profile', None)  
00023 
00024         if len(cfg)>0:
00025             log.warn("%s unpopped cfg %r " % (self.__class__.__name__, cfg ) )
00026 
00027 
00028     def _insertfake(self, index ):
00029         """
00030         :param index:  None or zero based fake index, typically the iteration loop counter 
00031 
00032         An index of `None` causes and SA source query to be performed to determine the 
00033         `id` of the last source entry for each source. 
00034         """
00035         for src in self:
00036             
00037             inst = src()
00038             if index == None:        
00039                 last = src.last()   
00040                 id = 1000 if last == None else last.id
00041                 id += 1
00042             else:
00043                 id = index  
00044             dt = self.timestart + (index*self.interval)
00045 
00046             #
00047             # set defaults here, users can override if needed 
00048             # NOPE :. not so simple as inst could be a join with multiple id/date_time to set 
00049             # so for now leave it to specialized code
00050             #
00051             # setattr( inst , 'id' , id ) 
00052             # setattr( inst , 'date_time' , dt ) 
00053             #
00054 
00055             self.fake( inst , id , dt )
00056 
00057             log.info("IF %-3d %s %s %r " % (id, inst.date_time.strftime("%Y-%m-%d %H:%M:%S"), src.__name__, filter(lambda _:type(_) != datetime, inst.asdict.values() )))
00058             db = src.db
00059             db.add(inst)   
00060             db.commit()
00061 
00062     def __repr__(self):
00063         return "%s %s " % ( self.__class__.__name__, self.sleep )
00064 
00065     def __call__(self): 
00066 
00067         i = 0 
00068         while i<self.maxiter or self.maxiter==0:
00069             self._insertfake(i)
00070             time.sleep(self.sleep.seconds)
00071             i += 1
00072 
00073 
00074 
00075 if __name__ == '__main__':
00076     pass
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:50:03 for Scraper by doxygen 1.7.4