def __init__(self, func, delay, args=()): """The constructor. @param func: logger function to be used @param delay: how many seconds between each scan @args: optional arguments to be passed """ ### List of arguments for functions self.args = args ### Any function to be called periodically by the task self.function = func ### The schedular delay for the background task in seconds self.delay = delay ### Compute future time of scheduler self.next_run = time.time() + self.delay ## This is logging everything in the daemon log file with proper timestamps ## If you don't need logging just disable it from the myLogger decorator make_sure_path_exists('logs') logging.basicConfig(filename=os.path.join('logs', 'daemon.log'), level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') self.folder=FolderScanner(self.__configFolders) self.database=FileIndex() self.comm=Client() self.loadConfig()
class Task(object): """Task is a generic task for indexing your folder structure Called every often to update the database. """ ## Filename containing the list of folders to scan __configFolders=os.path.join('config', 'folders.jsn') ## Filename containg the parameters __configParams=os.path.join('config', 'parser.ini') def __init__(self, func, delay, args=()): """The constructor. @param func: logger function to be used @param delay: how many seconds between each scan @args: optional arguments to be passed """ ### List of arguments for functions self.args = args ### Any function to be called periodically by the task self.function = func ### The schedular delay for the background task in seconds self.delay = delay ### Compute future time of scheduler self.next_run = time.time() + self.delay ## This is logging everything in the daemon log file with proper timestamps ## If you don't need logging just disable it from the myLogger decorator make_sure_path_exists('logs') logging.basicConfig(filename=os.path.join('logs', 'daemon.log'), level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') self.folder=FolderScanner(self.__configFolders) self.database=FileIndex() self.comm=Client() self.loadConfig() def loadTestConfig(self): """Load a default test folder for Unit Testing The configuration should contain a path for TestFolder """ Config = ConfigParser.ConfigParser() Config.read(self.__configParams) self.top=Config.get("TestFolder", "Path") self.delay=Config.get("Params", "Frequency") self.delay=float(self.delay) logging.info('Scanning test folder ...') def loadConfig(self): """Load a generic test folder The configuration contains a rule set for the folder structure """ Config = ConfigParser.ConfigParser() Config.read(self.__configParams) self.delay=Config.get("Params", "Frequency") self.delay=float(self.delay) logging.info('Scanning projects ...') def compute(self): """Load a generic test folder The configuration contains a rule set for the folder structure """ ## Get the list of files self.list=self.folder.GetAutoList() if self.list==None: return 0 summary={} ## compute lines of code and density for filepath, size in self.list.items(): reader=FileReader(filepath) ## compute the number of new lines in the file currentLines=reader.GetLines() ## extrapolate the extension from the full file name path extension=os.path.splitext(filepath)[1][1:].strip() ## if the extension is the one we want to consider then sum the lines if extension in summary.keys(): summary[extension]+=currentLines ## otherwise keep the same number of lines else: summary[extension]=currentLines ## Add the entry into the database containg the full path, the file extension, ## number of lines and size self.database.addEntry(filepath, extension,currentLines, size) ## if we want a summary POST the results into the server if summary: self.UpdateResults(summary) ## log the summary if necessary logging.info(''.join(['File with .%s contains %s lines' % (key, value) for (key, value) in summary.items()])) def UpdateResults(self, summary): """Push the code summary on the RESTful server online""" ## TODO: we are not posting anything yet the server is offline # self.comm.postStats(summary) def shouldRun(self): """Generic method to verify if the task needs to run Check the current time and verify if required run() """ return time.time() >= self.next_run def run(self): """Task run method Call the provided function, compute the code size and schedule the next with the provided delay """ self.function(*(self.args)) self.compute() self.next_run += self.delay