def _standby(self, sleepTime): for second in xrange(int(sleepTime / 2)): time.sleep(2) if (not self._running) or self._runNow: log("Who dares to break the slumber of %s the great thread!" % self.name) break
def foldersAtPath(path, includeParent=False, addFiles=False): """ Returns a list of dictionaries with the folders contained at the given path Give the empty string as the path to list the contents of the root path under Unix this means "/", on Windows this will be a list of drive letters) """ assert os.path.isabs(path) or path == "" # walk up the tree until we find a valid path while path and not os.path.isdir(path): if path == os.path.dirname(path): path = '' break else: path = os.path.dirname(path) if path == "": if os.name == 'nt': entries = [{'current_path': 'Root'}] for letter in getWinDrives(): letterPath = letter + ':\\' entries.append({ 'name': letterPath, 'path': letterPath, 'isPath': True }) return entries else: path = '/' # fix up the path and find the parent path = os.path.abspath(os.path.normpath(path)) parentPath = os.path.dirname(path) # if we're at the root then the next step is the meta-node showing our drive letters if path == parentPath and os.name == 'nt': parentPath = "" fileList = [{ 'name': filename, 'path': os.path.join(path, filename), 'isPath': os.path.isdir(os.path.join(path, filename)) } for filename in os.listdir(path)] fileList = sorted( fileList, lambda x, y: cmp( os.path.basename(x['name']).lower(), os.path.basename(y['path']).lower())) finalFileList = filter(lambda entry: entry['isPath'], fileList) # always add folders if addFiles: logger.log("adding files") finalFileList.extend( filter(lambda entry: not entry['isPath'], fileList)) # add files entries = [{'current_path': path}] if includeParent and parentPath != path: entries.append({'name': "..", 'path': parentPath, 'isPath': True}) entries.extend(finalFileList) return entries
def foldersAtPath(path, includeParent=False, addFiles=False): """ Returns a list of dictionaries with the folders contained at the given path Give the empty string as the path to list the contents of the root path under Unix this means "/", on Windows this will be a list of drive letters) """ assert os.path.isabs(path) or path == "" # walk up the tree until we find a valid path while path and not os.path.isdir(path): if path == os.path.dirname(path): path = '' break else: path = os.path.dirname(path) if path == "": if os.name == 'nt': entries = [{'current_path': 'Root'}] for letter in getWinDrives(): letterPath = letter + ':\\' entries.append({'name': letterPath, 'path': letterPath, 'isPath': True}) return entries else: path = '/' # fix up the path and find the parent path = os.path.abspath(os.path.normpath(path)) parentPath = os.path.dirname(path) # if we're at the root then the next step is the meta-node showing our drive letters if path == parentPath and os.name == 'nt': parentPath = "" fileList = [{ 'name': filename, 'path': os.path.join(path, filename), 'isPath': os.path.isdir(os.path.join(path, filename)) } for filename in os.listdir(path)] fileList = sorted(fileList, lambda x, y: cmp(os.path.basename(x['name']).lower(), os.path.basename(y['path']).lower())) finalFileList = filter(lambda entry: entry['isPath'], fileList) # always add folders if addFiles: logger.log("adding files") finalFileList.extend(filter(lambda entry: not entry['isPath'], fileList)) # add files entries = [{'current_path': path}] if includeParent and parentPath != path: entries.append({ 'name': "..", 'path': parentPath, 'isPath': True}) entries.extend(finalFileList) return entries
def run(self): if self._initdelay: self._standby(self._initdelay) self._runtime = time.time() while self._running: start = time.time() if not (xdm.xdm_states[0] in xdm.common.STATES or\ xdm.xdm_states[1] in xdm.common.STATES or\ xdm.xdm_states[3] in xdm.common.STATES or\ xdm.xdm_states[6] in xdm.common.STATES): self._sleeping = 0 self._blockCount = 0 self._neverRun = False self._runNow = False self._lastRun = datetime.datetime.now() try: self._action() # this is here to test the failMessage gui """if 'coreUpdateCheck' == self.name and not self._fails: raise AttributeError("fake error on first run")""" except: self._fails += 1 self._failMessage = log.error('Error in the scheduler thread of %s. %s fails so far.' % (self.name, self._fails)) if self._fails >= MAXIMUM_FAILS: log.error('This scheduler %s is dead to me!' % self.name) self._running = 0 break else: self._blockCount += 1 log("XDM is in state %s not running action: %s" % (xdm.common.STATES, self.name)) self._runtime += self._loopdelay self._sleeping = 1 if self._neverRun and self._initdelay: # this will create times like -10 and -20 later this will be -(-20) = 20 timeDelta = min(5 * 60, (self._blockCount * self._initdelay)) sleepTime = max(0, self._initdelay + timeDelta) else: # this will reduce the _loopdelay by half for each time we have been blocked # e.g. looptime = 120s _blockCount = 1 # = minus 60s # e.g. looptime = 120s _blockCount = 2 # = minus 90s # the key is the 0.5 ** _blockCount ... ** means "to the power of" timeDelta = self._loopdelay - ((0.5 ** self._blockCount) * self._loopdelay) sleepTime = max(0, (self._runtime - start) - timeDelta) if self._blockCount: # these lines are from hell if self._neverRun: log("Adding %s seconds because %s has been blocked, before it's first run, %s times. I will sleep for %ss instead of %ss thats %s%% if the normal time" % \ (timeDelta, self.name, self._blockCount, sleepTime, self._initdelay, int((sleepTime / self._initdelay) * 100))) else: log("Removing %s seconds because %s has been blocked %s times. I will sleep for %ss instead of %ss thats %s%% if the normal time" % \ (timeDelta, self.name, self._blockCount, sleepTime, self._loopdelay, 100 - int((sleepTime / self._loopdelay) * 100))) self._nextRun = self._calcNextRun(sleepTime) self._standby(sleepTime)
def run(self): if self._initdelay: self._standby(self._initdelay) self._runtime = time.time() while self._running: start = time.time() if not (xdm.xdm_states[0] in xdm.common.STATES or\ xdm.xdm_states[1] in xdm.common.STATES or\ xdm.xdm_states[3] in xdm.common.STATES or\ xdm.xdm_states[6] in xdm.common.STATES): self._sleeping = 0 self._blockCount = 0 self._neverRun = False self._runNow = False self._lastRun = datetime.datetime.now() try: self._action() # this is here to test the failMessage gui """if 'coreUpdateCheck' == self.name and not self._fails: raise AttributeError("fake error on first run")""" except: self._fails += 1 self._failMessage = log.error( 'Error in the scheduler thread of %s. %s fails so far.' % (self.name, self._fails)) if self._fails >= MAXIMUM_FAILS: log.error('This scheduler %s is dead to me!' % self.name) self._running = 0 break else: self._blockCount += 1 log("XDM is in state %s not running action: %s" % (xdm.common.STATES, self.name)) self._runtime += self._loopdelay self._sleeping = 1 if self._neverRun and self._initdelay: # this will create times like -10 and -20 later this will be -(-20) = 20 timeDelta = min(5 * 60, (self._blockCount * self._initdelay)) sleepTime = max(0, self._initdelay + timeDelta) else: # this will reduce the _loopdelay by half for each time we have been blocked # e.g. looptime = 120s _blockCount = 1 # = minus 60s # e.g. looptime = 120s _blockCount = 2 # = minus 90s # the key is the 0.5 ** _blockCount ... ** means "to the power of" timeDelta = self._loopdelay - ( (0.5**self._blockCount) * self._loopdelay) sleepTime = max(0, (self._runtime - start) - timeDelta) if self._blockCount: # these lines are from hell if self._neverRun: log("Adding %s seconds because %s has been blocked, before it's first run, %s times. I will sleep for %ss instead of %ss thats %s%% if the normal time" % \ (timeDelta, self.name, self._blockCount, sleepTime, self._initdelay, int((sleepTime / self._initdelay) * 100))) else: log("Removing %s seconds because %s has been blocked %s times. I will sleep for %ss instead of %ss thats %s%% if the normal time" % \ (timeDelta, self.name, self._blockCount, sleepTime, self._loopdelay, 100 - int((sleepTime / self._loopdelay) * 100))) self._nextRun = self._calcNextRun(sleepTime) self._standby(sleepTime)