diff options
| author | 2018-04-03 06:50:04 +0200 | |
|---|---|---|
| committer | 2018-04-03 06:50:04 +0200 | |
| commit | dc580be8f9ef38a1c0903820b04e1b5c7217da16 (patch) | |
| tree | 4a214d88d3e094efdb9e4ff70920537a4d33ae9b /minidinstall/Dnotify.py | |
| parent | 23ac25c0b388b5ffebf66154b12a3950b89b977a (diff) | |
Various improvements in coding style.
Diffstat (limited to 'minidinstall/Dnotify.py')
| -rw-r--r-- | minidinstall/Dnotify.py | 49 |
1 files changed, 24 insertions, 25 deletions
diff --git a/minidinstall/Dnotify.py b/minidinstall/Dnotify.py index 18606e1..cdc2c48 100644 --- a/minidinstall/Dnotify.py +++ b/minidinstall/Dnotify.py @@ -2,7 +2,7 @@ # A simple FAM-like beast in Python -# Copyright © 2002 Colin Walters <walters@gnu.org> +# Copyright (c) 2002 Colin Walters <walters@gnu.org> # This file is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by @@ -18,9 +18,9 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -import os, re, sys, string, stat, threading, queue, time +import os, stat, threading, queue, time import logging -from minidinstall import misc +from . import misc class DnotifyException(Exception): def __init__(self, value): @@ -29,7 +29,7 @@ class DnotifyException(Exception): return repr(self._value) class DirectoryNotifierFactory: - def create(self, dirs, use_dnotify=1, poll_time=30, logger=None, cancel_event=None): + def create(self, dirs, use_dnotify=False, poll_time=30, logger=None, cancel_event=None): if use_dnotify and os.access('/usr/bin/dnotify', os.X_OK): if logger: logger.debug("Using dnotify directory notifier") @@ -47,11 +47,11 @@ class DirectoryNotifier: def __init__(self, dirs, logger, cancel_event=None): self._cwd = os.getcwd() self._dirs = dirs - if cancel_event is None: + if not cancel_event: self._cancel_event = threading.Event() else: self._cancel_event = cancel_event - if logger is None: + if not logger: self._logger = logging.getLogger("Dnotify") self._logger.addFilter(DnotifyNullLoggingFilter()) else: @@ -62,13 +62,13 @@ class DirectoryNotifier: class DirectoryNotifierAsyncWrapper(threading.Thread): def __init__(self, dnotify, queue, logger=None, name=None): - if not name is None: + if name: threading.Thread.__init__(self, name=name) else: threading.Thread.__init__(self) self._eventqueue = queue self._dnotify = dnotify - if logger is None: + if not logger: self._logger = logging.getLogger("Dnotify") self._logger.addFilter(DnotifyNullLoggingFilter()) else: @@ -78,7 +78,7 @@ class DirectoryNotifierAsyncWrapper(threading.Thread): self._cancel_event.set() def run(self): - self._logger.info('Created new thread (%s) for async directory notification' % (self.getName())) + self._logger.info('Created new thread (%s) for async directory notification' % self.getName()) while not self._dnotify.cancelled(): dir = self._dnotify.poll() self._eventqueue.put(dir) @@ -97,7 +97,7 @@ class MtimeDirectoryNotifier(DirectoryNotifier): timeout_time = None if timeout: timeout_time = time.time() + timeout - while self._changed == []: + while not self._changed: if timeout_time and time.time() > timeout_time: return None self._logger.debug('Polling...') @@ -105,16 +105,15 @@ class MtimeDirectoryNotifier(DirectoryNotifier): oldtime = self._dirmap[dir] mtime = os.stat(os.path.join(self._cwd, dir))[stat.ST_MTIME] if oldtime < mtime: - self._logger.debug('Directory "%s" has changed' % (dir,)) + self._logger.debug('Directory "%s" has changed' % dir) self._changed.append(dir) self._dirmap[dir] = mtime - if self._changed == []: + if not self._changed: for x in range(self._polltime): if self._cancel_event.isSet(): return None time.sleep(1) - ret = self._changed[0] - self._changed = self._changed[1:] + ret = self._changed.pop(0) return ret class DnotifyDirectoryNotifier(DirectoryNotifier): @@ -127,11 +126,11 @@ class DnotifyDirectoryNotifier(DirectoryNotifier): def poll(self, timeout=None): # delete duplicates i = self._queue.qsize() - self._logger.debug('Queue size: %d', (i,)) + self._logger.debug('Queue size: %d' % i) set = {} - while i > 0: + while i: dir = self._queue_get(timeout) - if dir is None: + if not dir: # We shouldn't have to do this; no one else is reading # from the queue. But we do it just to be safe. for key in list(set.keys()): @@ -142,11 +141,11 @@ class DnotifyDirectoryNotifier(DirectoryNotifier): for key in list(set.keys()): self._queue.put(key) i = self._queue.qsize() - self._logger.debug('Queue size (after duplicate filter): %d', (i,)) + self._logger.debug('Queue size (after duplicate filter): %d' % i) return self._queue_get(timeout) def _queue_get(self, timeout): - if timeout is None: + if not timeout: return self._queue.get() timeout_time = time.time() + timeout while True: @@ -179,21 +178,21 @@ class DnotifyThread(threading.Thread): os.close(outfd) stdout = os.fdopen(infd) c = 'x' - while c != '': + while c: curline = '' c = stdout.read(1) - while c != '' and c != '\0': + while c and c != '\0': curline += c c = stdout.read(1) - if c == '': + if not c: break - self._logger.debug('Directory "%s" changed' % (curline,)) + self._logger.debug('Directory "%s" changed' % curline) self._queue.put(curline) (pid, status) = os.waitpid(pid, 0) - if status is None: + if not status: ecode = 0 else: ecode = os.WEXITSTATUS(status) - raise DnotifyException("dnotify exited with code %s" % (ecode,)) + raise DnotifyException("dnotify exited with code %s" % ecode) # vim:ts=4:sw=4:et: |
